@@ -45,16 +45,15 @@ emscripten_fetch_queue* _emscripten_get_fetch_queue() {
45
45
if (!g_queue .queuedOperations ) {
46
46
g_queue .queueSize = 64 ;
47
47
g_queue .numQueuedItems = 0 ;
48
- g_queue .queuedOperations =
49
- (emscripten_fetch_t * * )malloc (sizeof (emscripten_fetch_t * ) * g_queue .queueSize );
48
+ g_queue .queuedOperations = malloc (sizeof (emscripten_fetch_t * ) * g_queue .queueSize );
50
49
}
51
50
return & g_queue ;
52
51
}
53
52
54
53
void emscripten_proxy_fetch (emscripten_fetch_t * fetch ) {
55
54
// TODO: mutex lock
56
55
emscripten_fetch_queue * queue = _emscripten_get_fetch_queue ();
57
- // TODO handle case when queue->numQueuedItems >= queue->queueSize
56
+ // TODO handle case when queue->numQueuedItems >= queue->queueSize
58
57
queue -> queuedOperations [queue -> numQueuedItems ++ ] = fetch ;
59
58
#ifdef FETCH_DEBUG
60
59
emscripten_dbgf ("Queued fetch to fetch-worker to process. There are "
@@ -118,8 +117,9 @@ emscripten_fetch_t* emscripten_fetch(emscripten_fetch_attr_t* fetch_attr, const
118
117
119
118
if (fetch_attr -> requestHeaders ) {
120
119
size_t headersCount = 0 ;
121
- while (fetch_attr -> requestHeaders [headersCount ])
120
+ while (fetch_attr -> requestHeaders [headersCount ]) {
122
121
++ headersCount ;
122
+ }
123
123
const char * * headers = (const char * * )malloc ((headersCount + 1 ) * sizeof (const char * ));
124
124
if (!headers ) {
125
125
fetch_free (fetch );
@@ -148,43 +148,50 @@ emscripten_fetch_t* emscripten_fetch(emscripten_fetch_attr_t* fetch_attr, const
148
148
149
149
EMSCRIPTEN_RESULT emscripten_fetch_wait (emscripten_fetch_t * fetch , double timeoutMsecs ) {
150
150
#if __EMSCRIPTEN_PTHREADS__
151
- if (!fetch )
151
+ if (!fetch ) {
152
152
return EMSCRIPTEN_RESULT_INVALID_PARAM ;
153
+ }
153
154
uint32_t proxyState = fetch -> __proxyState ;
154
- if (proxyState == 2 )
155
- return EMSCRIPTEN_RESULT_SUCCESS ; // already finished.
156
- if (proxyState != 1 )
157
- return EMSCRIPTEN_RESULT_INVALID_PARAM ; // the fetch should be ongoing?
155
+ if (proxyState == 2 ) {
156
+ // already finished.
157
+ return EMSCRIPTEN_RESULT_SUCCESS ;
158
+ }
159
+ if (proxyState != 1 ) {
160
+ // the fetch should be ongoing?
161
+ return EMSCRIPTEN_RESULT_INVALID_PARAM ;
162
+ }
158
163
#ifdef FETCH_DEBUG
159
164
emscripten_dbg ("fetch: emscripten_fetch_wait.." );
160
165
#endif
161
- if (timeoutMsecs <= 0 )
166
+ if (timeoutMsecs <= 0 ) {
162
167
return EMSCRIPTEN_RESULT_TIMED_OUT ;
168
+ }
163
169
while (proxyState == 1 /*sent to proxy worker*/ ) {
164
- if (!emscripten_is_main_browser_thread ()) {
165
- int ret = emscripten_futex_wait (& fetch -> __proxyState , proxyState , timeoutMsecs );
166
- if (ret == - ETIMEDOUT )
167
- return EMSCRIPTEN_RESULT_TIMED_OUT ;
168
- proxyState = fetch -> __proxyState ;
169
- } else {
170
+ if (emscripten_is_main_browser_thread ()) {
170
171
emscripten_err ("fetch: emscripten_fetch_wait failed: main thread cannot block to wait for long periods of time! Migrate the application to run in a worker to perform synchronous file IO, or switch to using asynchronous IO." );
171
172
return EMSCRIPTEN_RESULT_FAILED ;
172
173
}
174
+ int ret = emscripten_futex_wait (& fetch -> __proxyState , proxyState , timeoutMsecs );
175
+ if (ret == - ETIMEDOUT ) {
176
+ return EMSCRIPTEN_RESULT_TIMED_OUT ;
177
+ }
178
+ proxyState = fetch -> __proxyState ;
173
179
}
174
180
#ifdef FETCH_DEBUG
175
181
emscripten_dbg ("fetch: emscripten_fetch_wait done.." );
176
182
#endif
177
183
178
- if (proxyState == 2 )
179
- return EMSCRIPTEN_RESULT_SUCCESS ;
180
- else
184
+ if (proxyState != 2 ) {
181
185
return EMSCRIPTEN_RESULT_FAILED ;
186
+ }
187
+ return EMSCRIPTEN_RESULT_SUCCESS ;
182
188
#else
183
- if (fetch -> readyState >= STATE_DONE )
189
+ if (fetch -> readyState >= STATE_DONE ) {
184
190
return EMSCRIPTEN_RESULT_SUCCESS ; // already finished.
185
- if (timeoutMsecs == 0 )
191
+ }
192
+ if (timeoutMsecs == 0 ) {
186
193
return EMSCRIPTEN_RESULT_TIMED_OUT /*Main thread testing completion with sleep=0msecs*/ ;
187
- else {
194
+ } else {
188
195
#ifdef FETCH_DEBUG
189
196
emscripten_err ("fetch: emscripten_fetch_wait() cannot stop to wait when building without pthreads!" );
190
197
#endif
@@ -194,17 +201,19 @@ EMSCRIPTEN_RESULT emscripten_fetch_wait(emscripten_fetch_t* fetch, double timeou
194
201
}
195
202
196
203
EMSCRIPTEN_RESULT emscripten_fetch_close (emscripten_fetch_t * fetch ) {
197
- if (!fetch )
204
+ if (!fetch ) {
198
205
return EMSCRIPTEN_RESULT_SUCCESS ; // Closing null pointer is ok, same as with free().
206
+ }
199
207
200
208
#if __EMSCRIPTEN_PTHREADS__
201
209
fetch -> __proxyState = 0 ;
202
210
#endif
203
211
// This function frees the fetch pointer so that it is invalid to access it anymore.
204
212
// Use a few key fields as an integrity check that we are being passed a good pointer to a valid
205
213
// fetch structure, which has not been yet closed. (double close is an error)
206
- if (fetch -> id == 0 || fetch -> readyState > STATE_MAX )
214
+ if (fetch -> id == 0 || fetch -> readyState > STATE_MAX ) {
207
215
return EMSCRIPTEN_RESULT_INVALID_PARAM ;
216
+ }
208
217
209
218
// This fetch is aborted. Call the error handler if the fetch was still in progress and was
210
219
// canceled in flight.
@@ -219,22 +228,25 @@ EMSCRIPTEN_RESULT emscripten_fetch_close(emscripten_fetch_t* fetch) {
219
228
}
220
229
221
230
size_t emscripten_fetch_get_response_headers_length (emscripten_fetch_t * fetch ) {
222
- if (!fetch || fetch -> readyState < STATE_HEADERS_RECEIVED ) return 0 ;
231
+ if (!fetch || fetch -> readyState < STATE_HEADERS_RECEIVED ) {
232
+ return 0 ;
233
+ }
223
234
224
235
return (size_t )_emscripten_fetch_get_response_headers_length (fetch -> id );
225
236
}
226
237
227
238
size_t emscripten_fetch_get_response_headers (emscripten_fetch_t * fetch , char * dst , size_t dstSizeBytes ) {
228
- if (!fetch || fetch -> readyState < STATE_HEADERS_RECEIVED ) return 0 ;
239
+ if (!fetch || fetch -> readyState < STATE_HEADERS_RECEIVED ) {
240
+ return 0 ;
241
+ }
229
242
230
243
return (size_t )_emscripten_fetch_get_response_headers (fetch -> id , dst , dstSizeBytes );
231
244
}
232
245
233
246
char * * emscripten_fetch_unpack_response_headers (const char * headersString ) {
234
247
// Get size of output array and allocate.
235
248
size_t numHeaders = 0 ;
236
- for (const char * pos = strchr (headersString , '\n' ); pos ; pos = strchr (pos + 1 , '\n' ))
237
- {
249
+ for (const char * pos = strchr (headersString , '\n' ); pos ; pos = strchr (pos + 1 , '\n' )) {
238
250
numHeaders ++ ;
239
251
}
240
252
char * * unpackedHeaders = (char * * )malloc (sizeof (char * ) * ((numHeaders * 2 ) + 1 ));
@@ -243,8 +255,7 @@ char **emscripten_fetch_unpack_response_headers(const char *headersString) {
243
255
// Allocate each header.
244
256
const char * rowStart = headersString ;
245
257
const char * rowEnd = strchr (rowStart , '\n' );
246
- for (size_t headerNum = 0 ; rowEnd ; headerNum += 2 )
247
- {
258
+ for (size_t headerNum = 0 ; rowEnd ; headerNum += 2 ) {
248
259
const char * split = strchr (rowStart , ':' );
249
260
size_t headerSize = (size_t )split - (size_t )rowStart ;
250
261
char * header = (char * )malloc (headerSize + 1 );
@@ -267,10 +278,10 @@ char **emscripten_fetch_unpack_response_headers(const char *headersString) {
267
278
}
268
279
269
280
void emscripten_fetch_free_unpacked_response_headers (char * * unpackedHeaders ) {
270
- if (unpackedHeaders )
271
- {
272
- for (size_t i = 0 ; unpackedHeaders [i ]; ++ i )
281
+ if (unpackedHeaders ) {
282
+ for (size_t i = 0 ; unpackedHeaders [i ]; ++ i ) {
273
283
free ((void * )unpackedHeaders [i ]);
284
+ }
274
285
free ((void * )unpackedHeaders );
275
286
}
276
287
}
@@ -288,8 +299,9 @@ static void fetch_free(emscripten_fetch_t* fetch) {
288
299
free ((void * )fetch -> __attributes .userName );
289
300
free ((void * )fetch -> __attributes .password );
290
301
if (fetch -> __attributes .requestHeaders ) {
291
- for (size_t i = 0 ; fetch -> __attributes .requestHeaders [i ]; ++ i )
302
+ for (size_t i = 0 ; fetch -> __attributes .requestHeaders [i ]; ++ i ) {
292
303
free ((void * )fetch -> __attributes .requestHeaders [i ]);
304
+ }
293
305
free ((void * )fetch -> __attributes .requestHeaders );
294
306
}
295
307
free ((void * )fetch -> __attributes .overriddenMimeType );
0 commit comments