diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-11-07 13:46:37 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-11-07 13:46:37 +0200 |
commit | 127404e21851ebbcc42a3270161c422393fd1626 (patch) | |
tree | f33b24242e99dd28a0946b694ed77fbdea049932 /src | |
parent | d11b904438cb954f783245cfb4f1f06e5b2d8993 (diff) |
GmRequest: Cleanup
Diffstat (limited to 'src')
-rw-r--r-- | src/gmrequest.c | 120 |
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 | |||
110 | enum iGmRequestState { | 108 | enum 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 | ||
132 | iDefineObjectConstructionArgs(GmRequest, (iGmCerts *certs), certs) | 128 | iDefineObjectConstructionArgs(GmRequest, (iGmCerts *certs), certs) |
133 | iDefineAudienceGetter(GmRequest, updated) | 129 | iDefineAudienceGetter(GmRequest, updated) |
134 | //iDefineAudienceGetter(GmRequest, timeout) | ||
135 | iDefineAudienceGetter(GmRequest, finished) | 130 | iDefineAudienceGetter(GmRequest, finished) |
136 | 131 | ||
137 | void 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 | |||
150 | void 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 | |||
177 | void setUrl_GmRequest(iGmRequest *d, const iString *url) { | ||
178 | set_String(&d->url, url); | ||
179 | urlEncodeSpaces_String(&d->url); | ||
180 | } | ||
181 | |||
182 | #if 0 | ||
183 | static 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 | |||
193 | void cancel_GmRequest(iGmRequest *d) { | ||
194 | cancel_TlsRequest(d->req); | ||
195 | } | ||
196 | |||
197 | #if 0 | ||
198 | static 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 | |||
207 | static void checkServerCertificate_GmRequest_(iGmRequest *d) { | 132 | static 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 | |||
348 | void 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 | |||
359 | void 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 | |||
382 | void setUrl_GmRequest(iGmRequest *d, const iString *url) { | ||
383 | set_String(&d->url, url); | ||
384 | urlEncodeSpaces_String(&d->url); | ||
385 | } | ||
386 | |||
421 | void submit_GmRequest(iGmRequest *d) { | 387 | void 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 | ||
530 | void cancel_GmRequest(iGmRequest *d) { | ||
531 | cancel_TlsRequest(d->req); | ||
532 | } | ||
533 | |||
564 | iBool isFinished_GmRequest(const iGmRequest *d) { | 534 | iBool 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)); |