/src/gpac/src/utils/downloader.c
Line | Count | Source |
1 | | /* |
2 | | * GPAC Multimedia Framework |
3 | | * |
4 | | * Authors: Jean Le Feuvre |
5 | | * Copyright (c) Telecom ParisTech 2005-2026 |
6 | | * All rights reserved |
7 | | * |
8 | | * This file is part of GPAC / downloader sub-project |
9 | | * |
10 | | * GPAC is free software; you can redistribute it and/or modify |
11 | | * it under the terms of the GNU Lesser General Public License as published by |
12 | | * the Free Software Foundation; either version 2, or (at your option) |
13 | | * any later version. |
14 | | * |
15 | | * GPAC is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | * GNU Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public |
21 | | * License along with this library; see the file COPYING. If not, write to |
22 | | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
23 | | * |
24 | | */ |
25 | | |
26 | | #include "downloader.h" |
27 | | |
28 | | #ifndef GPAC_DISABLE_NETWORK |
29 | | |
30 | | static void gf_dm_connect(GF_DownloadSession *sess); |
31 | | |
32 | 0 | #define GF_CHUNK_MAX_SIZE 0x40000000 // 1GB |
33 | | |
34 | | void dm_sess_sk_del(GF_DownloadSession *sess) |
35 | 0 | { |
36 | | #ifdef GPAC_HAS_CURL |
37 | | if (sess->curl_hnd) { |
38 | | curl_destroy(sess); |
39 | | return; |
40 | | } |
41 | | #endif |
42 | 0 | if (sess->sock) { |
43 | 0 | GF_Socket *sock = sess->sock; |
44 | 0 | sess->sock = NULL; |
45 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[Downloader] closing socket\n")); |
46 | 0 | if (sess->sock_group) gf_sk_group_unregister(sess->sock_group, sock); |
47 | 0 | gf_sk_del(sock); |
48 | 0 | sess->sock = NULL; |
49 | | #ifdef GPAC_HAS_HTTP2 |
50 | | sess->h2_upgrade_state = 0; |
51 | | #endif |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | | static void sess_connection_closed(GF_DownloadSession *sess) |
56 | 0 | { |
57 | | #ifdef GPAC_HTTPMUX |
58 | | if (sess->hmux_sess) { |
59 | | sess->hmux_sess->do_shutdown = GF_TRUE; |
60 | | sess->hmux_switch_sess = GF_TRUE; |
61 | | } |
62 | | #endif |
63 | 0 | } |
64 | | |
65 | | GF_Err gf_dm_sess_write(GF_DownloadSession *session, const u8 *buffer, u32 size) |
66 | 0 | { |
67 | 0 | GF_Err e; |
68 | 0 | u32 written=0; |
69 | 0 | GF_DownloadSession *par_sess = session; |
70 | | #ifdef GPAC_HTTPMUX |
71 | | //if h2 we aggregate pending frames on the session currently holding the HTTP2 session |
72 | | if (session->hmux_sess) par_sess = session->hmux_sess->net_sess; |
73 | | #endif |
74 | | |
75 | | //we are writing and we already have pending data, append and try to flush after |
76 | 0 | if (par_sess->async_buf_size && (buffer != par_sess->async_buf)) { |
77 | 0 | e = GF_IP_NETWORK_EMPTY; |
78 | 0 | } else |
79 | 0 | #ifdef GPAC_HAS_SSL |
80 | 0 | if (session->ssl) { |
81 | 0 | e = gf_ssl_write(session, buffer, size, &written); |
82 | 0 | if (e==GF_IP_NETWORK_FAILURE) |
83 | 0 | e = GF_IP_CONNECTION_CLOSED; |
84 | 0 | } else |
85 | 0 | #endif |
86 | 0 | { |
87 | 0 | e = gf_sk_send_ex(session->sock, buffer, size, &written); |
88 | 0 | } |
89 | | |
90 | |
|
91 | 0 | if (!(session->flags & GF_NETIO_SESSION_NO_BLOCK) || (e!=GF_IP_NETWORK_EMPTY)) { |
92 | 0 | par_sess->async_buf_size = 0; |
93 | 0 | return e; |
94 | 0 | } |
95 | | |
96 | | //we are blocking, store content |
97 | 0 | u32 remain = size - written; |
98 | 0 | if (buffer == par_sess->async_buf) { |
99 | 0 | if (written) { |
100 | 0 | if (par_sess->async_buf_size >= written) { |
101 | 0 | memmove(par_sess->async_buf, par_sess->async_buf + written, remain); |
102 | 0 | par_sess->async_buf_size -= written; |
103 | 0 | } else { |
104 | 0 | gf_assert(0); |
105 | 0 | par_sess->async_buf_size = 0; |
106 | 0 | } |
107 | 0 | } |
108 | 0 | } else { |
109 | 0 | if (par_sess->async_buf_alloc < par_sess->async_buf_size + remain) { |
110 | 0 | par_sess->async_buf_alloc = par_sess->async_buf_size + remain; |
111 | 0 | par_sess->async_buf = gf_realloc(par_sess->async_buf, par_sess->async_buf_alloc); |
112 | 0 | if (!par_sess->async_buf) return GF_OUT_OF_MEM; |
113 | 0 | } |
114 | 0 | memcpy(par_sess->async_buf+par_sess->async_buf_size, buffer + written, remain); |
115 | 0 | par_sess->async_buf_size += remain; |
116 | 0 | } |
117 | 0 | return GF_OK; |
118 | 0 | } |
119 | | |
120 | | |
121 | | static Bool gf_dm_is_local(GF_DownloadManager *dm, const char *url) |
122 | 0 | { |
123 | 0 | if (!strnicmp(url, "file://", 7)) return GF_TRUE; |
124 | 0 | if (!strstr(url, "://")) return GF_TRUE; |
125 | 0 | return GF_FALSE; |
126 | 0 | } |
127 | | |
128 | | Bool gf_dm_can_handle_url(const char *url) |
129 | 4.43k | { |
130 | 4.43k | if (!strnicmp(url, "http://", 7)) return GF_TRUE; |
131 | 4.43k | #ifdef GPAC_HAS_SSL |
132 | 4.43k | if (!strnicmp(url, "https://", 8)) return GF_TRUE; |
133 | 4.43k | #endif |
134 | | |
135 | | #ifdef GPAC_HAS_CURL |
136 | | if (curl_can_handle_url(url)) return GF_TRUE; |
137 | | #endif |
138 | 4.43k | return GF_FALSE; |
139 | 4.43k | } |
140 | | |
141 | | |
142 | | void gf_dm_sess_set_header_ex(GF_DownloadSession *sess, const char *name, const char *value, Bool allow_overwrite) |
143 | 0 | { |
144 | 0 | GF_HTTPHeader *hdr; |
145 | 0 | if (!sess) return; |
146 | | |
147 | | #ifdef GPAC_HTTPMUX |
148 | | if (sess->hmux_sess |
149 | | #ifdef GPAC_HAS_HTTP2 |
150 | | || sess->h2_upgrade_settings |
151 | | #endif |
152 | | ) { |
153 | | if (!stricmp(name, "Transfer-Encoding")) |
154 | | return; |
155 | | if (!stricmp(name, "Connection")) return; |
156 | | if (!stricmp(name, "Keep-Alive")) return; |
157 | | } |
158 | | #endif |
159 | | //check existing headers |
160 | 0 | u32 i, count = gf_list_count(sess->headers); |
161 | 0 | for (i=0; i<count; i++) { |
162 | 0 | hdr = gf_list_get(sess->headers, i); |
163 | 0 | if (stricmp(hdr->name, name)) continue; |
164 | 0 | if (!allow_overwrite) return; |
165 | | |
166 | 0 | gf_free(hdr->value); |
167 | 0 | if (value) { |
168 | 0 | hdr->value = gf_strdup(value); |
169 | 0 | return; |
170 | 0 | } |
171 | 0 | gf_list_rem(sess->headers, i); |
172 | 0 | gf_free(hdr->name); |
173 | 0 | gf_free(hdr); |
174 | 0 | return; |
175 | 0 | } |
176 | | |
177 | 0 | GF_SAFEALLOC(hdr, GF_HTTPHeader) |
178 | 0 | if (hdr) { |
179 | 0 | hdr->name = gf_strdup(name); |
180 | 0 | hdr->value = gf_strdup(value); |
181 | 0 | gf_list_add(sess->headers, hdr); |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | void gf_dm_sess_set_header(GF_DownloadSession *sess, const char *name, const char *value) |
186 | 0 | { |
187 | 0 | gf_dm_sess_set_header_ex(sess, name, value, GF_TRUE); |
188 | 0 | } |
189 | | |
190 | | GF_Err gf_dm_sess_send_reply(GF_DownloadSession *sess, u32 reply_code, const char *response_body, u32 body_len, Bool no_body) |
191 | 0 | { |
192 | 0 | u32 i, count; |
193 | 0 | GF_Err e; |
194 | 0 | char szFmt[50]; |
195 | 0 | char *rsp_buf = NULL; |
196 | 0 | if (!sess || !sess->server_mode) return GF_BAD_PARAM; |
197 | | |
198 | 0 | count = gf_list_count(sess->headers); |
199 | |
|
200 | | #ifdef GPAC_HAS_HTTP2 |
201 | | e = http2_check_upgrade(sess); |
202 | | if (e) return e; |
203 | | #endif |
204 | |
|
205 | | #ifdef GPAC_HTTPMUX |
206 | | if (sess->hmux_sess) { |
207 | | return sess->hmux_sess->send_reply(sess, reply_code, response_body, body_len, no_body); |
208 | | } |
209 | | #endif |
210 | | |
211 | |
|
212 | 0 | sprintf(szFmt, "HTTP/1.1 %d ", reply_code); |
213 | 0 | gf_dynstrcat(&rsp_buf, szFmt, NULL); |
214 | 0 | switch (reply_code) { |
215 | 0 | case 400: gf_dynstrcat(&rsp_buf, "Bad Request", NULL); break; |
216 | 0 | case 401: gf_dynstrcat(&rsp_buf, "Unauthorized", NULL); break; |
217 | 0 | case 403: gf_dynstrcat(&rsp_buf, "Forbidden", NULL); break; |
218 | 0 | case 405: gf_dynstrcat(&rsp_buf, "Not Allowed", NULL); break; |
219 | 0 | case 416: gf_dynstrcat(&rsp_buf, "Requested Range Not Satisfiable", NULL); break; |
220 | 0 | case 411: gf_dynstrcat(&rsp_buf, "Length Required", NULL); break; |
221 | 0 | case 404: gf_dynstrcat(&rsp_buf, "Not Found", NULL); break; |
222 | 0 | case 501: gf_dynstrcat(&rsp_buf, "Not Implemented", NULL); break; |
223 | 0 | case 500: gf_dynstrcat(&rsp_buf, "Internal Server Error", NULL); break; |
224 | 0 | case 304: gf_dynstrcat(&rsp_buf, "Not Modified", NULL); break; |
225 | 0 | case 204: gf_dynstrcat(&rsp_buf, "No Content", NULL); break; |
226 | 0 | case 206: gf_dynstrcat(&rsp_buf, "Partial Content", NULL); break; |
227 | 0 | case 200: gf_dynstrcat(&rsp_buf, "OK", NULL); break; |
228 | 0 | case 201: gf_dynstrcat(&rsp_buf, "Created", NULL); break; |
229 | 0 | case 101: gf_dynstrcat(&rsp_buf, "Switching Protocols", NULL); break; |
230 | 0 | default: |
231 | 0 | gf_dynstrcat(&rsp_buf, "ERROR", NULL); break; |
232 | 0 | } |
233 | 0 | gf_dynstrcat(&rsp_buf, "\r\n", NULL); |
234 | 0 | if (!rsp_buf) return GF_OUT_OF_MEM; |
235 | | |
236 | 0 | for (i=0; i<count; i++) { |
237 | 0 | GF_HTTPHeader *hdr = gf_list_get(sess->headers, i); |
238 | 0 | gf_dynstrcat(&rsp_buf, hdr->name, NULL); |
239 | 0 | gf_dynstrcat(&rsp_buf, ": ", NULL); |
240 | 0 | gf_dynstrcat(&rsp_buf, hdr->value, NULL); |
241 | 0 | gf_dynstrcat(&rsp_buf, "\r\n", NULL); |
242 | 0 | } |
243 | 0 | gf_dynstrcat(&rsp_buf, "\r\n", NULL); |
244 | 0 | if (!rsp_buf) return GF_OUT_OF_MEM; |
245 | | |
246 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] send reply for %s:\n%s\n", sess->log_name, sess->orig_url, rsp_buf)); |
247 | |
|
248 | 0 | count = (u32) strlen(rsp_buf); |
249 | 0 | if (response_body) { |
250 | 0 | rsp_buf = gf_realloc(rsp_buf, count+body_len); |
251 | 0 | if (!rsp_buf) return GF_OUT_OF_MEM; |
252 | 0 | memcpy(rsp_buf+count, response_body, body_len); |
253 | 0 | count+=body_len; |
254 | 0 | } |
255 | 0 | e = gf_dm_sess_write(sess, rsp_buf, count); |
256 | 0 | gf_free(rsp_buf); |
257 | 0 | return e; |
258 | 0 | } |
259 | | |
260 | | void gf_dm_sess_clear_headers(GF_DownloadSession *sess) |
261 | 0 | { |
262 | 0 | while (gf_list_count(sess->headers)) { |
263 | 0 | GF_HTTPHeader *hdr = (GF_HTTPHeader*)gf_list_last(sess->headers); |
264 | 0 | gf_list_rem_last(sess->headers); |
265 | 0 | gf_free(hdr->name); |
266 | 0 | gf_free(hdr->value); |
267 | 0 | gf_free(hdr); |
268 | 0 | } |
269 | 0 | if (sess->mime_type) { |
270 | 0 | gf_free(sess->mime_type); |
271 | 0 | sess->mime_type = NULL; |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | | |
276 | | void gf_dm_disconnect(GF_DownloadSession *sess, HTTPCloseType close_type) |
277 | 0 | { |
278 | 0 | gf_assert( sess ); |
279 | 0 | if (sess->connection_close) close_type = HTTP_RESET_CONN; |
280 | 0 | sess->connection_close = GF_FALSE; |
281 | 0 | sess->remaining_data_size = 0; |
282 | 0 | if (sess->async_req_reply) gf_free(sess->async_req_reply); |
283 | 0 | sess->async_req_reply = NULL; |
284 | 0 | sess->async_req_reply_size = 0; |
285 | 0 | sess->async_buf_size = 0; |
286 | 0 | if (sess->cached_file) { |
287 | 0 | gf_fclose(sess->cached_file); |
288 | 0 | sess->cached_file = NULL; |
289 | 0 | } |
290 | |
|
291 | 0 | if ((sess->status >= GF_NETIO_DISCONNECTED) |
292 | | #ifdef GPAC_HAS_CURL |
293 | | && !sess->curl_hnd |
294 | | #endif |
295 | 0 | ) { |
296 | 0 | if (close_type && sess->use_cache_file && sess->cache_entry) { |
297 | 0 | gf_cache_close_write_cache(sess->cache_entry, sess, GF_FALSE); |
298 | 0 | } |
299 | 0 | return; |
300 | 0 | } |
301 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_CORE, ("[Downloader] gf_dm_disconnect(%p)\n", sess )); |
302 | |
|
303 | 0 | gf_mx_p(sess->mx); |
304 | |
|
305 | 0 | if (!sess->server_mode) { |
306 | 0 | Bool do_close = (close_type || !(sess->flags & GF_NETIO_SESSION_PERSISTENT)) ? GF_TRUE : GF_FALSE; |
307 | | #ifdef GPAC_HTTPMUX |
308 | | if (sess->hmux_sess) { |
309 | | do_close = (close_type==HTTP_RESET_CONN) ? GF_TRUE : GF_FALSE; |
310 | | } |
311 | | //if H2 stream is still valid, issue a reset stream |
312 | | if (sess->hmux_sess && (sess->hmux_stream_id>=0) && (sess->put_state!=2)) |
313 | | sess->hmux_sess->stream_reset(sess, GF_FALSE); |
314 | | #endif |
315 | |
|
316 | | #ifdef GPAC_HAS_CURL |
317 | | //always remove curl session, let multi-connection manager handle disconnect/reconnect |
318 | | if (sess->curl_hnd) { |
319 | | sess->local_buf_len = 0; |
320 | | dm_sess_sk_del(sess); |
321 | | } else |
322 | | #endif |
323 | |
|
324 | 0 | if (do_close) { |
325 | | #ifdef GPAC_HTTPMUX |
326 | | if (sess->hmux_sess) { |
327 | | sess->hmux_sess->do_shutdown = GF_TRUE; |
328 | | hmux_detach_session(sess->hmux_sess, sess); |
329 | | } |
330 | | #endif |
331 | |
|
332 | 0 | #ifdef GPAC_HAS_SSL |
333 | 0 | if (sess->ssl) { |
334 | | // SSL_shutdown(sess->ssl); |
335 | | // SSL_free(sess->ssl); |
336 | | // gnutls_bye(sess->ssl, GNUTLS_SHUT_WR); |
337 | | // gnutls_deinit(sess->ssl); |
338 | 0 | gf_ssl_shutdown(sess->ssl); |
339 | 0 | sess->ssl = NULL; |
340 | 0 | } |
341 | 0 | #endif |
342 | 0 | dm_sess_sk_del(sess); |
343 | 0 | } |
344 | |
|
345 | 0 | if (close_type && sess->use_cache_file && sess->cache_entry) { |
346 | 0 | gf_cache_close_write_cache(sess->cache_entry, sess, GF_FALSE); |
347 | 0 | } |
348 | 0 | } |
349 | | |
350 | |
|
351 | 0 | sess->status = GF_NETIO_DISCONNECTED; |
352 | 0 | if (sess->num_retry) sess->num_retry--; |
353 | |
|
354 | 0 | gf_mx_v(sess->mx); |
355 | 0 | } |
356 | | |
357 | | GF_EXPORT |
358 | | void gf_dm_sess_del(GF_DownloadSession *sess) |
359 | 10 | { |
360 | 10 | if (!sess) |
361 | 10 | return; |
362 | | |
363 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[Downloader] Destroy session URL %s\n", sess->orig_url)); |
364 | | /*self-destruction, let the download manager destroy us*/ |
365 | 0 | if (sess->th || sess->ftask) { |
366 | 0 | sess->destroy = GF_TRUE; |
367 | 0 | if (sess->ftask->in_task) |
368 | 0 | return; |
369 | 0 | } |
370 | 0 | gf_dm_disconnect(sess, HTTP_CLOSE); |
371 | 0 | gf_dm_sess_clear_headers(sess); |
372 | | |
373 | | /*if threaded wait for thread exit*/ |
374 | 0 | if (sess->th) { |
375 | 0 | while (!(sess->flags & GF_DOWNLOAD_SESSION_THREAD_DEAD)) |
376 | 0 | gf_sleep(1); |
377 | 0 | gf_th_stop(sess->th); |
378 | 0 | gf_th_del(sess->th); |
379 | 0 | sess->th = NULL; |
380 | 0 | } |
381 | |
|
382 | 0 | if (sess->dm) { |
383 | 0 | gf_mx_p(sess->dm->cache_mx); |
384 | 0 | gf_list_del_item(sess->dm->all_sessions, sess); |
385 | 0 | gf_mx_v(sess->dm->cache_mx); |
386 | 0 | } |
387 | |
|
388 | 0 | gf_cache_remove_entry_from_session(sess); |
389 | 0 | if (sess->orig_url) gf_free(sess->orig_url); |
390 | 0 | if (sess->orig_url_before_redirect) gf_free(sess->orig_url_before_redirect); |
391 | 0 | if (sess->server_name) gf_free(sess->server_name); |
392 | 0 | sess->server_name = NULL; |
393 | 0 | if (sess->remote_path) gf_free(sess->remote_path); |
394 | | /* Credentials are stored into the sess->dm */ |
395 | 0 | if (sess->creds) sess->creds = NULL; |
396 | 0 | if (sess->init_data) gf_free(sess->init_data); |
397 | 0 | if (sess->remaining_data) gf_free(sess->remaining_data); |
398 | 0 | if (sess->async_req_reply) gf_free(sess->async_req_reply); |
399 | |
|
400 | 0 | sess->orig_url = sess->server_name = sess->remote_path; |
401 | 0 | sess->creds = NULL; |
402 | |
|
403 | | #ifdef GPAC_HTTPMUX |
404 | | { |
405 | | GF_Mutex *mx = sess->mx; |
406 | | Bool detached = GF_FALSE; |
407 | | gf_mx_p(mx); |
408 | | if (sess->hmux_sess) { |
409 | | sess->hmux_sess->setup_session(sess, GF_TRUE); |
410 | | hmux_detach_session(sess->hmux_sess, sess); |
411 | | detached = GF_TRUE; |
412 | | } |
413 | | if (!detached) gf_mx_v(mx); |
414 | | else if (sess->mx) gf_mx_v(sess->mx); |
415 | | } |
416 | | #endif |
417 | |
|
418 | | #ifdef GPAC_HAS_HTTP2 |
419 | | if (sess->h2_upgrade_settings) |
420 | | gf_free(sess->h2_upgrade_settings); |
421 | | #endif |
422 | |
|
423 | | #if defined(GPAC_HTTPMUX) || defined(GPAC_HAS_CURL) |
424 | | if (sess->local_buf) |
425 | | gf_free(sess->local_buf); |
426 | | #endif |
427 | | |
428 | | //free this once we have reassigned the session |
429 | 0 | if (sess->async_buf) gf_free(sess->async_buf); |
430 | |
|
431 | 0 | #ifdef GPAC_HAS_SSL |
432 | | //in server mode SSL context is managed by caller |
433 | 0 | if (sess->ssl) { |
434 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[Downloader] shut down SSL context\n")); |
435 | | // SSL_shutdown(sess->ssl); |
436 | | // SSL_free(sess->ssl); |
437 | | // gnutls_bye(sess->ssl, GNUTLS_SHUT_WR); |
438 | | // gnutls_deinit(sess->ssl); |
439 | 0 | gf_ssl_shutdown(sess->ssl); |
440 | 0 | sess->ssl = NULL; |
441 | 0 | } |
442 | 0 | #endif |
443 | 0 | dm_sess_sk_del(sess); |
444 | |
|
445 | 0 | gf_list_del(sess->headers); |
446 | |
|
447 | | #ifdef GPAC_HAS_CURL |
448 | | gf_assert(!sess->curl_hnd); |
449 | | if (sess->curl_hdrs) curl_slist_free_all(sess->curl_hdrs); |
450 | | #endif |
451 | |
|
452 | 0 | #ifndef GPAC_DISABLE_LOG |
453 | 0 | if (sess->log_name) gf_free(sess->log_name); |
454 | 0 | #endif |
455 | 0 | assert(!sess->ftask || !sess->ftask->in_task || !sess->mx); |
456 | 0 | gf_mx_del(sess->mx); |
457 | |
|
458 | 0 | if (sess->http_buf) gf_free(sess->http_buf); |
459 | |
|
460 | 0 | if (sess->ftask) { |
461 | 0 | sess->ftask->sess = NULL; |
462 | 0 | sess->ftask = NULL; |
463 | 0 | } |
464 | |
|
465 | 0 | gf_free(sess); |
466 | 0 | } |
467 | | |
468 | | void gf_dm_sess_notify_state(GF_DownloadSession *sess, GF_NetIOStatus dnload_status, GF_Err error) |
469 | 0 | { |
470 | 0 | if (!sess->user_proc) return; |
471 | 0 | GF_NETIO_Parameter par; |
472 | 0 | sess->in_callback = GF_TRUE; |
473 | 0 | memset(&par, 0, sizeof(GF_NETIO_Parameter)); |
474 | 0 | par.msg_type = dnload_status; |
475 | 0 | par.error = error; |
476 | 0 | par.sess = sess; |
477 | 0 | par.reply = 200; |
478 | 0 | sess->user_proc(sess->usr_cbk, &par); |
479 | 0 | sess->in_callback = GF_FALSE; |
480 | 0 | } |
481 | | |
482 | | static void gf_dm_sess_user_io(GF_DownloadSession *sess, GF_NETIO_Parameter *par) |
483 | 0 | { |
484 | 0 | if (sess->user_proc) { |
485 | 0 | sess->in_callback = GF_TRUE; |
486 | 0 | par->sess = sess; |
487 | 0 | sess->user_proc(sess->usr_cbk, par); |
488 | 0 | sess->in_callback = GF_FALSE; |
489 | 0 | } |
490 | 0 | } |
491 | | |
492 | | |
493 | | GF_EXPORT |
494 | | GF_Err gf_dm_sess_last_error(GF_DownloadSession *sess) |
495 | 0 | { |
496 | 0 | if (!sess) |
497 | 0 | return GF_BAD_PARAM; |
498 | 0 | return sess->last_error; |
499 | 0 | } |
500 | | |
501 | | GF_EXPORT |
502 | | void gf_dm_url_info_init(GF_URL_Info * info) |
503 | 0 | { |
504 | 0 | memset(info, 0, sizeof(GF_URL_Info)); |
505 | 0 | } |
506 | | |
507 | | GF_EXPORT |
508 | 0 | void gf_dm_url_info_del(GF_URL_Info * info) { |
509 | 0 | if (!info) |
510 | 0 | return; |
511 | 0 | if (info->protocol) |
512 | 0 | gf_free(info->protocol); |
513 | 0 | if (info->canonicalRepresentation) |
514 | 0 | gf_free(info->canonicalRepresentation); |
515 | 0 | if (info->password) |
516 | 0 | gf_free(info->password); |
517 | 0 | if (info->userName) |
518 | 0 | gf_free(info->userName); |
519 | 0 | if (info->remotePath) |
520 | 0 | gf_free(info->remotePath); |
521 | 0 | if (info->server_name) |
522 | 0 | gf_free(info->server_name); |
523 | 0 | gf_dm_url_info_init(info); |
524 | 0 | } |
525 | | |
526 | | /** |
527 | | \param url The url to parse for protocol |
528 | | \param info The info to fill |
529 | | \return Returns the offset in url of the protocol found -1 if not found |
530 | | */ |
531 | | static s32 gf_dm_parse_protocol(const char * url, GF_URL_Info * info) |
532 | 0 | { |
533 | 0 | gf_assert(info); |
534 | 0 | gf_assert(url); |
535 | 0 | if (!strnicmp(url, "http://", 7)) { |
536 | 0 | info->port = 80; |
537 | 0 | info->protocol = gf_strdup("http"); |
538 | 0 | return 7; |
539 | 0 | } |
540 | 0 | else if (!strnicmp(url, "https://", 8)) { |
541 | 0 | info->port = 443; |
542 | | #ifndef GPAC_HAS_SSL |
543 | | return -1; |
544 | | #endif |
545 | 0 | info->protocol = gf_strdup("https"); |
546 | 0 | return 8; |
547 | 0 | } |
548 | 0 | char *sep = strstr(url, "://"); |
549 | 0 | if (sep) { |
550 | 0 | sep[0] = 0; |
551 | 0 | info->protocol = gf_strdup(url); |
552 | 0 | sep[0] = ':'; |
553 | 0 | return 3 + (u32) (sep-url); |
554 | 0 | } |
555 | 0 | return -1; |
556 | 0 | } |
557 | | |
558 | | GF_EXPORT |
559 | 0 | GF_Err gf_dm_get_url_info(const char * url, GF_URL_Info * info, const char * baseURL) { |
560 | 0 | char *tmp, *tmp_url, *current_pos, *urlConcatenateWithBaseURL, *ipv6; |
561 | 0 | char * copyOfUrl; |
562 | 0 | s32 proto_offset; |
563 | 0 | u32 default_port; |
564 | 0 | gf_dm_url_info_del(info); |
565 | 0 | urlConcatenateWithBaseURL = NULL; |
566 | 0 | proto_offset = gf_dm_parse_protocol(url, info); |
567 | 0 | default_port = info->port; |
568 | |
|
569 | 0 | if (proto_offset > 0) { |
570 | 0 | url += proto_offset; |
571 | 0 | } else { |
572 | | /*relative URL*/ |
573 | 0 | if (!strstr(url, "://")) { |
574 | 0 | u32 i; |
575 | 0 | gf_dm_url_info_del(info); |
576 | 0 | gf_dm_url_info_init(info); |
577 | 0 | if (baseURL) { |
578 | 0 | urlConcatenateWithBaseURL = gf_url_concatenate(baseURL, url); |
579 | | /*relative file path*/ |
580 | 0 | if (!strstr(baseURL, "://")) { |
581 | 0 | info->protocol = gf_strdup("file"); |
582 | 0 | info->canonicalRepresentation = urlConcatenateWithBaseURL; |
583 | 0 | return GF_OK; |
584 | 0 | } |
585 | 0 | proto_offset = gf_dm_parse_protocol(urlConcatenateWithBaseURL, info); |
586 | 0 | } else { |
587 | 0 | proto_offset = -1; |
588 | 0 | } |
589 | | |
590 | 0 | if (proto_offset < 0) { |
591 | 0 | tmp = urlConcatenateWithBaseURL; |
592 | 0 | gf_assert( ! info->remotePath ); |
593 | 0 | info->remotePath = gf_url_percent_encode(tmp); |
594 | 0 | gf_free( urlConcatenateWithBaseURL ); |
595 | 0 | urlConcatenateWithBaseURL = NULL; |
596 | |
|
597 | 0 | if (!info->remotePath) { |
598 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[Network] No supported protocol for url %s\n", url)); |
599 | 0 | gf_dm_url_info_del(info); |
600 | 0 | return GF_BAD_PARAM; |
601 | 0 | } |
602 | 0 | for (i=0; i<strlen(info->remotePath); i++) |
603 | 0 | if (info->remotePath[i]=='\\') info->remotePath[i]='/'; |
604 | |
|
605 | 0 | info->canonicalRepresentation = NULL; |
606 | 0 | gf_dynstrcat(&info->canonicalRepresentation, info->protocol, NULL); |
607 | 0 | gf_dynstrcat(&info->canonicalRepresentation, "://", NULL); |
608 | 0 | gf_dynstrcat(&info->canonicalRepresentation, info->remotePath, NULL); |
609 | |
|
610 | 0 | return GF_OK; |
611 | 0 | } else { |
612 | | /* We continue the parsing as usual */ |
613 | 0 | url = urlConcatenateWithBaseURL + proto_offset; |
614 | 0 | } |
615 | 0 | } else { |
616 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[Network] No supported protocol for url %s\n", url)); |
617 | 0 | gf_dm_url_info_del(info); |
618 | 0 | return GF_BAD_PARAM; |
619 | 0 | } |
620 | 0 | } |
621 | 0 | gf_assert( proto_offset >= 0 ); |
622 | 0 | tmp = strchr(url, '/'); |
623 | | //patch for some broken url http://servername?cgi |
624 | 0 | if (!tmp) |
625 | 0 | tmp = strchr(url, '?'); |
626 | 0 | gf_assert( !info->remotePath ); |
627 | 0 | info->remotePath = gf_url_percent_encode(tmp ? tmp : "/"); |
628 | 0 | if (info->remotePath[0]=='?') { |
629 | 0 | char *rpath = NULL; |
630 | 0 | gf_dynstrcat(&rpath, "/", NULL); |
631 | 0 | gf_dynstrcat(&rpath, info->remotePath, NULL); |
632 | 0 | gf_free(info->remotePath); |
633 | 0 | info->remotePath = rpath; |
634 | 0 | } |
635 | |
|
636 | 0 | if (tmp) { |
637 | 0 | tmp[0] = 0; |
638 | 0 | copyOfUrl = gf_strdup(url); |
639 | 0 | tmp[0] = '/'; |
640 | 0 | } else { |
641 | 0 | copyOfUrl = gf_strdup(url); |
642 | 0 | } |
643 | 0 | tmp_url = copyOfUrl; |
644 | 0 | current_pos = tmp_url; |
645 | 0 | tmp = strrchr(tmp_url, '@'); |
646 | 0 | if (tmp) { |
647 | 0 | current_pos = tmp + 1; |
648 | 0 | gf_assert( ! info->server_name ); |
649 | 0 | info->server_name = gf_strdup(current_pos); |
650 | 0 | tmp[0] = 0; |
651 | 0 | tmp = strchr(tmp_url, ':'); |
652 | |
|
653 | 0 | if (tmp) { |
654 | 0 | tmp[0] = 0; |
655 | 0 | info->password = gf_strdup(tmp+1); |
656 | 0 | } |
657 | 0 | info->userName = gf_strdup(tmp_url); |
658 | 0 | } else { |
659 | 0 | gf_assert( ! info->server_name ); |
660 | 0 | info->server_name = gf_strdup(tmp_url); |
661 | 0 | } |
662 | | |
663 | | //scan for port number after IPv6 address ']' end char |
664 | 0 | ipv6 = strrchr(current_pos, ']'); |
665 | 0 | tmp = strrchr(ipv6 ? ipv6 : current_pos, ':'); |
666 | |
|
667 | 0 | if (tmp) { |
668 | 0 | info->port = atoi(tmp+1); |
669 | 0 | tmp[0] = 0; |
670 | 0 | if (info->server_name) { |
671 | 0 | gf_free(info->server_name); |
672 | 0 | } |
673 | 0 | info->server_name = gf_strdup(current_pos); |
674 | 0 | } |
675 | | |
676 | | /* We dont't want orig_url to contain user/passwords for security reasons or mismatch in cache hit */ |
677 | 0 | info->canonicalRepresentation = NULL; |
678 | 0 | gf_dynstrcat(&info->canonicalRepresentation, info->protocol, NULL); |
679 | 0 | gf_dynstrcat(&info->canonicalRepresentation, "://", NULL); |
680 | 0 | gf_dynstrcat(&info->canonicalRepresentation, info->server_name, NULL); |
681 | 0 | if (info->port && (info->port!=default_port)) { |
682 | 0 | char port[8]; |
683 | 0 | snprintf(port, sizeof(port)-1, ":%d", info->port); |
684 | 0 | gf_dynstrcat(&info->canonicalRepresentation, port, NULL); |
685 | 0 | } |
686 | 0 | gf_dynstrcat(&info->canonicalRepresentation, info->remotePath, NULL); |
687 | |
|
688 | 0 | gf_free(copyOfUrl); |
689 | 0 | if (urlConcatenateWithBaseURL) |
690 | 0 | gf_free(urlConcatenateWithBaseURL); |
691 | 0 | return GF_OK; |
692 | 0 | } |
693 | | |
694 | | GF_EXPORT |
695 | | GF_Err gf_dm_sess_setup_from_url(GF_DownloadSession *sess, const char *url, Bool allow_direct_reuse) |
696 | 0 | { |
697 | 0 | Bool socket_changed = GF_FALSE; |
698 | 0 | GF_URL_Info info; |
699 | 0 | char *sep_frag=NULL; |
700 | 0 | if (!url) |
701 | 0 | return GF_BAD_PARAM; |
702 | | |
703 | 0 | gf_dm_sess_clear_headers(sess); |
704 | 0 | sess->allow_direct_reuse = allow_direct_reuse; |
705 | 0 | gf_dm_url_info_init(&info); |
706 | |
|
707 | 0 | if (!sess->sock) |
708 | 0 | socket_changed = GF_TRUE; |
709 | 0 | else if (sess->status>GF_NETIO_DISCONNECTED) |
710 | 0 | socket_changed = GF_TRUE; |
711 | | |
712 | 0 | else if (sess->connection_timeout_ms) { |
713 | 0 | u32 diff = (u32) ( gf_sys_clock_high_res() - sess->last_fetch_time) / 1000; |
714 | 0 | if (diff >= sess->connection_timeout_ms) { |
715 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Timeout reached (%d ms since last vs %d ms timeout), reconnecting\n", sess->log_name, diff, sess->connection_timeout_ms)); |
716 | 0 | socket_changed = GF_TRUE; |
717 | 0 | } |
718 | 0 | } |
719 | |
|
720 | 0 | gf_fatal_assert(sess->status != GF_NETIO_WAIT_FOR_REPLY); |
721 | 0 | gf_fatal_assert(sess->status != GF_NETIO_DATA_EXCHANGE); |
722 | | //always detach task if any |
723 | 0 | if (sess->ftask) { |
724 | 0 | sess->ftask->sess = NULL; |
725 | 0 | sess->ftask = NULL; |
726 | 0 | } |
727 | | |
728 | | //strip fragment |
729 | 0 | sep_frag = strchr(url, '#'); |
730 | 0 | if (sep_frag) sep_frag[0]=0; |
731 | 0 | sess->last_error = gf_dm_get_url_info(url, &info, sess->orig_url); |
732 | 0 | if (sess->last_error) { |
733 | 0 | if (sep_frag) sep_frag[0]='#'; |
734 | 0 | return sess->last_error; |
735 | 0 | } |
736 | | |
737 | 0 | if (!strstr(url, "://")) { |
738 | 0 | char *sep; |
739 | 0 | gf_dm_url_info_del(&info); |
740 | 0 | gf_dm_url_info_init(&info); |
741 | 0 | info.port = sess->port; |
742 | 0 | info.server_name = sess->server_name ? gf_strdup(sess->server_name) : NULL; |
743 | 0 | info.remotePath = gf_strdup(url); |
744 | 0 | sep = strstr(sess->orig_url_before_redirect, "://"); |
745 | 0 | gf_assert(sep); |
746 | 0 | sep[0] = 0; |
747 | 0 | info.protocol = gf_strdup(sess->orig_url_before_redirect); |
748 | 0 | sep[0] = ':'; |
749 | 0 | } |
750 | |
|
751 | 0 | if (sess->port != info.port) { |
752 | 0 | socket_changed = GF_TRUE; |
753 | 0 | sess->port = info.port; |
754 | 0 | } |
755 | | #ifdef GPAC_HTTPMUX |
756 | | //safety in case we had a previous error but underlying stream_id was not reset |
757 | | sess->hmux_stream_id = -1; |
758 | | #endif |
759 | |
|
760 | 0 | if (sess->cached_file) { |
761 | 0 | socket_changed = GF_TRUE; |
762 | 0 | gf_fclose(sess->cached_file); |
763 | 0 | sess->cached_file = NULL; |
764 | 0 | if (sess->cache_entry) { |
765 | 0 | gf_cache_remove_entry_from_session(sess); |
766 | 0 | sess->cache_entry = NULL; |
767 | 0 | } |
768 | 0 | } |
769 | 0 | if (sess->log_name) { |
770 | 0 | gf_free(sess->log_name); |
771 | 0 | sess->log_name = NULL; |
772 | 0 | } |
773 | 0 | if (!strcmp("http", info.protocol) || !strcmp("https", info.protocol)) { |
774 | 0 | sess->log_name = gf_strdup("HTTP"); |
775 | 0 | if (sess->do_requests != http_do_requests) { |
776 | 0 | sess->do_requests = http_do_requests; |
777 | 0 | socket_changed = GF_TRUE; |
778 | 0 | } |
779 | 0 | Bool use_ssl = !strcmp("https", info.protocol) ? GF_TRUE : GF_FALSE; |
780 | | //if proxy, check scheme and port |
781 | 0 | const char *proxy = (sess->flags & GF_NETIO_SESSION_NO_PROXY) ? NULL : gf_opts_get_key("core", "proxy"); |
782 | 0 | if (proxy) { |
783 | 0 | if (!strnicmp(proxy, "http://", 7)) use_ssl = GF_FALSE; |
784 | 0 | else if (!strnicmp(proxy, "https://", 8)) use_ssl = GF_TRUE; |
785 | 0 | else if (strstr(proxy, ":80")) use_ssl = GF_FALSE; |
786 | 0 | else if (strstr(proxy, ":443")) use_ssl = GF_TRUE; |
787 | 0 | } |
788 | |
|
789 | 0 | if (use_ssl) { |
790 | 0 | if (!(sess->flags & GF_DOWNLOAD_SESSION_USE_SSL)) { |
791 | 0 | sess->flags |= GF_DOWNLOAD_SESSION_USE_SSL; |
792 | 0 | socket_changed = GF_TRUE; |
793 | 0 | } |
794 | 0 | } else if (sess->flags & GF_DOWNLOAD_SESSION_USE_SSL) { |
795 | 0 | sess->flags &= ~GF_DOWNLOAD_SESSION_USE_SSL; |
796 | 0 | socket_changed = GF_TRUE; |
797 | 0 | } |
798 | 0 | } else { |
799 | 0 | sess->do_requests = NULL; |
800 | |
|
801 | | #ifdef GPAC_HAS_CURL |
802 | | if (!sess->dm->curl_multi) { |
803 | | sess->dm->curl_multi = curl_multi_init(); |
804 | | } |
805 | | #endif |
806 | |
|
807 | 0 | } |
808 | |
|
809 | 0 | if (sess->server_name && info.server_name && !strcmp(sess->server_name, info.server_name)) { |
810 | 0 | } else { |
811 | 0 | socket_changed = GF_TRUE; |
812 | 0 | if (sess->server_name) gf_free(sess->server_name); |
813 | 0 | sess->server_name = info.server_name ? gf_strdup(info.server_name) : NULL; |
814 | 0 | } |
815 | |
|
816 | 0 | if (info.canonicalRepresentation) { |
817 | 0 | if (sess->orig_url) gf_free(sess->orig_url); |
818 | 0 | sess->orig_url = gf_strdup(info.canonicalRepresentation); |
819 | 0 | } else { |
820 | 0 | if (sess->orig_url) gf_free(sess->orig_url); |
821 | 0 | sess->orig_url = gf_strdup(info.protocol); |
822 | 0 | gf_dynstrcat(&sess->orig_url, info.server_name, "://"); |
823 | 0 | if (info.port) { |
824 | 0 | char szTmp[10]; |
825 | 0 | sprintf(szTmp, ":%u", info.port); |
826 | 0 | gf_dynstrcat(&sess->orig_url, szTmp, NULL); |
827 | 0 | } |
828 | 0 | gf_dynstrcat(&sess->orig_url, info.remotePath, NULL); |
829 | 0 | } |
830 | |
|
831 | 0 | if (!sess->orig_url_before_redirect) |
832 | 0 | sess->orig_url_before_redirect = gf_strdup(url); |
833 | |
|
834 | 0 | if (sess->remote_path) gf_free(sess->remote_path); |
835 | 0 | sess->remote_path = gf_strdup(info.remotePath); |
836 | |
|
837 | 0 | if (sess->status==GF_NETIO_STATE_ERROR) |
838 | 0 | socket_changed = GF_TRUE; |
839 | |
|
840 | 0 | if (!socket_changed && info.userName && !strcmp(info.userName, sess->creds->username)) { |
841 | 0 | } else { |
842 | 0 | sess->creds = NULL; |
843 | 0 | if (info.userName) { |
844 | 0 | if (! sess->dm) { |
845 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] Did not found any download manager, credentials not supported\n", sess->log_name)); |
846 | 0 | } else |
847 | 0 | sess->creds = gf_user_credentials_register(sess->dm, !strcmp("https", info.protocol), sess->server_name, info.userName, info.password, info.userName && info.password); |
848 | 0 | } |
849 | 0 | } |
850 | 0 | gf_dm_url_info_del(&info); |
851 | 0 | if (sep_frag) sep_frag[0]='#'; |
852 | |
|
853 | | #ifdef GPAC_HTTPMUX |
854 | | if (sess->hmux_sess) { |
855 | | if (sess->hmux_sess->do_shutdown) |
856 | | socket_changed = GF_TRUE; |
857 | | sess->hmux_buf.size = 0; |
858 | | sess->hmux_buf.offset = 0; |
859 | | } |
860 | | #endif |
861 | | |
862 | |
|
863 | | #ifdef GPAC_HAS_CURL |
864 | | if (sess->dm->curl_multi && (!sess->do_requests || gf_opts_get_bool("core", "curl")) |
865 | | //if the caller wants a GF_Socket interface, we cannot use curl |
866 | | && !(sess->flags & GF_NETIO_SESSION_SHARE_SOCKET) |
867 | | ) { |
868 | | GF_Err e = curl_setup_session(sess); |
869 | | if (e) return e; |
870 | | } else |
871 | | #endif |
872 | |
|
873 | 0 | if (sess->sock && !socket_changed) { |
874 | 0 | if (!sess->do_requests) return GF_NOT_SUPPORTED; |
875 | 0 | sess->status = GF_NETIO_CONNECTED; |
876 | 0 | sess->last_error = GF_OK; |
877 | | //reset number of retry and start time |
878 | 0 | sess->num_retry = SESSION_RETRY_COUNT; |
879 | 0 | sess->start_time = 0; |
880 | 0 | sess->needs_cache_reconfig = 1; |
881 | 0 | } else { |
882 | |
|
883 | 0 | if (!sess->do_requests) return GF_NOT_SUPPORTED; |
884 | | #ifdef GPAC_HTTPMUX |
885 | | if (sess->hmux_sess) { |
886 | | gf_mx_p(sess->mx); |
887 | | hmux_detach_session(sess->hmux_sess, sess); |
888 | | gf_mx_v(sess->mx); |
889 | | } |
890 | | #endif |
891 | | |
892 | 0 | dm_sess_sk_del(sess); |
893 | |
|
894 | 0 | sess->status = GF_NETIO_SETUP; |
895 | 0 | sess->last_error = GF_OK; |
896 | | //reset number of retry and start time |
897 | 0 | sess->num_retry = SESSION_RETRY_COUNT; |
898 | 0 | sess->start_time = 0; |
899 | 0 | #ifdef GPAC_HAS_SSL |
900 | 0 | if (sess->ssl) { |
901 | | // SSL_shutdown(sess->ssl); |
902 | | // SSL_free(sess->ssl); |
903 | | // gnutls_bye(sess->ssl, GNUTLS_SHUT_WR); |
904 | | // gnutls_deinit(sess->ssl); |
905 | 0 | gf_ssl_shutdown(sess->ssl); |
906 | 0 | sess->ssl = NULL; |
907 | 0 | } |
908 | 0 | #endif |
909 | 0 | } |
910 | 0 | sess->total_size = 0; |
911 | 0 | sess->bytes_done = 0; |
912 | 0 | sess->full_resource_size = 0; |
913 | | //could be not-0 after a byte-range request using chunk transfer |
914 | 0 | sess->remaining_data_size = 0; |
915 | |
|
916 | 0 | sess->local_cache_only = GF_FALSE; |
917 | 0 | if (sess->dm && sess->dm->local_cache_url_provider_cbk) { |
918 | 0 | Bool res = sess->dm->local_cache_url_provider_cbk(sess->dm->lc_udta, (char *)url, GF_FALSE); |
919 | 0 | if (res == GF_TRUE) { |
920 | 0 | sess->local_cache_only = GF_TRUE; |
921 | 0 | gf_free(sess->orig_url); |
922 | 0 | sess->orig_url = gf_strdup(url); |
923 | 0 | SET_LAST_ERR(sess->last_error) |
924 | 0 | sess->use_cache_file = GF_TRUE; |
925 | 0 | gf_dm_configure_cache(sess); |
926 | 0 | sess->bytes_done = 0; |
927 | 0 | if (sess->cache_entry && gf_cache_is_deleted(sess->cache_entry)) { |
928 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
929 | 0 | SET_LAST_ERR(GF_URL_REMOVED) |
930 | | //return GF_OK; |
931 | 0 | } else if (! gf_cache_is_done(sess->cache_entry)) { |
932 | 0 | sess->total_size = 0; |
933 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
934 | 0 | } else { |
935 | 0 | sess->total_size = gf_cache_get_content_length(sess->cache_entry); |
936 | 0 | sess->bytes_done = sess->total_size; |
937 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
938 | 0 | if (!sess->cache_entry) { |
939 | 0 | SET_LAST_ERR(GF_URL_ERROR) |
940 | 0 | } else { |
941 | 0 | SET_LAST_ERR(GF_OK) |
942 | 0 | } |
943 | 0 | } |
944 | |
|
945 | 0 | sess->total_time_since_req = gf_cache_get_downtime(sess->cache_entry); |
946 | 0 | if (sess->total_time_since_req) |
947 | 0 | sess->bytes_per_sec = (u32) ((1000 * (u64) sess->bytes_done) / sess->total_time_since_req); |
948 | 0 | else |
949 | 0 | sess->bytes_per_sec = 0; |
950 | 0 | gf_dm_sess_reload_cached_headers(sess); |
951 | 0 | } |
952 | 0 | } |
953 | 0 | if (sess->last_error) |
954 | 0 | return sess->last_error; |
955 | 0 | return gf_dm_sess_set_range(sess, 0, 0, GF_TRUE); |
956 | 0 | } |
957 | | |
958 | | |
959 | | Bool gf_dm_session_do_task(GF_DownloadSession *sess) |
960 | 0 | { |
961 | 0 | Bool do_run = GF_TRUE; |
962 | |
|
963 | 0 | if (sess->destroy) { |
964 | 0 | do_run = GF_FALSE; |
965 | 0 | } else { |
966 | 0 | Bool unlock = sess->mx ? GF_TRUE : GF_FALSE; |
967 | 0 | gf_mx_p(sess->mx); |
968 | 0 | if (sess->status >= GF_NETIO_DATA_TRANSFERED) { |
969 | 0 | do_run = GF_FALSE; |
970 | 0 | } else { |
971 | 0 | if (sess->status < GF_NETIO_CONNECTED) { |
972 | 0 | gf_dm_connect(sess); |
973 | 0 | } else { |
974 | 0 | sess->do_requests(sess); |
975 | 0 | } |
976 | 0 | } |
977 | 0 | if (unlock) |
978 | 0 | gf_mx_v(sess->mx); |
979 | 0 | } |
980 | 0 | if (do_run) return GF_TRUE; |
981 | | |
982 | | /*destroy all session but keep connection active*/ |
983 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
984 | 0 | if (sess->last_error==GF_EOS) sess->last_error = GF_OK; |
985 | 0 | else if (sess->last_error==GF_IP_NETWORK_EMPTY) sess->last_error = GF_OK; |
986 | 0 | else if (sess->last_error) { |
987 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
988 | 0 | } |
989 | 0 | return GF_FALSE; |
990 | 0 | } |
991 | | |
992 | | Bool gf_dm_session_task(GF_FilterSession *fsess, void *callback, u32 *reschedule_ms) |
993 | 0 | { |
994 | 0 | GF_SessTask *task = callback; |
995 | 0 | GF_DownloadSession *sess = task->sess; |
996 | 0 | if (!sess || sess->destroy) { |
997 | 0 | gf_free(task); |
998 | 0 | if (sess) sess->ftask = NULL; |
999 | 0 | return GF_FALSE; |
1000 | 0 | } |
1001 | 0 | task->in_task = GF_TRUE; |
1002 | 0 | Bool ret = gf_dm_session_do_task(sess); |
1003 | 0 | task->in_task = GF_FALSE; |
1004 | 0 | if (ret) { |
1005 | 0 | if (sess->rate_regulated) { |
1006 | 0 | *reschedule_ms = (sess->last_cap_rate_bytes_per_sec > sess->max_data_rate) ? 1000 : 100; |
1007 | 0 | } else { |
1008 | 0 | *reschedule_ms = 1; |
1009 | 0 | }; |
1010 | 0 | return GF_TRUE; |
1011 | 0 | } |
1012 | 0 | gf_assert(sess->ftask); |
1013 | 0 | gf_free(sess->ftask); |
1014 | 0 | sess->ftask = NULL; |
1015 | 0 | if (sess->destroy) { |
1016 | 0 | gf_dm_sess_del(sess); |
1017 | 0 | } |
1018 | 0 | return GF_FALSE; |
1019 | 0 | } |
1020 | | |
1021 | | #ifndef GPAC_DISABLE_THREADS |
1022 | | static u32 gf_dm_session_thread(void *par) |
1023 | 0 | { |
1024 | 0 | GF_DownloadSession *sess = (GF_DownloadSession *)par; |
1025 | 0 | if (!sess) return 0; |
1026 | | |
1027 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_CORE, ("[Downloader] Entering thread ID %d\n", gf_th_id() )); |
1028 | 0 | sess->flags &= ~GF_DOWNLOAD_SESSION_THREAD_DEAD; |
1029 | 0 | while (!sess->destroy) { |
1030 | 0 | Bool ret = gf_dm_session_do_task(sess); |
1031 | 0 | if (!ret) break; |
1032 | 0 | gf_sleep(sess->rate_regulated ? 100 : 0); |
1033 | 0 | } |
1034 | 0 | sess->flags |= GF_DOWNLOAD_SESSION_THREAD_DEAD; |
1035 | 0 | if (sess->destroy) |
1036 | 0 | gf_dm_sess_del(sess); |
1037 | 0 | return 1; |
1038 | 0 | } |
1039 | | #endif |
1040 | | |
1041 | | GF_DownloadSession *gf_dm_sess_new_internal(GF_DownloadManager * dm, const char *url, u32 dl_flags, |
1042 | | gf_dm_user_io user_io, |
1043 | | void *usr_cbk, |
1044 | | GF_Socket *server, |
1045 | | Bool force_server, |
1046 | | GF_Err *e) |
1047 | 0 | { |
1048 | 0 | GF_DownloadSession *sess; |
1049 | |
|
1050 | 0 | GF_SAFEALLOC(sess, GF_DownloadSession); |
1051 | 0 | if (!sess) { |
1052 | 0 | return NULL; |
1053 | 0 | } |
1054 | | #ifdef GPAC_HTTPMUX |
1055 | | sess->hmux_stream_id = -1; |
1056 | | #endif |
1057 | 0 | sess->headers = gf_list_new(); |
1058 | 0 | sess->flags = dl_flags; |
1059 | 0 | if (sess->flags & GF_NETIO_SESSION_NOTIFY_DATA) |
1060 | 0 | sess->force_data_write_callback = GF_TRUE; |
1061 | 0 | sess->user_proc = user_io; |
1062 | 0 | sess->usr_cbk = usr_cbk; |
1063 | 0 | sess->creds = NULL; |
1064 | 0 | sess->log_name = gf_strdup("HTTP"); |
1065 | 0 | sess->http_buf_size = dm ? dm->read_buf_size : GF_DOWNLOAD_BUFFER_SIZE; |
1066 | 0 | sess->http_buf = gf_malloc(sess->http_buf_size + 1); |
1067 | |
|
1068 | 0 | sess->conn_timeout = gf_opts_get_int("core", "tcp-timeout"); |
1069 | 0 | if (!sess->conn_timeout) sess->conn_timeout = 5000; |
1070 | |
|
1071 | 0 | sess->request_timeout = gf_opts_get_int("core", "req-timeout"); |
1072 | |
|
1073 | 0 | sess->chunk_wnd_dur = gf_opts_get_int("core", "cte-rate-wnd") * 1000; |
1074 | 0 | if (!sess->chunk_wnd_dur) sess->chunk_wnd_dur = 20000; |
1075 | |
|
1076 | 0 | sess->dm = dm; |
1077 | 0 | if (server || force_server) { |
1078 | 0 | sess->sock = server; |
1079 | 0 | sess->flags = GF_NETIO_SESSION_NOT_THREADED; |
1080 | 0 | sess->status = GF_NETIO_CONNECTED; |
1081 | 0 | sess->server_mode = GF_TRUE; |
1082 | 0 | sess->do_requests = http_do_requests; |
1083 | 0 | if (e) *e = GF_OK; |
1084 | 0 | if (dl_flags & GF_NETIO_SESSION_NO_BLOCK) { |
1085 | 0 | sess->flags |= GF_NETIO_SESSION_NO_BLOCK; |
1086 | 0 | if (server) |
1087 | 0 | gf_sk_set_block_mode(server, GF_TRUE); |
1088 | 0 | } |
1089 | 0 | return sess; |
1090 | 0 | } |
1091 | | |
1092 | 0 | if (!sess->conn_timeout) sess->server_only_understand_get = GF_TRUE; |
1093 | 0 | if (dm) |
1094 | 0 | sess->disable_cache = dm->disable_cache; |
1095 | |
|
1096 | 0 | if (! (dl_flags & GF_NETIO_SESSION_NOT_THREADED) |
1097 | | #ifdef GPAC_HAS_CURL |
1098 | | && !sess->curl_hnd |
1099 | | #endif |
1100 | 0 | ) { |
1101 | 0 | sess->mx = gf_mx_new(url); |
1102 | 0 | if (!sess->mx) { |
1103 | 0 | gf_free(sess); |
1104 | 0 | return NULL; |
1105 | 0 | } |
1106 | 0 | } |
1107 | | |
1108 | 0 | if ((dm->h3_mode == H3_MODE_FIRST) || (dm->h3_mode == H3_MODE_ONLY)) |
1109 | 0 | sess->flags |= GF_NETIO_SESSION_USE_QUIC; |
1110 | 0 | else if (dm->h3_mode == H3_MODE_AUTO) |
1111 | 0 | sess->flags |= GF_NETIO_SESSION_RETRY_QUIC; |
1112 | |
|
1113 | 0 | *e = gf_dm_sess_setup_from_url(sess, url, GF_FALSE); |
1114 | 0 | if (*e) { |
1115 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[Downloader] failed to create session for %s: %s\n", url, gf_error_to_string(*e))); |
1116 | 0 | gf_dm_sess_del(sess); |
1117 | 0 | return NULL; |
1118 | 0 | } |
1119 | 0 | gf_assert( sess ); |
1120 | 0 | sess->num_retry = SESSION_RETRY_COUNT; |
1121 | | /*threaded session must be started with gf_dm_sess_process*/ |
1122 | 0 | return sess; |
1123 | 0 | } |
1124 | | |
1125 | | GF_EXPORT |
1126 | | GF_DownloadSession *gf_dm_sess_new_server(GF_DownloadManager *dm, GF_Socket *server, |
1127 | | void *ssl_sock_ctx, |
1128 | | gf_dm_user_io user_io, |
1129 | | void *usr_cbk, |
1130 | | Bool async, |
1131 | | GF_Err *e) |
1132 | 0 | { |
1133 | 0 | GF_DownloadSession *sess = gf_dm_sess_new_internal(dm, NULL, async ? GF_NETIO_SESSION_NO_BLOCK : 0, user_io, usr_cbk, server, GF_TRUE, e); |
1134 | 0 | if (!sess) return NULL; |
1135 | | |
1136 | 0 | #ifdef GPAC_HAS_SSL |
1137 | 0 | if (ssl_sock_ctx) { |
1138 | 0 | sess->ssl = ssl_sock_ctx; |
1139 | 0 | gf_dm_sess_server_setup_ssl(sess); |
1140 | 0 | } |
1141 | 0 | #endif |
1142 | 0 | return sess; |
1143 | 0 | } |
1144 | | |
1145 | | |
1146 | | u32 gf_dm_sess_subsession_count(GF_DownloadSession *sess) |
1147 | 0 | { |
1148 | | #ifdef GPAC_HTTPMUX |
1149 | | if (sess->hmux_sess) |
1150 | | return gf_list_count(sess->hmux_sess->sessions); |
1151 | | #endif |
1152 | 0 | return 1; |
1153 | 0 | } |
1154 | | |
1155 | | |
1156 | | void gf_dm_sess_server_reset(GF_DownloadSession *sess) |
1157 | 0 | { |
1158 | 0 | if (!sess->server_mode) return; |
1159 | | //DO NOT clear headers if H2: the underlying h2 session could have been ended (stream_id=0) and then reassigned |
1160 | | //when processsing another session, so the headers would be the ones of the new stream_id |
1161 | | //for H2, the reset is done when reassigning sessions |
1162 | | #ifdef GPAC_HTTPMUX |
1163 | | if (!sess->hmux_sess) |
1164 | | #endif |
1165 | 0 | gf_dm_sess_clear_headers(sess); |
1166 | |
|
1167 | 0 | sess->total_size = sess->bytes_done = 0; |
1168 | 0 | sess->async_buf_size = 0; |
1169 | 0 | sess->chunk_bytes = 0; |
1170 | 0 | sess->chunk_header_bytes = 0; |
1171 | 0 | sess->chunked = GF_FALSE; |
1172 | 0 | sess->status = GF_NETIO_CONNECTED; |
1173 | 0 | } |
1174 | | |
1175 | | |
1176 | | GF_EXPORT |
1177 | | GF_DownloadSession *gf_dm_sess_new_simple(GF_DownloadManager * dm, const char *url, u32 dl_flags, |
1178 | | gf_dm_user_io user_io, |
1179 | | void *usr_cbk, |
1180 | | GF_Err *e) |
1181 | 0 | { |
1182 | 0 | return gf_dm_sess_new_internal(dm, url, dl_flags, user_io, usr_cbk, NULL, GF_FALSE, e); |
1183 | 0 | } |
1184 | | GF_EXPORT |
1185 | | GF_DownloadSession *gf_dm_sess_new(GF_DownloadManager *dm, const char *url, u32 dl_flags, |
1186 | | gf_dm_user_io user_io, |
1187 | | void *usr_cbk, |
1188 | | GF_Err *e) |
1189 | 0 | { |
1190 | 0 | GF_DownloadSession *sess; |
1191 | 0 | *e = GF_OK; |
1192 | 0 | if (gf_dm_is_local(dm, url)) { |
1193 | 0 | *e = GF_NOT_SUPPORTED; |
1194 | 0 | return NULL; |
1195 | 0 | } |
1196 | | |
1197 | 0 | if (!gf_dm_can_handle_url(url)) { |
1198 | 0 | *e = GF_NOT_SUPPORTED; |
1199 | 0 | return NULL; |
1200 | 0 | } |
1201 | 0 | sess = gf_dm_sess_new_simple(dm, url, dl_flags, user_io, usr_cbk, e); |
1202 | 0 | if (sess && dm) { |
1203 | 0 | sess->dm = dm; |
1204 | 0 | gf_mx_p(dm->cache_mx); |
1205 | 0 | gf_list_add(dm->all_sessions, sess); |
1206 | 0 | gf_mx_v(dm->cache_mx); |
1207 | 0 | } |
1208 | 0 | return sess; |
1209 | 0 | } |
1210 | | |
1211 | | |
1212 | | GF_Err gf_dm_sess_read(GF_DownloadSession *sess, char *data, u32 data_size, u32 *out_read) |
1213 | 0 | { |
1214 | 0 | GF_Err e; |
1215 | |
|
1216 | 0 | if (sess->cached_file) { |
1217 | 0 | *out_read = (u32) gf_fread(data, data_size, sess->cached_file); |
1218 | 0 | if (! *out_read && gf_cache_is_done(sess->cache_entry)) { |
1219 | 0 | sess->total_size = gf_cache_get_content_length(sess->cache_entry); |
1220 | 0 | if (sess->total_size != sess->bytes_done) { |
1221 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
1222 | 0 | SET_LAST_ERR(GF_CORRUPTED_DATA); |
1223 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] Broken cache for %s: %u bytes in cache but %u advertized\n", sess->log_name, sess->orig_url, sess->bytes_done, sess->total_size)); |
1224 | 0 | return GF_CORRUPTED_DATA; |
1225 | 0 | } |
1226 | 0 | } |
1227 | 0 | return GF_OK; |
1228 | 0 | } |
1229 | | |
1230 | | #ifdef GPAC_HAS_CURL |
1231 | | if (sess->curl_hnd) { |
1232 | | return curl_read_data(sess, data, data_size, out_read); |
1233 | | } |
1234 | | #endif |
1235 | | |
1236 | 0 | if (sess->dm && sess->dm->simulate_no_connection) { |
1237 | 0 | if (sess->sock) { |
1238 | 0 | sess->status = GF_NETIO_DISCONNECTED; |
1239 | 0 | } |
1240 | 0 | return GF_IP_NETWORK_FAILURE; |
1241 | 0 | } |
1242 | | |
1243 | 0 | if (!sess) |
1244 | 0 | return GF_BAD_PARAM; |
1245 | | |
1246 | 0 | if (sess->server_mode && (sess->flags & GF_NETIO_SESSION_USE_QUIC)) |
1247 | 0 | return GF_IP_NETWORK_EMPTY; |
1248 | | |
1249 | 0 | gf_mx_p(sess->mx); |
1250 | 0 | if (!sess->sock) { |
1251 | 0 | sess->status = GF_NETIO_DISCONNECTED; |
1252 | 0 | gf_mx_v(sess->mx); |
1253 | 0 | return GF_IP_CONNECTION_CLOSED; |
1254 | 0 | } |
1255 | | |
1256 | 0 | *out_read = 0; |
1257 | |
|
1258 | 0 | #ifdef GPAC_HAS_SSL |
1259 | 0 | if (sess->ssl && !(sess->flags & GF_NETIO_SESSION_USE_QUIC)) { |
1260 | 0 | e = gf_ssl_read_data(sess, data, data_size, out_read); |
1261 | 0 | if (e==GF_NOT_READY) { |
1262 | 0 | gf_mx_v(sess->mx); |
1263 | 0 | return GF_IP_NETWORK_EMPTY; |
1264 | 0 | } |
1265 | 0 | } else |
1266 | 0 | #endif |
1267 | 0 | e = gf_sk_receive(sess->sock, data, data_size, out_read); |
1268 | | |
1269 | | #ifdef GPAC_HTTPMUX |
1270 | | if (sess->hmux_sess) { |
1271 | | GF_Err hme = sess->hmux_sess->data_received(sess, data, *out_read); |
1272 | | if (hme) { |
1273 | | gf_mx_v(sess->mx); |
1274 | | *out_read = 0; |
1275 | | return hme; |
1276 | | } |
1277 | | } |
1278 | | #endif //GPAC_HTTPMUX |
1279 | | |
1280 | 0 | if (*out_read) |
1281 | 0 | sess->last_fetch_time = gf_sys_clock_high_res(); |
1282 | |
|
1283 | 0 | gf_mx_v(sess->mx); |
1284 | 0 | return e; |
1285 | 0 | } |
1286 | | |
1287 | | static void gf_dm_connect(GF_DownloadSession *sess) |
1288 | 0 | { |
1289 | 0 | GF_Err e; |
1290 | 0 | Bool register_sock; |
1291 | 0 | Bool allow_offline = sess->dm ? sess->dm->allow_offline_cache : GF_FALSE; |
1292 | 0 | u16 proxy_port = 0; |
1293 | 0 | char szProxy[GF_MAX_PATH]; |
1294 | 0 | const char *proxy; |
1295 | | |
1296 | | //offline cache enabled, setup cache before connection |
1297 | 0 | if (!sess->connect_pending && allow_offline) { |
1298 | 0 | gf_dm_configure_cache(sess); |
1299 | 0 | if (sess->cached_file) return; |
1300 | 0 | } |
1301 | | |
1302 | | #ifdef GPAC_HAS_CURL |
1303 | | if (sess->curl_hnd) { |
1304 | | curl_connect(sess); |
1305 | | return; |
1306 | | } |
1307 | | #endif |
1308 | | |
1309 | | #ifdef GPAC_HTTPMUX |
1310 | | if (sess->hmux_switch_sess) { |
1311 | | sess->hmux_switch_sess = 0; |
1312 | | gf_mx_p(sess->mx); |
1313 | | hmux_detach_session(sess->hmux_sess, sess); |
1314 | | gf_mx_v(sess->mx); |
1315 | | |
1316 | | if (sess->num_retry) { |
1317 | | SET_LAST_ERR(GF_OK) |
1318 | | sess->num_retry--; |
1319 | | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] stream_id "LLD" (%s) refused by server, retrying and marking session as no longer available\n", sess->log_name, sess->hmux_stream_id, sess->remote_path ? sess->remote_path : sess->orig_url)); |
1320 | | |
1321 | | sess->hmux_stream_id = -1; |
1322 | | } else { |
1323 | | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("%s] stream_id "LLD" (%s) refused by server after all retries, marking session as no longer available\n", sess->log_name, sess->hmux_stream_id, sess->remote_path ? sess->remote_path : sess->orig_url)); |
1324 | | sess->status = GF_NETIO_STATE_ERROR; |
1325 | | SET_LAST_ERR(GF_REMOTE_SERVICE_ERROR) |
1326 | | sess->hmux_stream_id = -1; |
1327 | | return; |
1328 | | } |
1329 | | } |
1330 | | if (sess->hmux_sess && sess->hmux_sess->connected) { |
1331 | | sess->connect_time = 0; |
1332 | | sess->status = GF_NETIO_CONNECTED; |
1333 | | sess->last_error = GF_OK; |
1334 | | gf_dm_sess_notify_state(sess, GF_NETIO_CONNECTED, GF_OK); |
1335 | | if (!allow_offline) |
1336 | | gf_dm_configure_cache(sess); |
1337 | | return; |
1338 | | } |
1339 | | Bool attach_to_parent_sess = GF_FALSE; |
1340 | | if (sess->dm && !sess->sock) { |
1341 | | #ifdef GPAC_HAS_HTTP2 |
1342 | | if (!sess->dm->disable_http2 && (sess->h2_upgrade_state<3)) attach_to_parent_sess = GF_TRUE; |
1343 | | #endif |
1344 | | } |
1345 | | |
1346 | | if (attach_to_parent_sess) { |
1347 | | gf_mx_p(sess->dm->cache_mx); |
1348 | | u32 i, count = gf_list_count(sess->dm->all_sessions); |
1349 | | for (i=0; i<count; i++) { |
1350 | | GF_DownloadSession *a_sess = gf_list_get(sess->dm->all_sessions, i); |
1351 | | if (strcmp(a_sess->server_name, sess->server_name) || (a_sess->port != sess->port)) continue; |
1352 | | if (!a_sess->hmux_sess) { |
1353 | | #ifdef GPAC_HAS_HTTP2 |
1354 | | //we already ahd a connection to this server with H2 failure, do not try h2 |
1355 | | if (a_sess->h2_upgrade_state==4) { |
1356 | | sess->h2_upgrade_state=4; |
1357 | | break; |
1358 | | } |
1359 | | #endif |
1360 | | //we have the same server name |
1361 | | //trick in non-block mode, we want to wait for upgrade to be completed |
1362 | | //before creating a new socket |
1363 | | if ((a_sess != sess) |
1364 | | && a_sess->sock |
1365 | | && (a_sess->status <= GF_NETIO_WAIT_FOR_REPLY) |
1366 | | && (sess->flags & GF_NETIO_SESSION_NO_BLOCK) |
1367 | | #ifdef GPAC_HAS_HTTP2 |
1368 | | && (a_sess->h2_upgrade_state<2) |
1369 | | && !sess->dm->disable_http2 |
1370 | | && !gf_opts_get_bool("core", "no-h2c") |
1371 | | #endif |
1372 | | //TODO: H3 test |
1373 | | ) { |
1374 | | sess->connect_pending = 1; |
1375 | | SET_LAST_ERR(GF_IP_NETWORK_EMPTY) |
1376 | | gf_mx_v(sess->dm->cache_mx); |
1377 | | return; |
1378 | | } |
1379 | | continue; |
1380 | | } |
1381 | | if (a_sess->hmux_sess->do_shutdown) continue; |
1382 | | |
1383 | | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] associating session %s to existing %s session: %s:%u\n", sess->log_name, sess->remote_path ? sess->remote_path : sess->orig_url, a_sess->log_name, a_sess->server_name, a_sess->port)); |
1384 | | |
1385 | | u32 nb_locks=0; |
1386 | | if (sess->mx) { |
1387 | | nb_locks = gf_mx_get_num_locks(sess->mx); |
1388 | | if (nb_locks) gf_mx_v(sess->mx); |
1389 | | gf_mx_del(sess->mx); |
1390 | | } |
1391 | | sess->hmux_sess = a_sess->hmux_sess; |
1392 | | sess->mx = a_sess->hmux_sess->mx; |
1393 | | if (nb_locks) |
1394 | | gf_mx_p(sess->mx); |
1395 | | |
1396 | | sess->sock = a_sess->sock; |
1397 | | #ifdef GPAC_HAS_SSL |
1398 | | sess->ssl = a_sess->ssl; |
1399 | | #endif |
1400 | | sess->proxy_enabled = a_sess->proxy_enabled; |
1401 | | sess->hmux_sess->setup_session(sess, GF_FALSE); |
1402 | | |
1403 | | gf_list_add(sess->hmux_sess->sessions, sess); |
1404 | | |
1405 | | //reconfigure cache |
1406 | | if (sess->allow_direct_reuse) { |
1407 | | gf_dm_configure_cache(sess); |
1408 | | if (sess->cached_file) { |
1409 | | gf_mx_v(sess->dm->cache_mx); |
1410 | | return; |
1411 | | } |
1412 | | } |
1413 | | |
1414 | | sess->connect_time = 0; |
1415 | | if (sess->hmux_sess->connected) { |
1416 | | sess->status = GF_NETIO_CONNECTED; |
1417 | | gf_dm_sess_notify_state(sess, GF_NETIO_CONNECTED, GF_OK); |
1418 | | gf_dm_configure_cache(sess); |
1419 | | } |
1420 | | gf_mx_v(sess->dm->cache_mx); |
1421 | | return; |
1422 | | } // end all_sessions loop |
1423 | | gf_mx_v(sess->dm->cache_mx); |
1424 | | } |
1425 | | #endif |
1426 | | |
1427 | 0 | resetup_socket: |
1428 | |
|
1429 | 0 | register_sock = GF_FALSE; |
1430 | 0 | if (!sess->sock) { |
1431 | 0 | u32 sock_type = GF_SOCK_TYPE_TCP; |
1432 | 0 | if (sess->flags & GF_NETIO_SESSION_USE_QUIC) |
1433 | 0 | sock_type = GF_SOCK_TYPE_UDP; |
1434 | |
|
1435 | 0 | sess->sock = gf_sk_new_ex(sock_type, sess->netcap_id); |
1436 | |
|
1437 | 0 | if (sess->sock && (sess->flags & GF_NETIO_SESSION_NO_BLOCK)) |
1438 | 0 | gf_sk_set_block_mode(sess->sock, GF_TRUE); |
1439 | |
|
1440 | 0 | if (sess->sock_group) register_sock = GF_TRUE; |
1441 | 0 | } |
1442 | | |
1443 | | /*connect*/ |
1444 | 0 | sess->status = GF_NETIO_SETUP; |
1445 | 0 | gf_dm_sess_notify_state(sess, sess->status, GF_OK); |
1446 | | |
1447 | | /*PROXY setup*/ |
1448 | 0 | if (sess->proxy_enabled!=2) { |
1449 | 0 | proxy = (sess->flags & GF_NETIO_SESSION_NO_PROXY) ? NULL : gf_opts_get_key("core", "proxy"); |
1450 | 0 | if (proxy) { |
1451 | 0 | u32 i; |
1452 | 0 | proxy_port = 80; |
1453 | 0 | char *sep = strstr(proxy, "://"); |
1454 | 0 | gf_strcpy(szProxy, sep ? sep+3 : proxy); |
1455 | 0 | sep = strchr(szProxy, ':'); |
1456 | 0 | if (sep) { |
1457 | 0 | proxy_port = atoi(sep+1); |
1458 | 0 | sep[0]=0; |
1459 | 0 | } |
1460 | 0 | proxy = szProxy; |
1461 | 0 | Bool use_proxy=GF_TRUE; |
1462 | 0 | for (i=0; i<gf_list_count(sess->dm->skip_proxy_servers); i++) { |
1463 | 0 | char *skip = (char*)gf_list_get(sess->dm->skip_proxy_servers, i); |
1464 | 0 | if (!strcmp(skip, sess->server_name)) { |
1465 | 0 | use_proxy=GF_FALSE; |
1466 | 0 | break; |
1467 | 0 | } |
1468 | 0 | } |
1469 | 0 | if (!use_proxy) { |
1470 | 0 | proxy = NULL; |
1471 | 0 | } else { |
1472 | 0 | sess->proxy_enabled = 1; |
1473 | 0 | } |
1474 | 0 | } else { |
1475 | 0 | proxy = NULL; |
1476 | 0 | sess->proxy_enabled = 0; |
1477 | 0 | } |
1478 | 0 | } else { |
1479 | 0 | proxy = NULL; |
1480 | 0 | } |
1481 | | |
1482 | |
|
1483 | 0 | if (!proxy) { |
1484 | 0 | proxy = sess->server_name; |
1485 | 0 | proxy_port = sess->port; |
1486 | 0 | } |
1487 | 0 | if (!sess->connect_pending) { |
1488 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Connecting to %s:%d for URL %s\n", sess->log_name, proxy, proxy_port, sess->remote_path ? sess->remote_path : "undefined")); |
1489 | 0 | } |
1490 | |
|
1491 | 0 | if ((sess->status == GF_NETIO_SETUP) && (sess->connect_pending<2)) { |
1492 | 0 | u64 now; |
1493 | 0 | if (sess->dm && sess->dm->simulate_no_connection) { |
1494 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
1495 | 0 | SET_LAST_ERR(GF_IP_NETWORK_FAILURE) |
1496 | 0 | gf_dm_sess_notify_state(sess, sess->status, sess->last_error); |
1497 | 0 | return; |
1498 | 0 | } |
1499 | | |
1500 | 0 | sess->async_buf_size = 0; |
1501 | 0 | now = gf_sys_clock_high_res(); |
1502 | 0 | if ((sess->flags & GF_NETIO_SESSION_NO_BLOCK) && !sess->start_time) |
1503 | 0 | sess->start_time = now; |
1504 | |
|
1505 | | #ifdef GPAC_HAS_QUIC |
1506 | | if (sess->flags & GF_NETIO_SESSION_USE_QUIC) { |
1507 | | e = http3_connect(sess, (char *) proxy, proxy_port); |
1508 | | if (sess->num_retry && (e==GF_IP_CONNECTION_FAILURE) ) { |
1509 | | register_sock = GF_FALSE; |
1510 | | e = GF_IP_NETWORK_EMPTY; |
1511 | | } |
1512 | | } else |
1513 | | #endif |
1514 | 0 | e = gf_sk_connect(sess->sock, (char *) proxy, proxy_port, NULL); |
1515 | | |
1516 | | /*retry*/ |
1517 | 0 | if (e == GF_IP_NETWORK_EMPTY) { |
1518 | 0 | if (sess->num_retry) { |
1519 | 0 | if (register_sock) gf_sk_group_register(sess->sock_group, sess->sock); |
1520 | 0 | sess->status = GF_NETIO_SETUP; |
1521 | | //reset pending flag |
1522 | 0 | sess->connect_pending = 0; |
1523 | 0 | if (sess->flags & GF_NETIO_SESSION_NO_BLOCK) { |
1524 | | //either timeout or set pending flag |
1525 | 0 | if ((now - sess->start_time) / 1000 > sess->conn_timeout) { |
1526 | 0 | sess->num_retry = 0; |
1527 | 0 | } else { |
1528 | 0 | sess->connect_pending = 1; |
1529 | 0 | } |
1530 | 0 | } else { |
1531 | 0 | sess->num_retry--; |
1532 | 0 | sess->connect_pending = 1; |
1533 | 0 | } |
1534 | 0 | if (sess->connect_pending) { |
1535 | 0 | SET_LAST_ERR(GF_IP_NETWORK_EMPTY) |
1536 | 0 | return; |
1537 | 0 | } |
1538 | 0 | } |
1539 | | |
1540 | 0 | e = GF_IP_CONNECTION_FAILURE; |
1541 | 0 | } |
1542 | | |
1543 | 0 | sess->connect_pending = 0; |
1544 | 0 | SET_LAST_ERR(GF_OK) |
1545 | | |
1546 | | /*failed*/ |
1547 | 0 | if (e) { |
1548 | 0 | if ((sess->flags & GF_NETIO_SESSION_RETRY_QUIC) && !(sess->flags & GF_NETIO_SESSION_USE_QUIC)) { |
1549 | 0 | sess->status = GF_NETIO_SETUP; |
1550 | 0 | sess->connect_pending = 0; |
1551 | 0 | sess->start_time = 0; |
1552 | 0 | gf_sk_group_unregister(sess->sock_group, sess->sock); |
1553 | 0 | gf_sk_del(sess->sock); |
1554 | 0 | sess->sock = NULL; |
1555 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] Failed to connect through TCP (%s), retrying with QUIC\n", sess->log_name, gf_error_to_string(e))); |
1556 | 0 | sess->flags |= GF_NETIO_SESSION_USE_QUIC; |
1557 | 0 | sess->flags &= ~GF_NETIO_SESSION_RETRY_QUIC; |
1558 | 0 | goto resetup_socket; |
1559 | 0 | } |
1560 | 0 | if ((sess->flags & GF_NETIO_SESSION_USE_QUIC) |
1561 | 0 | && !(sess->flags & GF_NETIO_SESSION_RETRY_QUIC) |
1562 | 0 | && (sess->dm->h3_mode!=H3_MODE_ONLY) |
1563 | 0 | ) { |
1564 | 0 | sess->flags &= ~GF_NETIO_SESSION_USE_QUIC; |
1565 | 0 | sess->status = GF_NETIO_SETUP; |
1566 | 0 | sess->connect_pending = 0; |
1567 | 0 | sess->start_time = 0; |
1568 | 0 | gf_sk_group_unregister(sess->sock_group, sess->sock); |
1569 | 0 | gf_sk_del(sess->sock); |
1570 | 0 | sess->sock = NULL; |
1571 | | #ifdef GPAC_HTTPMUX |
1572 | | if (sess->hmux_sess) { |
1573 | | sess->hmux_sess->destroy(sess->hmux_sess); |
1574 | | gf_list_del(sess->hmux_sess->sessions); |
1575 | | gf_free(sess->hmux_sess); |
1576 | | sess->hmux_sess = NULL; |
1577 | | } |
1578 | | #endif |
1579 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] Failed to connect through QUIC (%s), retrying with TCP\n", sess->log_name, gf_error_to_string(e))); |
1580 | 0 | goto resetup_socket; |
1581 | 0 | } |
1582 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
1583 | 0 | SET_LAST_ERR(e) |
1584 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
1585 | 0 | return; |
1586 | 0 | } |
1587 | 0 | if (register_sock) gf_sk_group_register(sess->sock_group, sess->sock); |
1588 | 0 | if (sess->allow_direct_reuse) { |
1589 | 0 | gf_dm_configure_cache(sess); |
1590 | 0 | if (sess->cached_file) return; |
1591 | 0 | } |
1592 | | |
1593 | 0 | sess->connect_time = (u32) (gf_sys_clock_high_res() - now); |
1594 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Connected to %s:%d\n", sess->log_name, proxy, proxy_port)); |
1595 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_CONNECTED, GF_OK); |
1596 | 0 | } |
1597 | | |
1598 | 0 | if (sess->status == GF_NETIO_SETUP) |
1599 | 0 | sess->status = GF_NETIO_CONNECTED; |
1600 | |
|
1601 | 0 | #ifdef GPAC_HAS_SSL |
1602 | 0 | if ((!sess->ssl || (sess->connect_pending==2)) |
1603 | 0 | && (sess->flags & (GF_DOWNLOAD_SESSION_USE_SSL|GF_DOWNLOAD_SESSION_SSL_FORCED)) |
1604 | 0 | ) { |
1605 | 0 | SSLConnectStatus ret = gf_ssl_try_connect(sess, proxy); |
1606 | 0 | if (ret != SSL_CONNECT_OK) { |
1607 | 0 | if (ret == SSL_CONNECT_RETRY) |
1608 | 0 | gf_dm_connect(sess); |
1609 | |
|
1610 | 0 | return; |
1611 | 0 | } |
1612 | 0 | } else |
1613 | 0 | #endif |
1614 | 0 | { |
1615 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] connected\n", sess->log_name)); |
1616 | 0 | } |
1617 | | |
1618 | 0 | if (!allow_offline) |
1619 | 0 | gf_dm_configure_cache(sess); |
1620 | |
|
1621 | 0 | } |
1622 | | |
1623 | | DownloadedCacheEntry gf_dm_refresh_cache_entry(GF_DownloadSession *sess) |
1624 | 0 | { |
1625 | 0 | Bool go; |
1626 | 0 | u32 timer = 0; |
1627 | 0 | u32 flags; |
1628 | 0 | if (!sess) return NULL; |
1629 | 0 | flags = sess->flags; |
1630 | 0 | sess->flags |= GF_NETIO_SESSION_NOT_CACHED; |
1631 | 0 | go = GF_TRUE; |
1632 | 0 | while (go) { |
1633 | 0 | switch (sess->status) { |
1634 | | /*setup download*/ |
1635 | 0 | case GF_NETIO_SETUP: |
1636 | 0 | gf_dm_connect(sess); |
1637 | 0 | break; |
1638 | 0 | case GF_NETIO_WAIT_FOR_REPLY: |
1639 | 0 | if (timer == 0) |
1640 | 0 | timer = gf_sys_clock(); |
1641 | 0 | { |
1642 | 0 | u32 timer2 = gf_sys_clock(); |
1643 | 0 | if (timer2 - timer > 5000) { |
1644 | 0 | GF_Err e; |
1645 | | /* Since HEAD is not understood by this server, we use a GET instead */ |
1646 | 0 | sess->http_read_type = GET; |
1647 | 0 | sess->flags |= GF_NETIO_SESSION_NOT_CACHED; |
1648 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
1649 | 0 | sess->status = GF_NETIO_SETUP; |
1650 | 0 | sess->server_only_understand_get = GF_TRUE; |
1651 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("gf_dm_refresh_cache_entry() : Timeout with HEAD, try with GET\n")); |
1652 | 0 | e = gf_dm_sess_setup_from_url(sess, sess->orig_url, GF_FALSE); |
1653 | 0 | if (e) { |
1654 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("gf_dm_refresh_cache_entry() : Error with GET %d\n", e)); |
1655 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
1656 | 0 | SET_LAST_ERR(e) |
1657 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
1658 | 0 | } else { |
1659 | 0 | timer = 0; |
1660 | 0 | continue; |
1661 | 0 | } |
1662 | 0 | } |
1663 | 0 | } |
1664 | 0 | case GF_NETIO_CONNECTED: |
1665 | 0 | sess->do_requests(sess); |
1666 | 0 | break; |
1667 | 0 | case GF_NETIO_DATA_EXCHANGE: |
1668 | 0 | case GF_NETIO_DISCONNECTED: |
1669 | 0 | case GF_NETIO_STATE_ERROR: |
1670 | 0 | case GF_NETIO_DATA_TRANSFERED: |
1671 | 0 | go = GF_FALSE; |
1672 | 0 | break; |
1673 | 0 | default: |
1674 | 0 | break; |
1675 | 0 | } |
1676 | 0 | } |
1677 | 0 | sess->flags = flags; |
1678 | 0 | if (sess->status==GF_NETIO_STATE_ERROR) return NULL; |
1679 | 0 | return sess->cache_entry; |
1680 | 0 | } |
1681 | | |
1682 | | GF_EXPORT |
1683 | | const char *gf_dm_sess_mime_type(GF_DownloadSession *sess) |
1684 | 0 | { |
1685 | 0 | DownloadedCacheEntry entry; |
1686 | 0 | if (sess->cache_entry) { |
1687 | 0 | const char * oldMimeIfAny = gf_cache_get_mime_type(sess->cache_entry); |
1688 | 0 | if (oldMimeIfAny) |
1689 | 0 | return oldMimeIfAny; |
1690 | 0 | } |
1691 | 0 | entry = gf_dm_refresh_cache_entry (sess); |
1692 | 0 | if (!entry) |
1693 | 0 | return sess->mime_type; |
1694 | 0 | gf_assert( entry == sess->cache_entry && entry); |
1695 | 0 | return gf_cache_get_mime_type( sess->cache_entry ); |
1696 | 0 | } |
1697 | | |
1698 | | GF_EXPORT |
1699 | | GF_Err gf_dm_sess_set_range(GF_DownloadSession *sess, u64 start_range, u64 end_range, Bool discontinue_cache) |
1700 | 0 | { |
1701 | 0 | if (!sess) |
1702 | 0 | return GF_BAD_PARAM; |
1703 | 0 | if (sess->cache_entry) { |
1704 | 0 | if (!discontinue_cache) { |
1705 | 0 | if (gf_cache_get_end_range(sess->cache_entry) + 1 != start_range) |
1706 | 0 | discontinue_cache = GF_TRUE; |
1707 | 0 | } |
1708 | 0 | if (sess->sock) { |
1709 | 0 | switch (sess->status) { |
1710 | 0 | case GF_NETIO_CONNECTED: |
1711 | 0 | case GF_NETIO_DISCONNECTED: |
1712 | 0 | case GF_NETIO_DATA_TRANSFERED: |
1713 | 0 | break; |
1714 | 0 | default: |
1715 | 0 | return GF_BAD_PARAM; |
1716 | 0 | } |
1717 | 0 | } |
1718 | 0 | if (!sess->local_cache_only) { |
1719 | 0 | sess->status = sess->sock ? GF_NETIO_CONNECTED : GF_NETIO_SETUP; |
1720 | 0 | sess->num_retry = SESSION_RETRY_COUNT; |
1721 | |
|
1722 | 0 | if (!discontinue_cache) { |
1723 | 0 | gf_cache_set_end_range(sess->cache_entry, end_range); |
1724 | | /*remember this in case we get disconnected*/ |
1725 | 0 | sess->is_range_continuation = GF_TRUE; |
1726 | 0 | } else { |
1727 | 0 | sess->needs_cache_reconfig = 1; |
1728 | 0 | sess->reused_cache_entry = GF_FALSE; |
1729 | 0 | } |
1730 | 0 | } |
1731 | 0 | } else { |
1732 | 0 | if (sess->status == GF_NETIO_DISCONNECTED) |
1733 | 0 | sess->status = GF_NETIO_SETUP; |
1734 | 0 | else if ((sess->status != GF_NETIO_SETUP) && (sess->status != GF_NETIO_CONNECTED)) |
1735 | 0 | return GF_BAD_PARAM; |
1736 | 0 | } |
1737 | 0 | sess->range_start = start_range; |
1738 | 0 | sess->range_end = end_range; |
1739 | 0 | sess->needs_range = (start_range || end_range) ? GF_TRUE : GF_FALSE; |
1740 | 0 | return GF_OK; |
1741 | 0 | } |
1742 | | |
1743 | | #ifdef GPAC_HTTPMUX |
1744 | | static void gf_dm_sess_flush_input(GF_DownloadSession *sess) |
1745 | | { |
1746 | | u32 res; |
1747 | | sess->http_buf[0] = 0; |
1748 | | GF_Err e = gf_dm_sess_read(sess, sess->http_buf, sess->http_buf_size, &res); |
1749 | | switch (e) { |
1750 | | case GF_IP_NETWORK_EMPTY: |
1751 | | case GF_OK: |
1752 | | return; |
1753 | | default: |
1754 | | sess->status = GF_NETIO_STATE_ERROR; |
1755 | | SET_LAST_ERR(e) |
1756 | | return; |
1757 | | } |
1758 | | } |
1759 | | #endif |
1760 | | |
1761 | | GF_EXPORT |
1762 | | GF_Err gf_dm_sess_process(GF_DownloadSession *sess) |
1763 | 0 | { |
1764 | 0 | Bool go; |
1765 | |
|
1766 | 0 | #ifndef GPAC_DISABLE_THREADS |
1767 | | /*if session is threaded, start thread*/ |
1768 | 0 | if (! (sess->flags & GF_NETIO_SESSION_NOT_THREADED)) { |
1769 | 0 | if (sess->dm->filter_session && !gf_opts_get_bool("core", "dm-threads")) { |
1770 | 0 | if (sess->ftask) |
1771 | 0 | return GF_OK; |
1772 | 0 | GF_SAFEALLOC(sess->ftask, GF_SessTask); |
1773 | 0 | if (!sess->ftask) return GF_OUT_OF_MEM; |
1774 | 0 | sess->ftask->sess = sess; |
1775 | 0 | gf_fs_post_user_task(sess->dm->filter_session, gf_dm_session_task, sess->ftask, "download"); |
1776 | 0 | return GF_OK; |
1777 | 0 | } |
1778 | 0 | if (sess->th) { |
1779 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] Session already started - ignoring start\n", sess->log_name)); |
1780 | 0 | return GF_OK; |
1781 | 0 | } |
1782 | 0 | sess->th = gf_th_new( gf_file_basename(sess->orig_url) ); |
1783 | 0 | if (!sess->th) return GF_OUT_OF_MEM; |
1784 | 0 | gf_th_run(sess->th, gf_dm_session_thread, sess); |
1785 | 0 | return GF_OK; |
1786 | 0 | } |
1787 | 0 | #endif //GPAC_DISABLE_THREADS |
1788 | | |
1789 | 0 | if (sess->put_state==2) { |
1790 | 0 | if (sess->status==GF_NETIO_DATA_TRANSFERED) |
1791 | 0 | sess->status = GF_NETIO_WAIT_FOR_REPLY; |
1792 | 0 | } |
1793 | | |
1794 | | /*otherwise do a synchronous download*/ |
1795 | 0 | go = GF_TRUE; |
1796 | 0 | while (go) { |
1797 | 0 | switch (sess->status) { |
1798 | | /*setup download*/ |
1799 | 0 | case GF_NETIO_SETUP: |
1800 | 0 | gf_dm_connect(sess); |
1801 | 0 | if (sess->connect_pending) { |
1802 | 0 | go = GF_FALSE; |
1803 | 0 | if (!sess->last_error) { |
1804 | | //in case someone forgot to set this - if no error we must be in network empty state while connection is pending |
1805 | 0 | sess->last_error = GF_IP_NETWORK_EMPTY; |
1806 | 0 | } |
1807 | 0 | } |
1808 | 0 | break; |
1809 | 0 | case GF_NETIO_WAIT_FOR_REPLY: |
1810 | 0 | case GF_NETIO_CONNECTED: |
1811 | 0 | if (!sess->last_error) |
1812 | 0 | sess->last_error = GF_IP_NETWORK_EMPTY; |
1813 | |
|
1814 | 0 | sess->do_requests(sess); |
1815 | 0 | if (sess->server_mode) { |
1816 | 0 | if (sess->status == GF_NETIO_STATE_ERROR) { |
1817 | 0 | sess->status = GF_NETIO_DISCONNECTED; |
1818 | 0 | SET_LAST_ERR(GF_IP_CONNECTION_CLOSED) |
1819 | 0 | sess_connection_closed(sess); |
1820 | 0 | go = GF_FALSE; |
1821 | 0 | } else if (sess->last_error==GF_IP_NETWORK_EMPTY) { |
1822 | 0 | go = GF_FALSE; |
1823 | 0 | } else if (sess->status==GF_NETIO_DISCONNECTED) { |
1824 | 0 | go = GF_FALSE; |
1825 | 0 | } |
1826 | 0 | } else if (sess->flags & GF_NETIO_SESSION_NO_BLOCK) { |
1827 | 0 | if (sess->last_error==GF_IP_NETWORK_EMPTY) |
1828 | 0 | go = GF_FALSE; |
1829 | 0 | } |
1830 | 0 | break; |
1831 | 0 | case GF_NETIO_DATA_EXCHANGE: |
1832 | 0 | if (sess->put_state==2) { |
1833 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
1834 | 0 | SET_LAST_ERR(GF_OK) |
1835 | 0 | go = GF_FALSE; |
1836 | 0 | break; |
1837 | 0 | } |
1838 | 0 | sess->do_requests(sess); |
1839 | 0 | break; |
1840 | 0 | case GF_NETIO_DATA_TRANSFERED: |
1841 | | #ifdef GPAC_HTTPMUX |
1842 | | if (sess->hmux_sess && sess->server_mode) { |
1843 | | gf_dm_sess_flush_input(sess); |
1844 | | sess->hmux_sess->write(sess); |
1845 | | } |
1846 | | //in put and waiting for EOS flush |
1847 | | if ((sess->put_state==1) && sess->hmux_is_eos) return GF_IP_NETWORK_EMPTY; |
1848 | | #endif |
1849 | 0 | go = GF_FALSE; |
1850 | 0 | break; |
1851 | 0 | case GF_NETIO_DISCONNECTED: |
1852 | 0 | case GF_NETIO_STATE_ERROR: |
1853 | 0 | go = GF_FALSE; |
1854 | 0 | break; |
1855 | | |
1856 | 0 | case GF_NETIO_GET_METHOD: |
1857 | 0 | case GF_NETIO_GET_HEADER: |
1858 | 0 | case GF_NETIO_GET_CONTENT: |
1859 | 0 | case GF_NETIO_PARSE_REPLY: |
1860 | 0 | break; |
1861 | | |
1862 | 0 | default: |
1863 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[Downloader] Session in unknown state !! - aborting\n")); |
1864 | 0 | go = GF_FALSE; |
1865 | 0 | break; |
1866 | 0 | } |
1867 | 0 | } |
1868 | 0 | return sess->last_error; |
1869 | 0 | } |
1870 | | |
1871 | | GF_EXPORT |
1872 | | GF_Err gf_dm_sess_process_headers(GF_DownloadSession *sess) |
1873 | 0 | { |
1874 | 0 | Bool go; |
1875 | 0 | if (!(sess->flags & GF_NETIO_SESSION_NOT_THREADED)) { |
1876 | 0 | return GF_OK; |
1877 | 0 | } |
1878 | | |
1879 | 0 | go = GF_TRUE; |
1880 | 0 | while (go) { |
1881 | 0 | switch (sess->status) { |
1882 | | /*setup download*/ |
1883 | 0 | case GF_NETIO_SETUP: |
1884 | 0 | gf_dm_connect(sess); |
1885 | 0 | break; |
1886 | 0 | case GF_NETIO_WAIT_FOR_REPLY: |
1887 | 0 | case GF_NETIO_CONNECTED: |
1888 | 0 | sess->do_requests(sess); |
1889 | |
|
1890 | 0 | if (sess->reused_cache_entry && sess->cache_entry && gf_cache_are_headers_processed(sess->cache_entry) ) { |
1891 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
1892 | 0 | } |
1893 | 0 | break; |
1894 | 0 | case GF_NETIO_DATA_EXCHANGE: |
1895 | 0 | case GF_NETIO_DATA_TRANSFERED: |
1896 | 0 | case GF_NETIO_DISCONNECTED: |
1897 | 0 | case GF_NETIO_STATE_ERROR: |
1898 | 0 | go = GF_FALSE; |
1899 | 0 | break; |
1900 | 0 | default: |
1901 | 0 | break; |
1902 | 0 | } |
1903 | 0 | } |
1904 | 0 | return sess->last_error; |
1905 | 0 | } |
1906 | | |
1907 | | GF_EXPORT |
1908 | | GF_DownloadManager *gf_dm_new(GF_FilterSession *fsess) |
1909 | 55 | { |
1910 | 55 | const char *opt; |
1911 | 55 | const char * default_cache_dir; |
1912 | 55 | GF_DownloadManager *dm; |
1913 | 55 | GF_SAFEALLOC(dm, GF_DownloadManager); |
1914 | 55 | if (!dm) { |
1915 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[Downloader] Failed to allocate downloader\n")); |
1916 | 0 | return NULL; |
1917 | 0 | } |
1918 | 55 | dm->all_sessions = gf_list_new(); |
1919 | 55 | dm->cache_entries = gf_list_new(); |
1920 | 55 | dm->credentials = gf_list_new(); |
1921 | 55 | dm->skip_proxy_servers = gf_list_new(); |
1922 | 55 | dm->partial_downloads = gf_list_new(); |
1923 | 55 | dm->cache_mx = gf_mx_new("download_manager_cache_mx"); |
1924 | 55 | dm->filter_session = fsess; |
1925 | 55 | default_cache_dir = NULL; |
1926 | 55 | gf_mx_p( dm->cache_mx ); |
1927 | | |
1928 | | #ifdef GPAC_HAS_CURL |
1929 | | if (gf_opts_get_bool("core", "curl")) { |
1930 | | dm->curl_multi = curl_multi_init(); |
1931 | | curl_multi_setopt(dm->curl_multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
1932 | | dm->curl_mx = gf_mx_new("curl"); |
1933 | | curl_global_init(CURL_GLOBAL_ALL); |
1934 | | } |
1935 | | #endif |
1936 | | |
1937 | 55 | dm->disable_http2 = gf_opts_get_bool("core", "no-h2"); |
1938 | | #ifdef GPAC_HAS_QUIC |
1939 | | opt = gf_opts_get_key("core", "h3"); |
1940 | | if (!opt || !strcmp(opt, "auto")) dm->h3_mode = H3_MODE_AUTO; |
1941 | | else if (!strcmp(opt, "first")) dm->h3_mode = H3_MODE_FIRST; |
1942 | | else if (!strcmp(opt, "only")) dm->h3_mode = H3_MODE_ONLY; |
1943 | | else dm->h3_mode = H3_MODE_NO; |
1944 | | #else |
1945 | 55 | dm->h3_mode = H3_MODE_NO; |
1946 | 55 | #endif |
1947 | | |
1948 | 55 | opt = gf_opts_get_key("core", "cache"); |
1949 | | |
1950 | 55 | retry_cache: |
1951 | 55 | if (!opt) { |
1952 | 55 | default_cache_dir = gf_get_default_cache_directory(); |
1953 | 55 | opt = default_cache_dir; |
1954 | 55 | } |
1955 | 55 | if (opt[strlen(opt)-1] != GF_PATH_SEPARATOR) { |
1956 | 55 | dm->cache_directory = (char *) gf_malloc(sizeof(char)* (strlen(opt)+2)); |
1957 | 55 | sprintf(dm->cache_directory, "%s%c", opt, GF_PATH_SEPARATOR); |
1958 | 55 | } else { |
1959 | 0 | dm->cache_directory = gf_strdup(opt); |
1960 | 0 | } |
1961 | | |
1962 | | //check cache exists |
1963 | 55 | if (!default_cache_dir) { |
1964 | 0 | FILE *test; |
1965 | 0 | char szTemp[GF_MAX_PATH]; |
1966 | 0 | if (strlen(dm->cache_directory) + strlen("gpaccache.test") >= GF_ARRAY_LENGTH(szTemp)) { |
1967 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[Cache] Invalid cache directory (path too long): %s\n", dm->cache_directory )); |
1968 | 0 | gf_free(dm->cache_directory); |
1969 | 0 | dm->cache_directory = NULL; |
1970 | 0 | opt = NULL; |
1971 | 0 | goto retry_cache; |
1972 | 0 | } |
1973 | 0 | gf_strcpy(szTemp, dm->cache_directory); |
1974 | 0 | gf_strcat(szTemp, "gpaccache.test"); |
1975 | 0 | test = gf_fopen(szTemp, "wb"); |
1976 | 0 | if (!test) { |
1977 | 0 | gf_mkdir(dm->cache_directory); |
1978 | 0 | test = gf_fopen(szTemp, "wb"); |
1979 | 0 | if (!test) { |
1980 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[Cache] Cannot write to %s directory, using system temp cache\n", dm->cache_directory )); |
1981 | 0 | gf_free(dm->cache_directory); |
1982 | 0 | dm->cache_directory = NULL; |
1983 | 0 | opt = NULL; |
1984 | 0 | goto retry_cache; |
1985 | 0 | } |
1986 | 0 | } |
1987 | 0 | if (test) { |
1988 | 0 | gf_fclose(test); |
1989 | 0 | gf_file_delete(szTemp); |
1990 | 0 | } |
1991 | 0 | } |
1992 | | |
1993 | | /*use it in in BYTES per second*/ |
1994 | 55 | dm->limit_data_rate = gf_opts_get_int("core", "maxrate") / 8; |
1995 | | |
1996 | 55 | dm->read_buf_size = GF_DOWNLOAD_BUFFER_SIZE; |
1997 | | //when rate is limited, use smaller smaller read size |
1998 | 55 | if (dm->limit_data_rate) { |
1999 | 0 | dm->read_buf_size = GF_DOWNLOAD_BUFFER_SIZE_LIMIT_RATE; |
2000 | 0 | } |
2001 | | |
2002 | 55 | dm->disable_cache = gf_opts_get_bool("core", "no-cache"); |
2003 | | |
2004 | 55 | dm->allow_offline_cache = gf_opts_get_bool("core", "offline-cache"); |
2005 | | |
2006 | 55 | dm->allow_broken_certificate = GF_FALSE; |
2007 | 55 | Bool do_clean = GF_TRUE; |
2008 | 55 | if ( gf_opts_get_bool("core", "clean-cache")) { |
2009 | 0 | dm->max_cache_size = 0; |
2010 | 0 | dm->cur_cache_size = gf_cache_cleanup(dm->cache_directory, dm->max_cache_size); |
2011 | 0 | do_clean = GF_FALSE; |
2012 | 0 | } |
2013 | 55 | dm->cache_clean_ms = 1000*gf_opts_get_int("core", "cache-check"); |
2014 | 55 | dm->max_cache_size = gf_opts_get_int("core", "cache-size"); |
2015 | 55 | dm->allow_broken_certificate = gf_opts_get_bool("core", "broken-cert"); |
2016 | | |
2017 | 55 | gf_mx_v( dm->cache_mx ); |
2018 | | |
2019 | 55 | #ifdef GPAC_HAS_SSL |
2020 | 55 | dm->ssl_ctx = NULL; |
2021 | 55 | #endif |
2022 | | |
2023 | 55 | if (dm->max_cache_size && do_clean) |
2024 | 55 | dm->cur_cache_size = gf_cache_cleanup(dm->cache_directory, dm->max_cache_size); |
2025 | 55 | return dm; |
2026 | 55 | } |
2027 | | |
2028 | | GF_EXPORT |
2029 | | void gf_dm_set_auth_callback(GF_DownloadManager *dm, gf_dm_get_usr_pass get_user_password, void *usr_cbk) |
2030 | 55 | { |
2031 | 55 | if (dm) { |
2032 | 55 | dm->get_user_password = get_user_password; |
2033 | 55 | dm->usr_cbk = usr_cbk; |
2034 | 55 | } |
2035 | 55 | } |
2036 | | |
2037 | | GF_EXPORT |
2038 | | void gf_dm_del(GF_DownloadManager *dm) |
2039 | 55 | { |
2040 | 55 | if (!dm) |
2041 | 0 | return; |
2042 | 55 | gf_assert( dm->all_sessions); |
2043 | 55 | gf_assert( dm->cache_mx ); |
2044 | 55 | gf_mx_p( dm->cache_mx ); |
2045 | | |
2046 | 55 | while (gf_list_count(dm->partial_downloads)) { |
2047 | 0 | GF_PartialDownload * entry = (GF_PartialDownload*)gf_list_get( dm->partial_downloads, 0); |
2048 | 0 | gf_list_rem( dm->partial_downloads, 0); |
2049 | 0 | gf_assert( entry->filename ); |
2050 | 0 | gf_file_delete( entry->filename ); |
2051 | 0 | gf_free(entry->filename ); |
2052 | 0 | entry->filename = NULL; |
2053 | 0 | entry->url = NULL; |
2054 | 0 | gf_free( entry ); |
2055 | 0 | } |
2056 | | |
2057 | | /*destroy all pending sessions*/ |
2058 | 55 | while (gf_list_count(dm->all_sessions)) { |
2059 | 0 | GF_DownloadSession *sess = (GF_DownloadSession *) gf_list_get(dm->all_sessions, 0); |
2060 | 0 | gf_dm_sess_del(sess); |
2061 | 0 | } |
2062 | 55 | gf_list_del(dm->all_sessions); |
2063 | 55 | dm->all_sessions = NULL; |
2064 | 55 | gf_assert( dm->skip_proxy_servers ); |
2065 | 55 | while (gf_list_count(dm->skip_proxy_servers)) { |
2066 | 0 | char *serv = (char*)gf_list_get(dm->skip_proxy_servers, 0); |
2067 | 0 | gf_list_rem(dm->skip_proxy_servers, 0); |
2068 | 0 | gf_free(serv); |
2069 | 0 | } |
2070 | 55 | gf_list_del(dm->skip_proxy_servers); |
2071 | 55 | dm->skip_proxy_servers = NULL; |
2072 | 55 | gf_assert( dm->credentials); |
2073 | 55 | while (gf_list_count(dm->credentials)) { |
2074 | 0 | GF_UserCredentials * cred = (GF_UserCredentials*)gf_list_get( dm->credentials, 0); |
2075 | 0 | gf_list_rem( dm->credentials, 0); |
2076 | 0 | gf_free( cred ); |
2077 | 0 | } |
2078 | 55 | gf_list_del( dm->credentials); |
2079 | 55 | dm->credentials = NULL; |
2080 | 55 | gf_assert( dm->cache_entries ); |
2081 | | |
2082 | | /* Deletes DownloadedCacheEntry and associated files if required */ |
2083 | 55 | while (gf_list_count(dm->cache_entries)) { |
2084 | 0 | const DownloadedCacheEntry entry = (const DownloadedCacheEntry)gf_list_pop_front( dm->cache_entries ); |
2085 | 0 | if (!dm->max_cache_size) |
2086 | 0 | gf_cache_entry_set_delete_files_when_deleted(entry); |
2087 | 0 | gf_cache_delete_entry(entry); |
2088 | 0 | } |
2089 | 55 | gf_list_del( dm->cache_entries ); |
2090 | 55 | dm->cache_entries = NULL; |
2091 | | |
2092 | 55 | gf_list_del( dm->partial_downloads ); |
2093 | 55 | dm->partial_downloads = NULL; |
2094 | 55 | if (dm->cache_directory) |
2095 | 55 | gf_free(dm->cache_directory); |
2096 | 55 | dm->cache_directory = NULL; |
2097 | | |
2098 | | #ifdef GPAC_HAS_CURL |
2099 | | if (dm->curl_multi) { |
2100 | | curl_multi_cleanup(dm->curl_multi); |
2101 | | } |
2102 | | gf_mx_del(dm->curl_mx); |
2103 | | #endif |
2104 | | |
2105 | 55 | #ifdef GPAC_HAS_SSL |
2106 | | // if (dm->ssl_ctx) SSL_CTX_free(dm->ssl_ctx); |
2107 | | //if (dm->ssl_ctx) gnutls_certificate_free_credentials(dm->ssl_ctx); |
2108 | 55 | gf_ssl_server_context_del(dm->ssl_ctx); |
2109 | 55 | #endif |
2110 | | /* Stored elsewhere, no need to free */ |
2111 | 55 | gf_mx_v( dm->cache_mx ); |
2112 | 55 | gf_mx_del( dm->cache_mx); |
2113 | 55 | dm->cache_mx = NULL; |
2114 | 55 | gf_free(dm); |
2115 | 55 | } |
2116 | | |
2117 | | /*! |
2118 | | * Skip ICY metadata from SHOUTCAST or ICECAST streams. |
2119 | | * Data will be skipped and parsed and sent as a GF_NETIO_Parameter to the user_io, |
2120 | | * so modules interrested by those streams may use the data |
2121 | | \param sess The GF_DownloadSession |
2122 | | \param data last data received |
2123 | | \param nbBytes The number of bytes contained into data |
2124 | | */ |
2125 | | static void gf_icy_skip_data(GF_DownloadSession * sess, const char * data, u32 nbBytes) |
2126 | 0 | { |
2127 | 0 | u32 icy_metaint; |
2128 | 0 | if (!sess || !data ) return; |
2129 | | |
2130 | 0 | icy_metaint = sess->icy_metaint; |
2131 | 0 | gf_assert( icy_metaint > 0 ); |
2132 | 0 | while (nbBytes) { |
2133 | 0 | if (sess->icy_bytes == icy_metaint) { |
2134 | 0 | sess->icy_count = 1 + 16* (u8) data[0]; |
2135 | | /*skip icy metadata*/ |
2136 | 0 | if (sess->icy_count > nbBytes) { |
2137 | 0 | sess->icy_count -= nbBytes; |
2138 | 0 | nbBytes = 0; |
2139 | 0 | } else { |
2140 | 0 | if (sess->icy_count > 1) { |
2141 | 0 | GF_NETIO_Parameter par; |
2142 | 0 | char szData[4096]; |
2143 | 0 | memset(szData, 0, 4096); |
2144 | 0 | memcpy(szData, data+1, sess->icy_count-1); |
2145 | 0 | szData[sess->icy_count] = 0; |
2146 | |
|
2147 | 0 | par.error = GF_OK; |
2148 | 0 | par.msg_type = GF_NETIO_ICY_META; |
2149 | 0 | par.name = "icy-meta"; |
2150 | 0 | par.value = szData; |
2151 | 0 | par.sess = sess; |
2152 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[ICY] Found metainfo in stream=%s, (every %d bytes)\n", szData, icy_metaint)); |
2153 | 0 | gf_dm_sess_user_io(sess, &par); |
2154 | 0 | } else { |
2155 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[ICY] Empty metainfo in stream, (every %d bytes)\n", icy_metaint)); |
2156 | 0 | } |
2157 | 0 | nbBytes -= sess->icy_count; |
2158 | 0 | data += sess->icy_count; |
2159 | 0 | sess->icy_count = 0; |
2160 | 0 | sess->icy_bytes = 0; |
2161 | 0 | } |
2162 | 0 | } else { |
2163 | 0 | GF_NETIO_Parameter par; |
2164 | 0 | u32 left = icy_metaint - sess->icy_bytes; |
2165 | 0 | if (left > nbBytes) { |
2166 | 0 | left = nbBytes; |
2167 | 0 | sess->icy_bytes += left; |
2168 | 0 | nbBytes = 0; |
2169 | 0 | } else { |
2170 | 0 | sess->icy_bytes = icy_metaint; |
2171 | 0 | nbBytes -= left; |
2172 | 0 | } |
2173 | |
|
2174 | 0 | par.msg_type = GF_NETIO_DATA_EXCHANGE; |
2175 | 0 | par.data = data; |
2176 | 0 | par.size = left; |
2177 | 0 | gf_dm_sess_user_io(sess, &par); |
2178 | |
|
2179 | 0 | data += left; |
2180 | 0 | } |
2181 | 0 | } |
2182 | 0 | } |
2183 | | |
2184 | | |
2185 | | static char *gf_dm_get_chunk_data(GF_DownloadSession *sess, Bool first_chunk_in_payload, char *body_start, u32 *payload_size, u32 *header_size) |
2186 | 0 | { |
2187 | 0 | u32 size; |
2188 | 0 | s32 res; |
2189 | 0 | char *te_header, *sep; |
2190 | |
|
2191 | 0 | if (!sess || !body_start) return NULL; |
2192 | 0 | if (!sess->chunked) return body_start; |
2193 | | |
2194 | 0 | if (sess->nb_left_in_chunk) { |
2195 | 0 | if (sess->nb_left_in_chunk > *payload_size) { |
2196 | 0 | sess->nb_left_in_chunk -= (*payload_size); |
2197 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] Chunk encoding: still %d bytes to get\n", sess->log_name, sess->nb_left_in_chunk)); |
2198 | 0 | } else { |
2199 | 0 | *payload_size = sess->nb_left_in_chunk; |
2200 | 0 | sess->nb_left_in_chunk = 0; |
2201 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] Chunk encoding: last bytes in chunk received\n", sess->log_name)); |
2202 | 0 | } |
2203 | 0 | *header_size = 0; |
2204 | 0 | return body_start; |
2205 | 0 | } |
2206 | | |
2207 | 0 | if (*payload_size == 2) { |
2208 | 0 | *header_size = 0; |
2209 | 0 | } |
2210 | 0 | *header_size = 0; |
2211 | | /*skip remaining CRLF from previous chunk if any*/ |
2212 | 0 | if (*payload_size >= 2) { |
2213 | 0 | if ((body_start[0]=='\r') && (body_start[1]=='\n')) { |
2214 | 0 | body_start += 2; |
2215 | 0 | *header_size = 2; |
2216 | 0 | } |
2217 | 0 | if (*payload_size <= 4) { |
2218 | 0 | *header_size = 0; |
2219 | 0 | return NULL; |
2220 | 0 | } |
2221 | 0 | te_header = strstr((char *) body_start, "\r\n"); |
2222 | 0 | } else { |
2223 | | //not enough bytes to read CRLF, don't bother parsing |
2224 | 0 | te_header = NULL; |
2225 | 0 | } |
2226 | | |
2227 | | //cannot parse now, copy over the bytes |
2228 | 0 | if (!te_header) { |
2229 | 0 | *header_size = 0; |
2230 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] Chunk encoding: current buffer does not contain enough bytes (%d) to read the size\n", sess->log_name, *payload_size)); |
2231 | 0 | return NULL; |
2232 | 0 | } |
2233 | | |
2234 | 0 | te_header[0] = 0; |
2235 | 0 | *header_size += (u32) (strlen(body_start)) + 2; |
2236 | |
|
2237 | 0 | sep = strchr(body_start, ';'); |
2238 | 0 | if (sep) sep[0] = 0; |
2239 | 0 | res = sscanf(body_start, "%x", &size); |
2240 | 0 | if (res<0 || size > GF_CHUNK_MAX_SIZE) { |
2241 | 0 | te_header[0] = '\r'; |
2242 | 0 | if (sep) sep[0] = ';'; |
2243 | 0 | *header_size = 0; |
2244 | 0 | *payload_size = 0; |
2245 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] Chunk encoding: fail to read chunk size from buffer %s, aborting\n", sess->log_name, body_start)); |
2246 | 0 | sess->last_error = GF_REMOTE_SERVICE_ERROR; |
2247 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
2248 | 0 | return NULL; |
2249 | 0 | } |
2250 | 0 | if (sep) sep[0] = ';'; |
2251 | 0 | *payload_size = size; |
2252 | |
|
2253 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] Chunk Start: Header \"%s\" - header size %d - payload size %d (bytes done %d) at UTC "LLD"\n", sess->log_name, body_start, 2+strlen(body_start), size, sess->bytes_done, gf_net_get_utc())); |
2254 | |
|
2255 | 0 | te_header[0] = '\r'; |
2256 | 0 | if (!size) |
2257 | 0 | sess->last_chunk_found = GF_TRUE; |
2258 | |
|
2259 | 0 | sess->current_chunk_size = size; |
2260 | 0 | sess->current_chunk_start = gf_sys_clock_high_res(); |
2261 | 0 | return te_header+2; |
2262 | 0 | } |
2263 | | |
2264 | | |
2265 | | static void dm_sess_update_download_rate(GF_DownloadSession * sess) |
2266 | 0 | { |
2267 | 0 | if (!sess->bytes_done) { |
2268 | 0 | sess->bytes_per_sec = 0; |
2269 | 0 | return; |
2270 | 0 | } |
2271 | | |
2272 | | //session is chunked and we have reached our first full window |
2273 | 0 | if (sess->chunked && sess->cumulated_chunk_rate) { |
2274 | | /*use our cumulated weighted rate in bytes per seconds, and divide by total size*/ |
2275 | 0 | sess->bytes_per_sec = (u32) (sess->cumulated_chunk_rate / (sess->bytes_done + sess->cumulated_chunk_header_bytes) ); |
2276 | |
|
2277 | 0 | #ifndef GPAC_DISABLE_LOG |
2278 | 0 | if (gf_log_tool_level_on(GF_LOG_HTTP, GF_LOG_DEBUG)) { |
2279 | 0 | u64 runtime = (gf_sys_clock_high_res() - sess->request_start_time); |
2280 | 0 | if (!runtime) runtime=1; |
2281 | 0 | u32 kbps = (u32) ((1000000 * (u64) (sess->bytes_done + sess->cumulated_chunk_header_bytes)) / runtime) / 125; |
2282 | |
|
2283 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] bandwidth estimation: download time "LLD" us - bytes %u - chunk rate %u kbps (overall rate rate %u kbps)\n", sess->log_name, runtime, sess->bytes_done, sess->bytes_per_sec / 125, kbps)); |
2284 | 0 | } |
2285 | 0 | #endif |
2286 | 0 | } else { |
2287 | | /*compute bps starting from request send time*/ |
2288 | 0 | u64 runtime = (gf_sys_clock_high_res() - sess->request_start_time); |
2289 | 0 | if (!runtime) runtime=1; |
2290 | |
|
2291 | 0 | sess->bytes_per_sec = (u32) ((1000000 * (u64) sess->bytes_done) / runtime); |
2292 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] bandwidth estimation: download time "LLD" us - bytes %u - rate %u kbps\n", sess->log_name, runtime, sess->bytes_done, sess->bytes_per_sec / 125)); |
2293 | 0 | } |
2294 | 0 | } |
2295 | | |
2296 | | |
2297 | | void gf_dm_data_received(GF_DownloadSession *sess, u8 *payload, u32 payload_size, Bool store_in_init, u32 *rewrite_size, u8 *original_payload) |
2298 | 0 | { |
2299 | 0 | u32 nbBytes, remaining, hdr_size; |
2300 | 0 | u8* data = NULL; |
2301 | 0 | Bool first_chunk_in_payload = GF_TRUE; |
2302 | 0 | Bool flush_chunk = GF_FALSE; |
2303 | 0 | GF_NETIO_Parameter par; |
2304 | |
|
2305 | 0 | nbBytes = payload_size; |
2306 | 0 | hdr_size = 0; |
2307 | 0 | remaining = 0; |
2308 | 0 | if (!payload) |
2309 | 0 | return; //nothing to do |
2310 | 0 | if (sess->chunked) { |
2311 | 0 | data = (u8 *) gf_dm_get_chunk_data(sess, first_chunk_in_payload, (char *) payload, &nbBytes, &hdr_size); |
2312 | |
|
2313 | 0 | if (nbBytes >= GF_UINT_MAX-hdr_size) { |
2314 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] invalid payload size received: %u\n", sess->log_name, nbBytes )); |
2315 | 0 | return; |
2316 | 0 | } |
2317 | | |
2318 | 0 | if (!hdr_size && !data && nbBytes) { |
2319 | | /* keep the data and wait for the rest */ |
2320 | 0 | sess->remaining_data_size = nbBytes; |
2321 | 0 | sess->remaining_data = (char *)gf_realloc(sess->remaining_data, nbBytes * sizeof(char)); |
2322 | 0 | memcpy(sess->remaining_data, payload, nbBytes); |
2323 | 0 | payload_size = 0; |
2324 | 0 | payload = NULL; |
2325 | 0 | } else if (hdr_size + nbBytes > payload_size) { |
2326 | | /* chunk header is processed but we will need several TCP frames to get the entire chunk*/ |
2327 | 0 | remaining = nbBytes + hdr_size - payload_size; |
2328 | 0 | gf_assert(payload_size >= hdr_size); |
2329 | 0 | nbBytes = payload_size - hdr_size; |
2330 | 0 | payload_size = 0; |
2331 | 0 | payload = NULL; |
2332 | 0 | sess->chunk_header_bytes += hdr_size; |
2333 | 0 | } else { |
2334 | 0 | payload_size -= hdr_size + nbBytes; |
2335 | 0 | payload += hdr_size + nbBytes; |
2336 | 0 | flush_chunk = GF_TRUE; |
2337 | 0 | sess->chunk_header_bytes += hdr_size; |
2338 | 0 | } |
2339 | | |
2340 | | /*chunk transfer is done*/ |
2341 | 0 | if (sess->last_chunk_found) { |
2342 | 0 | sess->total_size = sess->bytes_done; |
2343 | 0 | } |
2344 | 0 | } else { |
2345 | 0 | data = payload; |
2346 | 0 | remaining = payload_size = 0; |
2347 | 0 | } |
2348 | | |
2349 | 0 | if (data && nbBytes && store_in_init) { |
2350 | 0 | sess->init_data = (char *) gf_realloc(sess->init_data , sizeof(char) * (sess->init_data_size + nbBytes) ); |
2351 | 0 | memcpy(sess->init_data+sess->init_data_size, data, nbBytes); |
2352 | 0 | sess->init_data_size += nbBytes; |
2353 | 0 | } |
2354 | | |
2355 | | //we have some new bytes received |
2356 | 0 | if (nbBytes && !sess->remaining_data_size) { |
2357 | 0 | sess->bytes_done += nbBytes; |
2358 | 0 | dm_sess_update_download_rate(sess); |
2359 | |
|
2360 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] url %s received %d new bytes (%d kbps)\n", sess->log_name, sess->orig_url, nbBytes, 8*sess->bytes_per_sec/1000)); |
2361 | 0 | if (sess->total_size && (sess->bytes_done > sess->total_size)) { |
2362 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] url %s received more bytes than planned!! Got %d bytes vs %d content length\n", sess->log_name, sess->orig_url, sess->bytes_done , sess->total_size )); |
2363 | 0 | sess->bytes_done = sess->total_size; |
2364 | 0 | } |
2365 | |
|
2366 | 0 | if (sess->icy_metaint > 0) |
2367 | 0 | gf_icy_skip_data(sess, (char *) data, nbBytes); |
2368 | 0 | else { |
2369 | 0 | if (sess->use_cache_file && !sess->cached_file) |
2370 | 0 | gf_cache_write_to_cache( sess->cache_entry, sess, (char *) data, nbBytes, sess->dm ? sess->dm->cache_mx : NULL); |
2371 | |
|
2372 | 0 | par.msg_type = GF_NETIO_DATA_EXCHANGE; |
2373 | 0 | par.error = GF_OK; |
2374 | 0 | par.data = (char *) data; |
2375 | 0 | par.size = nbBytes; |
2376 | 0 | par.reply = flush_chunk; |
2377 | 0 | gf_dm_sess_user_io(sess, &par); |
2378 | 0 | } |
2379 | 0 | } |
2380 | | //and we're done |
2381 | 0 | if (sess->total_size && (sess->bytes_done == sess->total_size)) { |
2382 | 0 | u64 run_time; |
2383 | |
|
2384 | | #ifdef GPAC_HTTPMUX |
2385 | | if (sess->hmux_sess && !sess->server_mode) |
2386 | | sess->hmux_stream_id = -1; |
2387 | | #endif |
2388 | |
|
2389 | 0 | if (sess->use_cache_file) { |
2390 | | //for chunk transfer or H2/H3 |
2391 | 0 | gf_cache_set_content_length(sess->cache_entry, sess->total_size); |
2392 | 0 | gf_cache_close_write_cache(sess->cache_entry, sess, GF_TRUE); |
2393 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_CACHE, |
2394 | 0 | ("[CACHE] url %s saved as %s\n", gf_cache_get_url(sess->cache_entry), gf_cache_get_cache_filename(sess->cache_entry))); |
2395 | |
|
2396 | 0 | if (sess->dm && sess->dm->cache_clean_ms) { |
2397 | 0 | sess->dm->cur_cache_size += sess->total_size; |
2398 | 0 | if (sess->dm->cur_cache_size > sess->dm->max_cache_size) { |
2399 | 0 | if (!sess->dm->next_cache_clean) sess->dm->next_cache_clean = gf_sys_clock()+sess->dm->cache_clean_ms; |
2400 | 0 | else if (gf_sys_clock()>sess->dm->next_cache_clean) { |
2401 | 0 | sess->dm->cur_cache_size = gf_cache_cleanup(sess->dm->cache_directory, sess->dm->max_cache_size); |
2402 | 0 | sess->dm->next_cache_clean = 0; |
2403 | 0 | } |
2404 | 0 | } |
2405 | 0 | } |
2406 | 0 | } |
2407 | |
|
2408 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
2409 | 0 | par.msg_type = GF_NETIO_DATA_TRANSFERED; |
2410 | 0 | par.error = GF_OK; |
2411 | |
|
2412 | 0 | gf_dm_sess_user_io(sess, &par); |
2413 | 0 | sess->total_time_since_req = (u32) (gf_sys_clock_high_res() - sess->request_start_time); |
2414 | 0 | run_time = gf_sys_clock_high_res() - sess->start_time; |
2415 | |
|
2416 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] %s (%d bytes) downloaded in "LLU" us (%d kbps) (%d us since request - got response in %d us)\n", sess->log_name, gf_file_basename(gf_cache_get_url(sess->cache_entry)), sess->bytes_done, |
2417 | 0 | run_time, 8*sess->bytes_per_sec/1000, sess->total_time_since_req, sess->reply_time)); |
2418 | |
|
2419 | 0 | if (sess->chunked && (payload_size==2)) |
2420 | 0 | payload_size=0; |
2421 | 0 | sess->last_error = GF_OK; |
2422 | 0 | } |
2423 | |
|
2424 | 0 | if (rewrite_size && sess->chunked && data) { |
2425 | 0 | if (original_payload) { |
2426 | | //use memmove since regions overlap |
2427 | 0 | memmove(original_payload + *rewrite_size, data, nbBytes); |
2428 | 0 | } |
2429 | 0 | *rewrite_size += nbBytes; |
2430 | 0 | } |
2431 | |
|
2432 | 0 | if (!sess->nb_left_in_chunk && remaining) { |
2433 | 0 | sess->nb_left_in_chunk = remaining; |
2434 | 0 | } else if (payload_size && (sess->status < GF_NETIO_DISCONNECTED)) { |
2435 | 0 | gf_dm_data_received(sess, payload, payload_size, store_in_init, rewrite_size, original_payload); |
2436 | 0 | } |
2437 | 0 | } |
2438 | | |
2439 | | static Bool dm_exceeds_cap_rate(GF_DownloadManager * dm, GF_DownloadSession *for_sess) |
2440 | 0 | { |
2441 | 0 | u32 cumul_rate = 0; |
2442 | 0 | u32 i, count; |
2443 | |
|
2444 | 0 | gf_mx_p(dm->cache_mx); |
2445 | 0 | u64 now = gf_sys_clock_high_res(); |
2446 | 0 | count = gf_list_count(dm->all_sessions); |
2447 | | //check if this fits with all other sessions |
2448 | 0 | for (i=0; i<count; i++) { |
2449 | 0 | GF_DownloadSession * sess = (GF_DownloadSession*)gf_list_get(dm->all_sessions, i); |
2450 | 0 | if (for_sess && (sess != for_sess)) continue; |
2451 | | |
2452 | | //session not running done |
2453 | 0 | if (sess->status != GF_NETIO_DATA_EXCHANGE) continue; |
2454 | | |
2455 | | //compute average rate on a window of 200 ms |
2456 | | //we cannot just use sess->bytes_per_sec because the rate limit might be changed dynamically |
2457 | | //so we need a recent history, not the session history |
2458 | | //note that we don't try to use the estimated bps of chunk transfer when capping |
2459 | 0 | if (!sess->last_cap_rate_time) { |
2460 | 0 | u64 runtime; |
2461 | 0 | sess->last_cap_rate_time = sess->request_start_time; |
2462 | 0 | sess->last_cap_rate_bytes = sess->bytes_done; |
2463 | | |
2464 | | /*compute bps starting from request send time, do not call update_download_rate as we don't want the chunk transfer rate*/ |
2465 | 0 | runtime = (gf_sys_clock_high_res() - sess->request_start_time); |
2466 | 0 | if (!runtime) runtime=1; |
2467 | 0 | sess->last_cap_rate_bytes_per_sec = (u32) ((1000000 * (u64) sess->bytes_done) / runtime); |
2468 | 0 | } else if (now > sess->last_cap_rate_time) { |
2469 | 0 | u64 time = now - sess->last_cap_rate_time; |
2470 | 0 | u64 bytes = sess->bytes_done - sess->last_cap_rate_bytes; |
2471 | 0 | sess->last_cap_rate_bytes_per_sec = (u32) ((1000000 * (u64) bytes) / time); |
2472 | 0 | if (time > 200000) { |
2473 | | //this is an approximation we don't know precisely when these were received |
2474 | | //and we don't really care since next rate estimation will be really high anyway and will exceed cap |
2475 | 0 | sess->last_cap_rate_bytes = sess->bytes_done; |
2476 | 0 | sess->last_cap_rate_time = now; |
2477 | 0 | } |
2478 | 0 | } else { |
2479 | 0 | gf_mx_v(dm->cache_mx); |
2480 | 0 | return GF_TRUE; |
2481 | 0 | } |
2482 | 0 | cumul_rate += sess->last_cap_rate_bytes_per_sec; |
2483 | 0 | if (for_sess) break; |
2484 | 0 | } |
2485 | 0 | gf_mx_v(dm->cache_mx); |
2486 | 0 | if (for_sess) { |
2487 | 0 | if (cumul_rate >= for_sess->max_data_rate) |
2488 | 0 | return GF_TRUE; |
2489 | 0 | } else if ( cumul_rate >= dm->limit_data_rate) { |
2490 | 0 | return GF_TRUE; |
2491 | 0 | } |
2492 | | |
2493 | 0 | return GF_FALSE; |
2494 | 0 | } |
2495 | | |
2496 | | static void gf_dm_sess_estimate_chunk_rate(GF_DownloadSession *sess, u32 nb_bytes) |
2497 | 0 | { |
2498 | 0 | u64 now = gf_sys_clock_high_res(); |
2499 | 0 | sess->chunk_bytes += nb_bytes; |
2500 | 0 | if ((now > sess->last_chunk_start_time + sess->chunk_wnd_dur) || (sess->total_size==sess->bytes_done) ) { |
2501 | 0 | if (sess->chunk_bytes) { |
2502 | 0 | u32 tot_bytes = sess->chunk_bytes + sess->chunk_header_bytes; |
2503 | | //compute rate in bytes per seconds |
2504 | 0 | Double rate = 1000000.0 * tot_bytes; |
2505 | 0 | rate /= (now - sess->last_chunk_start_time); |
2506 | | |
2507 | | //cumulated rate is the weighted sum of our probe rates, the weight being the number of bytes |
2508 | | //when comuting the bitrate, we will divide by the total size |
2509 | 0 | sess->cumulated_chunk_rate += rate * tot_bytes; |
2510 | |
|
2511 | 0 | sess->chunk_bytes = 0; |
2512 | 0 | sess->cumulated_chunk_header_bytes += sess->chunk_header_bytes; |
2513 | 0 | sess->chunk_header_bytes = 0; |
2514 | | |
2515 | | //we are done, update rate |
2516 | 0 | if (sess->total_size==sess->bytes_done) |
2517 | 0 | dm_sess_update_download_rate(sess); |
2518 | 0 | } |
2519 | 0 | sess->last_chunk_start_time = now; |
2520 | 0 | } |
2521 | 0 | } |
2522 | | |
2523 | | GF_EXPORT |
2524 | | GF_Err gf_dm_sess_fetch_data(GF_DownloadSession *sess, char *buffer, u32 buffer_size, u32 *read_size) |
2525 | 0 | { |
2526 | 0 | u32 size; |
2527 | 0 | GF_Err e; |
2528 | |
|
2529 | 0 | if (!buffer || !buffer_size) { |
2530 | 0 | if (sess->put_state) { |
2531 | 0 | sess->put_state = 2; |
2532 | 0 | sess->status = GF_NETIO_WAIT_FOR_REPLY; |
2533 | 0 | return GF_OK; |
2534 | 0 | } |
2535 | 0 | return GF_BAD_PARAM; |
2536 | 0 | } |
2537 | 0 | if (sess->th) |
2538 | 0 | return GF_BAD_PARAM; |
2539 | 0 | if (sess->status == GF_NETIO_DISCONNECTED) { |
2540 | 0 | if (!sess->init_data_size) |
2541 | 0 | return GF_EOS; |
2542 | 0 | } |
2543 | 0 | else if (sess->status == GF_NETIO_STATE_ERROR) { |
2544 | 0 | return sess->last_error; |
2545 | 0 | } |
2546 | 0 | else if (sess->status > GF_NETIO_DATA_TRANSFERED) |
2547 | 0 | return GF_BAD_PARAM; |
2548 | | |
2549 | 0 | *read_size = 0; |
2550 | 0 | if (sess->status == GF_NETIO_DATA_TRANSFERED) { |
2551 | 0 | if (!sess->server_mode) |
2552 | 0 | return GF_EOS; |
2553 | 0 | if (!sess->init_data_size && sess->total_size && (sess->total_size==sess->bytes_done)) |
2554 | 0 | return GF_EOS; |
2555 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
2556 | 0 | } |
2557 | | |
2558 | 0 | if (sess->status == GF_NETIO_SETUP) { |
2559 | 0 | gf_dm_connect(sess); |
2560 | 0 | if (sess->last_error) |
2561 | 0 | return sess->last_error; |
2562 | 0 | e = GF_IP_NETWORK_EMPTY; |
2563 | 0 | } else if (sess->status < GF_NETIO_DATA_EXCHANGE) { |
2564 | 0 | sess->do_requests(sess); |
2565 | 0 | if (!sess->server_mode |
2566 | 0 | && (sess->status > GF_NETIO_WAIT_FOR_REPLY) |
2567 | 0 | && sess->needs_range |
2568 | 0 | && (sess->rsp_code==200) |
2569 | 0 | ) { |
2570 | | //reset for next call to process if user wants so |
2571 | 0 | sess->needs_range = GF_FALSE; |
2572 | 0 | return GF_IO_BYTE_RANGE_NOT_SUPPORTED; |
2573 | 0 | } |
2574 | 0 | e = sess->last_error ? sess->last_error : GF_IP_NETWORK_EMPTY; |
2575 | 0 | } |
2576 | | /*we're running but we had data previously*/ |
2577 | 0 | else if (sess->init_data) { |
2578 | 0 | e = GF_OK; |
2579 | 0 | if (sess->init_data_size<=buffer_size) { |
2580 | 0 | memcpy(buffer, sess->init_data, sizeof(char)*sess->init_data_size); |
2581 | 0 | *read_size = sess->init_data_size; |
2582 | 0 | gf_free(sess->init_data); |
2583 | 0 | sess->init_data = NULL; |
2584 | 0 | if (sess->init_data_size==sess->total_size) |
2585 | 0 | e = GF_EOS; |
2586 | 0 | sess->init_data_size = 0; |
2587 | 0 | } else { |
2588 | 0 | memcpy(buffer, sess->init_data, sizeof(char)*buffer_size); |
2589 | 0 | *read_size = buffer_size; |
2590 | 0 | sess->init_data_size -= buffer_size; |
2591 | 0 | memmove(sess->init_data, sess->init_data+buffer_size, sizeof(char)*sess->init_data_size); |
2592 | 0 | e = GF_OK; |
2593 | 0 | } |
2594 | 0 | } else if (sess->local_cache_only) { |
2595 | 0 | Bool was_modified; |
2596 | 0 | u32 to_copy, full_cache_size, max_valid_size=0, cache_done; |
2597 | 0 | const u8 *ptr; |
2598 | 0 | e = GF_OK; |
2599 | 0 | gf_assert(sess->cache_entry); |
2600 | | //always refresh total size |
2601 | 0 | sess->total_size = gf_cache_get_content_length(sess->cache_entry); |
2602 | |
|
2603 | 0 | ptr = gf_cache_get_content(sess->cache_entry, &full_cache_size, &max_valid_size, &was_modified); |
2604 | |
|
2605 | 0 | cache_done = gf_cache_is_done(sess->cache_entry); |
2606 | | //something went wrong, we cannot have less valid bytes than what we had at previous call(s) |
2607 | 0 | if (max_valid_size < sess->bytes_done) |
2608 | 0 | cache_done = 2; |
2609 | |
|
2610 | 0 | if ((sess->bytes_done >= full_cache_size)|| (cache_done==2)) { |
2611 | 0 | *read_size = 0; |
2612 | 0 | gf_cache_release_content(sess->cache_entry); |
2613 | 0 | if (cache_done==2) { |
2614 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
2615 | 0 | SET_LAST_ERR(GF_IP_NETWORK_FAILURE) |
2616 | 0 | return GF_IP_NETWORK_FAILURE; |
2617 | 0 | } |
2618 | 0 | else if (cache_done) { |
2619 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
2620 | 0 | SET_LAST_ERR( GF_OK) |
2621 | 0 | return GF_EOS; |
2622 | 0 | } |
2623 | 0 | return was_modified ? GF_OK : GF_IP_NETWORK_EMPTY; |
2624 | 0 | } |
2625 | 0 | if (!ptr) return GF_OUT_OF_MEM; |
2626 | | |
2627 | | //only copy valid bytes for http |
2628 | 0 | to_copy = max_valid_size - sess->bytes_done; |
2629 | 0 | if (to_copy > buffer_size) to_copy = buffer_size; |
2630 | |
|
2631 | 0 | memcpy(buffer, ptr + sess->bytes_done, to_copy); |
2632 | 0 | sess->bytes_done += to_copy; |
2633 | 0 | *read_size = to_copy; |
2634 | 0 | if ((cache_done==1) && (sess->bytes_done == sess->total_size) ) { |
2635 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
2636 | 0 | SET_LAST_ERR( (cache_done==2) ? GF_IP_NETWORK_FAILURE : GF_OK) |
2637 | 0 | } else { |
2638 | 0 | sess->total_size = 0; |
2639 | 0 | } |
2640 | 0 | gf_cache_release_content(sess->cache_entry); |
2641 | 0 | } else { |
2642 | |
|
2643 | 0 | if (sess->dm && (sess->dm->limit_data_rate || sess->max_data_rate)) { |
2644 | 0 | if (dm_exceeds_cap_rate(sess->dm, sess->max_data_rate ? sess : NULL)) |
2645 | 0 | return GF_IP_NETWORK_EMPTY; |
2646 | | |
2647 | 0 | if (buffer_size > sess->dm->read_buf_size) |
2648 | 0 | buffer_size = sess->dm->read_buf_size; |
2649 | 0 | } |
2650 | | |
2651 | 0 | e = GF_OK; |
2652 | 0 | *read_size = 0; |
2653 | 0 | u32 nb_read = 0; |
2654 | | //perform a loop, mostly for chunk-tranfer mode where a server may push a lot of small TCP frames, |
2655 | | //we want to flush everything as fast as possible |
2656 | 0 | while (1) { |
2657 | 0 | u32 single_read = 0; |
2658 | |
|
2659 | 0 | if (sess->remaining_data && sess->remaining_data_size) { |
2660 | 0 | if (nb_read + sess->remaining_data_size >= buffer_size) { |
2661 | 0 | if (!nb_read) { |
2662 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] No HTTP chunk header found for %d bytes, assuming broken chunk transfer and aborting\n", sess->log_name, sess->remaining_data_size)); |
2663 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
2664 | 0 | } |
2665 | 0 | break; |
2666 | 0 | } |
2667 | 0 | memcpy(buffer + nb_read, sess->remaining_data, sess->remaining_data_size); |
2668 | 0 | } else if (nb_read >= buffer_size) { |
2669 | 0 | break; |
2670 | 0 | } |
2671 | 0 | e = gf_dm_sess_read(sess, buffer + nb_read + sess->remaining_data_size, buffer_size - sess->remaining_data_size - nb_read, &single_read); |
2672 | 0 | if (e<0) { |
2673 | 0 | gf_assert(single_read==0); |
2674 | 0 | break; |
2675 | 0 | } |
2676 | | |
2677 | 0 | size = sess->remaining_data_size + single_read; |
2678 | 0 | sess->remaining_data_size = 0; |
2679 | 0 | single_read = 0; |
2680 | |
|
2681 | | #ifdef GPAC_HTTPMUX |
2682 | | //do NOT call data received if hmux, done once input is empty |
2683 | | if (!sess->hmux_sess || sess->cached_file) |
2684 | | #endif |
2685 | 0 | gf_dm_data_received(sess, (u8 *) buffer + nb_read, size, GF_FALSE, &single_read, buffer + nb_read); |
2686 | | |
2687 | |
|
2688 | 0 | if (!sess->chunked) |
2689 | 0 | single_read = size; |
2690 | |
|
2691 | 0 | nb_read += single_read; |
2692 | 0 | } |
2693 | | |
2694 | | #ifdef GPAC_HTTPMUX |
2695 | | //process any received data |
2696 | | if (sess->hmux_sess) { |
2697 | | nb_read = 0; |
2698 | | hmux_fetch_data(sess, buffer, buffer_size, &nb_read); |
2699 | | sess->hmux_sess->write(sess); |
2700 | | |
2701 | | //stream is over and all data flushed, move to GF_NETIO_DATA_TRANSFERED in client mode |
2702 | | if ((sess->hmux_stream_id<0) && sess->hmux_data_done && !sess->hmux_buf.size && !sess->server_mode) { |
2703 | | sess->status = GF_NETIO_DATA_TRANSFERED; |
2704 | | SET_LAST_ERR(GF_OK) |
2705 | | } |
2706 | | } |
2707 | | #endif |
2708 | | |
2709 | 0 | *read_size = nb_read; |
2710 | | //we had data but last call to gf_dm_sess_read may have returned network empty |
2711 | 0 | if (nb_read && (e<0)) |
2712 | 0 | e = GF_OK; |
2713 | | |
2714 | | |
2715 | | //estimate rate for chunk-transfer - we only do that for fetch_data |
2716 | 0 | if (sess->chunked |
2717 | | #ifdef GPAC_HTTPMUX |
2718 | | || sess->hmux_sess |
2719 | | #endif |
2720 | 0 | ) |
2721 | 0 | gf_dm_sess_estimate_chunk_rate(sess, nb_read); |
2722 | |
|
2723 | 0 | if (! (*read_size) && (e==GF_IP_NETWORK_EMPTY)) { |
2724 | | #ifdef GPAC_HTTPMUX |
2725 | | if (sess->hmux_sess && !sess->total_size |
2726 | | //for client, wait for close - for server move to data_transfered as soon as we're done pushing data |
2727 | | && (((sess->hmux_stream_id<0) && sess->bytes_done) || (sess->hmux_data_done && sess->server_mode)) |
2728 | | ) { |
2729 | | sess->status = GF_NETIO_DATA_TRANSFERED; |
2730 | | SET_LAST_ERR(GF_OK) |
2731 | | return GF_EOS; |
2732 | | } |
2733 | | #endif |
2734 | |
|
2735 | | #ifdef GPAC_HAS_CURL |
2736 | | if (sess->curl_hnd) { |
2737 | | if (sess->status == GF_NETIO_WAIT_FOR_REPLY) |
2738 | | return GF_IP_NETWORK_EMPTY; |
2739 | | } else |
2740 | | #endif |
2741 | 0 | { |
2742 | 0 | if (sess->sock) |
2743 | 0 | e = gf_sk_probe(sess->sock); |
2744 | 0 | } |
2745 | |
|
2746 | 0 | if ((e==GF_IP_CONNECTION_CLOSED) |
2747 | 0 | || (sess->request_timeout && (gf_sys_clock_high_res() - sess->last_fetch_time > 1000 * sess->request_timeout)) |
2748 | 0 | ) { |
2749 | 0 | if (e==GF_IP_CONNECTION_CLOSED) { |
2750 | 0 | SET_LAST_ERR(GF_IP_CONNECTION_CLOSED) |
2751 | 0 | sess_connection_closed(sess); |
2752 | 0 | } else { |
2753 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, |
2754 | 0 | ("[%s] Session timeout for %s after %u ms, aborting\n", sess->log_name, sess->orig_url, sess->request_timeout)); |
2755 | |
|
2756 | 0 | SET_LAST_ERR(GF_IP_NETWORK_FAILURE) |
2757 | 0 | } |
2758 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
2759 | 0 | return GF_IP_NETWORK_EMPTY; |
2760 | 0 | } |
2761 | 0 | } |
2762 | 0 | } |
2763 | | |
2764 | | #ifdef GPAC_HTTPMUX |
2765 | | if (sess->hmux_sess && sess->hmux_buf.size) { |
2766 | | return e; |
2767 | | } |
2768 | | #endif |
2769 | | |
2770 | 0 | if (sess->server_mode && (sess->status == GF_NETIO_DATA_EXCHANGE)) { |
2771 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
2772 | 0 | SET_LAST_ERR(GF_OK) |
2773 | 0 | } |
2774 | |
|
2775 | 0 | return e; |
2776 | 0 | } |
2777 | | |
2778 | | GF_EXPORT |
2779 | | GF_Err gf_dm_sess_get_stats(GF_DownloadSession * sess, const char **server, const char **path, u64 *total_size, u64 *bytes_done, u32 *bytes_per_sec, GF_NetIOStatus *net_status) |
2780 | 0 | { |
2781 | 0 | if (!sess) |
2782 | 0 | return GF_BAD_PARAM; |
2783 | 0 | if (server) *server = sess->server_name; |
2784 | 0 | if (path) *path = sess->remote_path; |
2785 | 0 | if (total_size) { |
2786 | 0 | if (sess->total_size==SIZE_IN_STREAM) *total_size = 0; |
2787 | 0 | else *total_size = sess->total_size; |
2788 | 0 | } |
2789 | 0 | if (bytes_done) *bytes_done = sess->bytes_done; |
2790 | 0 | if (bytes_per_sec) { |
2791 | 0 | if (sess->dm && (sess->dm->limit_data_rate || sess->max_data_rate) && sess->last_cap_rate_bytes_per_sec) { |
2792 | 0 | *bytes_per_sec = sess->last_cap_rate_bytes_per_sec; |
2793 | 0 | } else { |
2794 | 0 | *bytes_per_sec = sess->bytes_per_sec; |
2795 | 0 | } |
2796 | 0 | } |
2797 | |
|
2798 | 0 | if (net_status) *net_status = sess->status; |
2799 | |
|
2800 | 0 | if (sess->status == GF_NETIO_DISCONNECTED) { |
2801 | 0 | if (sess->last_error) return sess->last_error; |
2802 | 0 | return GF_EOS; |
2803 | 0 | } |
2804 | 0 | else if (sess->status == GF_NETIO_STATE_ERROR) |
2805 | 0 | return sess->last_error ? sess->last_error : GF_SERVICE_ERROR; |
2806 | 0 | return GF_OK; |
2807 | 0 | } |
2808 | | |
2809 | | GF_EXPORT |
2810 | | u64 gf_dm_sess_get_utc_start(GF_DownloadSession * sess) |
2811 | 0 | { |
2812 | 0 | if (!sess) return 0; |
2813 | 0 | return sess->start_time_utc; |
2814 | 0 | } |
2815 | | |
2816 | | GF_EXPORT |
2817 | | const char *gf_dm_sess_get_cache_name(GF_DownloadSession * sess) |
2818 | 0 | { |
2819 | 0 | if (!sess) return NULL; |
2820 | 0 | if (! sess->cache_entry || sess->needs_cache_reconfig) return NULL; |
2821 | 0 | if (!sess->use_cache_file) return NULL; |
2822 | 0 | return gf_cache_get_cache_filename(sess->cache_entry); |
2823 | 0 | } |
2824 | | |
2825 | | GF_EXPORT |
2826 | | void gf_dm_sess_abort(GF_DownloadSession * sess) |
2827 | 0 | { |
2828 | 0 | if (sess) { |
2829 | 0 | gf_mx_p(sess->mx); |
2830 | |
|
2831 | | #ifdef GPAC_HTTPMUX |
2832 | | if (sess->hmux_sess && (sess->status==GF_NETIO_DATA_EXCHANGE)) { |
2833 | | sess->hmux_sess->stream_reset(sess, GF_TRUE); |
2834 | | sess->hmux_sess->write(sess); |
2835 | | } |
2836 | | #endif |
2837 | 0 | gf_dm_disconnect(sess, HTTP_CLOSE); |
2838 | | //not an error, move to DISCONNECTED state |
2839 | 0 | sess->status = GF_NETIO_DISCONNECTED; |
2840 | 0 | SET_LAST_ERR(GF_IP_CONNECTION_CLOSED) |
2841 | 0 | gf_mx_v(sess->mx); |
2842 | 0 | } |
2843 | 0 | } |
2844 | | |
2845 | | /*! |
2846 | | * Sends the HTTP headers |
2847 | | \param sess The GF_DownloadSession |
2848 | | \return GF_OK if everything went fine, the error otherwise |
2849 | | */ |
2850 | 0 | static GF_Err http_send_headers(GF_DownloadSession *sess) { |
2851 | 0 | GF_Err e; |
2852 | 0 | GF_NETIO_Parameter par; |
2853 | 0 | Bool no_cache = GF_FALSE; |
2854 | 0 | char range_buf[1024]; |
2855 | 0 | char pass_buf[1124]; |
2856 | 0 | char req_name[20]; |
2857 | 0 | const char *user_agent; |
2858 | 0 | const char *url; |
2859 | 0 | const char *user_profile; |
2860 | 0 | const char *param_string; |
2861 | 0 | Bool inject_icy = GF_FALSE; |
2862 | 0 | u32 i, count; |
2863 | 0 | GF_HTTPHeader *hdr; |
2864 | 0 | Bool has_accept, has_connection, has_range, has_agent, has_language, send_profile, has_mime, has_chunk_transfer; |
2865 | 0 | assert (sess->status == GF_NETIO_CONNECTED); |
2866 | |
|
2867 | 0 | char * sHTTP = sess->http_buf; |
2868 | |
|
2869 | 0 | gf_assert(sess->remaining_data_size == 0); |
2870 | |
|
2871 | 0 | if (sess->needs_cache_reconfig) { |
2872 | 0 | gf_dm_sess_clear_headers(sess); |
2873 | 0 | gf_dm_configure_cache(sess); |
2874 | 0 | sess->needs_cache_reconfig = 0; |
2875 | 0 | } |
2876 | 0 | if (sess->cached_file) { |
2877 | 0 | sess->last_fetch_time = sess->request_start_time = gf_sys_clock_high_res(); |
2878 | 0 | sess->req_hdr_size = 0; |
2879 | 0 | sess->status = GF_NETIO_WAIT_FOR_REPLY; |
2880 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_WAIT_FOR_REPLY, GF_OK); |
2881 | 0 | return GF_OK; |
2882 | 0 | } |
2883 | 0 | gf_dm_sess_clear_headers(sess); |
2884 | | |
2885 | | //in case we got disconnected, reconnect |
2886 | | #ifdef GPAC_HAS_CURL |
2887 | | if (sess->curl_hnd) |
2888 | | e = GF_OK; |
2889 | | else |
2890 | | #endif |
2891 | 0 | e = gf_sk_probe(sess->sock); |
2892 | |
|
2893 | 0 | if (e && (e!=GF_IP_NETWORK_EMPTY)) { |
2894 | 0 | sess_connection_closed(sess); |
2895 | 0 | if ((e==GF_IP_CONNECTION_CLOSED) && sess->num_retry) { |
2896 | 0 | sess->num_retry--; |
2897 | 0 | sess->status = GF_NETIO_SETUP; |
2898 | 0 | return GF_OK; |
2899 | 0 | } |
2900 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
2901 | 0 | SET_LAST_ERR(e) |
2902 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_STATE_ERROR, e); |
2903 | 0 | return e; |
2904 | 0 | } |
2905 | | |
2906 | | /*setup authentication*/ |
2907 | 0 | gf_strcpy(pass_buf, ""); |
2908 | 0 | sess->creds = gf_user_credentials_find_for_site( sess->dm, sess->server_name, NULL); |
2909 | 0 | if (sess->creds && sess->creds->valid) { |
2910 | | #ifdef GPAC_HAS_CURL |
2911 | | //let curl handle authentication methods |
2912 | | if (sess->curl_hnd) { |
2913 | | char szUsrPass[101]; |
2914 | | u32 len = gf_base64_decode(sess->creds->digest, (u32) strlen(sess->creds->digest), szUsrPass, 100); |
2915 | | szUsrPass[len]=0; |
2916 | | curl_easy_setopt(sess->curl_hnd, CURLOPT_USERPWD, szUsrPass); |
2917 | | } else |
2918 | | #endif |
2919 | 0 | sprintf(pass_buf, "Basic %s", sess->creds->digest); |
2920 | 0 | } |
2921 | |
|
2922 | 0 | user_agent = gf_opts_get_key("core", "user-agent"); |
2923 | 0 | if (!user_agent) user_agent = GF_DOWNLOAD_AGENT_NAME; |
2924 | |
|
2925 | 0 | sess->put_state = 0; |
2926 | |
|
2927 | 0 | par.error = GF_OK; |
2928 | 0 | par.msg_type = GF_NETIO_GET_METHOD; |
2929 | 0 | par.name = NULL; |
2930 | 0 | gf_dm_sess_user_io(sess, &par); |
2931 | 0 | if (!par.name || sess->server_only_understand_get) { |
2932 | 0 | par.name = "GET"; |
2933 | 0 | } |
2934 | |
|
2935 | 0 | gf_strcpy(req_name, par.name); |
2936 | |
|
2937 | 0 | if (!strcmp(req_name, "GET")) { |
2938 | 0 | sess->http_read_type = GET; |
2939 | | #ifdef GPAC_HTTPMUX |
2940 | | if (!sess->hmux_sess) |
2941 | | #endif |
2942 | 0 | inject_icy = GF_TRUE; |
2943 | 0 | } else if (!strcmp(req_name, "HEAD")) sess->http_read_type = HEAD; |
2944 | 0 | else sess->http_read_type = OTHER; |
2945 | |
|
2946 | 0 | if (!strcmp(req_name, "PUT") || !strcmp(req_name, "POST")) |
2947 | 0 | sess->put_state = 1; |
2948 | | |
2949 | | //url is the remote path event if proxy (Host header will point to desired host) |
2950 | | //note that url is not used for CURL, already setup together with proxy |
2951 | 0 | url = sess->remote_path; |
2952 | | |
2953 | | /*get all headers*/ |
2954 | 0 | gf_dm_sess_clear_headers(sess); |
2955 | | |
2956 | |
|
2957 | | #ifdef GPAC_HTTPMUX |
2958 | | if (!sess->hmux_sess) |
2959 | | #endif |
2960 | 0 | { |
2961 | | //always put port number when proxy is enabled |
2962 | 0 | if (sess->proxy_enabled || ((sess->port!=80) && (sess->port!=443))) { |
2963 | 0 | sprintf(sHTTP, "%s:%u", sess->server_name, sess->port); |
2964 | 0 | PUSH_HDR("Host", sHTTP); |
2965 | 0 | } else { |
2966 | 0 | PUSH_HDR("Host", sess->server_name); |
2967 | 0 | } |
2968 | 0 | } |
2969 | |
|
2970 | 0 | has_agent = has_accept = has_connection = has_range = has_language = has_mime = has_chunk_transfer = GF_FALSE; |
2971 | 0 | while (1) { |
2972 | 0 | par.msg_type = GF_NETIO_GET_HEADER; |
2973 | 0 | par.value = NULL; |
2974 | 0 | gf_dm_sess_user_io(sess, &par); |
2975 | 0 | if (!par.value) break; |
2976 | | //if name is not set, skip this header |
2977 | 0 | if (!par.name) continue; |
2978 | | |
2979 | 0 | if (!stricmp(par.name, "Connection")) { |
2980 | 0 | if (!stricmp(par.value, "close")) |
2981 | 0 | has_connection = GF_TRUE; |
2982 | 0 | else |
2983 | 0 | continue; |
2984 | 0 | } |
2985 | 0 | else if (!stricmp(par.name, "Transfer-Encoding")) { |
2986 | 0 | if (!stricmp(par.value, "chunked")) |
2987 | 0 | has_chunk_transfer = GF_TRUE; |
2988 | 0 | continue; |
2989 | 0 | } |
2990 | | |
2991 | 0 | gf_dm_sess_set_header_ex(sess, par.name, par.value, GF_TRUE); |
2992 | |
|
2993 | 0 | if (!stricmp(par.name, "Accept")) has_accept = GF_TRUE; |
2994 | 0 | else if (!stricmp(par.name, "Range")) has_range = GF_TRUE; |
2995 | 0 | else if (!stricmp(par.name, "User-Agent")) has_agent = GF_TRUE; |
2996 | 0 | else if (!stricmp(par.name, "Accept-Language")) has_language = GF_TRUE; |
2997 | 0 | else if (!stricmp(par.name, "Content-Type")) has_mime = GF_TRUE; |
2998 | |
|
2999 | 0 | if (!par.msg_type) break; |
3000 | 0 | } |
3001 | 0 | if (!has_agent) PUSH_HDR("User-Agent", user_agent) |
3002 | | |
3003 | | /*no mime and POST/PUT, default to octet stream*/ |
3004 | 0 | if (!has_mime && (sess->http_read_type==OTHER)) PUSH_HDR("Content-Type", "application/octet-stream") |
3005 | |
|
3006 | 0 | if (!has_accept && (sess->http_read_type!=OTHER) ) PUSH_HDR("Accept", "*/*") |
3007 | |
|
3008 | | #ifdef GPAC_HTTPMUX |
3009 | | if (sess->hmux_sess) |
3010 | | has_connection = GF_TRUE; |
3011 | | #endif |
3012 | | |
3013 | |
|
3014 | | #ifdef GPAC_HAS_HTTP2 |
3015 | | //inject upgrade for h2 |
3016 | | if (!has_connection && !sess->hmux_sess |
3017 | | #ifdef GPAC_HAS_CURL |
3018 | | && !sess->curl_hnd |
3019 | | #endif |
3020 | | #ifdef GPAC_HAS_SSL |
3021 | | && !sess->ssl |
3022 | | #endif |
3023 | | && !sess->dm->disable_http2 |
3024 | | && !sess->h2_upgrade_state |
3025 | | && !gf_opts_get_bool("core", "no-h2c") |
3026 | | ) { |
3027 | | http2_set_upgrade_headers(sess); |
3028 | | inject_icy = GF_FALSE; |
3029 | | } else |
3030 | | #endif //GPAC_HAS_HTTP2 |
3031 | |
|
3032 | 0 | if (sess->proxy_enabled==1) { |
3033 | | #ifdef GPAC_HTTPMUX |
3034 | | if (!sess->hmux_sess) |
3035 | | #endif |
3036 | 0 | PUSH_HDR("Proxy-Connection", "Keep-alive") |
3037 | 0 | } |
3038 | 0 | else if (!has_connection) { |
3039 | 0 | PUSH_HDR("Connection", "Keep-Alive"); |
3040 | 0 | } |
3041 | | |
3042 | |
|
3043 | 0 | if (has_chunk_transfer |
3044 | | #ifdef GPAC_HTTPMUX |
3045 | | && !sess->hmux_sess |
3046 | | #endif |
3047 | 0 | ) { |
3048 | 0 | PUSH_HDR("Transfer-Encoding", "chunked"); |
3049 | 0 | sess->chunked = GF_TRUE; |
3050 | 0 | } |
3051 | |
|
3052 | 0 | if (!has_range && sess->needs_range) { |
3053 | 0 | if (!sess->range_end) |
3054 | 0 | sprintf(range_buf, "bytes="LLD"-", sess->range_start); |
3055 | | //if end is set to -1 use open end |
3056 | 0 | else if (sess->range_end==(u64)-1) |
3057 | 0 | sprintf(range_buf, "bytes="LLD"-", sess->range_start); |
3058 | 0 | else |
3059 | 0 | sprintf(range_buf, "bytes="LLD"-"LLD"", sess->range_start, sess->range_end); |
3060 | 0 | PUSH_HDR("Range", range_buf) |
3061 | 0 | no_cache = GF_TRUE; |
3062 | 0 | } |
3063 | 0 | if (!has_language) { |
3064 | 0 | const char *opt = gf_opts_get_key("core", "lang"); |
3065 | 0 | if (opt) PUSH_HDR("Accept-Language", opt) |
3066 | 0 | } |
3067 | | |
3068 | |
|
3069 | 0 | if (strlen(pass_buf)) { |
3070 | 0 | PUSH_HDR("Authorization", pass_buf) |
3071 | 0 | } |
3072 | |
|
3073 | 0 | par.msg_type = GF_NETIO_GET_CONTENT; |
3074 | 0 | par.data = NULL; |
3075 | 0 | par.size = 0; |
3076 | | |
3077 | | /*check if we have personalization info*/ |
3078 | 0 | send_profile = GF_FALSE; |
3079 | 0 | user_profile = gf_opts_get_key("core", "user-profileid"); |
3080 | |
|
3081 | 0 | if (user_profile) { |
3082 | 0 | PUSH_HDR("X-UserProfileID", user_profile); |
3083 | 0 | } else if ((sess->http_read_type == GET) || (sess->http_read_type == HEAD) ) { |
3084 | 0 | user_profile = gf_opts_get_key("core", "user-profile"); |
3085 | 0 | if (user_profile && gf_file_exists(user_profile)) { |
3086 | 0 | FILE *profile = gf_fopen(user_profile, "rb"); |
3087 | 0 | if (profile) { |
3088 | 0 | par.size = (u32) gf_fsize(profile); |
3089 | 0 | gf_fclose(profile); |
3090 | 0 | sprintf(range_buf, "%d", par.size); |
3091 | 0 | PUSH_HDR("Content-Length", range_buf); |
3092 | 0 | PUSH_HDR("Content-Type", "text/xml"); |
3093 | 0 | send_profile = GF_TRUE; |
3094 | 0 | } |
3095 | 0 | } |
3096 | 0 | } |
3097 | | |
3098 | |
|
3099 | 0 | if (!send_profile) { |
3100 | 0 | gf_dm_sess_user_io(sess, &par); |
3101 | 0 | if (par.data && par.size) { |
3102 | 0 | sprintf(range_buf, "%d", par.size); |
3103 | 0 | PUSH_HDR("Content-Length", range_buf); |
3104 | 0 | } else { |
3105 | 0 | par.data = NULL; |
3106 | 0 | par.size = 0; |
3107 | 0 | } |
3108 | 0 | } |
3109 | |
|
3110 | 0 | if (inject_icy) { |
3111 | | /* This will force the server to respond with Icy-Metaint */ |
3112 | 0 | PUSH_HDR("Icy-Metadata", "1"); |
3113 | 0 | } |
3114 | |
|
3115 | 0 | if (sess->http_read_type!=OTHER) { |
3116 | 0 | const char *etag=NULL, *last_modif=NULL; |
3117 | | |
3118 | | /*cached headers are not appended in POST*/ |
3119 | 0 | if (!no_cache && !sess->disable_cache && (GF_OK < gf_cache_get_http_headers( sess->cache_entry, &etag, &last_modif)) ) { |
3120 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("Cache Entry : %p, FAILED to append cache directives.", sess->cache_entry)); |
3121 | 0 | } |
3122 | |
|
3123 | 0 | if (etag) PUSH_HDR("If-None-Match", etag) |
3124 | 0 | if (last_modif) PUSH_HDR("If-Modified-Since", last_modif) |
3125 | 0 | } |
3126 | | |
3127 | | |
3128 | | //done gathering headers |
3129 | |
|
3130 | 0 | param_string = gf_opts_get_key("core", "query-string"); |
3131 | |
|
3132 | | #ifdef GPAC_HTTPMUX |
3133 | | if (sess->hmux_sess) { |
3134 | | Bool has_body = GF_FALSE; |
3135 | | |
3136 | | gf_mx_p(sess->mx); |
3137 | | |
3138 | | sess->hmux_is_eos = 0; |
3139 | | sess->hmux_send_data = NULL; |
3140 | | if (par.data && par.size) { |
3141 | | has_body = GF_TRUE; |
3142 | | sess->hmux_send_data = (u8 *) par.data; |
3143 | | sess->hmux_send_data_len = par.size; |
3144 | | sess->hmux_is_eos = 1; |
3145 | | } else if (sess->put_state==1) { |
3146 | | has_body = GF_TRUE; |
3147 | | } |
3148 | | e = sess->hmux_sess->submit_request(sess, req_name, url, param_string, has_body); |
3149 | | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[HTTP] Sending request %s %s:%u%s\n", req_name, sess->server_name, sess->port, url)); |
3150 | | |
3151 | | sess->last_fetch_time = sess->request_start_time = gf_sys_clock_high_res(); |
3152 | | |
3153 | | if (!e || (e==GF_IP_NETWORK_EMPTY)) { |
3154 | | e = sess->hmux_sess->send_pending_data(sess); |
3155 | | if (e==GF_IP_NETWORK_EMPTY) e = GF_OK; |
3156 | | } |
3157 | | |
3158 | | gf_mx_v(sess->mx); |
3159 | | goto req_sent; |
3160 | | } |
3161 | | #endif // GPAC_HTTPMUX |
3162 | |
|
3163 | | #ifdef GPAC_HAS_CURL |
3164 | | if (sess->curl_hnd) { |
3165 | | if (!sess->curl_not_http) |
3166 | | curl_easy_setopt(sess->curl_hnd, CURLOPT_CUSTOMREQUEST, req_name); |
3167 | | } else |
3168 | | #endif |
3169 | 0 | { |
3170 | |
|
3171 | 0 | if (param_string) { |
3172 | 0 | if (strchr(sess->remote_path, '?')) { |
3173 | 0 | sprintf(sHTTP, "%s %s&%s HTTP/1.1\r\n", req_name, url, param_string); |
3174 | 0 | } else { |
3175 | 0 | sprintf(sHTTP, "%s %s?%s HTTP/1.1\r\n", req_name, url, param_string); |
3176 | 0 | } |
3177 | 0 | } else { |
3178 | 0 | sprintf(sHTTP, "%s %s HTTP/1.1\r\n", req_name, url); |
3179 | 0 | } |
3180 | 0 | } |
3181 | | |
3182 | | //serialize headers |
3183 | 0 | count = gf_list_count(sess->headers); |
3184 | 0 | for (i=0; i<count; i++) { |
3185 | 0 | hdr = gf_list_get(sess->headers, i); |
3186 | | #ifdef GPAC_HAS_CURL |
3187 | | if (sess->curl_hnd) { |
3188 | | if (sess->curl_not_http) continue; |
3189 | | char szHDR[1000]; |
3190 | | sprintf(szHDR, "%s: %s", hdr->name, hdr->value); |
3191 | | sess->curl_hdrs = curl_slist_append(sess->curl_hdrs, szHDR); |
3192 | | continue; |
3193 | | } |
3194 | | #endif |
3195 | 0 | gf_strlcat(sHTTP, hdr->name, sess->http_buf_size); |
3196 | 0 | gf_strlcat(sHTTP, ": ", sess->http_buf_size); |
3197 | 0 | gf_strlcat(sHTTP, hdr->value, sess->http_buf_size); |
3198 | 0 | gf_strlcat(sHTTP, "\r\n", sess->http_buf_size); |
3199 | 0 | } |
3200 | | #ifdef GPAC_HAS_CURL |
3201 | | if (sess->curl_hnd) { |
3202 | | if (!sess->curl_not_http) { |
3203 | | curl_easy_setopt(sess->curl_hnd, CURLOPT_HTTPHEADER, sess->curl_hdrs); |
3204 | | } |
3205 | | } |
3206 | | #endif |
3207 | |
|
3208 | 0 | gf_strlcat(sHTTP, "\r\n", sess->http_buf_size); |
3209 | |
|
3210 | | #ifdef GPAC_HAS_CURL |
3211 | | if (sess->curl_hnd) { |
3212 | | gf_assert(!sess->curl_hnd_registered); |
3213 | | } else |
3214 | | #endif |
3215 | 0 | if (send_profile || par.data) { |
3216 | 0 | u32 len = (u32) strlen(sHTTP); |
3217 | 0 | char *tmp_buf = (char*)gf_malloc(sizeof(char)*(len+par.size+1)); |
3218 | 0 | memcpy(tmp_buf, sHTTP, len+1); |
3219 | 0 | if (par.data) { |
3220 | 0 | memcpy(tmp_buf+len, par.data, par.size); |
3221 | 0 | tmp_buf[len+par.size] = 0; |
3222 | |
|
3223 | 0 | sess->put_state = 2; |
3224 | 0 | } else { |
3225 | 0 | FILE *profile; |
3226 | 0 | user_profile = gf_opts_get_key("core", "user-profile"); |
3227 | 0 | assert (user_profile); |
3228 | 0 | profile = gf_fopen(user_profile, "rt"); |
3229 | 0 | if (profile) { |
3230 | 0 | s32 read = (s32) gf_fread(tmp_buf+len, par.size, profile); |
3231 | 0 | if ((read<0) || (read< (s32) par.size)) { |
3232 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, |
3233 | 0 | ("[%s] Error while loading UserProfile, size=%d, should be %d\n", sess->log_name, read, par.size)); |
3234 | 0 | for (; read < (s32) par.size; read++) { |
3235 | 0 | tmp_buf[len + read] = 0; |
3236 | 0 | } |
3237 | 0 | } |
3238 | 0 | gf_fclose(profile); |
3239 | 0 | } else { |
3240 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] Error while loading Profile file %s\n", sess->log_name, user_profile)); |
3241 | 0 | } |
3242 | 0 | } |
3243 | |
|
3244 | 0 | sess->last_fetch_time = sess->request_start_time = gf_sys_clock_high_res(); |
3245 | 0 | sess->req_hdr_size = len+par.size; |
3246 | |
|
3247 | 0 | e = gf_dm_sess_write(sess, tmp_buf, len+par.size); |
3248 | |
|
3249 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Sending request to %s %s\n\n", sess->log_name, sess->server_name, tmp_buf)); |
3250 | 0 | gf_free(tmp_buf); |
3251 | 0 | } else { |
3252 | 0 | u32 len = (u32) strlen(sHTTP); |
3253 | |
|
3254 | 0 | sess->last_fetch_time = sess->request_start_time = gf_sys_clock_high_res(); |
3255 | 0 | sess->req_hdr_size = len; |
3256 | |
|
3257 | 0 | e = gf_dm_sess_write(sess, sHTTP, len); |
3258 | |
|
3259 | 0 | #ifndef GPAC_DISABLE_LOG |
3260 | 0 | if (e) { |
3261 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] Error sending request %s\n", sess->log_name, gf_error_to_string(e) )); |
3262 | 0 | } else { |
3263 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Sending request to %s %s\n\n", sess->log_name, sess->server_name, sHTTP)); |
3264 | 0 | } |
3265 | 0 | #endif |
3266 | 0 | } |
3267 | | |
3268 | |
|
3269 | | #ifdef GPAC_HTTPMUX |
3270 | | req_sent: |
3271 | | #endif |
3272 | 0 | gf_dm_sess_clear_headers(sess); |
3273 | |
|
3274 | 0 | if (e) { |
3275 | 0 | SET_LAST_ERR(e) |
3276 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
3277 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_STATE_ERROR, e); |
3278 | 0 | return e; |
3279 | 0 | } |
3280 | | |
3281 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_WAIT_FOR_REPLY, GF_OK); |
3282 | 0 | if (sess->put_state==1) { |
3283 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
3284 | 0 | } else { |
3285 | 0 | sess->status = GF_NETIO_WAIT_FOR_REPLY; |
3286 | 0 | } |
3287 | 0 | SET_LAST_ERR(GF_OK) |
3288 | |
|
3289 | | #ifdef GPAC_HAS_CURL |
3290 | | if (sess->curl_hnd) { |
3291 | | sess->last_fetch_time = sess->request_start_time = gf_sys_clock_high_res(); |
3292 | | curl_flush(sess); |
3293 | | } |
3294 | | #endif |
3295 | 0 | return GF_OK; |
3296 | 0 | } |
3297 | | |
3298 | | |
3299 | | /*! |
3300 | | * Parse the remaining part of body |
3301 | | \param sess The session |
3302 | | \return The error code if any |
3303 | | */ |
3304 | | static GF_Err http_parse_remaining_body(GF_DownloadSession * sess) |
3305 | 0 | { |
3306 | 0 | GF_Err e; |
3307 | |
|
3308 | 0 | while (1) { |
3309 | 0 | u32 prev_remaining_data_size, size=0, rewrite_size=0; |
3310 | 0 | if (sess->status>=GF_NETIO_DISCONNECTED) |
3311 | 0 | return GF_REMOTE_SERVICE_ERROR; |
3312 | | |
3313 | 0 | if (sess->dm && sess->bytes_per_sec && (sess->max_data_rate || sess->dm->limit_data_rate)) { |
3314 | 0 | sess->rate_regulated = GF_FALSE; |
3315 | 0 | if (dm_exceeds_cap_rate(sess->dm, sess->max_data_rate ? sess : NULL)) { |
3316 | 0 | sess->rate_regulated = GF_TRUE; |
3317 | 0 | return GF_OK; |
3318 | 0 | } |
3319 | 0 | } |
3320 | | |
3321 | | //the data remaining from the last buffer (i.e size for chunk that couldn't be read because the buffer does not contain enough bytes) |
3322 | 0 | if (sess->remaining_data && sess->remaining_data_size) { |
3323 | 0 | if (sess->remaining_data_size >= sess->http_buf_size) { |
3324 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] No HTTP chunk header found for %d bytes, assuming broken chunk transfer and aborting\n", sess->log_name, sess->remaining_data_size)); |
3325 | 0 | return GF_NON_COMPLIANT_BITSTREAM; |
3326 | 0 | } |
3327 | 0 | memcpy(sess->http_buf, sess->remaining_data, sess->remaining_data_size); |
3328 | 0 | } |
3329 | 0 | e = gf_dm_sess_read(sess, sess->http_buf + sess->remaining_data_size, sess->http_buf_size - sess->remaining_data_size, &size); |
3330 | | #ifdef GPAC_HTTPMUX |
3331 | | //flush even if this read got nothing: another session's read may have received (and closed) our stream |
3332 | | if (sess->hmux_sess) { |
3333 | | hmux_flush_internal_data(sess, GF_FALSE); |
3334 | | if (sess->hmux_stream_id<0) { |
3335 | | sess->status = GF_NETIO_DATA_TRANSFERED; |
3336 | | SET_LAST_ERR(GF_OK) |
3337 | | } |
3338 | | } |
3339 | | #endif |
3340 | 0 | if ((e != GF_IP_CONNECTION_CLOSED) && (!size || e == GF_IP_NETWORK_EMPTY)) { |
3341 | 0 | if (!sess->total_size && !sess->chunked && (gf_sys_clock_high_res() - sess->start_time > 5000000)) { |
3342 | 0 | sess->total_size = sess->bytes_done; |
3343 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_DATA_TRANSFERED, GF_OK); |
3344 | 0 | gf_assert(sess->server_name); |
3345 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] Disconnected from %s: %s\n", sess->log_name, sess->server_name, gf_error_to_string(e))); |
3346 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
3347 | 0 | } |
3348 | 0 | return GF_OK; |
3349 | 0 | } |
3350 | | |
3351 | 0 | if (e) { |
3352 | 0 | if (sess->sock && (e == GF_IP_CONNECTION_CLOSED)) { |
3353 | 0 | u32 len = gf_cache_get_content_length(sess->cache_entry); |
3354 | 0 | if (size > 0) { |
3355 | | #ifdef GPAC_HTTPMUX |
3356 | | if (!sess->hmux_sess) |
3357 | | #endif |
3358 | 0 | gf_dm_data_received(sess, (u8 *) sess->http_buf, size, GF_FALSE, NULL, NULL); |
3359 | 0 | } |
3360 | |
|
3361 | 0 | if ( ( (len == 0) && sess->use_cache_file) || sess->bytes_done) { |
3362 | 0 | sess->total_size = sess->bytes_done; |
3363 | | // HTTP 1.1 without content length... |
3364 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_DATA_TRANSFERED, GF_OK); |
3365 | 0 | gf_assert(sess->server_name); |
3366 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] Disconnected from %s: %s\n", sess->log_name, sess->server_name, gf_error_to_string(e))); |
3367 | 0 | if (sess->use_cache_file) |
3368 | 0 | gf_cache_set_content_length(sess->cache_entry, sess->bytes_done); |
3369 | 0 | e = GF_OK; |
3370 | 0 | } |
3371 | 0 | } |
3372 | 0 | gf_dm_disconnect(sess, HTTP_CLOSE); |
3373 | 0 | SET_LAST_ERR(e) |
3374 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
3375 | 0 | return e; |
3376 | 0 | } |
3377 | | |
3378 | 0 | prev_remaining_data_size = sess->remaining_data_size; |
3379 | 0 | sess->remaining_data_size = 0; |
3380 | |
|
3381 | 0 | sess->http_buf[size + prev_remaining_data_size] = 0; |
3382 | |
|
3383 | | #ifdef GPAC_HTTPMUX |
3384 | | if (!sess->hmux_sess) |
3385 | | #endif |
3386 | 0 | gf_dm_data_received(sess, (u8 *) sess->http_buf, size + prev_remaining_data_size, GF_FALSE, &rewrite_size, NULL); |
3387 | |
|
3388 | 0 | if (sess->chunked) |
3389 | 0 | gf_dm_sess_estimate_chunk_rate(sess, rewrite_size); |
3390 | | |
3391 | | |
3392 | | /*socket empty*/ |
3393 | 0 | if (size < sess->http_buf_size) { |
3394 | 0 | return GF_OK; |
3395 | 0 | } |
3396 | 0 | } |
3397 | 0 | return GF_OK; |
3398 | 0 | } |
3399 | | |
3400 | | static void notify_error_body(GF_DownloadSession *sess, char * sHTTP, s32 bytesRead, s32 BodyStart) |
3401 | 0 | { |
3402 | 0 | GF_NETIO_Parameter par; |
3403 | |
|
3404 | 0 | if (sHTTP) { |
3405 | 0 | sHTTP[bytesRead]=0; |
3406 | 0 | par.error = GF_BAD_PARAM; |
3407 | 0 | par.reply = sess->rsp_code; |
3408 | 0 | par.data = sHTTP + BodyStart; |
3409 | 0 | par.size = (u32) strlen(par.data); |
3410 | 0 | par.msg_type = GF_NETIO_DATA_EXCHANGE; |
3411 | 0 | gf_dm_sess_user_io(sess, &par); |
3412 | 0 | } |
3413 | 0 | } |
3414 | | |
3415 | | static u32 http_parse_method(const char *comp) |
3416 | 0 | { |
3417 | 0 | if (!strcmp(comp, "GET")) return GF_HTTP_GET; |
3418 | 0 | else if (!strcmp(comp, "HEAD")) return GF_HTTP_HEAD; |
3419 | 0 | else if (!strcmp(comp, "OPTIONS")) return GF_HTTP_OPTIONS; |
3420 | 0 | else if (!strcmp(comp, "PUT")) return GF_HTTP_PUT; |
3421 | 0 | else if (!strcmp(comp, "POST")) return GF_HTTP_POST; |
3422 | 0 | else if (!strcmp(comp, "DELETE")) return GF_HTTP_DELETE; |
3423 | 0 | else if (!strcmp(comp, "CONNECT")) return GF_HTTP_CONNECT; |
3424 | 0 | else if (!strcmp(comp, "TRACE")) return GF_HTTP_TRACE; |
3425 | 0 | else return 0; |
3426 | 0 | } |
3427 | | |
3428 | | /*! |
3429 | | * Waits for the response HEADERS, parse the information... and so on |
3430 | | \param sess The session |
3431 | | */ |
3432 | | static GF_Err wait_for_header_and_parse(GF_DownloadSession *sess) |
3433 | 0 | { |
3434 | 0 | GF_NETIO_Parameter par; |
3435 | 0 | s32 bytesRead=0, BodyStart=0; |
3436 | 0 | u32 res, i, buf_size = sess->http_buf_size; |
3437 | 0 | s32 LinePos, Pos; |
3438 | 0 | u32 method=0; |
3439 | 0 | u32 ContentLength=0, first_byte, last_byte, total_size, range, no_range; |
3440 | 0 | Bool connection_closed = GF_FALSE; |
3441 | 0 | Bool has_content_length = GF_FALSE; |
3442 | 0 | Bool connection_keep_alive = GF_FALSE; |
3443 | 0 | u32 connection_timeout=0; |
3444 | 0 | char buf[1025]; |
3445 | 0 | char comp[400]; |
3446 | 0 | GF_Err e; |
3447 | 0 | char * new_location; |
3448 | 0 | const char * mime_type; |
3449 | | #ifdef GPAC_HAS_HTTP2 |
3450 | | Bool upgrade_to_http2 = GF_FALSE; |
3451 | | #endif |
3452 | |
|
3453 | 0 | char *sHTTP = sess->http_buf; |
3454 | 0 | sHTTP[0] = 0; |
3455 | |
|
3456 | 0 | if (sess->creds && sess->creds->req_state) { |
3457 | 0 | if (sess->creds->req_state==GF_CREDS_STATE_PENDING) |
3458 | 0 | return GF_OK; |
3459 | 0 | sess->creds->req_state = GF_CREDS_STATE_NONE; |
3460 | 0 | if (!sess->creds->valid) { |
3461 | 0 | gf_dm_disconnect(sess, HTTP_CLOSE); |
3462 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
3463 | 0 | par.error = GF_AUTHENTICATION_FAILURE; |
3464 | 0 | par.msg_type = GF_NETIO_DISCONNECTED; |
3465 | 0 | gf_dm_sess_user_io(sess, &par); |
3466 | 0 | e = GF_AUTHENTICATION_FAILURE; |
3467 | 0 | SET_LAST_ERR(e) |
3468 | 0 | goto exit; |
3469 | 0 | } |
3470 | | |
3471 | 0 | sess->status = GF_NETIO_SETUP; |
3472 | 0 | e = gf_dm_sess_setup_from_url(sess, sess->orig_url, GF_FALSE); |
3473 | 0 | if (e) { |
3474 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
3475 | 0 | SET_LAST_ERR(e) |
3476 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
3477 | 0 | } |
3478 | 0 | return e; |
3479 | 0 | } |
3480 | | |
3481 | 0 | if (sess->server_mode) { |
3482 | 0 | gf_fatal_assert( sess->status == GF_NETIO_CONNECTED ); |
3483 | 0 | } else { |
3484 | 0 | gf_fatal_assert( sess->status == GF_NETIO_WAIT_FOR_REPLY ); |
3485 | 0 | if (!(sess->flags & GF_NETIO_SESSION_NOT_CACHED)) { |
3486 | 0 | sess->use_cache_file = sess->dm->disable_cache ? GF_FALSE : GF_TRUE; |
3487 | 0 | } |
3488 | 0 | } |
3489 | 0 | bytesRead = res = 0; |
3490 | 0 | new_location = NULL; |
3491 | |
|
3492 | 0 | if (sess->cached_file) { |
3493 | 0 | sess->reply_time = (u32) (gf_sys_clock_high_res() - sess->request_start_time); |
3494 | 0 | sess->rsp_hdr_size = 0; |
3495 | 0 | sess->total_size = gf_cache_get_content_length(sess->cache_entry); |
3496 | 0 | sess->bytes_done = 0; |
3497 | |
|
3498 | 0 | memset(&par, 0, sizeof(GF_NETIO_Parameter)); |
3499 | 0 | par.msg_type = GF_NETIO_DATA_EXCHANGE; |
3500 | 0 | par.error = GF_OK; |
3501 | 0 | gf_dm_sess_user_io(sess, &par); |
3502 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
3503 | | // gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
3504 | 0 | return GF_OK; |
3505 | 0 | } |
3506 | | |
3507 | | #ifdef GPAC_HAS_CURL |
3508 | | if (sess->curl_hnd) { |
3509 | | e = curl_process_reply(sess, &ContentLength); |
3510 | | if (e) return e; |
3511 | | goto process_reply; |
3512 | | } |
3513 | | #endif |
3514 | | |
3515 | | //always set start time to the time at last attempt reply parsing |
3516 | 0 | sess->start_time = gf_sys_clock_high_res(); |
3517 | 0 | sess->start_time_utc = gf_net_get_utc(); |
3518 | 0 | sess->chunked = GF_FALSE; |
3519 | 0 | sess->last_chunk_found = GF_FALSE; |
3520 | |
|
3521 | 0 | sess->last_chunk_start_time = sess->request_start_time; |
3522 | 0 | sess->chunk_bytes = 0; |
3523 | 0 | sess->cumulated_chunk_rate = 0; |
3524 | |
|
3525 | 0 | while (1) { |
3526 | 0 | Bool probe = (!bytesRead || (sess->flags & GF_NETIO_SESSION_NO_BLOCK) ) ? GF_TRUE : GF_FALSE; |
3527 | | #ifdef GPAC_HAS_QUIC |
3528 | | if (sess->server_mode && sess->hmux_sess && (sess->hmux_sess->net_sess->flags & GF_NETIO_SESSION_USE_QUIC)) { |
3529 | | GF_Err h3_check_sess(GF_DownloadSession *sess); |
3530 | | probe = GF_FALSE; |
3531 | | e = h3_check_sess(sess); |
3532 | | } else |
3533 | | #endif |
3534 | 0 | e = gf_dm_sess_read(sess, sHTTP + bytesRead, buf_size - bytesRead, &res); |
3535 | |
|
3536 | | #ifdef GPAC_HTTPMUX |
3537 | | /* break as soon as we have a header frame*/ |
3538 | | if (sess->hmux_headers_seen) { |
3539 | | sess->hmux_headers_seen = 0; |
3540 | | res = 0; |
3541 | | bytesRead = 0; |
3542 | | BodyStart = 0; |
3543 | | e = GF_OK; |
3544 | | SET_LAST_ERR(GF_OK) |
3545 | | break; |
3546 | | } |
3547 | | #endif |
3548 | | //should not happen, but do it for safety |
3549 | 0 | if (!res && !bytesRead && !e) e = GF_IP_NETWORK_EMPTY; |
3550 | |
|
3551 | 0 | switch (e) { |
3552 | 0 | case GF_IP_NETWORK_EMPTY: |
3553 | 0 | if (probe) { |
3554 | 0 | e = gf_sk_probe(sess->sock); |
3555 | |
|
3556 | 0 | if (e==GF_IP_CONNECTION_CLOSED) { |
3557 | 0 | SET_LAST_ERR(GF_IP_CONNECTION_CLOSED) |
3558 | 0 | sess_connection_closed(sess); |
3559 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
3560 | 0 | return GF_IP_NETWORK_EMPTY; |
3561 | 0 | } |
3562 | 0 | if (!sess->server_mode |
3563 | 0 | && sess->request_timeout |
3564 | 0 | && (gf_sys_clock_high_res() - sess->request_start_time > 1000 * sess->request_timeout) |
3565 | 0 | ) { |
3566 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, |
3567 | 0 | ("[%s] Session timeout for %s after %u ms, aborting\n", sess->log_name, sess->orig_url, sess->request_timeout)); |
3568 | 0 | SET_LAST_ERR(GF_IP_NETWORK_FAILURE) |
3569 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
3570 | 0 | return GF_IP_NETWORK_FAILURE; |
3571 | 0 | } |
3572 | 0 | if (sess->status != GF_NETIO_STATE_ERROR) { |
3573 | 0 | if (sess->server_mode |
3574 | 0 | || (sess->flags & GF_NETIO_SESSION_NO_BLOCK) |
3575 | 0 | ) { |
3576 | 0 | SET_LAST_ERR(GF_IP_NETWORK_EMPTY) |
3577 | 0 | } |
3578 | 0 | } |
3579 | 0 | return GF_OK; |
3580 | 0 | } |
3581 | 0 | if (sess->status==GF_NETIO_STATE_ERROR) |
3582 | 0 | return sess->last_error; |
3583 | 0 | if (!res && sess->status<=GF_NETIO_CONNECTED) |
3584 | 0 | return GF_OK; |
3585 | | |
3586 | | #ifdef GPAC_HTTPMUX |
3587 | | //we may have received bytes (bytesRead>0) yet none for this session, return GF_IP_NETWORK_EMPTY if empty |
3588 | | if (sess->hmux_sess) |
3589 | | return GF_IP_NETWORK_EMPTY; |
3590 | | #endif |
3591 | | |
3592 | 0 | continue; |
3593 | | /*socket has been closed while configuring, retry (not sure if the server got the GET)*/ |
3594 | 0 | case GF_IP_CONNECTION_CLOSED: |
3595 | 0 | if (sess->http_read_type == HEAD) { |
3596 | | /* Some servers such as shoutcast directly close connection if HEAD or an unknown method is issued */ |
3597 | 0 | sess->server_only_understand_get = GF_TRUE; |
3598 | 0 | } |
3599 | 0 | if (sess->server_mode) { |
3600 | 0 | SET_LAST_ERR(GF_IP_CONNECTION_CLOSED) |
3601 | 0 | sess_connection_closed(sess); |
3602 | 0 | sess->status = GF_NETIO_DISCONNECTED; |
3603 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Connection closed by client\n", sess->log_name, sess->remote_path)); |
3604 | 0 | return GF_IP_CONNECTION_CLOSED; |
3605 | 0 | } |
3606 | 0 | gf_dm_disconnect(sess, HTTP_RESET_CONN); |
3607 | |
|
3608 | 0 | if (sess->num_retry) { |
3609 | 0 | #ifdef GPAC_HAS_SSL |
3610 | 0 | if ((sess->num_retry < SESSION_RETRY_SSL) |
3611 | 0 | && !(sess->flags & GF_DOWNLOAD_SESSION_USE_SSL) |
3612 | 0 | && !gf_opts_get_bool("core", "no-tls-rcfg") |
3613 | 0 | ) { |
3614 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Connection closed by server when processing %s - retrying using SSL\n", sess->log_name, sess->remote_path)); |
3615 | 0 | sess->flags |= GF_DOWNLOAD_SESSION_SSL_FORCED; |
3616 | 0 | } else |
3617 | 0 | #endif |
3618 | 0 | { |
3619 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Connection closed by server when processing %s - retrying\n", sess->log_name, sess->remote_path)); |
3620 | 0 | } |
3621 | 0 | sess->status = GF_NETIO_SETUP; |
3622 | 0 | } else { |
3623 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] Connection closed by server when processing %s - aborting\n", sess->log_name, sess->remote_path)); |
3624 | 0 | SET_LAST_ERR(e) |
3625 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
3626 | 0 | } |
3627 | 0 | return e; |
3628 | 0 | case GF_OK: |
3629 | 0 | SET_LAST_ERR(GF_OK) |
3630 | 0 | if (!res) |
3631 | 0 | return GF_OK; |
3632 | 0 | break; |
3633 | 0 | default: |
3634 | 0 | goto exit; |
3635 | 0 | } |
3636 | 0 | bytesRead += res; |
3637 | |
|
3638 | | #ifdef GPAC_HTTPMUX |
3639 | | //in case we got a refused stream |
3640 | | if (sess->status==GF_NETIO_SETUP) { |
3641 | | return GF_OK; |
3642 | | } |
3643 | | if (sess->hmux_sess) |
3644 | | continue; |
3645 | | #endif |
3646 | | |
3647 | | //HTTP1.1 only |
3648 | |
|
3649 | 0 | char *hdr_buf = sHTTP; |
3650 | 0 | u32 hdr_buf_len = bytesRead; |
3651 | 0 | if (sess->flags & GF_NETIO_SESSION_NO_BLOCK) { |
3652 | 0 | sess->async_req_reply = gf_realloc(sess->async_req_reply, sess->async_req_reply_size+res+1); |
3653 | 0 | if (!sess->async_req_reply) { |
3654 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
3655 | 0 | SET_LAST_ERR(GF_OUT_OF_MEM) |
3656 | 0 | return sess->last_error; |
3657 | 0 | } |
3658 | 0 | memcpy(sess->async_req_reply + sess->async_req_reply_size, sHTTP, res); |
3659 | 0 | sess->async_req_reply_size += res; |
3660 | 0 | bytesRead = 0; |
3661 | 0 | sHTTP[0] = 0; |
3662 | 0 | hdr_buf = sess->async_req_reply; |
3663 | 0 | hdr_buf_len = sess->async_req_reply_size; |
3664 | 0 | } |
3665 | | |
3666 | | //weird bug on some servers sending twice the last chunk |
3667 | 0 | if (!strncmp(hdr_buf, "0\r\n\r\n", 5) ) { |
3668 | 0 | if (bytesRead) { |
3669 | 0 | bytesRead -= res; |
3670 | 0 | } else { |
3671 | 0 | sess->async_req_reply_size -= res; |
3672 | 0 | } |
3673 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] End of chunk found while waiting server response when processing %s - retrying\n", sess->log_name, sess->remote_path)); |
3674 | 0 | continue; |
3675 | 0 | } |
3676 | | |
3677 | | /*locate body start*/ |
3678 | 0 | BodyStart = gf_token_find(hdr_buf, 0, hdr_buf_len, "\r\n\r\n"); |
3679 | 0 | if (BodyStart > 0) { |
3680 | 0 | BodyStart += 4; |
3681 | 0 | break; |
3682 | 0 | } |
3683 | 0 | BodyStart = gf_token_find(hdr_buf, 0, hdr_buf_len, "\n\n"); |
3684 | 0 | if (BodyStart > 0) { |
3685 | 0 | BodyStart += 2; |
3686 | 0 | break; |
3687 | 0 | } |
3688 | 0 | } |
3689 | | |
3690 | 0 | no_range = range = ContentLength = first_byte = last_byte = total_size = sess->rsp_code = 0; |
3691 | |
|
3692 | 0 | if (sess->flags & GF_NETIO_SESSION_NO_BLOCK) { |
3693 | 0 | sHTTP = sess->async_req_reply; |
3694 | 0 | buf_size = bytesRead = sess->async_req_reply_size; |
3695 | 0 | sess->async_req_reply_size = 0; |
3696 | 0 | } |
3697 | |
|
3698 | | #ifdef GPAC_HTTPMUX |
3699 | | if (!sess->hmux_sess) { |
3700 | | #endif |
3701 | 0 | if (bytesRead < 0) { |
3702 | 0 | e = GF_REMOTE_SERVICE_ERROR; |
3703 | 0 | goto exit; |
3704 | 0 | } |
3705 | | |
3706 | 0 | if (!BodyStart) |
3707 | 0 | BodyStart = bytesRead; |
3708 | |
|
3709 | 0 | if (BodyStart) sHTTP[BodyStart-1] = 0; |
3710 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] %s\n\n", sess->log_name, sHTTP)); |
3711 | |
|
3712 | 0 | sess->reply_time = (u32) (gf_sys_clock_high_res() - sess->request_start_time); |
3713 | 0 | sess->rsp_hdr_size = BodyStart; |
3714 | |
|
3715 | 0 | LinePos = gf_token_get_line(sHTTP, 0, bytesRead, buf, 1024); |
3716 | 0 | Pos = gf_token_get(buf, 0, " \t\r\n", comp, 400); |
3717 | | |
3718 | | //TODO for HTTP2 |
3719 | 0 | if (sess->server_mode) { |
3720 | 0 | method = http_parse_method(comp); |
3721 | |
|
3722 | 0 | Pos = gf_token_get(buf, Pos, " \t\r\n", comp, 400); |
3723 | 0 | if (sess->orig_url) gf_free(sess->orig_url); |
3724 | 0 | sess->orig_url = gf_strdup(comp); |
3725 | 0 | /*Pos = */gf_token_get(buf, Pos, " \t\r\n", comp, 400); |
3726 | 0 | if ((strncmp("HTTP", comp, 4) != 0)) { |
3727 | 0 | e = GF_REMOTE_SERVICE_ERROR; |
3728 | 0 | goto exit; |
3729 | 0 | } |
3730 | | //flush potential body except for PUT/POST |
3731 | 0 | if ((method==GF_HTTP_PUT) || (method==GF_HTTP_POST)) |
3732 | 0 | sess->rsp_code = 200; |
3733 | 0 | else |
3734 | 0 | sess->rsp_code = 300; |
3735 | 0 | } else { |
3736 | |
|
3737 | 0 | if (!strncmp("ICY", comp, 3)) { |
3738 | 0 | sess->use_cache_file = GF_FALSE; |
3739 | | /*be prepared not to receive any mime type from ShoutCast servers*/ |
3740 | 0 | if (!gf_cache_get_mime_type(sess->cache_entry)) |
3741 | 0 | gf_cache_set_mime_type(sess->cache_entry, "audio/mpeg"); |
3742 | 0 | } else if ((strncmp("HTTP", comp, 4) != 0)) { |
3743 | 0 | e = GF_REMOTE_SERVICE_ERROR; |
3744 | 0 | goto exit; |
3745 | 0 | } |
3746 | 0 | Pos = gf_token_get(buf, Pos, " ", comp, 400); |
3747 | 0 | if (Pos <= 0) { |
3748 | 0 | e = GF_REMOTE_SERVICE_ERROR; |
3749 | 0 | goto exit; |
3750 | 0 | } |
3751 | 0 | sess->rsp_code = (u32) atoi(comp); |
3752 | 0 | /*Pos = */gf_token_get(buf, Pos, " \r\n", comp, 400); |
3753 | |
|
3754 | 0 | } |
3755 | | |
3756 | | /* parse headers*/ |
3757 | 0 | while (1) { |
3758 | 0 | GF_HTTPHeader *hdrp; |
3759 | 0 | char *sep, *hdr_sep, *hdr, *hdr_val; |
3760 | 0 | if ( (s32) LinePos + 4 > BodyStart) break; |
3761 | 0 | LinePos = gf_token_get_line(sHTTP, LinePos , bytesRead, buf, 1024); |
3762 | 0 | if (LinePos < 0) break; |
3763 | | |
3764 | 0 | hdr_sep = NULL; |
3765 | 0 | hdr_val = NULL; |
3766 | 0 | hdr = buf; |
3767 | 0 | sep = strchr(buf, ':'); |
3768 | 0 | if (sep) { |
3769 | 0 | sep[0]=0; |
3770 | 0 | hdr_val = sep+1; |
3771 | 0 | while (hdr_val[0]==' ') hdr_val++; |
3772 | 0 | hdr_sep = strrchr(hdr_val, '\r'); |
3773 | 0 | if (hdr_sep) hdr_sep[0] = 0; |
3774 | 0 | } |
3775 | |
|
3776 | 0 | GF_SAFEALLOC(hdrp, GF_HTTPHeader); |
3777 | 0 | if (hdrp) { |
3778 | 0 | hdrp->name = gf_strdup(hdr); |
3779 | 0 | if (hdr_val) |
3780 | 0 | hdrp->value = gf_strdup(hdr_val); |
3781 | 0 | gf_list_add(sess->headers, hdrp); |
3782 | 0 | } |
3783 | |
|
3784 | 0 | if (sep) sep[0]=':'; |
3785 | 0 | if (hdr_sep) hdr_sep[0] = '\r'; |
3786 | |
|
3787 | 0 | if (sess->server_mode) { |
3788 | 0 | if (!stricmp(hdrp->name, "Transfer-Encoding") && !stricmp(hdrp->value, "chunked")) |
3789 | 0 | sess->chunked = GF_TRUE; |
3790 | 0 | } |
3791 | 0 | } |
3792 | |
|
3793 | | #ifdef GPAC_HTTPMUX |
3794 | | } |
3795 | | #endif |
3796 | | |
3797 | |
|
3798 | | #ifdef GPAC_HAS_CURL |
3799 | | process_reply: |
3800 | | #endif |
3801 | |
|
3802 | 0 | if (!sess->server_mode) { |
3803 | 0 | Bool cache_no_store = GF_FALSE; |
3804 | 0 | Bool cache_must_revalidate = GF_FALSE; |
3805 | 0 | u32 delta_age = 0; |
3806 | 0 | u32 max_age = 0; |
3807 | | |
3808 | | //default pre-processing of headers - needs cleanup, not all of these have to be parsed before checking reply code |
3809 | 0 | for (i=0; i<gf_list_count(sess->headers); i++) { |
3810 | 0 | char *val; |
3811 | 0 | GF_HTTPHeader *hdr = (GF_HTTPHeader*)gf_list_get(sess->headers, i); |
3812 | |
|
3813 | | #ifdef GPAC_HTTPMUX |
3814 | | if (!stricmp(hdr->name, ":status") ) { |
3815 | | sess->rsp_code = (u32) atoi(hdr->value); |
3816 | | } else |
3817 | | #endif |
3818 | 0 | if (!stricmp(hdr->name, "Content-Length") ) { |
3819 | 0 | ContentLength = (u32) atoi(hdr->value); |
3820 | 0 | has_content_length=GF_TRUE; |
3821 | |
|
3822 | 0 | if ((sess->rsp_code<300) && sess->cache_entry) |
3823 | 0 | gf_cache_set_content_length(sess->cache_entry, ContentLength); |
3824 | |
|
3825 | 0 | } |
3826 | 0 | else if (!stricmp(hdr->name, "Content-Type")) { |
3827 | 0 | char *mime = gf_strdup(hdr->value); |
3828 | 0 | while (1) { |
3829 | 0 | u32 len = (u32) strlen(mime); |
3830 | 0 | char c = len ? mime[len-1] : 0; |
3831 | 0 | if ((c=='\r') || (c=='\n')) { |
3832 | 0 | mime[len-1] = 0; |
3833 | 0 | } else { |
3834 | 0 | break; |
3835 | 0 | } |
3836 | 0 | } |
3837 | 0 | val = strchr(mime, ';'); |
3838 | 0 | if (val) val[0] = 0; |
3839 | |
|
3840 | 0 | strlwr(mime); |
3841 | 0 | if (sess->rsp_code<300) { |
3842 | 0 | if (sess->cache_entry) { |
3843 | 0 | gf_cache_set_mime_type(sess->cache_entry, mime); |
3844 | 0 | } else { |
3845 | 0 | sess->mime_type = mime; |
3846 | 0 | mime = NULL; |
3847 | 0 | } |
3848 | 0 | } |
3849 | 0 | if (mime) gf_free(mime); |
3850 | 0 | } |
3851 | 0 | else if (!stricmp(hdr->name, "Content-Range")) { |
3852 | 0 | if (!strnicmp(hdr->value, "bytes", 5)) { |
3853 | 0 | val = hdr->value + 5; |
3854 | 0 | while (val && val[0] && strchr(":= ", val[0])) |
3855 | 0 | val++; |
3856 | |
|
3857 | 0 | if (val[0] == '*') { |
3858 | 0 | sscanf(val, "*/%u", &total_size); |
3859 | 0 | sess->full_resource_size = total_size; |
3860 | 0 | sess->rsp_code = 416; |
3861 | 0 | } else if (strstr(val, "/*")) { |
3862 | 0 | sscanf(val, "%u-%u/*", &first_byte, &last_byte); |
3863 | 0 | sess->full_resource_size = 0; |
3864 | 0 | } else { |
3865 | 0 | sscanf(val, "%u-%u/%u", &first_byte, &last_byte, &total_size); |
3866 | 0 | sess->full_resource_size = total_size; |
3867 | 0 | } |
3868 | 0 | } |
3869 | 0 | } |
3870 | 0 | else if (!stricmp(hdr->name, "Accept-Ranges")) { |
3871 | 0 | if (strstr(hdr->value, "none")) no_range = 1; |
3872 | 0 | } |
3873 | 0 | else if (!stricmp(hdr->name, "Location")) |
3874 | 0 | new_location = gf_strdup(hdr->value); |
3875 | 0 | else if (!strnicmp(hdr->name, "ice", 3) || !strnicmp(hdr->name, "icy", 3) ) { |
3876 | | /* For HTTP icy servers, we disable cache */ |
3877 | 0 | if (sess->icy_metaint == 0) |
3878 | 0 | sess->icy_metaint = -1; |
3879 | 0 | sess->use_cache_file = GF_FALSE; |
3880 | 0 | if (!stricmp(hdr->name, "icy-metaint")) { |
3881 | 0 | sess->icy_metaint = atoi(hdr->value); |
3882 | 0 | } |
3883 | 0 | } |
3884 | 0 | else if (!stricmp(hdr->name, "Age")) { |
3885 | 0 | sscanf(hdr->value, "%u", &delta_age); |
3886 | 0 | } |
3887 | 0 | else if (!stricmp(hdr->name, "Cache-Control")) { |
3888 | 0 | char *hval = hdr->value; |
3889 | 0 | while (hval[0]) { |
3890 | 0 | char *hsep = strchr(hval, ','); |
3891 | 0 | if (hsep) hsep[0] = 0; |
3892 | 0 | while (hval[0]==' ') hval++; |
3893 | 0 | char *vsep = strchr(hval, '='); |
3894 | 0 | if (vsep) vsep[0] = 0; |
3895 | 0 | if (!strcmp(hval, "no-store")) cache_no_store = GF_TRUE; |
3896 | 0 | else if (!strcmp(hval, "no-cache")) cache_no_store = GF_TRUE; |
3897 | 0 | else if (!strcmp(hval, "private")) { |
3898 | | //we need a way to differentiate proxy modes and client modes |
3899 | 0 | } |
3900 | 0 | else if (!strcmp(hval, "public")) {} |
3901 | 0 | else if (!strcmp(hval, "max-age") && vsep && !max_age) { |
3902 | 0 | sscanf(vsep+1, "%u", &max_age); |
3903 | 0 | } |
3904 | 0 | else if (!strcmp(hval, "s-maxage") && vsep) { |
3905 | 0 | sscanf(vsep+1, "%u", &max_age); |
3906 | 0 | } |
3907 | 0 | else if (!strcmp(hval, "must-revalidate")) cache_must_revalidate = GF_TRUE; |
3908 | 0 | else if (!strcmp(hval, "proxy-revalidate")) cache_must_revalidate = GF_TRUE; |
3909 | |
|
3910 | 0 | if (vsep) vsep[0] = '='; |
3911 | 0 | if (!hsep) break; |
3912 | 0 | hsep[0] = ','; |
3913 | 0 | hval = hsep+1; |
3914 | 0 | } |
3915 | 0 | } |
3916 | 0 | else if (!stricmp(hdr->name, "ETag")) { |
3917 | 0 | if (sess->rsp_code<300) |
3918 | 0 | gf_cache_set_etag_on_server(sess->cache_entry, hdr->value); |
3919 | 0 | } |
3920 | 0 | else if (!stricmp(hdr->name, "Last-Modified")) { |
3921 | 0 | if (sess->rsp_code<300) |
3922 | 0 | gf_cache_set_last_modified_on_server(sess->cache_entry, hdr->value); |
3923 | 0 | } |
3924 | 0 | else if (!stricmp(hdr->name, "Transfer-Encoding")) { |
3925 | 0 | if (!stricmp(hdr->value, "chunked") |
3926 | | #ifdef GPAC_HAS_CURL |
3927 | | && !sess->curl_hnd |
3928 | | #endif |
3929 | 0 | ) |
3930 | 0 | sess->chunked = GF_TRUE; |
3931 | 0 | } |
3932 | 0 | else if (!stricmp(hdr->name, "X-UserProfileID") ) { |
3933 | 0 | gf_opts_set_key("core", "user-profileid", hdr->value); |
3934 | 0 | } |
3935 | 0 | else if (!stricmp(hdr->name, "Connection") ) { |
3936 | 0 | if (strstr(hdr->value, "close")) |
3937 | 0 | connection_closed = GF_TRUE; |
3938 | 0 | else if (strstr(hdr->value, "Keep-Alive")) |
3939 | 0 | connection_keep_alive = GF_TRUE; |
3940 | 0 | } |
3941 | 0 | else if (!stricmp(hdr->name, "Keep-Alive") ) { |
3942 | 0 | char *tout = strstr(hdr->value, "timeout="); |
3943 | 0 | if (tout) { |
3944 | 0 | char c=0; |
3945 | 0 | s32 end = gf_token_find(tout, 0, (u32) strlen(tout), ", "); |
3946 | 0 | if (end>=0) { |
3947 | 0 | c = tout[end]; |
3948 | 0 | tout[end] = 0; |
3949 | 0 | } |
3950 | 0 | connection_timeout = atoi(tout+8); |
3951 | 0 | if (end>=0) tout[end] = c; |
3952 | 0 | } |
3953 | 0 | if (strstr(hdr->value, "close")) |
3954 | 0 | connection_closed = GF_TRUE; |
3955 | 0 | } |
3956 | | #ifdef GPAC_HAS_HTTP2 |
3957 | | else if (!stricmp(hdr->name, "Upgrade") ) { |
3958 | | if (!sess->dm->disable_http2 && !gf_opts_get_bool("core", "no-h2c") && !strncmp(hdr->value,"h2c", 3)) { |
3959 | | upgrade_to_http2 = GF_TRUE; |
3960 | | } |
3961 | | } |
3962 | | #endif |
3963 | |
|
3964 | 0 | if (sess->status==GF_NETIO_DISCONNECTED) return GF_OK; |
3965 | 0 | } |
3966 | | |
3967 | 0 | if ((sess->flags & GF_NETIO_SESSION_AUTO_CACHE) && !ContentLength && (sess->rsp_code>=200) && (sess->rsp_code<300) ) { |
3968 | 0 | sess->use_cache_file = GF_FALSE; |
3969 | 0 | if (sess->cache_entry) { |
3970 | 0 | gf_cache_entry_set_delete_files_when_deleted(sess->cache_entry); |
3971 | 0 | gf_cache_remove_session_from_cache_entry(sess->cache_entry, sess); |
3972 | 0 | sess->cache_entry = NULL; |
3973 | 0 | } |
3974 | 0 | } |
3975 | |
|
3976 | 0 | sess->flags &= ~GF_NETIO_SESSION_NO_STORE; |
3977 | 0 | if (cache_no_store) { |
3978 | 0 | gf_cache_entry_set_delete_files_when_deleted(sess->cache_entry); |
3979 | 0 | sess->flags |= GF_NETIO_SESSION_NO_STORE; |
3980 | |
|
3981 | 0 | if (sess->cache_entry && !ContentLength && !sess->chunked && (sess->rsp_code<300) |
3982 | | #ifdef GPAC_HTTPMUX |
3983 | | && !sess->hmux_sess |
3984 | | #endif |
3985 | 0 | ) { |
3986 | 0 | sess->use_cache_file = GF_FALSE; |
3987 | 0 | gf_cache_remove_session_from_cache_entry(sess->cache_entry, sess); |
3988 | 0 | sess->cache_entry = NULL; |
3989 | 0 | } |
3990 | 0 | } |
3991 | 0 | else if (sess->cache_entry) { |
3992 | 0 | if (max_age) max_age += delta_age; |
3993 | 0 | gf_cache_set_max_age(sess->cache_entry, max_age, cache_must_revalidate); |
3994 | 0 | } |
3995 | | |
3996 | |
|
3997 | 0 | if (no_range) first_byte = 0; |
3998 | | |
3999 | |
|
4000 | 0 | gf_cache_set_headers_processed(sess->cache_entry); |
4001 | |
|
4002 | 0 | if (connection_keep_alive |
4003 | 0 | && !gf_opts_get_bool("core", "no-timeout") |
4004 | 0 | && !sess->server_mode |
4005 | | #ifdef GPAC_HTTPMUX |
4006 | | && !sess->hmux_sess |
4007 | | #endif |
4008 | 0 | ) { |
4009 | 0 | sess->connection_timeout_ms = connection_timeout*1000; |
4010 | 0 | } |
4011 | 0 | } else { |
4012 | | //server mode, start timers as soon as we see the request headers |
4013 | 0 | sess->last_fetch_time = sess->request_start_time = gf_sys_clock_high_res(); |
4014 | 0 | } |
4015 | | |
4016 | | //if we issued an open-range from end of file till unknown we may get a 416. If the server is indicating |
4017 | | //resource size and it matches our range, move to 206 |
4018 | 0 | if ((sess->rsp_code==416) && (sess->range_start==sess->full_resource_size)) { |
4019 | 0 | sess->rsp_code = 206; |
4020 | 0 | ContentLength = 0; |
4021 | 0 | has_content_length = GF_TRUE; |
4022 | 0 | } |
4023 | | //if no start range, a server may reply with 200 if open end range or if end range is file size |
4024 | | //move this to 200 to avoid triggering a byte range not supported detection |
4025 | 0 | else if (sess->needs_range && (sess->rsp_code==200) && !sess->range_start && (!sess->range_end || (sess->range_end+1==ContentLength))) { |
4026 | 0 | sess->rsp_code = 206; |
4027 | 0 | } |
4028 | |
|
4029 | 0 | par.msg_type = GF_NETIO_PARSE_REPLY; |
4030 | 0 | par.error = GF_OK; |
4031 | 0 | par.reply = sess->rsp_code; |
4032 | 0 | par.value = comp; |
4033 | | /* |
4034 | | * If response is correct, it means our credentials are correct |
4035 | | */ |
4036 | 0 | if (sess->creds && sess->rsp_code != 304) |
4037 | 0 | sess->creds->valid = GF_TRUE; |
4038 | |
|
4039 | | #ifdef GPAC_HAS_HTTP2 |
4040 | | if ((sess->rsp_code == 101) && upgrade_to_http2) { |
4041 | | char *body = NULL; |
4042 | | u32 body_len = 0; |
4043 | | if (bytesRead > BodyStart) { |
4044 | | body = sHTTP + BodyStart; |
4045 | | body_len = bytesRead - BodyStart; |
4046 | | } |
4047 | | return http2_do_upgrade(sess, body, body_len); |
4048 | | } |
4049 | | if (sess->h2_upgrade_state<4) |
4050 | | sess->h2_upgrade_state = 2; |
4051 | | #endif |
4052 | | |
4053 | | |
4054 | | /*try to flush body */ |
4055 | 0 | if ((sess->rsp_code>=300) |
4056 | | #ifdef GPAC_HTTPMUX |
4057 | | && !sess->hmux_sess |
4058 | | #endif |
4059 | | #ifdef GPAC_HAS_CURL |
4060 | | && !sess->curl_hnd |
4061 | | #endif |
4062 | 0 | ) { |
4063 | 0 | u32 start = gf_sys_clock(); |
4064 | 0 | while (BodyStart + ContentLength > (u32) bytesRead) { |
4065 | 0 | e = gf_dm_sess_read(sess, sHTTP + bytesRead, buf_size - bytesRead, &res); |
4066 | 0 | switch (e) { |
4067 | 0 | case GF_IP_NETWORK_EMPTY: |
4068 | 0 | break; |
4069 | 0 | case GF_OK: |
4070 | 0 | bytesRead += res; |
4071 | 0 | break; |
4072 | 0 | default: |
4073 | 0 | start=0; |
4074 | 0 | break; |
4075 | 0 | } |
4076 | 0 | if (gf_sys_clock()-start>100) |
4077 | 0 | break; |
4078 | | |
4079 | | //does not fit in our buffer, too bad we'll kill the connection |
4080 | 0 | if (bytesRead == buf_size) |
4081 | 0 | break; |
4082 | 0 | } |
4083 | | |
4084 | 0 | if (BodyStart + ContentLength > (u32) bytesRead) { |
4085 | 0 | ContentLength = 0; |
4086 | | //cannot flush, discard socket |
4087 | 0 | sess->connection_close = GF_TRUE; |
4088 | 0 | } |
4089 | 0 | } |
4090 | | |
4091 | | #ifdef GPAC_HTTPMUX |
4092 | | if (sess->hmux_sess) { |
4093 | | u32 count = gf_list_count(sess->headers); |
4094 | | for (i=0; i<count; i++) { |
4095 | | GF_HTTPHeader *hdr = gf_list_get(sess->headers, i); |
4096 | | if (!stricmp(hdr->name, ":method")) { |
4097 | | method = http_parse_method(hdr->value); |
4098 | | sess->rsp_code = 200; |
4099 | | } |
4100 | | else if (!stricmp(hdr->name, ":path")) { |
4101 | | if (sess->orig_url) gf_free(sess->orig_url); |
4102 | | sess->orig_url = gf_strdup(hdr->value); |
4103 | | } |
4104 | | } |
4105 | | } |
4106 | | #endif |
4107 | | #ifdef GPAC_HAS_HTTP2 |
4108 | | else if (sess->server_mode |
4109 | | && !gf_opts_get_bool("core", "no-h2") |
4110 | | && !gf_opts_get_bool("core", "no-h2c") |
4111 | | && !(sess->flags & GF_NETIO_SESSION_USE_QUIC) |
4112 | | ) { |
4113 | | Bool is_upgradeable = GF_FALSE; |
4114 | | char *h2_settings = NULL; |
4115 | | u32 count = gf_list_count(sess->headers); |
4116 | | for (i=0; i<count; i++) { |
4117 | | GF_HTTPHeader *hdr = gf_list_get(sess->headers, i); |
4118 | | if (!stricmp(hdr->name, "Upgrade")) { |
4119 | | if (strstr(hdr->value, "h2c")) |
4120 | | is_upgradeable = GF_TRUE; |
4121 | | } |
4122 | | else if (!stricmp(hdr->name, "HTTP2-Settings")) { |
4123 | | h2_settings = hdr->value; |
4124 | | } |
4125 | | } |
4126 | | |
4127 | | if (is_upgradeable && h2_settings) { |
4128 | | u32 len = (u32) strlen(h2_settings); |
4129 | | sess->h2_upgrade_settings = gf_malloc(sizeof(char) * len * 2); |
4130 | | sess->h2_upgrade_settings_len = gf_base64_decode(h2_settings, len, sess->h2_upgrade_settings, len*2); |
4131 | | } |
4132 | | } |
4133 | | #endif |
4134 | | |
4135 | 0 | if (sess->server_mode) { |
4136 | 0 | if (ContentLength) { |
4137 | 0 | par.data = sHTTP + BodyStart; |
4138 | 0 | par.size = ContentLength; |
4139 | 0 | } else if ((BodyStart < (s32) bytesRead) |
4140 | | #ifdef GPAC_HTTPMUX |
4141 | | && !sess->hmux_sess |
4142 | | #endif |
4143 | 0 | ) { |
4144 | 0 | if (sess->init_data) gf_free(sess->init_data); |
4145 | 0 | sess->init_data_size = 0; |
4146 | 0 | sess->init_data = NULL; |
4147 | |
|
4148 | 0 | gf_dm_data_received(sess, (u8 *) sHTTP + BodyStart, bytesRead - BodyStart, GF_TRUE, NULL, NULL); |
4149 | 0 | } |
4150 | |
|
4151 | 0 | sess->request_start_time = gf_sys_clock_high_res(); |
4152 | |
|
4153 | 0 | par.reply = method; |
4154 | 0 | gf_dm_sess_user_io(sess, &par); |
4155 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
4156 | 0 | SET_LAST_ERR(GF_OK) |
4157 | 0 | return GF_OK; |
4158 | 0 | } |
4159 | | //remember if we can keep the session alive after the transfer is done |
4160 | 0 | sess->connection_close = connection_closed; |
4161 | 0 | gf_assert(sess->rsp_code); |
4162 | | |
4163 | |
|
4164 | 0 | switch (sess->rsp_code) { |
4165 | | //100 continue |
4166 | 0 | case 100: |
4167 | 0 | break; |
4168 | 0 | case 200: |
4169 | 0 | case 201: |
4170 | 0 | case 202: |
4171 | 0 | case 206: |
4172 | 0 | gf_dm_sess_user_io(sess, &par); |
4173 | 0 | e = GF_OK; |
4174 | 0 | if (sess->proxy_enabled==2) { |
4175 | 0 | sess->proxy_enabled=0; |
4176 | 0 | if (sess->dm) |
4177 | 0 | gf_list_add(sess->dm->skip_proxy_servers, gf_strdup(sess->server_name)); |
4178 | 0 | } |
4179 | 0 | sess->nb_redirect=0; |
4180 | 0 | break; |
4181 | | /*redirection: extract the new location*/ |
4182 | 0 | case 301: |
4183 | 0 | case 302: |
4184 | 0 | case 303: |
4185 | 0 | case 307: |
4186 | 0 | if ((sess->nb_redirect > 4) || !new_location || !strlen(new_location) ) { |
4187 | 0 | gf_dm_sess_user_io(sess, &par); |
4188 | 0 | e = GF_URL_ERROR; |
4189 | 0 | goto exit; |
4190 | 0 | } |
4191 | 0 | while ( |
4192 | 0 | (new_location[strlen(new_location)-1] == '\n') |
4193 | 0 | || (new_location[strlen(new_location)-1] == '\r') ) |
4194 | 0 | new_location[strlen(new_location)-1] = 0; |
4195 | | |
4196 | | /*reset and reconnect*/ |
4197 | 0 | gf_dm_disconnect(sess, HTTP_CLOSE); |
4198 | 0 | sess->status = GF_NETIO_SETUP; |
4199 | 0 | sess->nb_redirect++; |
4200 | 0 | e = gf_dm_sess_setup_from_url(sess, new_location, GF_FALSE); |
4201 | 0 | if (e) { |
4202 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
4203 | 0 | SET_LAST_ERR(e) |
4204 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
4205 | 0 | } |
4206 | 0 | gf_free(new_location); |
4207 | 0 | return e; |
4208 | 0 | case 304: |
4209 | 0 | { |
4210 | 0 | gf_assert(sess->cache_entry); |
4211 | 0 | gf_assert(!sess->cached_file); |
4212 | | |
4213 | | //special case for resources stored as persistent (mpd, init seg): we don't push the data |
4214 | 0 | if (gf_cache_entry_persistent(sess->cache_entry)) { |
4215 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_PARSE_REPLY, GF_OK); |
4216 | | |
4217 | | /* Cache file is the most recent */ |
4218 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
4219 | 0 | SET_LAST_ERR(GF_OK) |
4220 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_DATA_TRANSFERED, GF_OK); |
4221 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4222 | 0 | return GF_OK; |
4223 | 0 | } |
4224 | | |
4225 | 0 | sess->status = GF_NETIO_PARSE_REPLY; |
4226 | 0 | if (!gf_cache_is_mem(sess->cache_entry)) { |
4227 | 0 | sess->cached_file = gf_cache_open_read(sess->cache_entry); |
4228 | 0 | if (!sess->cached_file) { |
4229 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] FAILED to open cache file %s for reading contents !\n", sess->log_name, gf_cache_get_cache_filename(sess->cache_entry))); |
4230 | | /* Ooops, no cache, redownload everything ! */ |
4231 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4232 | 0 | sess->status = GF_NETIO_SETUP; |
4233 | 0 | e = gf_dm_sess_setup_from_url(sess, sess->orig_url, GF_FALSE); |
4234 | 0 | sess->total_size = gf_cache_get_cache_filesize(sess->cache_entry); |
4235 | 0 | if (e) { |
4236 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
4237 | 0 | SET_LAST_ERR(e) |
4238 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
4239 | 0 | } |
4240 | 0 | return e; |
4241 | 0 | } |
4242 | 0 | } else { |
4243 | | //we read from mem cache |
4244 | 0 | sess->local_cache_only = GF_TRUE; |
4245 | 0 | } |
4246 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
4247 | 0 | sess->total_size = ContentLength = gf_cache_get_cache_filesize(sess->cache_entry); |
4248 | |
|
4249 | 0 | gf_dm_sess_user_io(sess, &par); |
4250 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
4251 | 0 | e = GF_EOS; |
4252 | | //e = GF_OK; |
4253 | 0 | break; |
4254 | 0 | } |
4255 | 0 | case 401: |
4256 | 0 | { |
4257 | 0 | if (sess->creds && sess->creds->valid) { |
4258 | 0 | gf_opts_set_key("credentials", sess->creds->site, NULL); |
4259 | 0 | sess->creds->valid = GF_FALSE; |
4260 | 0 | } |
4261 | 0 | Bool secure = GF_FALSE; |
4262 | 0 | #ifdef GPAC_HAS_SSL |
4263 | 0 | if (sess->ssl) secure = GF_TRUE; |
4264 | 0 | #endif |
4265 | | /* Do we have a credentials struct ? */ |
4266 | 0 | sess->creds = gf_user_credentials_register(sess->dm, secure, sess->server_name, NULL, NULL, GF_FALSE); |
4267 | 0 | if (!sess->creds) { |
4268 | | /* User credentials have not been filled properly, we have to abort */ |
4269 | 0 | gf_dm_disconnect(sess, HTTP_CLOSE); |
4270 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
4271 | 0 | par.error = GF_AUTHENTICATION_FAILURE; |
4272 | 0 | par.msg_type = GF_NETIO_DISCONNECTED; |
4273 | 0 | gf_dm_sess_user_io(sess, &par); |
4274 | 0 | e = GF_AUTHENTICATION_FAILURE; |
4275 | 0 | SET_LAST_ERR(e) |
4276 | 0 | goto exit; |
4277 | 0 | } |
4278 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4279 | 0 | sess->status = GF_NETIO_SETUP; |
4280 | |
|
4281 | 0 | if (sess->creds->req_state==GF_CREDS_STATE_PENDING) { |
4282 | | //force a wait for reply until we have resolved user pass |
4283 | 0 | sess->status = GF_NETIO_WAIT_FOR_REPLY; |
4284 | 0 | return GF_OK; |
4285 | 0 | } |
4286 | | |
4287 | 0 | e = gf_dm_sess_setup_from_url(sess, sess->orig_url, GF_FALSE); |
4288 | 0 | if (e) { |
4289 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
4290 | 0 | SET_LAST_ERR(e) |
4291 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
4292 | 0 | } |
4293 | 0 | return e; |
4294 | 0 | } |
4295 | 0 | case 400: |
4296 | 0 | case 501: |
4297 | | /* Method not implemented ! */ |
4298 | 0 | if (sess->http_read_type == HEAD) { |
4299 | | /* Since HEAD is not understood by this server, we use a GET instead */ |
4300 | 0 | sess->http_read_type = GET; |
4301 | 0 | sess->flags |= GF_NETIO_SESSION_NOT_CACHED; |
4302 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4303 | 0 | sess->status = GF_NETIO_SETUP; |
4304 | 0 | sess->server_only_understand_get = GF_TRUE; |
4305 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("Method not supported, try with GET.\n")); |
4306 | 0 | e = gf_dm_sess_setup_from_url(sess, sess->orig_url, GF_FALSE); |
4307 | 0 | if (e) { |
4308 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
4309 | 0 | SET_LAST_ERR(e) |
4310 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
4311 | 0 | } |
4312 | 0 | return e; |
4313 | 0 | } |
4314 | | |
4315 | 0 | gf_dm_sess_user_io(sess, &par); |
4316 | 0 | notify_error_body(sess, sHTTP, bytesRead, BodyStart); |
4317 | 0 | e = GF_REMOTE_SERVICE_ERROR; |
4318 | 0 | goto exit; |
4319 | | |
4320 | 0 | case 503: |
4321 | | /*retry without proxy*/ |
4322 | 0 | if (sess->proxy_enabled==1) { |
4323 | 0 | sess->proxy_enabled=2; |
4324 | 0 | gf_dm_disconnect(sess, HTTP_CLOSE); |
4325 | 0 | sess->status = GF_NETIO_SETUP; |
4326 | 0 | return GF_OK; |
4327 | 0 | } |
4328 | | //fall-through |
4329 | | |
4330 | | /* case 204: |
4331 | | case 504: |
4332 | | case 404: |
4333 | | case 403: |
4334 | | case 416: |
4335 | | */ |
4336 | 0 | default: |
4337 | 0 | gf_dm_sess_user_io(sess, &par); |
4338 | 0 | if ((BodyStart < (s32) bytesRead)) { |
4339 | 0 | sHTTP[bytesRead] = 0; |
4340 | 0 | GF_LOG(GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] Failure - body: %s\n", sess->log_name, sHTTP + BodyStart)); |
4341 | 0 | } |
4342 | 0 | notify_error_body(sess, sHTTP, bytesRead, BodyStart); |
4343 | |
|
4344 | 0 | switch (sess->rsp_code) { |
4345 | 0 | case 204: e = GF_EOS; break; |
4346 | | /* File not found */ |
4347 | 0 | case 404: |
4348 | | //too early |
4349 | 0 | case 425: |
4350 | 0 | e = GF_URL_ERROR; break; |
4351 | | /* Forbidden */ |
4352 | 0 | case 403: e = GF_AUTHENTICATION_FAILURE; break; |
4353 | | /* Range not accepted */ |
4354 | 0 | case 416: |
4355 | 0 | e = GF_SERVICE_ERROR; |
4356 | 0 | break; |
4357 | 0 | case 504: e = GF_URL_ERROR; break; |
4358 | 0 | default: |
4359 | 0 | if (sess->rsp_code>=500) e = GF_REMOTE_SERVICE_ERROR; |
4360 | 0 | else e = GF_SERVICE_ERROR; |
4361 | 0 | break; |
4362 | 0 | } |
4363 | 0 | goto exit; |
4364 | 0 | } |
4365 | | |
4366 | 0 | if (sess->http_read_type != GET) |
4367 | 0 | sess->use_cache_file = GF_FALSE; |
4368 | |
|
4369 | 0 | if (sess->http_read_type==HEAD) { |
4370 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4371 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_DATA_TRANSFERED, GF_OK); |
4372 | 0 | sess->http_read_type = GET; |
4373 | 0 | return GF_OK; |
4374 | 0 | } |
4375 | | |
4376 | | |
4377 | 0 | mime_type = gf_cache_get_mime_type(sess->cache_entry); |
4378 | 0 | if (!ContentLength && mime_type && ((strstr(mime_type, "ogg") || (!strcmp(mime_type, "audio/mpeg"))))) { |
4379 | 0 | if (0 == sess->icy_metaint) |
4380 | 0 | sess->icy_metaint = -1; |
4381 | 0 | sess->use_cache_file = GF_FALSE; |
4382 | 0 | } |
4383 | |
|
4384 | 0 | #ifndef GPAC_DISABLE_LOG |
4385 | 0 | if (e<0) { |
4386 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[%s] Error processing rely from %s: %s\n", sess->log_name, sess->server_name, gf_error_to_string(e) ) ); |
4387 | 0 | } else { |
4388 | 0 | GF_LOG(GF_LOG_DEBUG, GF_LOG_HTTP, ("[%s] Reply processed from %s\n", sess->log_name, sess->server_name ) ); |
4389 | 0 | } |
4390 | 0 | #endif |
4391 | | |
4392 | | //in HTTP2 we may resume to setup state if we got a refused stream |
4393 | 0 | if (sess->status == GF_NETIO_SETUP) { |
4394 | 0 | return GF_OK; |
4395 | 0 | } |
4396 | | |
4397 | | |
4398 | | /*some servers may reply without content length, but we MUST have it*/ |
4399 | 0 | if (e) goto exit; |
4400 | 0 | if (sess->icy_metaint != 0) { |
4401 | 0 | gf_assert( ! sess->use_cache_file ); |
4402 | 0 | GF_LOG(GF_LOG_INFO, GF_LOG_HTTP, ("[%s] ICY protocol detected\n", sess->log_name)); |
4403 | 0 | if (mime_type && !stricmp(mime_type, "video/nsv")) { |
4404 | 0 | gf_cache_set_mime_type(sess->cache_entry, "audio/aac"); |
4405 | 0 | } |
4406 | 0 | sess->icy_bytes = 0; |
4407 | 0 | sess->total_size = SIZE_IN_STREAM; |
4408 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
4409 | 0 | } else if (!ContentLength && !has_content_length && !sess->chunked |
4410 | | #ifdef GPAC_HTTPMUX |
4411 | | && !sess->hmux_sess |
4412 | | #endif |
4413 | | #ifdef GPAC_HAS_CURL |
4414 | | && !sess->curl_hnd |
4415 | | #endif |
4416 | 0 | ) { |
4417 | 0 | if (sess->http_read_type == GET) { |
4418 | 0 | sess->total_size = SIZE_IN_STREAM; |
4419 | 0 | sess->use_cache_file = GF_FALSE; |
4420 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
4421 | 0 | sess->bytes_done = 0; |
4422 | 0 | } else { |
4423 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_DATA_TRANSFERED, GF_OK); |
4424 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4425 | 0 | return GF_OK; |
4426 | 0 | } |
4427 | | #ifdef GPAC_HTTPMUX |
4428 | | } else if (sess->hmux_sess && !ContentLength && (sess->http_read_type != GET)) { |
4429 | | gf_dm_sess_notify_state(sess, GF_NETIO_DATA_TRANSFERED, GF_OK); |
4430 | | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4431 | | return GF_OK; |
4432 | | #endif |
4433 | 0 | } else { |
4434 | 0 | sess->total_size = ContentLength; |
4435 | 0 | if (sess->use_cache_file && !sess->cached_file && (sess->http_read_type == GET)) { |
4436 | |
|
4437 | 0 | e = gf_cache_open_write_cache(sess->cache_entry, sess); |
4438 | 0 | if (e) { |
4439 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ( "[CACHE] Failed to open cache, error=%d\n", e)); |
4440 | 0 | goto exit; |
4441 | 0 | } |
4442 | 0 | } |
4443 | 0 | sess->status = GF_NETIO_DATA_EXCHANGE; |
4444 | 0 | sess->bytes_done = 0; |
4445 | 0 | if (!ContentLength && has_content_length) { |
4446 | 0 | gf_dm_sess_notify_state(sess, GF_NETIO_DATA_TRANSFERED, GF_OK); |
4447 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4448 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
4449 | 0 | return GF_OK; |
4450 | 0 | } |
4451 | 0 | } |
4452 | | |
4453 | | /* we may have existing data in this buffer ... */ |
4454 | | #ifdef GPAC_HTTPMUX |
4455 | | if (!e && sess->hmux_sess) { |
4456 | | hmux_flush_internal_data(sess, GF_TRUE); |
4457 | | } else |
4458 | | #endif |
4459 | 0 | if (!e && (BodyStart < (s32) bytesRead)) { |
4460 | 0 | u32 rewrite_size=0; |
4461 | 0 | if (sess->init_data) gf_free(sess->init_data); |
4462 | 0 | sess->init_data_size = 0; |
4463 | 0 | sess->init_data = NULL; |
4464 | |
|
4465 | 0 | gf_dm_data_received(sess, (u8 *) sHTTP + BodyStart, bytesRead - BodyStart, GF_TRUE, &rewrite_size, NULL); |
4466 | |
|
4467 | 0 | if (sess->chunked) |
4468 | 0 | gf_dm_sess_estimate_chunk_rate(sess, rewrite_size); |
4469 | 0 | } |
4470 | 0 | exit: |
4471 | 0 | if (e) { |
4472 | 0 | if (e<0) { |
4473 | 0 | GF_LOG((e==GF_URL_ERROR) ? GF_LOG_INFO : GF_LOG_WARNING, GF_LOG_HTTP, ("[%s] Error parsing reply for URL %s: %s (code %d)\n", sess->log_name, sess->orig_url, gf_error_to_string(e), sess->rsp_code )); |
4474 | 0 | } else { |
4475 | 0 | e = GF_OK; |
4476 | 0 | } |
4477 | 0 | if (e) { |
4478 | 0 | gf_cache_entry_set_delete_files_when_deleted(sess->cache_entry); |
4479 | 0 | gf_cache_remove_entry_from_session(sess); |
4480 | 0 | sess->cache_entry = NULL; |
4481 | 0 | } |
4482 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4483 | 0 | if ((e<0) && connection_closed) |
4484 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
4485 | 0 | else |
4486 | 0 | sess->status = GF_NETIO_DATA_TRANSFERED; |
4487 | 0 | SET_LAST_ERR(e) |
4488 | 0 | gf_dm_sess_notify_state(sess, sess->status, e); |
4489 | 0 | return e; |
4490 | 0 | } |
4491 | | /*DO NOT call parse_body yet, as the final user may not be connected to our session*/ |
4492 | 0 | return GF_OK; |
4493 | 0 | } |
4494 | | |
4495 | | /** |
4496 | | * Default performing behavior |
4497 | | \param sess The session |
4498 | | */ |
4499 | | void http_do_requests(GF_DownloadSession *sess) |
4500 | 0 | { |
4501 | 0 | sess->http_buf[0] = 0; |
4502 | |
|
4503 | 0 | if (sess->reused_cache_entry) { |
4504 | | //main session is done downloading, notify - to do we should send progress events on this session also ... |
4505 | 0 | if (!gf_cache_is_in_progress(sess->cache_entry)) { |
4506 | 0 | GF_NETIO_Parameter par; |
4507 | 0 | gf_dm_disconnect(sess, HTTP_NO_CLOSE); |
4508 | 0 | sess->reused_cache_entry = GF_FALSE; |
4509 | 0 | memset(&par, 0, sizeof(GF_NETIO_Parameter)); |
4510 | 0 | par.msg_type = GF_NETIO_DATA_TRANSFERED; |
4511 | 0 | par.error = GF_OK; |
4512 | 0 | gf_dm_sess_user_io(sess, &par); |
4513 | 0 | } |
4514 | 0 | return; |
4515 | 0 | } |
4516 | | |
4517 | 0 | if (sess->async_buf_size && (gf_dm_sess_flush_async(sess, GF_FALSE) == GF_IP_NETWORK_EMPTY)) { |
4518 | 0 | return; |
4519 | 0 | } |
4520 | | |
4521 | 0 | switch (sess->status) { |
4522 | 0 | case GF_NETIO_CONNECTED: |
4523 | 0 | if (sess->server_mode) { |
4524 | 0 | wait_for_header_and_parse(sess); |
4525 | 0 | } else { |
4526 | 0 | http_send_headers(sess); |
4527 | 0 | } |
4528 | 0 | break; |
4529 | 0 | case GF_NETIO_WAIT_FOR_REPLY: |
4530 | 0 | if (sess->server_mode) { |
4531 | 0 | http_send_headers(sess); |
4532 | 0 | } else { |
4533 | 0 | wait_for_header_and_parse(sess); |
4534 | 0 | } |
4535 | 0 | break; |
4536 | 0 | case GF_NETIO_DATA_EXCHANGE: |
4537 | 0 | if (sess->server_mode) { |
4538 | 0 | sess->status = GF_NETIO_CONNECTED; |
4539 | 0 | break; |
4540 | 0 | } |
4541 | | /*session has been reassigned, resend data retrieved in first GET reply to user but don't write to cache*/ |
4542 | 0 | if (sess->reassigned) { |
4543 | |
|
4544 | 0 | if (sess->icy_metaint > 0) { |
4545 | | //we are reparsing init data, reset icy status |
4546 | 0 | sess->icy_bytes = 0; |
4547 | 0 | gf_icy_skip_data(sess, sess->init_data, sess->init_data_size); |
4548 | 0 | } else { |
4549 | 0 | GF_NETIO_Parameter par; |
4550 | 0 | par.msg_type = GF_NETIO_DATA_EXCHANGE; |
4551 | 0 | par.error = GF_OK; |
4552 | 0 | par.data = sess->init_data; |
4553 | 0 | par.size = sess->init_data_size; |
4554 | 0 | gf_dm_sess_user_io(sess, &par); |
4555 | 0 | } |
4556 | 0 | sess->reassigned = GF_FALSE; |
4557 | 0 | } |
4558 | 0 | http_parse_remaining_body(sess); |
4559 | 0 | break; |
4560 | 0 | default: |
4561 | 0 | break; |
4562 | 0 | } |
4563 | 0 | } |
4564 | | |
4565 | | GF_EXPORT |
4566 | | void gf_dm_sess_set_max_rate(GF_DownloadSession *sess, u32 max_rate) |
4567 | 0 | { |
4568 | 0 | if (sess) { |
4569 | 0 | sess->max_data_rate = max_rate/8; |
4570 | 0 | sess->rate_regulated = GF_FALSE; |
4571 | 0 | } |
4572 | 0 | } |
4573 | | GF_EXPORT |
4574 | | u32 gf_dm_sess_get_max_rate(GF_DownloadSession *sess) |
4575 | 0 | { |
4576 | 0 | return sess ? 8*sess->max_data_rate : 0; |
4577 | 0 | } |
4578 | | GF_EXPORT |
4579 | | Bool gf_dm_sess_is_regulated(GF_DownloadSession *sess) |
4580 | 0 | { |
4581 | 0 | return sess ? sess->rate_regulated : GF_FALSE; |
4582 | 0 | } |
4583 | | |
4584 | | |
4585 | | /** |
4586 | | * NET IO for MPD, we don't need this anymore since mime-type can be given by session |
4587 | | */ |
4588 | | static void wget_NetIO(void *cbk, GF_NETIO_Parameter *param) |
4589 | 0 | { |
4590 | 0 | FILE * f = (FILE*) cbk; |
4591 | | |
4592 | | /*handle service message*/ |
4593 | 0 | if (param->msg_type == GF_NETIO_DATA_EXCHANGE) { |
4594 | 0 | s32 written = (u32) gf_fwrite( param->data, param->size, f); |
4595 | 0 | if (written != param->size) { |
4596 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("Failed to write data on disk\n")); |
4597 | 0 | } |
4598 | 0 | } |
4599 | 0 | } |
4600 | | |
4601 | | GF_EXPORT |
4602 | | GF_Err gf_dm_wget(const char *url, const char *filename, u64 start_range, u64 end_range, char **redirected_url) |
4603 | 0 | { |
4604 | 0 | GF_Err e; |
4605 | 0 | GF_DownloadManager * dm = gf_dm_new(NULL); |
4606 | 0 | if (!dm) |
4607 | 0 | return GF_OUT_OF_MEM; |
4608 | 0 | e = gf_dm_wget_with_cache(dm, url, filename, start_range, end_range, redirected_url); |
4609 | 0 | gf_dm_del(dm); |
4610 | 0 | return e; |
4611 | 0 | } |
4612 | | |
4613 | | GF_EXPORT |
4614 | | GF_Err gf_dm_wget_with_cache(GF_DownloadManager * dm, const char *url, const char *filename, u64 start_range, u64 end_range, char **redirected_url) |
4615 | 0 | { |
4616 | 0 | GF_Err e; |
4617 | 0 | FILE * f; |
4618 | 0 | GF_DownloadSession *dnload; |
4619 | 0 | if (!filename || !url || !dm) |
4620 | 0 | return GF_BAD_PARAM; |
4621 | 0 | f = gf_fopen(filename, "wb"); |
4622 | 0 | if (!f) { |
4623 | 0 | GF_LOG(GF_LOG_ERROR, GF_LOG_HTTP, ("[WGET] Failed to open file %s for write.\n", filename)); |
4624 | 0 | return GF_IO_ERR; |
4625 | 0 | } |
4626 | 0 | dnload = gf_dm_sess_new_simple(dm, (char *)url, GF_NETIO_SESSION_NOT_THREADED, &wget_NetIO, f, &e); |
4627 | 0 | if (!dnload) { |
4628 | 0 | return GF_BAD_PARAM; |
4629 | 0 | } |
4630 | 0 | dnload->use_cache_file = GF_FALSE; |
4631 | 0 | dnload->force_data_write_callback = GF_TRUE; |
4632 | 0 | if (end_range) { |
4633 | 0 | dnload->range_start = start_range; |
4634 | 0 | dnload->range_end = end_range; |
4635 | 0 | dnload->needs_range = GF_TRUE; |
4636 | 0 | } |
4637 | 0 | while (e == GF_OK) { |
4638 | 0 | e = gf_dm_sess_process(dnload); |
4639 | 0 | if (e && (e!=GF_IP_NETWORK_EMPTY)) |
4640 | 0 | break; |
4641 | 0 | if (dnload->status>=GF_NETIO_DATA_TRANSFERED) break; |
4642 | 0 | e = GF_OK; |
4643 | 0 | if (dnload->connect_pending) |
4644 | 0 | gf_sleep(100); |
4645 | 0 | } |
4646 | 0 | if (e==GF_OK) |
4647 | 0 | gf_cache_set_content_length(dnload->cache_entry, dnload->total_size); |
4648 | 0 | e |= gf_cache_close_write_cache(dnload->cache_entry, dnload, (e == GF_OK) ? GF_TRUE : GF_FALSE); |
4649 | 0 | gf_fclose(f); |
4650 | |
|
4651 | 0 | if (redirected_url) { |
4652 | 0 | if (dnload->orig_url_before_redirect) *redirected_url = gf_strdup(dnload->orig_url); |
4653 | 0 | } |
4654 | 0 | gf_dm_sess_del(dnload); |
4655 | 0 | return e; |
4656 | 0 | } |
4657 | | |
4658 | | GF_EXPORT |
4659 | | const char *gf_dm_sess_get_resource_name(GF_DownloadSession *dnload) |
4660 | 0 | { |
4661 | 0 | return dnload ? dnload->orig_url : NULL; |
4662 | 0 | } |
4663 | | |
4664 | | |
4665 | | |
4666 | | GF_EXPORT |
4667 | | void gf_dm_set_data_rate(GF_DownloadManager *dm, u32 rate_in_bits_per_sec) |
4668 | 0 | { |
4669 | 0 | if (rate_in_bits_per_sec == 0xFFFFFFFF) { |
4670 | 0 | dm->simulate_no_connection=GF_TRUE; |
4671 | 0 | } else { |
4672 | 0 | char opt[100]; |
4673 | 0 | dm->simulate_no_connection=GF_FALSE; |
4674 | 0 | dm->limit_data_rate = rate_in_bits_per_sec/8; |
4675 | |
|
4676 | 0 | sprintf(opt, "%d", rate_in_bits_per_sec); |
4677 | | //temporary store of maxrate |
4678 | 0 | gf_opts_set_key("temp", "maxrate", opt); |
4679 | 0 | } |
4680 | 0 | } |
4681 | | |
4682 | | GF_EXPORT |
4683 | | u32 gf_dm_get_data_rate(GF_DownloadManager *dm) |
4684 | 0 | { |
4685 | 0 | return dm->limit_data_rate*8; |
4686 | 0 | } |
4687 | | |
4688 | | GF_EXPORT |
4689 | | u32 gf_dm_get_global_rate(GF_DownloadManager *dm) |
4690 | 0 | { |
4691 | 0 | u32 ret = 0; |
4692 | 0 | u32 i, count; |
4693 | 0 | if (!dm) return 0; |
4694 | 0 | gf_mx_p(dm->cache_mx); |
4695 | 0 | count = gf_list_count(dm->all_sessions); |
4696 | |
|
4697 | 0 | for (i=0; i<count; i++) { |
4698 | 0 | GF_DownloadSession *sess = (GF_DownloadSession*)gf_list_get(dm->all_sessions, i); |
4699 | 0 | if (sess->status >= GF_NETIO_DATA_TRANSFERED) { |
4700 | 0 | if (sess->total_size==sess->bytes_done) { |
4701 | | //do not aggregate session if done/interrupted since more than 1/2 a sec |
4702 | 0 | if (gf_sys_clock_high_res() - sess->start_time > 500000) { |
4703 | 0 | continue; |
4704 | 0 | } |
4705 | 0 | } |
4706 | 0 | } |
4707 | 0 | ret += sess->bytes_per_sec; |
4708 | 0 | } |
4709 | 0 | gf_mx_v(dm->cache_mx); |
4710 | 0 | return 8*ret; |
4711 | 0 | } |
4712 | | |
4713 | | GF_HTTPSessionType gf_dm_sess_is_hmux(GF_DownloadSession *sess) |
4714 | 0 | { |
4715 | | #ifdef GPAC_HTTPMUX |
4716 | | if (sess->hmux_sess) { |
4717 | | if (sess->hmux_sess->net_sess && sess->hmux_sess->net_sess->flags & GF_NETIO_SESSION_USE_QUIC) |
4718 | | return GF_SESS_TYPE_HTTP3; |
4719 | | if (sess->hmux_sess->net_sess && !sess->hmux_sess->net_sess->sock) |
4720 | | return GF_SESS_TYPE_HTTP3; |
4721 | | return GF_SESS_TYPE_HTTP2; |
4722 | | } |
4723 | | #endif |
4724 | 0 | return GF_SESS_TYPE_HTTP; |
4725 | 0 | } |
4726 | | |
4727 | | Bool gf_dm_sess_use_tls(GF_DownloadSession * sess) |
4728 | 0 | { |
4729 | 0 | #ifdef GPAC_HAS_SSL |
4730 | 0 | if (sess->ssl) |
4731 | 0 | return GF_TRUE; |
4732 | 0 | #endif |
4733 | | |
4734 | | #ifdef GPAC_HTTPMUX |
4735 | | if (sess->hmux_sess && (sess->hmux_sess->net_sess->flags & GF_NETIO_SESSION_USE_QUIC)) |
4736 | | return GF_TRUE; |
4737 | | #endif |
4738 | 0 | return GF_FALSE; |
4739 | 0 | } |
4740 | | |
4741 | | u32 gf_dm_sess_get_resource_size(GF_DownloadSession * sess) |
4742 | 0 | { |
4743 | 0 | if (!sess) return 0; |
4744 | 0 | if (sess->full_resource_size) return sess->full_resource_size; |
4745 | 0 | if (sess->needs_range) return 0; |
4746 | 0 | return sess->total_size; |
4747 | 0 | } |
4748 | | |
4749 | | GF_EXPORT |
4750 | | const char *gf_dm_sess_get_header(GF_DownloadSession *sess, const char *name) |
4751 | 5 | { |
4752 | 5 | u32 i, count; |
4753 | 5 | if( !sess || !name) return NULL; |
4754 | 0 | count = gf_list_count(sess->headers); |
4755 | 0 | for (i=0; i<count; i++) { |
4756 | 0 | GF_HTTPHeader *header = (GF_HTTPHeader*)gf_list_get(sess->headers, i); |
4757 | 0 | if (!stricmp(header->name, name)) return header->value; |
4758 | 0 | } |
4759 | 0 | return NULL; |
4760 | 0 | } |
4761 | | |
4762 | | GF_EXPORT |
4763 | | GF_Err gf_dm_sess_enum_headers(GF_DownloadSession *sess, u32 *idx, const char **hdr_name, const char **hdr_val) |
4764 | 0 | { |
4765 | 0 | GF_HTTPHeader *hdr; |
4766 | 0 | if( !sess || !idx || !hdr_name || !hdr_val) |
4767 | 0 | return GF_BAD_PARAM; |
4768 | 0 | hdr = gf_list_get(sess->headers, *idx); |
4769 | 0 | if (!hdr) return GF_EOS; |
4770 | 0 | (*idx) = (*idx) + 1; |
4771 | 0 | (*hdr_name) = hdr->name; |
4772 | 0 | (*hdr_val) = hdr->value; |
4773 | 0 | return GF_OK; |
4774 | 0 | } |
4775 | | |
4776 | | GF_EXPORT |
4777 | | GF_Err gf_dm_sess_get_header_sizes_and_times(GF_DownloadSession *sess, u32 *req_hdr_size, u32 *rsp_hdr_size, u32 *connect_time, u32 *reply_time, u32 *download_time) |
4778 | 0 | { |
4779 | 0 | if (!sess) |
4780 | 0 | return GF_BAD_PARAM; |
4781 | | |
4782 | 0 | if (req_hdr_size) *req_hdr_size = sess->req_hdr_size; |
4783 | 0 | if (rsp_hdr_size) *rsp_hdr_size = sess->rsp_hdr_size; |
4784 | 0 | if (connect_time) *connect_time = sess->connect_time; |
4785 | 0 | if (reply_time) *reply_time = sess->reply_time; |
4786 | 0 | if (download_time) *download_time = sess->total_time_since_req; |
4787 | 0 | return GF_OK; |
4788 | 0 | } |
4789 | | |
4790 | | GF_EXPORT |
4791 | | void gf_dm_sess_force_memory_mode(GF_DownloadSession *sess, u32 force_keep) |
4792 | 0 | { |
4793 | 0 | if (sess) { |
4794 | 0 | sess->flags |= GF_NETIO_SESSION_MEMORY_CACHE; |
4795 | 0 | sess->flags &= ~GF_NETIO_SESSION_NOT_CACHED; |
4796 | 0 | if (force_keep==1) { |
4797 | 0 | sess->flags |= GF_NETIO_SESSION_KEEP_CACHE; |
4798 | 0 | } else if (force_keep==2) |
4799 | 0 | sess->flags |= GF_NETIO_SESSION_KEEP_FIRST_CACHE; |
4800 | 0 | } |
4801 | 0 | } |
4802 | | |
4803 | | |
4804 | | static GF_Err gf_dm_sess_flush_async_close(GF_DownloadSession *sess, Bool no_select, Bool for_close) |
4805 | 0 | { |
4806 | 0 | if (!sess) return GF_OK; |
4807 | 0 | if (sess->status==GF_NETIO_STATE_ERROR) return sess->last_error; |
4808 | | |
4809 | 0 | if (!no_select && sess->sock && (gf_sk_select(sess->sock, GF_SK_SELECT_WRITE)!=GF_OK)) { |
4810 | 0 | return sess->async_buf_size ? GF_IP_NETWORK_EMPTY : GF_OK; |
4811 | 0 | } |
4812 | 0 | GF_Err ret = GF_OK; |
4813 | |
|
4814 | | #ifdef GPAC_HTTPMUX |
4815 | | if(sess->hmux_sess) |
4816 | | ret = sess->hmux_sess->async_flush(sess, for_close); |
4817 | | |
4818 | | //if H2 flush the parent session holding the http2 session |
4819 | | if (sess->hmux_sess) |
4820 | | sess = sess->hmux_sess->net_sess; |
4821 | | #endif |
4822 | |
|
4823 | 0 | if (sess->async_buf_size) { |
4824 | 0 | GF_Err e = gf_dm_sess_write(sess, sess->async_buf, sess->async_buf_size); |
4825 | 0 | if (e) return e; |
4826 | 0 | if (sess->async_buf_size) return GF_IP_NETWORK_EMPTY; |
4827 | 0 | } |
4828 | 0 | return ret; |
4829 | 0 | } |
4830 | | |
4831 | | GF_Err gf_dm_sess_flush_async(GF_DownloadSession *sess, Bool no_select) |
4832 | 0 | { |
4833 | 0 | return gf_dm_sess_flush_async_close(sess, no_select, GF_FALSE); |
4834 | 0 | } |
4835 | | GF_Err gf_dm_sess_flush_close(GF_DownloadSession *sess) |
4836 | 0 | { |
4837 | 0 | return gf_dm_sess_flush_async_close(sess, GF_TRUE, GF_TRUE); |
4838 | 0 | } |
4839 | | |
4840 | | u32 gf_dm_sess_async_pending(GF_DownloadSession *sess) |
4841 | 0 | { |
4842 | 0 | if (!sess) return 0; |
4843 | | #ifdef GPAC_HTTPMUX |
4844 | | if (sess->local_buf_len) return sess->local_buf_len; |
4845 | | if (sess->hmux_sess) |
4846 | | sess = sess->hmux_sess->net_sess; |
4847 | | #endif |
4848 | 0 | return sess ? sess->async_buf_size : 0; |
4849 | 0 | } |
4850 | | |
4851 | | GF_EXPORT |
4852 | | GF_Err gf_dm_sess_send(GF_DownloadSession *sess, u8 *data, u32 size) |
4853 | 0 | { |
4854 | 0 | GF_Err e = GF_OK; |
4855 | |
|
4856 | | #ifdef GPAC_HTTPMUX |
4857 | | if (sess->hmux_sess) { |
4858 | | return hmux_send_payload(sess, data, size); |
4859 | | } |
4860 | | #endif |
4861 | |
|
4862 | 0 | if (!data || !size) { |
4863 | 0 | if (sess->put_state) { |
4864 | 0 | sess->put_state = 2; |
4865 | 0 | sess->status = GF_NETIO_WAIT_FOR_REPLY; |
4866 | 0 | return GF_OK; |
4867 | 0 | } |
4868 | 0 | return GF_OK; |
4869 | 0 | } |
4870 | | |
4871 | 0 | e = gf_dm_sess_write(sess, data, size); |
4872 | |
|
4873 | 0 | if (e==GF_IP_CONNECTION_CLOSED) { |
4874 | 0 | sess_connection_closed(sess); |
4875 | 0 | sess->status = GF_NETIO_STATE_ERROR; |
4876 | 0 | return e; |
4877 | 0 | } |
4878 | 0 | else if (e==GF_IP_NETWORK_EMPTY) { |
4879 | 0 | if (sess->flags & GF_NETIO_SESSION_NO_BLOCK) |
4880 | 0 | return GF_OK; |
4881 | | |
4882 | 0 | return gf_dm_sess_send(sess, data, size); |
4883 | 0 | } |
4884 | 0 | return e; |
4885 | 0 | } |
4886 | | |
4887 | | GF_Socket *gf_dm_sess_get_socket(GF_DownloadSession *sess) |
4888 | 0 | { |
4889 | 0 | return sess ? sess->sock : NULL; |
4890 | 0 | } |
4891 | | |
4892 | | void gf_dm_sess_set_sock_group(GF_DownloadSession *sess, GF_SockGroup *sg) |
4893 | 0 | { |
4894 | 0 | if (sess) { |
4895 | 0 | if (sess->sock_group!=sg) { |
4896 | 0 | if (sess->sock_group) |
4897 | 0 | gf_sk_group_unregister(sess->sock_group, sess->sock); |
4898 | 0 | if (sg) |
4899 | 0 | gf_sk_group_register(sg, sess->sock); |
4900 | 0 | } |
4901 | 0 | sess->sock_group = sg; |
4902 | 0 | } |
4903 | 0 | } |
4904 | | |
4905 | | void gf_dm_sess_detach_async(GF_DownloadSession *sess) |
4906 | 0 | { |
4907 | 0 | if (sess->sock) { |
4908 | 0 | while (sess->async_buf_size) { |
4909 | 0 | gf_dm_sess_flush_async(sess, GF_FALSE); |
4910 | 0 | gf_sleep(1); |
4911 | 0 | } |
4912 | 0 | gf_sk_set_block_mode(sess->sock, GF_TRUE); |
4913 | 0 | } |
4914 | 0 | sess->flags |= GF_NETIO_SESSION_NO_BLOCK; |
4915 | | //FOR TEST ONLY |
4916 | 0 | sess->flags &= ~GF_NETIO_SESSION_NOT_THREADED; |
4917 | | //mutex may already be created for H2 sessions |
4918 | 0 | if (!sess->mx) |
4919 | 0 | sess->mx = gf_mx_new(sess->orig_url); |
4920 | 0 | } |
4921 | | |
4922 | | void gf_dm_sess_set_timeout(GF_DownloadSession *sess, u32 timeout) |
4923 | 0 | { |
4924 | 0 | if (sess) sess->request_timeout = 1000*timeout; |
4925 | 0 | } |
4926 | | |
4927 | | void gf_dm_sess_set_netcap_id(GF_DownloadSession *sess, const char *netcap_id) |
4928 | 0 | { |
4929 | 0 | if (sess) sess->netcap_id = netcap_id; |
4930 | 0 | } |
4931 | | |
4932 | | |
4933 | | #endif //GPAC_DISABLE_NETWORK |