@@ -14,6 +14,8 @@ struct __emscripten_fetch_queue
1414 int queueSize;
1515};
1616
17+ static void fetch_free ( emscripten_fetch_t *fetch );
18+
1719extern " C" {
1820 void emscripten_start_fetch (emscripten_fetch_t *fetch);
1921 __emscripten_fetch_queue *_emscripten_get_fetch_work_queue ();
@@ -66,16 +68,67 @@ emscripten_fetch_t *emscripten_fetch(emscripten_fetch_attr_t *fetch_attr, const
6668 }
6769
6870 emscripten_fetch_t *fetch = (emscripten_fetch_t *)malloc (sizeof (emscripten_fetch_t ));
71+ if (!fetch) return 0 ;
6972 memset (fetch, 0 , sizeof (emscripten_fetch_t ));
7073 fetch->id = globalFetchIdCounter++; // TODO: make this thread-safe!
7174 fetch->userData = fetch_attr->userData ;
72- fetch->url = strdup (url); // TODO: free
73- fetch->__attributes = *fetch_attr;
74- fetch->__attributes .destinationPath = fetch->__attributes .destinationPath ? strdup (fetch->__attributes .destinationPath ) : 0 ; // TODO: free
75- fetch->__attributes .userName = fetch->__attributes .userName ? strdup (fetch->__attributes .userName ) : 0 ; // TODO: free
76- fetch->__attributes .password = fetch->__attributes .password ? strdup (fetch->__attributes .password ) : 0 ; // TODO: free
77- fetch->__attributes .requestHeaders = 0 ;// TODO:strdup(fetch->__attributes.requestHeaders);
78- fetch->__attributes .overriddenMimeType = fetch->__attributes .overriddenMimeType ? strdup (fetch->__attributes .overriddenMimeType ) : 0 ; // TODO: free
75+ fetch->__attributes .timeoutMSecs = fetch_attr->timeoutMSecs ;
76+ fetch->__attributes .attributes = fetch_attr->attributes ;
77+ fetch->__attributes .withCredentials = fetch_attr->withCredentials ;
78+ fetch->__attributes .requestData = fetch_attr->requestData ;
79+ fetch->__attributes .requestDataSize = fetch_attr->requestDataSize ;
80+ strcpy (fetch->__attributes .requestMethod , fetch_attr->requestMethod );
81+ fetch->__attributes .onerror = fetch_attr->onerror ;
82+ fetch->__attributes .onsuccess = fetch_attr->onsuccess ;
83+ fetch->__attributes .onprogress = fetch_attr->onprogress ;
84+ #define STRDUP_OR_ABORT (s, str_to_dup ) \
85+ if (str_to_dup) \
86+ { \
87+ s = strdup (str_to_dup); \
88+ if (!s) \
89+ { \
90+ fetch_free (fetch); \
91+ return 0 ; \
92+ } \
93+ }
94+ STRDUP_OR_ABORT (fetch->url , url);
95+ STRDUP_OR_ABORT (fetch->__attributes .destinationPath , fetch_attr->destinationPath );
96+ STRDUP_OR_ABORT (fetch->__attributes .userName , fetch_attr->userName );
97+ STRDUP_OR_ABORT (fetch->__attributes .password ,fetch_attr->password );
98+ STRDUP_OR_ABORT (fetch->__attributes .overriddenMimeType , fetch_attr->overriddenMimeType );
99+ if (fetch_attr->requestHeaders )
100+ {
101+ size_t headersCount = 0 ;
102+ while (fetch_attr->requestHeaders [headersCount]) ++headersCount;
103+ const char ** headers = (const char **)malloc ((headersCount + 1 ) * sizeof (const char *));
104+ if (!headers)
105+ {
106+ fetch_free (fetch);
107+ return 0 ;
108+ }
109+ memset ((void *)headers, 0 , (headersCount + 1 ) * sizeof (const char *));
110+
111+ for (size_t i = 0 ; i < headersCount; ++i)
112+ {
113+ headers[i] = strdup (fetch_attr->requestHeaders [i]);
114+ if (!headers[i])
115+
116+ {
117+ for (size_t j = 0 ; j < i; ++j)
118+ {
119+ free ((void *)headers[j]);
120+ }
121+ free ((void *)headers);
122+ fetch_free (fetch);
123+ return 0 ;
124+ }
125+ }
126+ headers[headersCount] = 0 ;
127+ fetch->__attributes .requestHeaders = headers;
128+ }
129+
130+ #undef STRDUP_OR_ABORT
131+
79132
80133#if __EMSCRIPTEN_PTHREADS__
81134 const bool waitable = (fetch_attr->attributes & EMSCRIPTEN_FETCH_WAITABLE) != 0 ;
@@ -144,8 +197,25 @@ EMSCRIPTEN_RESULT emscripten_fetch_close(emscripten_fetch_t *fetch)
144197 strcpy (fetch->statusText , " aborted with emscripten_fetch_close()" );
145198 fetch->__attributes .onerror (fetch);
146199 }
200+
201+ fetch_free (fetch);
202+ return EMSCRIPTEN_RESULT_SUCCESS;
203+ }
204+
205+ static void fetch_free (emscripten_fetch_t *fetch)
206+ {
147207 fetch->id = 0 ;
148208 free ((void *)fetch->data );
209+ free ((void *)fetch->url );
210+ free ((void *)fetch->__attributes .destinationPath );
211+ free ((void *)fetch->__attributes .userName );
212+ free ((void *)fetch->__attributes .password );
213+ if (fetch->__attributes .requestHeaders )
214+ {
215+ for (size_t i = 0 ; fetch->__attributes .requestHeaders [i]; ++i)
216+ free ((void *)fetch->__attributes .requestHeaders [i]);
217+ free ((void *)fetch->__attributes .requestHeaders );
218+ }
219+ free ((void *)fetch->__attributes .overriddenMimeType );
149220 free (fetch);
150- return EMSCRIPTEN_RESULT_SUCCESS;
151221}
0 commit comments