FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/http.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 0 1173 0.0%
Functions: 0 51 0.0%
Branches: 0 941 0.0%

Line Branch Exec Source
1 /*
2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdbool.h>
23
24 #include "config.h"
25 #include "config_components.h"
26
27 #include <time.h>
28 #if CONFIG_ZLIB
29 #include <zlib.h>
30 #endif /* CONFIG_ZLIB */
31
32 #include "libavutil/avassert.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/bprint.h"
35 #include "libavutil/getenv_utf8.h"
36 #include "libavutil/macros.h"
37 #include "libavutil/mem.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/time.h"
40 #include "libavutil/parseutils.h"
41
42 #include "avformat.h"
43 #include "http.h"
44 #include "httpauth.h"
45 #include "internal.h"
46 #include "network.h"
47 #include "os_support.h"
48 #include "url.h"
49 #include "version.h"
50
51 /* XXX: POST protocol is not completely implemented because ffmpeg uses
52 * only a subset of it. */
53
54 /* The IO buffer size is unrelated to the max URL size in itself, but needs
55 * to be large enough to fit the full request headers (including long
56 * path names). */
57 #define BUFFER_SIZE (MAX_URL_SIZE + HTTP_HEADERS_SIZE)
58 #define MAX_REDIRECTS 8
59 #define MAX_CACHED_REDIRECTS 32
60 #define HTTP_SINGLE 1
61 #define HTTP_MUTLI 2
62 #define MAX_DATE_LEN 19
63 #define WHITESPACES " \n\t\r"
64 typedef enum {
65 LOWER_PROTO,
66 READ_HEADERS,
67 WRITE_REPLY_HEADERS,
68 FINISH
69 }HandshakeState;
70
71 typedef struct HTTPContext {
72 const AVClass *class;
73 URLContext *hd;
74 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
75 int line_count;
76 int http_code;
77 /* Used if "Transfer-Encoding: chunked" otherwise -1. */
78 uint64_t chunksize;
79 int chunkend;
80 uint64_t off, end_off, filesize;
81 char *uri;
82 char *location;
83 HTTPAuthState auth_state;
84 HTTPAuthState proxy_auth_state;
85 char *http_proxy;
86 char *headers;
87 char *mime_type;
88 char *http_version;
89 char *user_agent;
90 char *referer;
91 char *content_type;
92 /* Set if the server correctly handles Connection: close and will close
93 * the connection after feeding us the content. */
94 int willclose;
95 int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
96 int chunked_post;
97 /* A flag which indicates if the end of chunked encoding has been sent. */
98 int end_chunked_post;
99 /* A flag which indicates we have finished to read POST reply. */
100 int end_header;
101 /* A flag which indicates if we use persistent connections. */
102 int multiple_requests;
103 uint8_t *post_data;
104 int post_datalen;
105 int is_akamai;
106 int is_mediagateway;
107 char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
108 /* A dictionary containing cookies keyed by cookie name */
109 AVDictionary *cookie_dict;
110 int icy;
111 /* how much data was read since the last ICY metadata packet */
112 uint64_t icy_data_read;
113 /* after how many bytes of read data a new metadata packet will be found */
114 uint64_t icy_metaint;
115 char *icy_metadata_headers;
116 char *icy_metadata_packet;
117 AVDictionary *metadata;
118 #if CONFIG_ZLIB
119 int compressed;
120 z_stream inflate_stream;
121 uint8_t *inflate_buffer;
122 #endif /* CONFIG_ZLIB */
123 AVDictionary *chained_options;
124 /* -1 = try to send if applicable, 0 = always disabled, 1 = always enabled */
125 int send_expect_100;
126 char *method;
127 int reconnect;
128 int reconnect_at_eof;
129 int reconnect_on_network_error;
130 int reconnect_streamed;
131 int reconnect_delay_max;
132 char *reconnect_on_http_error;
133 int listen;
134 char *resource;
135 int reply_code;
136 int is_multi_client;
137 HandshakeState handshake_step;
138 int is_connected_server;
139 int short_seek_size;
140 int64_t expires;
141 char *new_location;
142 AVDictionary *redirect_cache;
143 uint64_t filesize_from_content_range;
144 int respect_retry_after;
145 unsigned int retry_after;
146 int reconnect_max_retries;
147 int reconnect_delay_total_max;
148 } HTTPContext;
149
150 #define OFFSET(x) offsetof(HTTPContext, x)
151 #define D AV_OPT_FLAG_DECODING_PARAM
152 #define E AV_OPT_FLAG_ENCODING_PARAM
153 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
154
155 static const AVOption options[] = {
156 { "seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D },
157 { "chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
158 { "http_proxy", "set HTTP proxy to tunnel through", OFFSET(http_proxy), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
159 { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
160 { "content_type", "set a specific content type for the POST messages", OFFSET(content_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
161 { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
162 { "referer", "override referer header", OFFSET(referer), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
163 { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D | E },
164 { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
165 { "mime_type", "export the MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
166 { "http_version", "export the http response version", OFFSET(http_version), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
167 { "cookies", "set cookies to be sent in applicable future requests, use newline delimited Set-Cookie HTTP field value syntax", OFFSET(cookies), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
168 { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, D },
169 { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
170 { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
171 { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
172 { "auth_type", "HTTP authentication type", OFFSET(auth_state.auth_type), AV_OPT_TYPE_INT, { .i64 = HTTP_AUTH_NONE }, HTTP_AUTH_NONE, HTTP_AUTH_BASIC, D | E, .unit = "auth_type"},
173 { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, .unit = "auth_type"},
174 { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, .unit = "auth_type"},
175 { "send_expect_100", "Force sending an Expect: 100-continue header for POST", OFFSET(send_expect_100), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, E },
176 { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
177 { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
178 { "end_offset", "try to limit the request to bytes preceding this offset", OFFSET(end_off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
179 { "method", "Override the HTTP method or set the expected HTTP method from a client", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
180 { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
181 { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
182 { "reconnect_on_network_error", "auto reconnect in case of tcp/tls error during connect", OFFSET(reconnect_on_network_error), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
183 { "reconnect_on_http_error", "list of http status codes to reconnect on", OFFSET(reconnect_on_http_error), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
184 { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
185 { "reconnect_delay_max", "max reconnect delay in seconds after which to give up", OFFSET(reconnect_delay_max), AV_OPT_TYPE_INT, { .i64 = 120 }, 0, UINT_MAX/1000/1000, D },
186 { "reconnect_max_retries", "the max number of times to retry a connection", OFFSET(reconnect_max_retries), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D },
187 { "reconnect_delay_total_max", "max total reconnect delay in seconds after which to give up", OFFSET(reconnect_delay_total_max), AV_OPT_TYPE_INT, { .i64 = 256 }, 0, UINT_MAX/1000/1000, D },
188 { "respect_retry_after", "respect the Retry-After header when retrying connections", OFFSET(respect_retry_after), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, D },
189 { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
190 { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
191 { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
192 { "short_seek_size", "Threshold to favor readahead over seek.", OFFSET(short_seek_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
193 { NULL }
194 };
195
196 static int http_connect(URLContext *h, const char *path, const char *local_path,
197 const char *hoststr, const char *auth,
198 const char *proxyauth);
199 static int http_read_header(URLContext *h);
200 static int http_shutdown(URLContext *h, int flags);
201
202 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
203 {
204 memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
205 &((HTTPContext *)src->priv_data)->auth_state,
206 sizeof(HTTPAuthState));
207 memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
208 &((HTTPContext *)src->priv_data)->proxy_auth_state,
209 sizeof(HTTPAuthState));
210 }
211
212 static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
213 {
214 const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
215 char *env_http_proxy, *env_no_proxy;
216 char *hashmark;
217 char hostname[1024], hoststr[1024], proto[10];
218 char auth[1024], proxyauth[1024] = "";
219 char path1[MAX_URL_SIZE], sanitized_path[MAX_URL_SIZE + 1];
220 char buf[1024], urlbuf[MAX_URL_SIZE];
221 int port, use_proxy, err = 0;
222 HTTPContext *s = h->priv_data;
223
224 av_url_split(proto, sizeof(proto), auth, sizeof(auth),
225 hostname, sizeof(hostname), &port,
226 path1, sizeof(path1), s->location);
227 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
228
229 env_http_proxy = getenv_utf8("http_proxy");
230 proxy_path = s->http_proxy ? s->http_proxy : env_http_proxy;
231
232 env_no_proxy = getenv_utf8("no_proxy");
233 use_proxy = !ff_http_match_no_proxy(env_no_proxy, hostname) &&
234 proxy_path && av_strstart(proxy_path, "http://", NULL);
235 freeenv_utf8(env_no_proxy);
236
237 if (!strcmp(proto, "https")) {
238 lower_proto = "tls";
239 use_proxy = 0;
240 if (port < 0)
241 port = 443;
242 /* pass http_proxy to underlying protocol */
243 if (s->http_proxy) {
244 err = av_dict_set(options, "http_proxy", s->http_proxy, 0);
245 if (err < 0)
246 goto end;
247 }
248 }
249 if (port < 0)
250 port = 80;
251
252 hashmark = strchr(path1, '#');
253 if (hashmark)
254 *hashmark = '\0';
255
256 if (path1[0] == '\0') {
257 path = "/";
258 } else if (path1[0] == '?') {
259 snprintf(sanitized_path, sizeof(sanitized_path), "/%s", path1);
260 path = sanitized_path;
261 } else {
262 path = path1;
263 }
264 local_path = path;
265 if (use_proxy) {
266 /* Reassemble the request URL without auth string - we don't
267 * want to leak the auth to the proxy. */
268 ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
269 path1);
270 path = urlbuf;
271 av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
272 hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
273 }
274
275 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
276
277 if (!s->hd) {
278 err = ffurl_open_whitelist(&s->hd, buf, AVIO_FLAG_READ_WRITE,
279 &h->interrupt_callback, options,
280 h->protocol_whitelist, h->protocol_blacklist, h);
281 }
282
283 end:
284 freeenv_utf8(env_http_proxy);
285 return err < 0 ? err : http_connect(
286 h, path, local_path, hoststr, auth, proxyauth);
287 }
288
289 static int http_should_reconnect(HTTPContext *s, int err)
290 {
291 const char *status_group;
292 char http_code[4];
293
294 switch (err) {
295 case AVERROR_HTTP_BAD_REQUEST:
296 case AVERROR_HTTP_UNAUTHORIZED:
297 case AVERROR_HTTP_FORBIDDEN:
298 case AVERROR_HTTP_NOT_FOUND:
299 case AVERROR_HTTP_TOO_MANY_REQUESTS:
300 case AVERROR_HTTP_OTHER_4XX:
301 status_group = "4xx";
302 break;
303
304 case AVERROR_HTTP_SERVER_ERROR:
305 status_group = "5xx";
306 break;
307
308 default:
309 return s->reconnect_on_network_error;
310 }
311
312 if (!s->reconnect_on_http_error)
313 return 0;
314
315 if (av_match_list(status_group, s->reconnect_on_http_error, ',') > 0)
316 return 1;
317
318 snprintf(http_code, sizeof(http_code), "%d", s->http_code);
319
320 return av_match_list(http_code, s->reconnect_on_http_error, ',') > 0;
321 }
322
323 static char *redirect_cache_get(HTTPContext *s)
324 {
325 AVDictionaryEntry *re;
326 int64_t expiry;
327 char *delim;
328
329 re = av_dict_get(s->redirect_cache, s->location, NULL, AV_DICT_MATCH_CASE);
330 if (!re) {
331 return NULL;
332 }
333
334 delim = strchr(re->value, ';');
335 if (!delim) {
336 return NULL;
337 }
338
339 expiry = strtoll(re->value, NULL, 10);
340 if (time(NULL) > expiry) {
341 return NULL;
342 }
343
344 return delim + 1;
345 }
346
347 static int redirect_cache_set(HTTPContext *s, const char *source, const char *dest, int64_t expiry)
348 {
349 char *value;
350 int ret;
351
352 value = av_asprintf("%"PRIi64";%s", expiry, dest);
353 if (!value) {
354 return AVERROR(ENOMEM);
355 }
356
357 ret = av_dict_set(&s->redirect_cache, source, value, AV_DICT_MATCH_CASE | AV_DICT_DONT_STRDUP_VAL);
358 if (ret < 0)
359 return ret;
360
361 return 0;
362 }
363
364 /* return non zero if error */
365 static int http_open_cnx(URLContext *h, AVDictionary **options)
366 {
367 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
368 HTTPContext *s = h->priv_data;
369 int ret, conn_attempts = 1, auth_attempts = 0, redirects = 0;
370 int reconnect_delay = 0;
371 int reconnect_delay_total = 0;
372 uint64_t off;
373 char *cached;
374
375 redo:
376
377 cached = redirect_cache_get(s);
378 if (cached) {
379 av_free(s->location);
380 s->location = av_strdup(cached);
381 if (!s->location) {
382 ret = AVERROR(ENOMEM);
383 goto fail;
384 }
385 goto redo;
386 }
387
388 av_dict_copy(options, s->chained_options, 0);
389
390 cur_auth_type = s->auth_state.auth_type;
391 cur_proxy_auth_type = s->auth_state.auth_type;
392
393 off = s->off;
394 ret = http_open_cnx_internal(h, options);
395 if (ret < 0) {
396 if (!http_should_reconnect(s, ret) ||
397 reconnect_delay > s->reconnect_delay_max ||
398 (s->reconnect_max_retries >= 0 && conn_attempts > s->reconnect_max_retries) ||
399 reconnect_delay_total > s->reconnect_delay_total_max)
400 goto fail;
401
402 /* Both fields here are in seconds. */
403 if (s->respect_retry_after && s->retry_after > 0) {
404 reconnect_delay = s->retry_after;
405 if (reconnect_delay > s->reconnect_delay_max)
406 goto fail;
407 s->retry_after = 0;
408 }
409
410 av_log(h, AV_LOG_WARNING, "Will reconnect at %"PRIu64" in %d second(s).\n", off, reconnect_delay);
411 ret = ff_network_sleep_interruptible(1000U * 1000 * reconnect_delay, &h->interrupt_callback);
412 if (ret != AVERROR(ETIMEDOUT))
413 goto fail;
414 reconnect_delay_total += reconnect_delay;
415 reconnect_delay = 1 + 2 * reconnect_delay;
416 conn_attempts++;
417
418 /* restore the offset (http_connect resets it) */
419 s->off = off;
420
421 ffurl_closep(&s->hd);
422 goto redo;
423 }
424
425 auth_attempts++;
426 if (s->http_code == 401) {
427 if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
428 s->auth_state.auth_type != HTTP_AUTH_NONE && auth_attempts < 4) {
429 ffurl_closep(&s->hd);
430 goto redo;
431 } else
432 goto fail;
433 }
434 if (s->http_code == 407) {
435 if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
436 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && auth_attempts < 4) {
437 ffurl_closep(&s->hd);
438 goto redo;
439 } else
440 goto fail;
441 }
442 if ((s->http_code == 301 || s->http_code == 302 ||
443 s->http_code == 303 || s->http_code == 307 || s->http_code == 308) &&
444 s->new_location) {
445 /* url moved, get next */
446 ffurl_closep(&s->hd);
447 if (redirects++ >= MAX_REDIRECTS)
448 return AVERROR(EIO);
449
450 if (!s->expires) {
451 s->expires = (s->http_code == 301 || s->http_code == 308) ? INT64_MAX : -1;
452 }
453
454 if (s->expires > time(NULL) && av_dict_count(s->redirect_cache) < MAX_CACHED_REDIRECTS) {
455 redirect_cache_set(s, s->location, s->new_location, s->expires);
456 }
457
458 av_free(s->location);
459 s->location = s->new_location;
460 s->new_location = NULL;
461
462 /* Restart the authentication process with the new target, which
463 * might use a different auth mechanism. */
464 memset(&s->auth_state, 0, sizeof(s->auth_state));
465 auth_attempts = 0;
466 goto redo;
467 }
468 return 0;
469
470 fail:
471 if (s->hd)
472 ffurl_closep(&s->hd);
473 if (ret < 0)
474 return ret;
475 return ff_http_averror(s->http_code, AVERROR(EIO));
476 }
477
478 int ff_http_do_new_request(URLContext *h, const char *uri) {
479 return ff_http_do_new_request2(h, uri, NULL);
480 }
481
482 int ff_http_do_new_request2(URLContext *h, const char *uri, AVDictionary **opts)
483 {
484 HTTPContext *s = h->priv_data;
485 AVDictionary *options = NULL;
486 int ret;
487 char hostname1[1024], hostname2[1024], proto1[10], proto2[10];
488 int port1, port2;
489
490 if (!h->prot ||
491 !(!strcmp(h->prot->name, "http") ||
492 !strcmp(h->prot->name, "https")))
493 return AVERROR(EINVAL);
494
495 av_url_split(proto1, sizeof(proto1), NULL, 0,
496 hostname1, sizeof(hostname1), &port1,
497 NULL, 0, s->location);
498 av_url_split(proto2, sizeof(proto2), NULL, 0,
499 hostname2, sizeof(hostname2), &port2,
500 NULL, 0, uri);
501 if (strcmp(proto1, proto2) != 0) {
502 av_log(h, AV_LOG_INFO, "Cannot reuse HTTP connection for different protocol %s vs %s\n",
503 proto1, proto2);
504 return AVERROR(EINVAL);
505 }
506 if (port1 != port2 || strncmp(hostname1, hostname2, sizeof(hostname2)) != 0) {
507 av_log(h, AV_LOG_INFO, "Cannot reuse HTTP connection for different host: %s:%d != %s:%d\n",
508 hostname1, port1,
509 hostname2, port2
510 );
511 return AVERROR(EINVAL);
512 }
513
514 if (!s->end_chunked_post) {
515 ret = http_shutdown(h, h->flags);
516 if (ret < 0)
517 return ret;
518 }
519
520 if (s->willclose)
521 return AVERROR_EOF;
522
523 s->end_chunked_post = 0;
524 s->chunkend = 0;
525 s->off = 0;
526 s->icy_data_read = 0;
527
528 av_free(s->location);
529 s->location = av_strdup(uri);
530 if (!s->location)
531 return AVERROR(ENOMEM);
532
533 av_free(s->uri);
534 s->uri = av_strdup(uri);
535 if (!s->uri)
536 return AVERROR(ENOMEM);
537
538 if ((ret = av_opt_set_dict(s, opts)) < 0)
539 return ret;
540
541 av_log(s, AV_LOG_INFO, "Opening \'%s\' for %s\n", uri, h->flags & AVIO_FLAG_WRITE ? "writing" : "reading");
542 ret = http_open_cnx(h, &options);
543 av_dict_free(&options);
544 return ret;
545 }
546
547 int ff_http_averror(int status_code, int default_averror)
548 {
549 switch (status_code) {
550 case 400: return AVERROR_HTTP_BAD_REQUEST;
551 case 401: return AVERROR_HTTP_UNAUTHORIZED;
552 case 403: return AVERROR_HTTP_FORBIDDEN;
553 case 404: return AVERROR_HTTP_NOT_FOUND;
554 case 429: return AVERROR_HTTP_TOO_MANY_REQUESTS;
555 default: break;
556 }
557 if (status_code >= 400 && status_code <= 499)
558 return AVERROR_HTTP_OTHER_4XX;
559 else if (status_code >= 500)
560 return AVERROR_HTTP_SERVER_ERROR;
561 else
562 return default_averror;
563 }
564
565 static int http_write_reply(URLContext* h, int status_code)
566 {
567 int ret, body = 0, reply_code, message_len;
568 const char *reply_text, *content_type;
569 HTTPContext *s = h->priv_data;
570 char message[BUFFER_SIZE];
571 content_type = "text/plain";
572
573 if (status_code < 0)
574 body = 1;
575 switch (status_code) {
576 case AVERROR_HTTP_BAD_REQUEST:
577 case 400:
578 reply_code = 400;
579 reply_text = "Bad Request";
580 break;
581 case AVERROR_HTTP_FORBIDDEN:
582 case 403:
583 reply_code = 403;
584 reply_text = "Forbidden";
585 break;
586 case AVERROR_HTTP_NOT_FOUND:
587 case 404:
588 reply_code = 404;
589 reply_text = "Not Found";
590 break;
591 case AVERROR_HTTP_TOO_MANY_REQUESTS:
592 case 429:
593 reply_code = 429;
594 reply_text = "Too Many Requests";
595 break;
596 case 200:
597 reply_code = 200;
598 reply_text = "OK";
599 content_type = s->content_type ? s->content_type : "application/octet-stream";
600 break;
601 case AVERROR_HTTP_SERVER_ERROR:
602 case 500:
603 reply_code = 500;
604 reply_text = "Internal server error";
605 break;
606 default:
607 return AVERROR(EINVAL);
608 }
609 if (body) {
610 s->chunked_post = 0;
611 message_len = snprintf(message, sizeof(message),
612 "HTTP/1.1 %03d %s\r\n"
613 "Content-Type: %s\r\n"
614 "Content-Length: %"SIZE_SPECIFIER"\r\n"
615 "%s"
616 "\r\n"
617 "%03d %s\r\n",
618 reply_code,
619 reply_text,
620 content_type,
621 strlen(reply_text) + 6, // 3 digit status code + space + \r\n
622 s->headers ? s->headers : "",
623 reply_code,
624 reply_text);
625 } else {
626 s->chunked_post = 1;
627 message_len = snprintf(message, sizeof(message),
628 "HTTP/1.1 %03d %s\r\n"
629 "Content-Type: %s\r\n"
630 "Transfer-Encoding: chunked\r\n"
631 "%s"
632 "\r\n",
633 reply_code,
634 reply_text,
635 content_type,
636 s->headers ? s->headers : "");
637 }
638 av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
639 if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
640 return ret;
641 return 0;
642 }
643
644 static void handle_http_errors(URLContext *h, int error)
645 {
646 av_assert0(error < 0);
647 http_write_reply(h, error);
648 }
649
650 static int http_handshake(URLContext *c)
651 {
652 int ret, err;
653 HTTPContext *ch = c->priv_data;
654 URLContext *cl = ch->hd;
655 switch (ch->handshake_step) {
656 case LOWER_PROTO:
657 av_log(c, AV_LOG_TRACE, "Lower protocol\n");
658 if ((ret = ffurl_handshake(cl)) > 0)
659 return 2 + ret;
660 if (ret < 0)
661 return ret;
662 ch->handshake_step = READ_HEADERS;
663 ch->is_connected_server = 1;
664 return 2;
665 case READ_HEADERS:
666 av_log(c, AV_LOG_TRACE, "Read headers\n");
667 if ((err = http_read_header(c)) < 0) {
668 handle_http_errors(c, err);
669 return err;
670 }
671 ch->handshake_step = WRITE_REPLY_HEADERS;
672 return 1;
673 case WRITE_REPLY_HEADERS:
674 av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
675 if ((err = http_write_reply(c, ch->reply_code)) < 0)
676 return err;
677 ch->handshake_step = FINISH;
678 return 1;
679 case FINISH:
680 return 0;
681 }
682 // this should never be reached.
683 return AVERROR(EINVAL);
684 }
685
686 static int http_listen(URLContext *h, const char *uri, int flags,
687 AVDictionary **options) {
688 HTTPContext *s = h->priv_data;
689 int ret;
690 char hostname[1024], proto[10];
691 char lower_url[100];
692 const char *lower_proto = "tcp";
693 int port;
694 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
695 NULL, 0, uri);
696 if (!strcmp(proto, "https"))
697 lower_proto = "tls";
698 ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
699 NULL);
700 if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
701 goto fail;
702 if ((ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
703 &h->interrupt_callback, options,
704 h->protocol_whitelist, h->protocol_blacklist, h
705 )) < 0)
706 goto fail;
707 s->handshake_step = LOWER_PROTO;
708 if (s->listen == HTTP_SINGLE) { /* single client */
709 s->reply_code = 200;
710 while ((ret = http_handshake(h)) > 0);
711 }
712 fail:
713 av_dict_free(&s->chained_options);
714 av_dict_free(&s->cookie_dict);
715 return ret;
716 }
717
718 static int http_open(URLContext *h, const char *uri, int flags,
719 AVDictionary **options)
720 {
721 HTTPContext *s = h->priv_data;
722 int ret;
723
724 if( s->seekable == 1 )
725 h->is_streamed = 0;
726 else
727 h->is_streamed = 1;
728
729 s->filesize = UINT64_MAX;
730
731 s->location = av_strdup(uri);
732 if (!s->location)
733 return AVERROR(ENOMEM);
734
735 s->uri = av_strdup(uri);
736 if (!s->uri)
737 return AVERROR(ENOMEM);
738
739 if (options)
740 av_dict_copy(&s->chained_options, *options, 0);
741
742 if (s->headers) {
743 int len = strlen(s->headers);
744 if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
745 av_log(h, AV_LOG_WARNING,
746 "No trailing CRLF found in HTTP header. Adding it.\n");
747 ret = av_reallocp(&s->headers, len + 3);
748 if (ret < 0)
749 goto bail_out;
750 s->headers[len] = '\r';
751 s->headers[len + 1] = '\n';
752 s->headers[len + 2] = '\0';
753 }
754 }
755
756 if (s->listen) {
757 return http_listen(h, uri, flags, options);
758 }
759 ret = http_open_cnx(h, options);
760 bail_out:
761 if (ret < 0) {
762 av_dict_free(&s->chained_options);
763 av_dict_free(&s->cookie_dict);
764 av_dict_free(&s->redirect_cache);
765 av_freep(&s->new_location);
766 av_freep(&s->uri);
767 }
768 return ret;
769 }
770
771 static int http_accept(URLContext *s, URLContext **c)
772 {
773 int ret;
774 HTTPContext *sc = s->priv_data;
775 HTTPContext *cc;
776 URLContext *sl = sc->hd;
777 URLContext *cl = NULL;
778
779 av_assert0(sc->listen);
780 if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
781 goto fail;
782 cc = (*c)->priv_data;
783 if ((ret = ffurl_accept(sl, &cl)) < 0)
784 goto fail;
785 cc->hd = cl;
786 cc->is_multi_client = 1;
787 return 0;
788 fail:
789 if (c) {
790 ffurl_closep(c);
791 }
792 return ret;
793 }
794
795 static int http_getc(HTTPContext *s)
796 {
797 int len;
798 if (s->buf_ptr >= s->buf_end) {
799 len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
800 if (len < 0) {
801 return len;
802 } else if (len == 0) {
803 return AVERROR_EOF;
804 } else {
805 s->buf_ptr = s->buffer;
806 s->buf_end = s->buffer + len;
807 }
808 }
809 return *s->buf_ptr++;
810 }
811
812 static int http_get_line(HTTPContext *s, char *line, int line_size)
813 {
814 int ch;
815 char *q;
816
817 q = line;
818 for (;;) {
819 ch = http_getc(s);
820 if (ch < 0)
821 return ch;
822 if (ch == '\n') {
823 /* process line */
824 if (q > line && q[-1] == '\r')
825 q--;
826 *q = '\0';
827
828 return 0;
829 } else {
830 if ((q - line) < line_size - 1)
831 *q++ = ch;
832 }
833 }
834 }
835
836 static int check_http_code(URLContext *h, int http_code, const char *end)
837 {
838 HTTPContext *s = h->priv_data;
839 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
840 * don't abort until all headers have been parsed. */
841 if (http_code >= 400 && http_code < 600 &&
842 (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
843 (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
844 end += strspn(end, SPACE_CHARS);
845 av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
846 return ff_http_averror(http_code, AVERROR(EIO));
847 }
848 return 0;
849 }
850
851 static int parse_location(HTTPContext *s, const char *p)
852 {
853 char redirected_location[MAX_URL_SIZE];
854 ff_make_absolute_url(redirected_location, sizeof(redirected_location),
855 s->location, p);
856 av_freep(&s->new_location);
857 s->new_location = av_strdup(redirected_location);
858 if (!s->new_location)
859 return AVERROR(ENOMEM);
860 return 0;
861 }
862
863 /* "bytes $from-$to/$document_size" */
864 static void parse_content_range(URLContext *h, const char *p)
865 {
866 HTTPContext *s = h->priv_data;
867 const char *slash;
868
869 if (!strncmp(p, "bytes ", 6)) {
870 p += 6;
871 s->off = strtoull(p, NULL, 10);
872 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
873 s->filesize_from_content_range = strtoull(slash + 1, NULL, 10);
874 }
875 if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
876 h->is_streamed = 0; /* we _can_ in fact seek */
877 }
878
879 static int parse_content_encoding(URLContext *h, const char *p)
880 {
881 if (!av_strncasecmp(p, "gzip", 4) ||
882 !av_strncasecmp(p, "deflate", 7)) {
883 #if CONFIG_ZLIB
884 HTTPContext *s = h->priv_data;
885
886 s->compressed = 1;
887 inflateEnd(&s->inflate_stream);
888 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
889 av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
890 s->inflate_stream.msg);
891 return AVERROR(ENOSYS);
892 }
893 if (zlibCompileFlags() & (1 << 17)) {
894 av_log(h, AV_LOG_WARNING,
895 "Your zlib was compiled without gzip support.\n");
896 return AVERROR(ENOSYS);
897 }
898 #else
899 av_log(h, AV_LOG_WARNING,
900 "Compressed (%s) content, need zlib with gzip support\n", p);
901 return AVERROR(ENOSYS);
902 #endif /* CONFIG_ZLIB */
903 } else if (!av_strncasecmp(p, "identity", 8)) {
904 // The normal, no-encoding case (although servers shouldn't include
905 // the header at all if this is the case).
906 } else {
907 av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
908 }
909 return 0;
910 }
911
912 // Concat all Icy- header lines
913 static int parse_icy(HTTPContext *s, const char *tag, const char *p)
914 {
915 int len = 4 + strlen(p) + strlen(tag);
916 int is_first = !s->icy_metadata_headers;
917 int ret;
918
919 av_dict_set(&s->metadata, tag, p, 0);
920
921 if (s->icy_metadata_headers)
922 len += strlen(s->icy_metadata_headers);
923
924 if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
925 return ret;
926
927 if (is_first)
928 *s->icy_metadata_headers = '\0';
929
930 av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
931
932 return 0;
933 }
934
935 static int parse_http_date(const char *date_str, struct tm *buf)
936 {
937 char date_buf[MAX_DATE_LEN];
938 int i, j, date_buf_len = MAX_DATE_LEN-1;
939 char *date;
940
941 // strip off any punctuation or whitespace
942 for (i = 0, j = 0; date_str[i] != '\0' && j < date_buf_len; i++) {
943 if ((date_str[i] >= '0' && date_str[i] <= '9') ||
944 (date_str[i] >= 'A' && date_str[i] <= 'Z') ||
945 (date_str[i] >= 'a' && date_str[i] <= 'z')) {
946 date_buf[j] = date_str[i];
947 j++;
948 }
949 }
950 date_buf[j] = '\0';
951 date = date_buf;
952
953 // move the string beyond the day of week
954 while ((*date < '0' || *date > '9') && *date != '\0')
955 date++;
956
957 return av_small_strptime(date, "%d%b%Y%H%M%S", buf) ? 0 : AVERROR(EINVAL);
958 }
959
960 static int parse_set_cookie(const char *set_cookie, AVDictionary **dict)
961 {
962 char *param, *next_param, *cstr, *back;
963 char *saveptr = NULL;
964
965 if (!set_cookie[0])
966 return 0;
967
968 if (!(cstr = av_strdup(set_cookie)))
969 return AVERROR(EINVAL);
970
971 // strip any trailing whitespace
972 back = &cstr[strlen(cstr)-1];
973 while (strchr(WHITESPACES, *back)) {
974 *back='\0';
975 if (back == cstr)
976 break;
977 back--;
978 }
979
980 next_param = cstr;
981 while ((param = av_strtok(next_param, ";", &saveptr))) {
982 char *name, *value;
983 next_param = NULL;
984 param += strspn(param, WHITESPACES);
985 if ((name = av_strtok(param, "=", &value))) {
986 if (av_dict_set(dict, name, value, 0) < 0) {
987 av_free(cstr);
988 return -1;
989 }
990 }
991 }
992
993 av_free(cstr);
994 return 0;
995 }
996
997 static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)
998 {
999 AVDictionary *new_params = NULL;
1000 const AVDictionaryEntry *e, *cookie_entry;
1001 char *eql, *name;
1002
1003 // ensure the cookie is parsable
1004 if (parse_set_cookie(p, &new_params))
1005 return -1;
1006
1007 // if there is no cookie value there is nothing to parse
1008 cookie_entry = av_dict_iterate(new_params, NULL);
1009 if (!cookie_entry || !cookie_entry->value) {
1010 av_dict_free(&new_params);
1011 return -1;
1012 }
1013
1014 // ensure the cookie is not expired or older than an existing value
1015 if ((e = av_dict_get(new_params, "expires", NULL, 0)) && e->value) {
1016 struct tm new_tm = {0};
1017 if (!parse_http_date(e->value, &new_tm)) {
1018 AVDictionaryEntry *e2;
1019
1020 // if the cookie has already expired ignore it
1021 if (av_timegm(&new_tm) < av_gettime() / 1000000) {
1022 av_dict_free(&new_params);
1023 return 0;
1024 }
1025
1026 // only replace an older cookie with the same name
1027 e2 = av_dict_get(*cookies, cookie_entry->key, NULL, 0);
1028 if (e2 && e2->value) {
1029 AVDictionary *old_params = NULL;
1030 if (!parse_set_cookie(p, &old_params)) {
1031 e2 = av_dict_get(old_params, "expires", NULL, 0);
1032 if (e2 && e2->value) {
1033 struct tm old_tm = {0};
1034 if (!parse_http_date(e->value, &old_tm)) {
1035 if (av_timegm(&new_tm) < av_timegm(&old_tm)) {
1036 av_dict_free(&new_params);
1037 av_dict_free(&old_params);
1038 return -1;
1039 }
1040 }
1041 }
1042 }
1043 av_dict_free(&old_params);
1044 }
1045 }
1046 }
1047 av_dict_free(&new_params);
1048
1049 // duplicate the cookie name (dict will dupe the value)
1050 if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);
1051 if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);
1052
1053 // add the cookie to the dictionary
1054 av_dict_set(cookies, name, eql, AV_DICT_DONT_STRDUP_KEY);
1055
1056 return 0;
1057 }
1058
1059 static int cookie_string(AVDictionary *dict, char **cookies)
1060 {
1061 const AVDictionaryEntry *e = NULL;
1062 int len = 1;
1063
1064 // determine how much memory is needed for the cookies string
1065 while ((e = av_dict_iterate(dict, e)))
1066 len += strlen(e->key) + strlen(e->value) + 1;
1067
1068 // reallocate the cookies
1069 e = NULL;
1070 if (*cookies) av_free(*cookies);
1071 *cookies = av_malloc(len);
1072 if (!*cookies) return AVERROR(ENOMEM);
1073 *cookies[0] = '\0';
1074
1075 // write out the cookies
1076 while ((e = av_dict_iterate(dict, e)))
1077 av_strlcatf(*cookies, len, "%s%s\n", e->key, e->value);
1078
1079 return 0;
1080 }
1081
1082 static void parse_expires(HTTPContext *s, const char *p)
1083 {
1084 struct tm tm;
1085
1086 if (!parse_http_date(p, &tm)) {
1087 s->expires = av_timegm(&tm);
1088 }
1089 }
1090
1091 static void parse_cache_control(HTTPContext *s, const char *p)
1092 {
1093 char *age;
1094 int offset;
1095
1096 /* give 'Expires' higher priority over 'Cache-Control' */
1097 if (s->expires) {
1098 return;
1099 }
1100
1101 if (av_stristr(p, "no-cache") || av_stristr(p, "no-store")) {
1102 s->expires = -1;
1103 return;
1104 }
1105
1106 age = av_stristr(p, "s-maxage=");
1107 offset = 9;
1108 if (!age) {
1109 age = av_stristr(p, "max-age=");
1110 offset = 8;
1111 }
1112
1113 if (age) {
1114 s->expires = time(NULL) + atoi(p + offset);
1115 }
1116 }
1117
1118 static int process_line(URLContext *h, char *line, int line_count, int *parsed_http_code)
1119 {
1120 HTTPContext *s = h->priv_data;
1121 const char *auto_method = h->flags & AVIO_FLAG_READ ? "POST" : "GET";
1122 char *tag, *p, *end, *method, *resource, *version;
1123 int ret;
1124
1125 /* end of header */
1126 if (line[0] == '\0') {
1127 s->end_header = 1;
1128 return 0;
1129 }
1130
1131 p = line;
1132 if (line_count == 0) {
1133 if (s->is_connected_server) {
1134 // HTTP method
1135 method = p;
1136 while (*p && !av_isspace(*p))
1137 p++;
1138 *(p++) = '\0';
1139 av_log(h, AV_LOG_TRACE, "Received method: %s\n", method);
1140 if (s->method) {
1141 if (av_strcasecmp(s->method, method)) {
1142 av_log(h, AV_LOG_ERROR, "Received and expected HTTP method do not match. (%s expected, %s received)\n",
1143 s->method, method);
1144 return ff_http_averror(400, AVERROR(EIO));
1145 }
1146 } else {
1147 // use autodetected HTTP method to expect
1148 av_log(h, AV_LOG_TRACE, "Autodetected %s HTTP method\n", auto_method);
1149 if (av_strcasecmp(auto_method, method)) {
1150 av_log(h, AV_LOG_ERROR, "Received and autodetected HTTP method did not match "
1151 "(%s autodetected %s received)\n", auto_method, method);
1152 return ff_http_averror(400, AVERROR(EIO));
1153 }
1154 if (!(s->method = av_strdup(method)))
1155 return AVERROR(ENOMEM);
1156 }
1157
1158 // HTTP resource
1159 while (av_isspace(*p))
1160 p++;
1161 resource = p;
1162 while (*p && !av_isspace(*p))
1163 p++;
1164 *(p++) = '\0';
1165 av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
1166 if (!(s->resource = av_strdup(resource)))
1167 return AVERROR(ENOMEM);
1168
1169 // HTTP version
1170 while (av_isspace(*p))
1171 p++;
1172 version = p;
1173 while (*p && !av_isspace(*p))
1174 p++;
1175 *p = '\0';
1176 if (av_strncasecmp(version, "HTTP/", 5)) {
1177 av_log(h, AV_LOG_ERROR, "Malformed HTTP version string.\n");
1178 return ff_http_averror(400, AVERROR(EIO));
1179 }
1180 av_log(h, AV_LOG_TRACE, "HTTP version string: %s\n", version);
1181 } else {
1182 if (av_strncasecmp(p, "HTTP/1.0", 8) == 0)
1183 s->willclose = 1;
1184 while (*p != '/' && *p != '\0')
1185 p++;
1186 while (*p == '/')
1187 p++;
1188 av_freep(&s->http_version);
1189 s->http_version = av_strndup(p, 3);
1190 while (!av_isspace(*p) && *p != '\0')
1191 p++;
1192 while (av_isspace(*p))
1193 p++;
1194 s->http_code = strtol(p, &end, 10);
1195
1196 av_log(h, AV_LOG_TRACE, "http_code=%d\n", s->http_code);
1197
1198 *parsed_http_code = 1;
1199
1200 if ((ret = check_http_code(h, s->http_code, end)) < 0)
1201 return ret;
1202 }
1203 } else {
1204 while (*p != '\0' && *p != ':')
1205 p++;
1206 if (*p != ':')
1207 return 1;
1208
1209 *p = '\0';
1210 tag = line;
1211 p++;
1212 while (av_isspace(*p))
1213 p++;
1214 if (!av_strcasecmp(tag, "Location")) {
1215 if ((ret = parse_location(s, p)) < 0)
1216 return ret;
1217 } else if (!av_strcasecmp(tag, "Content-Length") &&
1218 s->filesize == UINT64_MAX) {
1219 s->filesize = strtoull(p, NULL, 10);
1220 } else if (!av_strcasecmp(tag, "Content-Range")) {
1221 parse_content_range(h, p);
1222 } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
1223 !strncmp(p, "bytes", 5) &&
1224 s->seekable == -1) {
1225 h->is_streamed = 0;
1226 } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
1227 !av_strncasecmp(p, "chunked", 7)) {
1228 s->filesize = UINT64_MAX;
1229 s->chunksize = 0;
1230 } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
1231 ff_http_auth_handle_header(&s->auth_state, tag, p);
1232 } else if (!av_strcasecmp(tag, "Authentication-Info")) {
1233 ff_http_auth_handle_header(&s->auth_state, tag, p);
1234 } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
1235 ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
1236 } else if (!av_strcasecmp(tag, "Connection")) {
1237 if (!strcmp(p, "close"))
1238 s->willclose = 1;
1239 } else if (!av_strcasecmp(tag, "Server")) {
1240 if (!av_strcasecmp(p, "AkamaiGHost")) {
1241 s->is_akamai = 1;
1242 } else if (!av_strncasecmp(p, "MediaGateway", 12)) {
1243 s->is_mediagateway = 1;
1244 }
1245 } else if (!av_strcasecmp(tag, "Content-Type")) {
1246 av_free(s->mime_type);
1247 s->mime_type = av_get_token((const char **)&p, ";");
1248 } else if (!av_strcasecmp(tag, "Set-Cookie")) {
1249 if (parse_cookie(s, p, &s->cookie_dict))
1250 av_log(h, AV_LOG_WARNING, "Unable to parse '%s'\n", p);
1251 } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
1252 s->icy_metaint = strtoull(p, NULL, 10);
1253 } else if (!av_strncasecmp(tag, "Icy-", 4)) {
1254 if ((ret = parse_icy(s, tag, p)) < 0)
1255 return ret;
1256 } else if (!av_strcasecmp(tag, "Content-Encoding")) {
1257 if ((ret = parse_content_encoding(h, p)) < 0)
1258 return ret;
1259 } else if (!av_strcasecmp(tag, "Expires")) {
1260 parse_expires(s, p);
1261 } else if (!av_strcasecmp(tag, "Cache-Control")) {
1262 parse_cache_control(s, p);
1263 } else if (!av_strcasecmp(tag, "Retry-After")) {
1264 /* The header can be either an integer that represents seconds, or a date. */
1265 struct tm tm;
1266 int date_ret = parse_http_date(p, &tm);
1267 if (!date_ret) {
1268 time_t retry = av_timegm(&tm);
1269 int64_t now = av_gettime() / 1000000;
1270 int64_t diff = ((int64_t) retry) - now;
1271 s->retry_after = (unsigned int) FFMAX(0, diff);
1272 } else {
1273 s->retry_after = strtoul(p, NULL, 10);
1274 }
1275 }
1276 }
1277 return 1;
1278 }
1279
1280 /**
1281 * Create a string containing cookie values for use as a HTTP cookie header
1282 * field value for a particular path and domain from the cookie values stored in
1283 * the HTTP protocol context. The cookie string is stored in *cookies, and may
1284 * be NULL if there are no valid cookies.
1285 *
1286 * @return a negative value if an error condition occurred, 0 otherwise
1287 */
1288 static int get_cookies(HTTPContext *s, char **cookies, const char *path,
1289 const char *domain)
1290 {
1291 // cookie strings will look like Set-Cookie header field values. Multiple
1292 // Set-Cookie fields will result in multiple values delimited by a newline
1293 int ret = 0;
1294 char *cookie, *set_cookies, *next;
1295 char *saveptr = NULL;
1296
1297 // destroy any cookies in the dictionary.
1298 av_dict_free(&s->cookie_dict);
1299
1300 if (!s->cookies)
1301 return 0;
1302
1303 next = set_cookies = av_strdup(s->cookies);
1304 if (!next)
1305 return AVERROR(ENOMEM);
1306
1307 *cookies = NULL;
1308 while ((cookie = av_strtok(next, "\n", &saveptr)) && !ret) {
1309 AVDictionary *cookie_params = NULL;
1310 const AVDictionaryEntry *cookie_entry, *e;
1311
1312 next = NULL;
1313 // store the cookie in a dict in case it is updated in the response
1314 if (parse_cookie(s, cookie, &s->cookie_dict))
1315 av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);
1316
1317 // continue on to the next cookie if this one cannot be parsed
1318 if (parse_set_cookie(cookie, &cookie_params))
1319 goto skip_cookie;
1320
1321 // if the cookie has no value, skip it
1322 cookie_entry = av_dict_iterate(cookie_params, NULL);
1323 if (!cookie_entry || !cookie_entry->value)
1324 goto skip_cookie;
1325
1326 // if the cookie has expired, don't add it
1327 if ((e = av_dict_get(cookie_params, "expires", NULL, 0)) && e->value) {
1328 struct tm tm_buf = {0};
1329 if (!parse_http_date(e->value, &tm_buf)) {
1330 if (av_timegm(&tm_buf) < av_gettime() / 1000000)
1331 goto skip_cookie;
1332 }
1333 }
1334
1335 // if no domain in the cookie assume it appied to this request
1336 if ((e = av_dict_get(cookie_params, "domain", NULL, 0)) && e->value) {
1337 // find the offset comparison is on the min domain (b.com, not a.b.com)
1338 int domain_offset = strlen(domain) - strlen(e->value);
1339 if (domain_offset < 0)
1340 goto skip_cookie;
1341
1342 // match the cookie domain
1343 if (av_strcasecmp(&domain[domain_offset], e->value))
1344 goto skip_cookie;
1345 }
1346
1347 // if a cookie path is provided, ensure the request path is within that path
1348 e = av_dict_get(cookie_params, "path", NULL, 0);
1349 if (e && av_strncasecmp(path, e->value, strlen(e->value)))
1350 goto skip_cookie;
1351
1352 // cookie parameters match, so copy the value
1353 if (!*cookies) {
1354 *cookies = av_asprintf("%s=%s", cookie_entry->key, cookie_entry->value);
1355 } else {
1356 char *tmp = *cookies;
1357 *cookies = av_asprintf("%s; %s=%s", tmp, cookie_entry->key, cookie_entry->value);
1358 av_free(tmp);
1359 }
1360 if (!*cookies)
1361 ret = AVERROR(ENOMEM);
1362
1363 skip_cookie:
1364 av_dict_free(&cookie_params);
1365 }
1366
1367 av_free(set_cookies);
1368
1369 return ret;
1370 }
1371
1372 static inline int has_header(const char *str, const char *header)
1373 {
1374 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
1375 if (!str)
1376 return 0;
1377 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
1378 }
1379
1380 static int http_read_header(URLContext *h)
1381 {
1382 HTTPContext *s = h->priv_data;
1383 char line[MAX_URL_SIZE];
1384 int err = 0, http_err = 0;
1385
1386 av_freep(&s->new_location);
1387 s->expires = 0;
1388 s->chunksize = UINT64_MAX;
1389 s->filesize_from_content_range = UINT64_MAX;
1390
1391 for (;;) {
1392 int parsed_http_code = 0;
1393
1394 if ((err = http_get_line(s, line, sizeof(line))) < 0)
1395 return err;
1396
1397 av_log(h, AV_LOG_TRACE, "header='%s'\n", line);
1398
1399 err = process_line(h, line, s->line_count, &parsed_http_code);
1400 if (err < 0) {
1401 if (parsed_http_code) {
1402 http_err = err;
1403 } else {
1404 /* Prefer to return HTTP code error if we've already seen one. */
1405 if (http_err)
1406 return http_err;
1407 else
1408 return err;
1409 }
1410 }
1411 if (err == 0)
1412 break;
1413 s->line_count++;
1414 }
1415 if (http_err)
1416 return http_err;
1417
1418 // filesize from Content-Range can always be used, even if using chunked Transfer-Encoding
1419 if (s->filesize_from_content_range != UINT64_MAX)
1420 s->filesize = s->filesize_from_content_range;
1421
1422 if (s->seekable == -1 && s->is_mediagateway && s->filesize == 2000000000)
1423 h->is_streamed = 1; /* we can in fact _not_ seek */
1424
1425 // add any new cookies into the existing cookie string
1426 cookie_string(s->cookie_dict, &s->cookies);
1427 av_dict_free(&s->cookie_dict);
1428
1429 return err;
1430 }
1431
1432 /**
1433 * Escape unsafe characters in path in order to pass them safely to the HTTP
1434 * request. Insipred by the algorithm in GNU wget:
1435 * - escape "%" characters not followed by two hex digits
1436 * - escape all "unsafe" characters except which are also "reserved"
1437 * - pass through everything else
1438 */
1439 static void bprint_escaped_path(AVBPrint *bp, const char *path)
1440 {
1441 #define NEEDS_ESCAPE(ch) \
1442 ((ch) <= ' ' || (ch) >= '\x7f' || \
1443 (ch) == '"' || (ch) == '%' || (ch) == '<' || (ch) == '>' || (ch) == '\\' || \
1444 (ch) == '^' || (ch) == '`' || (ch) == '{' || (ch) == '}' || (ch) == '|')
1445 while (*path) {
1446 char buf[1024];
1447 char *q = buf;
1448 while (*path && q - buf < sizeof(buf) - 4) {
1449 if (path[0] == '%' && av_isxdigit(path[1]) && av_isxdigit(path[2])) {
1450 *q++ = *path++;
1451 *q++ = *path++;
1452 *q++ = *path++;
1453 } else if (NEEDS_ESCAPE(*path)) {
1454 q += snprintf(q, 4, "%%%02X", (uint8_t)*path++);
1455 } else {
1456 *q++ = *path++;
1457 }
1458 }
1459 av_bprint_append_data(bp, buf, q - buf);
1460 }
1461 }
1462
1463 static int http_connect(URLContext *h, const char *path, const char *local_path,
1464 const char *hoststr, const char *auth,
1465 const char *proxyauth)
1466 {
1467 HTTPContext *s = h->priv_data;
1468 int post, err;
1469 AVBPrint request;
1470 char *authstr = NULL, *proxyauthstr = NULL;
1471 uint64_t off = s->off;
1472 const char *method;
1473 int send_expect_100 = 0;
1474
1475 av_bprint_init_for_buffer(&request, s->buffer, sizeof(s->buffer));
1476
1477 /* send http header */
1478 post = h->flags & AVIO_FLAG_WRITE;
1479
1480 if (s->post_data) {
1481 /* force POST method and disable chunked encoding when
1482 * custom HTTP post data is set */
1483 post = 1;
1484 s->chunked_post = 0;
1485 }
1486
1487 if (s->method)
1488 method = s->method;
1489 else
1490 method = post ? "POST" : "GET";
1491
1492 authstr = ff_http_auth_create_response(&s->auth_state, auth,
1493 local_path, method);
1494 proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
1495 local_path, method);
1496
1497 if (post && !s->post_data) {
1498 if (s->send_expect_100 != -1) {
1499 send_expect_100 = s->send_expect_100;
1500 } else {
1501 send_expect_100 = 0;
1502 /* The user has supplied authentication but we don't know the auth type,
1503 * send Expect: 100-continue to get the 401 response including the
1504 * WWW-Authenticate header, or an 100 continue if no auth actually
1505 * is needed. */
1506 if (auth && *auth &&
1507 s->auth_state.auth_type == HTTP_AUTH_NONE &&
1508 s->http_code != 401)
1509 send_expect_100 = 1;
1510 }
1511 }
1512
1513 av_bprintf(&request, "%s ", method);
1514 bprint_escaped_path(&request, path);
1515 av_bprintf(&request, " HTTP/1.1\r\n");
1516
1517 if (post && s->chunked_post)
1518 av_bprintf(&request, "Transfer-Encoding: chunked\r\n");
1519 /* set default headers if needed */
1520 if (!has_header(s->headers, "\r\nUser-Agent: "))
1521 av_bprintf(&request, "User-Agent: %s\r\n", s->user_agent);
1522 if (s->referer) {
1523 /* set default headers if needed */
1524 if (!has_header(s->headers, "\r\nReferer: "))
1525 av_bprintf(&request, "Referer: %s\r\n", s->referer);
1526 }
1527 if (!has_header(s->headers, "\r\nAccept: "))
1528 av_bprintf(&request, "Accept: */*\r\n");
1529 // Note: we send the Range header on purpose, even when we're probing,
1530 // since it allows us to detect more reliably if a (non-conforming)
1531 // server supports seeking by analysing the reply headers.
1532 if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable != 0)) {
1533 av_bprintf(&request, "Range: bytes=%"PRIu64"-", s->off);
1534 if (s->end_off)
1535 av_bprintf(&request, "%"PRId64, s->end_off - 1);
1536 av_bprintf(&request, "\r\n");
1537 }
1538 if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
1539 av_bprintf(&request, "Expect: 100-continue\r\n");
1540
1541 if (!has_header(s->headers, "\r\nConnection: "))
1542 av_bprintf(&request, "Connection: %s\r\n", s->multiple_requests ? "keep-alive" : "close");
1543
1544 if (!has_header(s->headers, "\r\nHost: "))
1545 av_bprintf(&request, "Host: %s\r\n", hoststr);
1546 if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
1547 av_bprintf(&request, "Content-Length: %d\r\n", s->post_datalen);
1548
1549 if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
1550 av_bprintf(&request, "Content-Type: %s\r\n", s->content_type);
1551 if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
1552 char *cookies = NULL;
1553 if (!get_cookies(s, &cookies, path, hoststr) && cookies) {
1554 av_bprintf(&request, "Cookie: %s\r\n", cookies);
1555 av_free(cookies);
1556 }
1557 }
1558 if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
1559 av_bprintf(&request, "Icy-MetaData: 1\r\n");
1560
1561 /* now add in custom headers */
1562 if (s->headers)
1563 av_bprintf(&request, "%s", s->headers);
1564
1565 if (authstr)
1566 av_bprintf(&request, "%s", authstr);
1567 if (proxyauthstr)
1568 av_bprintf(&request, "Proxy-%s", proxyauthstr);
1569 av_bprintf(&request, "\r\n");
1570
1571 av_log(h, AV_LOG_DEBUG, "request: %s\n", request.str);
1572
1573 if (!av_bprint_is_complete(&request)) {
1574 av_log(h, AV_LOG_ERROR, "overlong headers\n");
1575 err = AVERROR(EINVAL);
1576 goto done;
1577 }
1578
1579 if ((err = ffurl_write(s->hd, request.str, request.len)) < 0)
1580 goto done;
1581
1582 if (s->post_data)
1583 if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
1584 goto done;
1585
1586 /* init input buffer */
1587 s->buf_ptr = s->buffer;
1588 s->buf_end = s->buffer;
1589 s->line_count = 0;
1590 s->off = 0;
1591 s->icy_data_read = 0;
1592 s->filesize = UINT64_MAX;
1593 s->willclose = 0;
1594 s->end_chunked_post = 0;
1595 s->end_header = 0;
1596 #if CONFIG_ZLIB
1597 s->compressed = 0;
1598 #endif
1599 if (post && !s->post_data && !send_expect_100) {
1600 /* Pretend that it did work. We didn't read any header yet, since
1601 * we've still to send the POST data, but the code calling this
1602 * function will check http_code after we return. */
1603 s->http_code = 200;
1604 err = 0;
1605 goto done;
1606 }
1607
1608 /* wait for header */
1609 err = http_read_header(h);
1610 if (err < 0)
1611 goto done;
1612
1613 if (s->new_location)
1614 s->off = off;
1615
1616 err = (off == s->off) ? 0 : -1;
1617 done:
1618 av_freep(&authstr);
1619 av_freep(&proxyauthstr);
1620 return err;
1621 }
1622
1623 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
1624 {
1625 HTTPContext *s = h->priv_data;
1626 int len;
1627
1628 if (s->chunksize != UINT64_MAX) {
1629 if (s->chunkend) {
1630 return AVERROR_EOF;
1631 }
1632 if (!s->chunksize) {
1633 char line[32];
1634 int err;
1635
1636 do {
1637 if ((err = http_get_line(s, line, sizeof(line))) < 0)
1638 return err;
1639 } while (!*line); /* skip CR LF from last chunk */
1640
1641 s->chunksize = strtoull(line, NULL, 16);
1642
1643 av_log(h, AV_LOG_TRACE,
1644 "Chunked encoding data size: %"PRIu64"\n",
1645 s->chunksize);
1646
1647 if (!s->chunksize && s->multiple_requests) {
1648 http_get_line(s, line, sizeof(line)); // read empty chunk
1649 s->chunkend = 1;
1650 return 0;
1651 }
1652 else if (!s->chunksize) {
1653 av_log(h, AV_LOG_DEBUG, "Last chunk received, closing conn\n");
1654 ffurl_closep(&s->hd);
1655 return 0;
1656 }
1657 else if (s->chunksize == UINT64_MAX) {
1658 av_log(h, AV_LOG_ERROR, "Invalid chunk size %"PRIu64"\n",
1659 s->chunksize);
1660 return AVERROR(EINVAL);
1661 }
1662 }
1663 size = FFMIN(size, s->chunksize);
1664 }
1665
1666 /* read bytes from input buffer first */
1667 len = s->buf_end - s->buf_ptr;
1668 if (len > 0) {
1669 if (len > size)
1670 len = size;
1671 memcpy(buf, s->buf_ptr, len);
1672 s->buf_ptr += len;
1673 } else {
1674 uint64_t target_end = s->end_off ? s->end_off : s->filesize;
1675 if ((!s->willclose || s->chunksize == UINT64_MAX) && s->off >= target_end)
1676 return AVERROR_EOF;
1677 len = ffurl_read(s->hd, buf, size);
1678 if ((!len || len == AVERROR_EOF) &&
1679 (!s->willclose || s->chunksize == UINT64_MAX) && s->off < target_end) {
1680 av_log(h, AV_LOG_ERROR,
1681 "Stream ends prematurely at %"PRIu64", should be %"PRIu64"\n",
1682 s->off, target_end
1683 );
1684 return AVERROR(EIO);
1685 }
1686 }
1687 if (len > 0) {
1688 s->off += len;
1689 if (s->chunksize > 0 && s->chunksize != UINT64_MAX) {
1690 av_assert0(s->chunksize >= len);
1691 s->chunksize -= len;
1692 }
1693 }
1694 return len;
1695 }
1696
1697 #if CONFIG_ZLIB
1698 #define DECOMPRESS_BUF_SIZE (256 * 1024)
1699 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
1700 {
1701 HTTPContext *s = h->priv_data;
1702 int ret;
1703
1704 if (!s->inflate_buffer) {
1705 s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
1706 if (!s->inflate_buffer)
1707 return AVERROR(ENOMEM);
1708 }
1709
1710 if (s->inflate_stream.avail_in == 0) {
1711 int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
1712 if (read <= 0)
1713 return read;
1714 s->inflate_stream.next_in = s->inflate_buffer;
1715 s->inflate_stream.avail_in = read;
1716 }
1717
1718 s->inflate_stream.avail_out = size;
1719 s->inflate_stream.next_out = buf;
1720
1721 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
1722 if (ret != Z_OK && ret != Z_STREAM_END)
1723 av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
1724 ret, s->inflate_stream.msg);
1725
1726 return size - s->inflate_stream.avail_out;
1727 }
1728 #endif /* CONFIG_ZLIB */
1729
1730 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect);
1731
1732 static int http_read_stream(URLContext *h, uint8_t *buf, int size)
1733 {
1734 HTTPContext *s = h->priv_data;
1735 int err, read_ret;
1736 int64_t seek_ret;
1737 int reconnect_delay = 0;
1738 int reconnect_delay_total = 0;
1739 int conn_attempts = 1;
1740
1741 if (!s->hd)
1742 return AVERROR_EOF;
1743
1744 if (s->end_chunked_post && !s->end_header) {
1745 err = http_read_header(h);
1746 if (err < 0)
1747 return err;
1748 }
1749
1750 #if CONFIG_ZLIB
1751 if (s->compressed)
1752 return http_buf_read_compressed(h, buf, size);
1753 #endif /* CONFIG_ZLIB */
1754 read_ret = http_buf_read(h, buf, size);
1755 while (read_ret < 0) {
1756 uint64_t target = h->is_streamed ? 0 : s->off;
1757 bool is_premature = s->filesize > 0 && s->off < s->filesize;
1758
1759 if (read_ret == AVERROR_EXIT)
1760 break;
1761
1762 if (h->is_streamed && !s->reconnect_streamed)
1763 break;
1764
1765 if (!(s->reconnect && is_premature) &&
1766 !(s->reconnect_at_eof && read_ret == AVERROR_EOF)) {
1767 if (is_premature)
1768 return AVERROR(EIO);
1769 else
1770 break;
1771 }
1772
1773 if (reconnect_delay > s->reconnect_delay_max || (s->reconnect_max_retries >= 0 && conn_attempts > s->reconnect_max_retries) ||
1774 reconnect_delay_total > s->reconnect_delay_total_max)
1775 return AVERROR(EIO);
1776
1777 av_log(h, AV_LOG_WARNING, "Will reconnect at %"PRIu64" in %d second(s), error=%s.\n", s->off, reconnect_delay, av_err2str(read_ret));
1778 err = ff_network_sleep_interruptible(1000U*1000*reconnect_delay, &h->interrupt_callback);
1779 if (err != AVERROR(ETIMEDOUT))
1780 return err;
1781 reconnect_delay_total += reconnect_delay;
1782 reconnect_delay = 1 + 2*reconnect_delay;
1783 conn_attempts++;
1784 seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
1785 if (seek_ret >= 0 && seek_ret != target) {
1786 av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRIu64".\n", target);
1787 return read_ret;
1788 }
1789
1790 read_ret = http_buf_read(h, buf, size);
1791 }
1792
1793 return read_ret;
1794 }
1795
1796 // Like http_read_stream(), but no short reads.
1797 // Assumes partial reads are an error.
1798 static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
1799 {
1800 int pos = 0;
1801 while (pos < size) {
1802 int len = http_read_stream(h, buf + pos, size - pos);
1803 if (len < 0)
1804 return len;
1805 pos += len;
1806 }
1807 return pos;
1808 }
1809
1810 static void update_metadata(URLContext *h, char *data)
1811 {
1812 char *key;
1813 char *val;
1814 char *end;
1815 char *next = data;
1816 HTTPContext *s = h->priv_data;
1817
1818 while (*next) {
1819 key = next;
1820 val = strstr(key, "='");
1821 if (!val)
1822 break;
1823 end = strstr(val, "';");
1824 if (!end)
1825 break;
1826
1827 *val = '\0';
1828 *end = '\0';
1829 val += 2;
1830
1831 av_dict_set(&s->metadata, key, val, 0);
1832 av_log(h, AV_LOG_VERBOSE, "Metadata update for %s: %s\n", key, val);
1833
1834 next = end + 2;
1835 }
1836 }
1837
1838 static int store_icy(URLContext *h, int size)
1839 {
1840 HTTPContext *s = h->priv_data;
1841 /* until next metadata packet */
1842 uint64_t remaining;
1843
1844 if (s->icy_metaint < s->icy_data_read)
1845 return AVERROR_INVALIDDATA;
1846 remaining = s->icy_metaint - s->icy_data_read;
1847
1848 if (!remaining) {
1849 /* The metadata packet is variable sized. It has a 1 byte header
1850 * which sets the length of the packet (divided by 16). If it's 0,
1851 * the metadata doesn't change. After the packet, icy_metaint bytes
1852 * of normal data follows. */
1853 uint8_t ch;
1854 int len = http_read_stream_all(h, &ch, 1);
1855 if (len < 0)
1856 return len;
1857 if (ch > 0) {
1858 char data[255 * 16 + 1];
1859 int ret;
1860 len = ch * 16;
1861 ret = http_read_stream_all(h, data, len);
1862 if (ret < 0)
1863 return ret;
1864 data[len + 1] = 0;
1865 if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
1866 return ret;
1867 update_metadata(h, data);
1868 }
1869 s->icy_data_read = 0;
1870 remaining = s->icy_metaint;
1871 }
1872
1873 return FFMIN(size, remaining);
1874 }
1875
1876 static int http_read(URLContext *h, uint8_t *buf, int size)
1877 {
1878 HTTPContext *s = h->priv_data;
1879
1880 if (s->icy_metaint > 0) {
1881 size = store_icy(h, size);
1882 if (size < 0)
1883 return size;
1884 }
1885
1886 size = http_read_stream(h, buf, size);
1887 if (size > 0)
1888 s->icy_data_read += size;
1889 return size;
1890 }
1891
1892 /* used only when posting data */
1893 static int http_write(URLContext *h, const uint8_t *buf, int size)
1894 {
1895 char temp[11] = ""; /* 32-bit hex + CRLF + nul */
1896 int ret;
1897 char crlf[] = "\r\n";
1898 HTTPContext *s = h->priv_data;
1899
1900 if (!s->chunked_post) {
1901 /* non-chunked data is sent without any special encoding */
1902 return ffurl_write(s->hd, buf, size);
1903 }
1904
1905 /* silently ignore zero-size data since chunk encoding that would
1906 * signal EOF */
1907 if (size > 0) {
1908 /* upload data using chunked encoding */
1909 snprintf(temp, sizeof(temp), "%x\r\n", size);
1910
1911 if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
1912 (ret = ffurl_write(s->hd, buf, size)) < 0 ||
1913 (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
1914 return ret;
1915 }
1916 return size;
1917 }
1918
1919 static int http_shutdown(URLContext *h, int flags)
1920 {
1921 int ret = 0;
1922 char footer[] = "0\r\n\r\n";
1923 HTTPContext *s = h->priv_data;
1924
1925 /* signal end of chunked encoding if used */
1926 if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
1927 ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
1928 ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
1929 ret = ret > 0 ? 0 : ret;
1930 /* flush the receive buffer when it is write only mode */
1931 if (!(flags & AVIO_FLAG_READ)) {
1932 char buf[1024];
1933 int read_ret;
1934 s->hd->flags |= AVIO_FLAG_NONBLOCK;
1935 read_ret = ffurl_read(s->hd, buf, sizeof(buf));
1936 s->hd->flags &= ~AVIO_FLAG_NONBLOCK;
1937 if (read_ret < 0 && read_ret != AVERROR(EAGAIN)) {
1938 av_log(h, AV_LOG_ERROR, "URL read error: %s\n", av_err2str(read_ret));
1939 ret = read_ret;
1940 }
1941 }
1942 s->end_chunked_post = 1;
1943 }
1944
1945 return ret;
1946 }
1947
1948 static int http_close(URLContext *h)
1949 {
1950 int ret = 0;
1951 HTTPContext *s = h->priv_data;
1952
1953 #if CONFIG_ZLIB
1954 inflateEnd(&s->inflate_stream);
1955 av_freep(&s->inflate_buffer);
1956 #endif /* CONFIG_ZLIB */
1957
1958 if (s->hd && !s->end_chunked_post)
1959 /* Close the write direction by sending the end of chunked encoding. */
1960 ret = http_shutdown(h, h->flags);
1961
1962 if (s->hd)
1963 ffurl_closep(&s->hd);
1964 av_dict_free(&s->chained_options);
1965 av_dict_free(&s->cookie_dict);
1966 av_dict_free(&s->redirect_cache);
1967 av_freep(&s->new_location);
1968 av_freep(&s->uri);
1969 return ret;
1970 }
1971
1972 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
1973 {
1974 HTTPContext *s = h->priv_data;
1975 URLContext *old_hd = s->hd;
1976 uint64_t old_off = s->off;
1977 uint8_t old_buf[BUFFER_SIZE];
1978 int old_buf_size, ret;
1979 AVDictionary *options = NULL;
1980
1981 if (whence == AVSEEK_SIZE)
1982 return s->filesize;
1983 else if (!force_reconnect &&
1984 ((whence == SEEK_CUR && off == 0) ||
1985 (whence == SEEK_SET && off == s->off)))
1986 return s->off;
1987 else if ((s->filesize == UINT64_MAX && whence == SEEK_END))
1988 return AVERROR(ENOSYS);
1989
1990 if (whence == SEEK_CUR)
1991 off += s->off;
1992 else if (whence == SEEK_END)
1993 off += s->filesize;
1994 else if (whence != SEEK_SET)
1995 return AVERROR(EINVAL);
1996 if (off < 0)
1997 return AVERROR(EINVAL);
1998 s->off = off;
1999
2000 if (s->off && h->is_streamed)
2001 return AVERROR(ENOSYS);
2002
2003 /* do not try to make a new connection if seeking past the end of the file */
2004 if (s->end_off || s->filesize != UINT64_MAX) {
2005 uint64_t end_pos = s->end_off ? s->end_off : s->filesize;
2006 if (s->off >= end_pos)
2007 return s->off;
2008 }
2009
2010 /* if the location changed (redirect), revert to the original uri */
2011 if (strcmp(s->uri, s->location)) {
2012 char *new_uri;
2013 new_uri = av_strdup(s->uri);
2014 if (!new_uri)
2015 return AVERROR(ENOMEM);
2016 av_free(s->location);
2017 s->location = new_uri;
2018 }
2019
2020 /* we save the old context in case the seek fails */
2021 old_buf_size = s->buf_end - s->buf_ptr;
2022 memcpy(old_buf, s->buf_ptr, old_buf_size);
2023 s->hd = NULL;
2024
2025 /* if it fails, continue on old connection */
2026 if ((ret = http_open_cnx(h, &options)) < 0) {
2027 av_dict_free(&options);
2028 memcpy(s->buffer, old_buf, old_buf_size);
2029 s->buf_ptr = s->buffer;
2030 s->buf_end = s->buffer + old_buf_size;
2031 s->hd = old_hd;
2032 s->off = old_off;
2033 return ret;
2034 }
2035 av_dict_free(&options);
2036 ffurl_close(old_hd);
2037 return off;
2038 }
2039
2040 static int64_t http_seek(URLContext *h, int64_t off, int whence)
2041 {
2042 return http_seek_internal(h, off, whence, 0);
2043 }
2044
2045 static int http_get_file_handle(URLContext *h)
2046 {
2047 HTTPContext *s = h->priv_data;
2048 return ffurl_get_file_handle(s->hd);
2049 }
2050
2051 static int http_get_short_seek(URLContext *h)
2052 {
2053 HTTPContext *s = h->priv_data;
2054 if (s->short_seek_size >= 1)
2055 return s->short_seek_size;
2056 return ffurl_get_short_seek(s->hd);
2057 }
2058
2059 #define HTTP_CLASS(flavor) \
2060 static const AVClass flavor ## _context_class = { \
2061 .class_name = # flavor, \
2062 .item_name = av_default_item_name, \
2063 .option = options, \
2064 .version = LIBAVUTIL_VERSION_INT, \
2065 }
2066
2067 #if CONFIG_HTTP_PROTOCOL
2068 HTTP_CLASS(http);
2069
2070 const URLProtocol ff_http_protocol = {
2071 .name = "http",
2072 .url_open2 = http_open,
2073 .url_accept = http_accept,
2074 .url_handshake = http_handshake,
2075 .url_read = http_read,
2076 .url_write = http_write,
2077 .url_seek = http_seek,
2078 .url_close = http_close,
2079 .url_get_file_handle = http_get_file_handle,
2080 .url_get_short_seek = http_get_short_seek,
2081 .url_shutdown = http_shutdown,
2082 .priv_data_size = sizeof(HTTPContext),
2083 .priv_data_class = &http_context_class,
2084 .flags = URL_PROTOCOL_FLAG_NETWORK,
2085 .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy,data"
2086 };
2087 #endif /* CONFIG_HTTP_PROTOCOL */
2088
2089 #if CONFIG_HTTPS_PROTOCOL
2090 HTTP_CLASS(https);
2091
2092 const URLProtocol ff_https_protocol = {
2093 .name = "https",
2094 .url_open2 = http_open,
2095 .url_read = http_read,
2096 .url_write = http_write,
2097 .url_seek = http_seek,
2098 .url_close = http_close,
2099 .url_get_file_handle = http_get_file_handle,
2100 .url_get_short_seek = http_get_short_seek,
2101 .url_shutdown = http_shutdown,
2102 .priv_data_size = sizeof(HTTPContext),
2103 .priv_data_class = &https_context_class,
2104 .flags = URL_PROTOCOL_FLAG_NETWORK,
2105 .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy"
2106 };
2107 #endif /* CONFIG_HTTPS_PROTOCOL */
2108
2109 #if CONFIG_HTTPPROXY_PROTOCOL
2110 static int http_proxy_close(URLContext *h)
2111 {
2112 HTTPContext *s = h->priv_data;
2113 if (s->hd)
2114 ffurl_closep(&s->hd);
2115 return 0;
2116 }
2117
2118 static int http_proxy_open(URLContext *h, const char *uri, int flags)
2119 {
2120 HTTPContext *s = h->priv_data;
2121 char hostname[1024], hoststr[1024];
2122 char auth[1024], pathbuf[1024], *path;
2123 char lower_url[100];
2124 int port, ret = 0, auth_attempts = 0;
2125 HTTPAuthType cur_auth_type;
2126 char *authstr;
2127
2128 if( s->seekable == 1 )
2129 h->is_streamed = 0;
2130 else
2131 h->is_streamed = 1;
2132
2133 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
2134 pathbuf, sizeof(pathbuf), uri);
2135 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
2136 path = pathbuf;
2137 if (*path == '/')
2138 path++;
2139
2140 ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
2141 NULL);
2142 redo:
2143 ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
2144 &h->interrupt_callback, NULL,
2145 h->protocol_whitelist, h->protocol_blacklist, h);
2146 if (ret < 0)
2147 return ret;
2148
2149 authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
2150 path, "CONNECT");
2151 snprintf(s->buffer, sizeof(s->buffer),
2152 "CONNECT %s HTTP/1.1\r\n"
2153 "Host: %s\r\n"
2154 "Connection: close\r\n"
2155 "%s%s"
2156 "\r\n",
2157 path,
2158 hoststr,
2159 authstr ? "Proxy-" : "", authstr ? authstr : "");
2160 av_freep(&authstr);
2161
2162 if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
2163 goto fail;
2164
2165 s->buf_ptr = s->buffer;
2166 s->buf_end = s->buffer;
2167 s->line_count = 0;
2168 s->filesize = UINT64_MAX;
2169 cur_auth_type = s->proxy_auth_state.auth_type;
2170
2171 /* Note: This uses buffering, potentially reading more than the
2172 * HTTP header. If tunneling a protocol where the server starts
2173 * the conversation, we might buffer part of that here, too.
2174 * Reading that requires using the proper ffurl_read() function
2175 * on this URLContext, not using the fd directly (as the tls
2176 * protocol does). This shouldn't be an issue for tls though,
2177 * since the client starts the conversation there, so there
2178 * is no extra data that we might buffer up here.
2179 */
2180 ret = http_read_header(h);
2181 if (ret < 0)
2182 goto fail;
2183
2184 auth_attempts++;
2185 if (s->http_code == 407 &&
2186 (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
2187 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && auth_attempts < 2) {
2188 ffurl_closep(&s->hd);
2189 goto redo;
2190 }
2191
2192 if (s->http_code < 400)
2193 return 0;
2194 ret = ff_http_averror(s->http_code, AVERROR(EIO));
2195
2196 fail:
2197 http_proxy_close(h);
2198 return ret;
2199 }
2200
2201 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
2202 {
2203 HTTPContext *s = h->priv_data;
2204 return ffurl_write(s->hd, buf, size);
2205 }
2206
2207 const URLProtocol ff_httpproxy_protocol = {
2208 .name = "httpproxy",
2209 .url_open = http_proxy_open,
2210 .url_read = http_buf_read,
2211 .url_write = http_proxy_write,
2212 .url_close = http_proxy_close,
2213 .url_get_file_handle = http_get_file_handle,
2214 .priv_data_size = sizeof(HTTPContext),
2215 .flags = URL_PROTOCOL_FLAG_NETWORK,
2216 };
2217 #endif /* CONFIG_HTTPPROXY_PROTOCOL */
2218