summaryrefslogtreecommitdiff
path: root/src/gmrequest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gmrequest.c')
-rw-r--r--src/gmrequest.c120
1 files changed, 45 insertions, 75 deletions
diff --git a/src/gmrequest.c b/src/gmrequest.c
index bad45604..83cbbba7 100644
--- a/src/gmrequest.c
+++ b/src/gmrequest.c
@@ -105,8 +105,6 @@ void deserialize_GmResponse(iGmResponse *d, iStream *ins) {
105 105
106/*----------------------------------------------------------------------------------------------*/ 106/*----------------------------------------------------------------------------------------------*/
107 107
108//static const int bodyTimeout_GmRequest_ = 3000; /* ms */
109
110enum iGmRequestState { 108enum iGmRequestState {
111 initialized_GmRequestState, 109 initialized_GmRequestState,
112 receivingHeader_GmRequestState, 110 receivingHeader_GmRequestState,
@@ -123,87 +121,14 @@ struct Impl_GmRequest {
123 iString url; 121 iString url;
124 iTlsRequest * req; 122 iTlsRequest * req;
125 iGmResponse resp; 123 iGmResponse resp;
126 //uint32_t timeoutId; /* in case server doesn't close the connection */
127 iAudience * updated; 124 iAudience * updated;
128// iAudience * timeout;
129 iAudience * finished; 125 iAudience * finished;
130}; 126};
131 127
132iDefineObjectConstructionArgs(GmRequest, (iGmCerts *certs), certs) 128iDefineObjectConstructionArgs(GmRequest, (iGmCerts *certs), certs)
133iDefineAudienceGetter(GmRequest, updated) 129iDefineAudienceGetter(GmRequest, updated)
134//iDefineAudienceGetter(GmRequest, timeout)
135iDefineAudienceGetter(GmRequest, finished) 130iDefineAudienceGetter(GmRequest, finished)
136 131
137void init_GmRequest(iGmRequest *d, iGmCerts *certs) {
138 init_Mutex(&d->mutex);
139 init_GmResponse(&d->resp);
140 init_String(&d->url);
141 d->certs = certs;
142// d->timeoutId = 0;
143 d->req = NULL;
144 d->state = initialized_GmRequestState;
145 d->updated = NULL;
146// d->timeout = NULL;
147 d->finished = NULL;
148}
149
150void deinit_GmRequest(iGmRequest *d) {
151 if (d->req) {
152 iDisconnectObject(TlsRequest, d->req, readyRead, d);
153 iDisconnectObject(TlsRequest, d->req, finished, d);
154 }
155 lock_Mutex(&d->mutex);
156// if (d->timeoutId) {
157// SDL_RemoveTimer(d->timeoutId);
158// }
159 if (!isFinished_GmRequest(d)) {
160 unlock_Mutex(&d->mutex);
161 cancel_TlsRequest(d->req);
162 d->state = finished_GmRequestState;
163 }
164 else {
165 unlock_Mutex(&d->mutex);
166 }
167 iRelease(d->req);
168 d->req = NULL;
169// delete_Audience(d->timeout);
170 delete_Audience(d->finished);
171 delete_Audience(d->updated);
172 deinit_GmResponse(&d->resp);
173 deinit_String(&d->url);
174 deinit_Mutex(&d->mutex);
175}
176
177void setUrl_GmRequest(iGmRequest *d, const iString *url) {
178 set_String(&d->url, url);
179 urlEncodeSpaces_String(&d->url);
180}
181
182#if 0
183static uint32_t timedOutWhileReceivingBody_GmRequest_(uint32_t interval, void *obj) {
184 /* Note: Called from SDL's timer thread. */
185 iGmRequest *d = obj;
186 //postCommandf_App("gmrequest.timeout request:%p", obj);
187 iNotifyAudience(d, timeout, GmRequestTimeout);
188 iUnused(interval);
189 return 0;
190}
191#endif
192
193void cancel_GmRequest(iGmRequest *d) {
194 cancel_TlsRequest(d->req);
195}
196
197#if 0
198static void restartTimeout_GmRequest_(iGmRequest *d) {
199 /* Note: `d` is currently locked. */
200 if (d->timeoutId) {
201 SDL_RemoveTimer(d->timeoutId);
202 }
203 d->timeoutId = SDL_AddTimer(bodyTimeout_GmRequest_, timedOutWhileReceivingBody_GmRequest_, d);
204}
205#endif
206
207static void checkServerCertificate_GmRequest_(iGmRequest *d) { 132static void checkServerCertificate_GmRequest_(iGmRequest *d) {
208 const iTlsCertificate *cert = serverCertificate_TlsRequest(d->req); 133 const iTlsCertificate *cert = serverCertificate_TlsRequest(d->req);
209 d->resp.certFlags = 0; 134 d->resp.certFlags = 0;
@@ -418,6 +343,47 @@ static const iBlock *replaceVariables_(const iBlock *block) {
418 return block; 343 return block;
419} 344}
420 345
346/*----------------------------------------------------------------------------------------------*/
347
348void init_GmRequest(iGmRequest *d, iGmCerts *certs) {
349 init_Mutex(&d->mutex);
350 init_GmResponse(&d->resp);
351 init_String(&d->url);
352 d->certs = certs;
353 d->req = NULL;
354 d->state = initialized_GmRequestState;
355 d->updated = NULL;
356 d->finished = NULL;
357}
358
359void deinit_GmRequest(iGmRequest *d) {
360 if (d->req) {
361 iDisconnectObject(TlsRequest, d->req, readyRead, d);
362 iDisconnectObject(TlsRequest, d->req, finished, d);
363 }
364 lock_Mutex(&d->mutex);
365 if (!isFinished_GmRequest(d)) {
366 unlock_Mutex(&d->mutex);
367 cancel_TlsRequest(d->req);
368 d->state = finished_GmRequestState;
369 }
370 else {
371 unlock_Mutex(&d->mutex);
372 }
373 iRelease(d->req);
374 d->req = NULL;
375 delete_Audience(d->finished);
376 delete_Audience(d->updated);
377 deinit_GmResponse(&d->resp);
378 deinit_String(&d->url);
379 deinit_Mutex(&d->mutex);
380}
381
382void setUrl_GmRequest(iGmRequest *d, const iString *url) {
383 set_String(&d->url, url);
384 urlEncodeSpaces_String(&d->url);
385}
386
421void submit_GmRequest(iGmRequest *d) { 387void submit_GmRequest(iGmRequest *d) {
422 iAssert(d->state == initialized_GmRequestState); 388 iAssert(d->state == initialized_GmRequestState);
423 if (d->state != initialized_GmRequestState) { 389 if (d->state != initialized_GmRequestState) {
@@ -561,6 +527,10 @@ void submit_GmRequest(iGmRequest *d) {
561 submit_TlsRequest(d->req); 527 submit_TlsRequest(d->req);
562} 528}
563 529
530void cancel_GmRequest(iGmRequest *d) {
531 cancel_TlsRequest(d->req);
532}
533
564iBool isFinished_GmRequest(const iGmRequest *d) { 534iBool isFinished_GmRequest(const iGmRequest *d) {
565 iBool done; 535 iBool done;
566 iGuardMutex(&d->mutex, done = (d->state == finished_GmRequestState)); 536 iGuardMutex(&d->mutex, done = (d->state == finished_GmRequestState));