2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
26 #endif /* CONFIG_ZLIB */
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/time.h"
38 #include "os_support.h"
41 /* XXX: POST protocol is not completely implemented because ffmpeg uses
42 * only a subset of it. */
44 /* The IO buffer size is unrelated to the max URL size in itself, but needs
45 * to be large enough to fit the full request headers (including long
47 #define BUFFER_SIZE MAX_URL_SIZE
48 #define MAX_REDIRECTS 8
58 typedef struct HTTPContext {
61 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
64 /* Used if "Transfer-Encoding: chunked" otherwise -1. */
66 int64_t off, end_off, filesize;
68 HTTPAuthState auth_state;
69 HTTPAuthState proxy_auth_state;
74 /* Set if the server correctly handles Connection: close and will close
75 * the connection after feeding us the content. */
77 int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
79 /* A flag which indicates if the end of chunked encoding has been sent. */
81 /* A flag which indicates we have finished to read POST reply. */
83 /* A flag which indicates if we use persistent connections. */
84 int multiple_requests;
89 char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
90 /* A dictionary containing cookies keyed by cookie name */
91 AVDictionary *cookie_dict;
93 /* how much data was read since the last ICY metadata packet */
95 /* after how many bytes of read data a new metadata packet will be found */
97 char *icy_metadata_headers;
98 char *icy_metadata_packet;
99 AVDictionary *metadata;
102 z_stream inflate_stream;
103 uint8_t *inflate_buffer;
104 #endif /* CONFIG_ZLIB */
105 AVDictionary *chained_options;
109 int reconnect_at_eof;
110 int reconnect_streamed;
116 HandshakeState handshake_step;
117 int is_connected_server;
120 #define OFFSET(x) offsetof(HTTPContext, x)
121 #define D AV_OPT_FLAG_DECODING_PARAM
122 #define E AV_OPT_FLAG_ENCODING_PARAM
123 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
125 static const AVOption options[] = {
126 { "seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, D },
127 { "chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
128 { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
129 { "content_type", "set a specific content type for the POST messages", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
130 { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
131 { "user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
132 { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D | E },
133 { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
134 { "mime_type", "export the MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
135 { "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, { 0 }, 0, 0, D },
136 { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
137 { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT },
138 { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT },
139 { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
140 { "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, "auth_type"},
141 { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, "auth_type"},
142 { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, "auth_type"},
143 { "send_expect_100", "Force sending an Expect: 100-continue header for POST", OFFSET(send_expect_100), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
144 { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
145 { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
146 { "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 },
147 { "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 },
148 { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
149 { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
150 { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
151 { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
152 { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
153 { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
157 static int http_connect(URLContext *h, const char *path, const char *local_path,
158 const char *hoststr, const char *auth,
159 const char *proxyauth, int *new_location);
160 static int http_read_header(URLContext *h, int *new_location);
162 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
164 memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
165 &((HTTPContext *)src->priv_data)->auth_state,
166 sizeof(HTTPAuthState));
167 memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
168 &((HTTPContext *)src->priv_data)->proxy_auth_state,
169 sizeof(HTTPAuthState));
172 static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
174 const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
175 char hostname[1024], hoststr[1024], proto[10];
176 char auth[1024], proxyauth[1024] = "";
177 char path1[MAX_URL_SIZE];
178 char buf[1024], urlbuf[MAX_URL_SIZE];
179 int port, use_proxy, err, location_changed = 0;
180 HTTPContext *s = h->priv_data;
182 av_url_split(proto, sizeof(proto), auth, sizeof(auth),
183 hostname, sizeof(hostname), &port,
184 path1, sizeof(path1), s->location);
185 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
187 proxy_path = getenv("http_proxy");
188 use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
189 proxy_path && av_strstart(proxy_path, "http://", NULL);
191 if (!strcmp(proto, "https")) {
200 if (path1[0] == '\0')
206 /* Reassemble the request URL without auth string - we don't
207 * want to leak the auth to the proxy. */
208 ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
211 av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
212 hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
215 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
218 err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE,
219 &h->interrupt_callback, options);
224 err = http_connect(h, path, local_path, hoststr,
225 auth, proxyauth, &location_changed);
229 return location_changed;
232 /* return non zero if error */
233 static int http_open_cnx(URLContext *h, AVDictionary **options)
235 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
236 HTTPContext *s = h->priv_data;
237 int location_changed, attempts = 0, redirects = 0;
239 av_dict_copy(options, s->chained_options, 0);
241 cur_auth_type = s->auth_state.auth_type;
242 cur_proxy_auth_type = s->auth_state.auth_type;
244 location_changed = http_open_cnx_internal(h, options);
245 if (location_changed < 0)
249 if (s->http_code == 401) {
250 if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
251 s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
252 ffurl_closep(&s->hd);
257 if (s->http_code == 407) {
258 if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
259 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
260 ffurl_closep(&s->hd);
265 if ((s->http_code == 301 || s->http_code == 302 ||
266 s->http_code == 303 || s->http_code == 307) &&
267 location_changed == 1) {
268 /* url moved, get next */
269 ffurl_closep(&s->hd);
270 if (redirects++ >= MAX_REDIRECTS)
272 /* Restart the authentication process with the new target, which
273 * might use a different auth mechanism. */
274 memset(&s->auth_state, 0, sizeof(s->auth_state));
276 location_changed = 0;
283 ffurl_closep(&s->hd);
284 if (location_changed < 0)
285 return location_changed;
286 return ff_http_averror(s->http_code, AVERROR(EIO));
289 int ff_http_do_new_request(URLContext *h, const char *uri)
291 HTTPContext *s = h->priv_data;
292 AVDictionary *options = NULL;
296 s->icy_data_read = 0;
297 av_free(s->location);
298 s->location = av_strdup(uri);
300 return AVERROR(ENOMEM);
302 ret = http_open_cnx(h, &options);
303 av_dict_free(&options);
307 int ff_http_averror(int status_code, int default_averror)
309 switch (status_code) {
310 case 400: return AVERROR_HTTP_BAD_REQUEST;
311 case 401: return AVERROR_HTTP_UNAUTHORIZED;
312 case 403: return AVERROR_HTTP_FORBIDDEN;
313 case 404: return AVERROR_HTTP_NOT_FOUND;
316 if (status_code >= 400 && status_code <= 499)
317 return AVERROR_HTTP_OTHER_4XX;
318 else if (status_code >= 500)
319 return AVERROR_HTTP_SERVER_ERROR;
321 return default_averror;
324 static int http_write_reply(URLContext* h, int status_code)
326 int ret, body = 0, reply_code, message_len;
327 const char *reply_text, *content_type;
328 HTTPContext *s = h->priv_data;
329 char message[BUFFER_SIZE];
330 content_type = "text/plain";
334 switch (status_code) {
335 case AVERROR_HTTP_BAD_REQUEST:
338 reply_text = "Bad Request";
340 case AVERROR_HTTP_FORBIDDEN:
343 reply_text = "Forbidden";
345 case AVERROR_HTTP_NOT_FOUND:
348 reply_text = "Not Found";
353 content_type = "application/octet-stream";
355 case AVERROR_HTTP_SERVER_ERROR:
358 reply_text = "Internal server error";
361 return AVERROR(EINVAL);
365 message_len = snprintf(message, sizeof(message),
366 "HTTP/1.1 %03d %s\r\n"
367 "Content-Type: %s\r\n"
368 "Content-Length: %zu\r\n"
374 strlen(reply_text) + 6, // 3 digit status code + space + \r\n
379 message_len = snprintf(message, sizeof(message),
380 "HTTP/1.1 %03d %s\r\n"
381 "Content-Type: %s\r\n"
382 "Transfer-Encoding: chunked\r\n"
388 av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
389 if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
394 static void handle_http_errors(URLContext *h, int error)
396 av_assert0(error < 0);
397 http_write_reply(h, error);
400 static int http_handshake(URLContext *c)
402 int ret, err, new_location;
403 HTTPContext *ch = c->priv_data;
404 URLContext *cl = ch->hd;
405 switch (ch->handshake_step) {
407 av_log(c, AV_LOG_TRACE, "Lower protocol\n");
408 if ((ret = ffurl_handshake(cl)) > 0)
412 ch->handshake_step = READ_HEADERS;
413 ch->is_connected_server = 1;
416 av_log(c, AV_LOG_TRACE, "Read headers\n");
417 if ((err = http_read_header(c, &new_location)) < 0) {
418 handle_http_errors(c, err);
421 ch->handshake_step = WRITE_REPLY_HEADERS;
423 case WRITE_REPLY_HEADERS:
424 av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
425 if ((err = http_write_reply(c, ch->reply_code)) < 0)
427 ch->handshake_step = FINISH;
432 // this should never be reached.
433 return AVERROR(EINVAL);
436 static int http_listen(URLContext *h, const char *uri, int flags,
437 AVDictionary **options) {
438 HTTPContext *s = h->priv_data;
440 char hostname[1024], proto[10];
442 const char *lower_proto = "tcp";
444 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
446 if (!strcmp(proto, "https"))
448 ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
450 if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
452 if ((ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
453 &h->interrupt_callback, options)) < 0)
455 s->handshake_step = LOWER_PROTO;
456 if (s->listen == HTTP_SINGLE) { /* single client */
458 while ((ret = http_handshake(h)) > 0);
461 av_dict_free(&s->chained_options);
465 static int http_open(URLContext *h, const char *uri, int flags,
466 AVDictionary **options)
468 HTTPContext *s = h->priv_data;
471 if( s->seekable == 1 )
477 s->location = av_strdup(uri);
479 return AVERROR(ENOMEM);
481 av_dict_copy(&s->chained_options, *options, 0);
484 int len = strlen(s->headers);
485 if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
486 av_log(h, AV_LOG_WARNING,
487 "No trailing CRLF found in HTTP header.\n");
488 ret = av_reallocp(&s->headers, len + 3);
491 s->headers[len] = '\r';
492 s->headers[len + 1] = '\n';
493 s->headers[len + 2] = '\0';
498 return http_listen(h, uri, flags, options);
500 ret = http_open_cnx(h, options);
502 av_dict_free(&s->chained_options);
506 static int http_accept(URLContext *s, URLContext **c)
509 HTTPContext *sc = s->priv_data;
511 URLContext *sl = sc->hd;
512 URLContext *cl = NULL;
514 av_assert0(sc->listen);
515 if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
517 cc = (*c)->priv_data;
518 if ((ret = ffurl_accept(sl, &cl)) < 0)
521 cc->is_multi_client = 1;
526 static int http_getc(HTTPContext *s)
529 if (s->buf_ptr >= s->buf_end) {
530 len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
533 } else if (len == 0) {
536 s->buf_ptr = s->buffer;
537 s->buf_end = s->buffer + len;
540 return *s->buf_ptr++;
543 static int http_get_line(HTTPContext *s, char *line, int line_size)
555 if (q > line && q[-1] == '\r')
561 if ((q - line) < line_size - 1)
567 static int check_http_code(URLContext *h, int http_code, const char *end)
569 HTTPContext *s = h->priv_data;
570 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
571 * don't abort until all headers have been parsed. */
572 if (http_code >= 400 && http_code < 600 &&
573 (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
574 (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
575 end += strspn(end, SPACE_CHARS);
576 av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
577 return ff_http_averror(http_code, AVERROR(EIO));
582 static int parse_location(HTTPContext *s, const char *p)
584 char redirected_location[MAX_URL_SIZE], *new_loc;
585 ff_make_absolute_url(redirected_location, sizeof(redirected_location),
587 new_loc = av_strdup(redirected_location);
589 return AVERROR(ENOMEM);
590 av_free(s->location);
591 s->location = new_loc;
595 /* "bytes $from-$to/$document_size" */
596 static void parse_content_range(URLContext *h, const char *p)
598 HTTPContext *s = h->priv_data;
601 if (!strncmp(p, "bytes ", 6)) {
603 s->off = strtoll(p, NULL, 10);
604 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
605 s->filesize = strtoll(slash + 1, NULL, 10);
607 if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
608 h->is_streamed = 0; /* we _can_ in fact seek */
611 static int parse_content_encoding(URLContext *h, const char *p)
613 if (!av_strncasecmp(p, "gzip", 4) ||
614 !av_strncasecmp(p, "deflate", 7)) {
616 HTTPContext *s = h->priv_data;
619 inflateEnd(&s->inflate_stream);
620 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
621 av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
622 s->inflate_stream.msg);
623 return AVERROR(ENOSYS);
625 if (zlibCompileFlags() & (1 << 17)) {
626 av_log(h, AV_LOG_WARNING,
627 "Your zlib was compiled without gzip support.\n");
628 return AVERROR(ENOSYS);
631 av_log(h, AV_LOG_WARNING,
632 "Compressed (%s) content, need zlib with gzip support\n", p);
633 return AVERROR(ENOSYS);
634 #endif /* CONFIG_ZLIB */
635 } else if (!av_strncasecmp(p, "identity", 8)) {
636 // The normal, no-encoding case (although servers shouldn't include
637 // the header at all if this is the case).
639 av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
644 // Concat all Icy- header lines
645 static int parse_icy(HTTPContext *s, const char *tag, const char *p)
647 int len = 4 + strlen(p) + strlen(tag);
648 int is_first = !s->icy_metadata_headers;
651 av_dict_set(&s->metadata, tag, p, 0);
653 if (s->icy_metadata_headers)
654 len += strlen(s->icy_metadata_headers);
656 if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
660 *s->icy_metadata_headers = '\0';
662 av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
667 static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)
671 // duplicate the cookie name (dict will dupe the value)
672 if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);
673 if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);
675 // add the cookie to the dictionary
676 av_dict_set(cookies, name, eql, AV_DICT_DONT_STRDUP_KEY);
681 static int cookie_string(AVDictionary *dict, char **cookies)
683 AVDictionaryEntry *e = NULL;
686 // determine how much memory is needed for the cookies string
687 while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
688 len += strlen(e->key) + strlen(e->value) + 1;
690 // reallocate the cookies
692 if (*cookies) av_free(*cookies);
693 *cookies = av_malloc(len);
694 if (!*cookies) return AVERROR(ENOMEM);
697 // write out the cookies
698 while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
699 av_strlcatf(*cookies, len, "%s%s\n", e->key, e->value);
704 static int process_line(URLContext *h, char *line, int line_count,
707 HTTPContext *s = h->priv_data;
708 const char *auto_method = h->flags & AVIO_FLAG_READ ? "POST" : "GET";
709 char *tag, *p, *end, *method, *resource, *version;
713 if (line[0] == '\0') {
719 if (line_count == 0) {
720 if (s->is_connected_server) {
723 while (*p && !av_isspace(*p))
726 av_log(h, AV_LOG_TRACE, "Received method: %s\n", method);
728 if (av_strcasecmp(s->method, method)) {
729 av_log(h, AV_LOG_ERROR, "Received and expected HTTP method do not match. (%s expected, %s received)\n",
731 return ff_http_averror(400, AVERROR(EIO));
734 // use autodetected HTTP method to expect
735 av_log(h, AV_LOG_TRACE, "Autodetected %s HTTP method\n", auto_method);
736 if (av_strcasecmp(auto_method, method)) {
737 av_log(h, AV_LOG_ERROR, "Received and autodetected HTTP method did not match "
738 "(%s autodetected %s received)\n", auto_method, method);
739 return ff_http_averror(400, AVERROR(EIO));
741 if (!(s->method = av_strdup(method)))
742 return AVERROR(ENOMEM);
746 while (av_isspace(*p))
749 while (!av_isspace(*p))
752 av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
753 if (!(s->resource = av_strdup(resource)))
754 return AVERROR(ENOMEM);
757 while (av_isspace(*p))
760 while (*p && !av_isspace(*p))
763 if (av_strncasecmp(version, "HTTP/", 5)) {
764 av_log(h, AV_LOG_ERROR, "Malformed HTTP version string.\n");
765 return ff_http_averror(400, AVERROR(EIO));
767 av_log(h, AV_LOG_TRACE, "HTTP version string: %s\n", version);
769 while (!av_isspace(*p) && *p != '\0')
771 while (av_isspace(*p))
773 s->http_code = strtol(p, &end, 10);
775 av_log(h, AV_LOG_TRACE, "http_code=%d\n", s->http_code);
777 if ((ret = check_http_code(h, s->http_code, end)) < 0)
781 while (*p != '\0' && *p != ':')
789 while (av_isspace(*p))
791 if (!av_strcasecmp(tag, "Location")) {
792 if ((ret = parse_location(s, p)) < 0)
795 } else if (!av_strcasecmp(tag, "Content-Length") && s->filesize == -1) {
796 s->filesize = strtoll(p, NULL, 10);
797 } else if (!av_strcasecmp(tag, "Content-Range")) {
798 parse_content_range(h, p);
799 } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
800 !strncmp(p, "bytes", 5) &&
803 } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
804 !av_strncasecmp(p, "chunked", 7)) {
807 } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
808 ff_http_auth_handle_header(&s->auth_state, tag, p);
809 } else if (!av_strcasecmp(tag, "Authentication-Info")) {
810 ff_http_auth_handle_header(&s->auth_state, tag, p);
811 } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
812 ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
813 } else if (!av_strcasecmp(tag, "Connection")) {
814 if (!strcmp(p, "close"))
816 } else if (!av_strcasecmp(tag, "Server")) {
817 if (!av_strcasecmp(p, "AkamaiGHost")) {
819 } else if (!av_strncasecmp(p, "MediaGateway", 12)) {
820 s->is_mediagateway = 1;
822 } else if (!av_strcasecmp(tag, "Content-Type")) {
823 av_free(s->mime_type);
824 s->mime_type = av_strdup(p);
825 } else if (!av_strcasecmp(tag, "Set-Cookie")) {
826 if (parse_cookie(s, p, &s->cookie_dict))
827 av_log(h, AV_LOG_WARNING, "Unable to parse '%s'\n", p);
828 } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
829 s->icy_metaint = strtoll(p, NULL, 10);
830 } else if (!av_strncasecmp(tag, "Icy-", 4)) {
831 if ((ret = parse_icy(s, tag, p)) < 0)
833 } else if (!av_strcasecmp(tag, "Content-Encoding")) {
834 if ((ret = parse_content_encoding(h, p)) < 0)
842 * Create a string containing cookie values for use as a HTTP cookie header
843 * field value for a particular path and domain from the cookie values stored in
844 * the HTTP protocol context. The cookie string is stored in *cookies.
846 * @return a negative value if an error condition occurred, 0 otherwise
848 static int get_cookies(HTTPContext *s, char **cookies, const char *path,
851 // cookie strings will look like Set-Cookie header field values. Multiple
852 // Set-Cookie fields will result in multiple values delimited by a newline
854 char *next, *cookie, *set_cookies = av_strdup(s->cookies), *cset_cookies = set_cookies;
856 if (!set_cookies) return AVERROR(EINVAL);
858 // destroy any cookies in the dictionary.
859 av_dict_free(&s->cookie_dict);
862 while ((cookie = av_strtok(set_cookies, "\n", &next))) {
863 int domain_offset = 0;
864 char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
867 // store the cookie in a dict in case it is updated in the response
868 if (parse_cookie(s, cookie, &s->cookie_dict))
869 av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);
871 while ((param = av_strtok(cookie, "; ", &next_param))) {
873 // first key-value pair is the actual cookie value
874 cvalue = av_strdup(param);
876 } else if (!av_strncasecmp("path=", param, 5)) {
878 cpath = av_strdup(¶m[5]);
879 } else if (!av_strncasecmp("domain=", param, 7)) {
880 // if the cookie specifies a sub-domain, skip the leading dot thereby
881 // supporting URLs that point to sub-domains and the master domain
882 int leading_dot = (param[7] == '.');
884 cdomain = av_strdup(¶m[7+leading_dot]);
886 // ignore unknown attributes
890 cdomain = av_strdup(domain);
892 // ensure all of the necessary values are valid
893 if (!cdomain || !cpath || !cvalue) {
894 av_log(s, AV_LOG_WARNING,
895 "Invalid cookie found, no value, path or domain specified\n");
899 // check if the request path matches the cookie path
900 if (av_strncasecmp(path, cpath, strlen(cpath)))
903 // the domain should be at least the size of our cookie domain
904 domain_offset = strlen(domain) - strlen(cdomain);
905 if (domain_offset < 0)
908 // match the cookie domain
909 if (av_strcasecmp(&domain[domain_offset], cdomain))
912 // cookie parameters match, so copy the value
914 if (!(*cookies = av_strdup(cvalue))) {
915 ret = AVERROR(ENOMEM);
919 char *tmp = *cookies;
920 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
921 if (!(*cookies = av_malloc(str_size))) {
922 ret = AVERROR(ENOMEM);
925 snprintf(*cookies, str_size, "%s; %s", tmp, cvalue);
934 if (*cookies) av_freep(cookies);
935 av_free(cset_cookies);
940 av_free(cset_cookies);
945 static inline int has_header(const char *str, const char *header)
947 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
950 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
953 static int http_read_header(URLContext *h, int *new_location)
955 HTTPContext *s = h->priv_data;
956 char line[MAX_URL_SIZE];
962 if ((err = http_get_line(s, line, sizeof(line))) < 0)
965 av_log(h, AV_LOG_TRACE, "header='%s'\n", line);
967 err = process_line(h, line, s->line_count, new_location);
975 if (s->seekable == -1 && s->is_mediagateway && s->filesize == 2000000000)
976 h->is_streamed = 1; /* we can in fact _not_ seek */
978 // add any new cookies into the existing cookie string
979 cookie_string(s->cookie_dict, &s->cookies);
980 av_dict_free(&s->cookie_dict);
985 static int http_connect(URLContext *h, const char *path, const char *local_path,
986 const char *hoststr, const char *auth,
987 const char *proxyauth, int *new_location)
989 HTTPContext *s = h->priv_data;
991 char headers[HTTP_HEADERS_SIZE] = "";
992 char *authstr = NULL, *proxyauthstr = NULL;
993 int64_t off = s->off;
996 int send_expect_100 = 0;
998 /* send http header */
999 post = h->flags & AVIO_FLAG_WRITE;
1002 /* force POST method and disable chunked encoding when
1003 * custom HTTP post data is set */
1005 s->chunked_post = 0;
1011 method = post ? "POST" : "GET";
1013 authstr = ff_http_auth_create_response(&s->auth_state, auth,
1014 local_path, method);
1015 proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
1016 local_path, method);
1017 if (post && !s->post_data) {
1018 send_expect_100 = s->send_expect_100;
1019 /* The user has supplied authentication but we don't know the auth type,
1020 * send Expect: 100-continue to get the 401 response including the
1021 * WWW-Authenticate header, or an 100 continue if no auth actually
1023 if (auth && *auth &&
1024 s->auth_state.auth_type == HTTP_AUTH_NONE &&
1025 s->http_code != 401)
1026 send_expect_100 = 1;
1029 /* set default headers if needed */
1030 if (!has_header(s->headers, "\r\nUser-Agent: "))
1031 len += av_strlcatf(headers + len, sizeof(headers) - len,
1032 "User-Agent: %s\r\n", s->user_agent);
1033 if (!has_header(s->headers, "\r\nAccept: "))
1034 len += av_strlcpy(headers + len, "Accept: */*\r\n",
1035 sizeof(headers) - len);
1036 // Note: we send this on purpose even when s->off is 0 when we're probing,
1037 // since it allows us to detect more reliably if a (non-conforming)
1038 // server supports seeking by analysing the reply headers.
1039 if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable == -1)) {
1040 len += av_strlcatf(headers + len, sizeof(headers) - len,
1041 "Range: bytes=%"PRId64"-", s->off);
1043 len += av_strlcatf(headers + len, sizeof(headers) - len,
1044 "%"PRId64, s->end_off - 1);
1045 len += av_strlcpy(headers + len, "\r\n",
1046 sizeof(headers) - len);
1048 if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
1049 len += av_strlcatf(headers + len, sizeof(headers) - len,
1050 "Expect: 100-continue\r\n");
1052 if (!has_header(s->headers, "\r\nConnection: ")) {
1053 if (s->multiple_requests)
1054 len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
1055 sizeof(headers) - len);
1057 len += av_strlcpy(headers + len, "Connection: close\r\n",
1058 sizeof(headers) - len);
1061 if (!has_header(s->headers, "\r\nHost: "))
1062 len += av_strlcatf(headers + len, sizeof(headers) - len,
1063 "Host: %s\r\n", hoststr);
1064 if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
1065 len += av_strlcatf(headers + len, sizeof(headers) - len,
1066 "Content-Length: %d\r\n", s->post_datalen);
1068 if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
1069 len += av_strlcatf(headers + len, sizeof(headers) - len,
1070 "Content-Type: %s\r\n", s->content_type);
1071 if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
1072 char *cookies = NULL;
1073 if (!get_cookies(s, &cookies, path, hoststr) && cookies) {
1074 len += av_strlcatf(headers + len, sizeof(headers) - len,
1075 "Cookie: %s\r\n", cookies);
1079 if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
1080 len += av_strlcatf(headers + len, sizeof(headers) - len,
1081 "Icy-MetaData: %d\r\n", 1);
1083 /* now add in custom headers */
1085 av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
1087 snprintf(s->buffer, sizeof(s->buffer),
1088 "%s %s HTTP/1.1\r\n"
1096 post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
1098 authstr ? authstr : "",
1099 proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
1101 av_log(h, AV_LOG_DEBUG, "request: %s\n", s->buffer);
1103 if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1107 if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
1110 /* init input buffer */
1111 s->buf_ptr = s->buffer;
1112 s->buf_end = s->buffer;
1115 s->icy_data_read = 0;
1118 s->end_chunked_post = 0;
1120 if (post && !s->post_data && !send_expect_100) {
1121 /* Pretend that it did work. We didn't read any header yet, since
1122 * we've still to send the POST data, but the code calling this
1123 * function will check http_code after we return. */
1129 /* wait for header */
1130 err = http_read_header(h, new_location);
1137 err = (off == s->off) ? 0 : -1;
1140 av_freep(&proxyauthstr);
1144 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
1146 HTTPContext *s = h->priv_data;
1148 /* read bytes from input buffer first */
1149 len = s->buf_end - s->buf_ptr;
1153 memcpy(buf, s->buf_ptr, len);
1156 if ((!s->willclose || s->chunksize < 0) &&
1157 s->filesize >= 0 && s->off >= s->filesize)
1159 len = ffurl_read(s->hd, buf, size);
1160 if (!len && (!s->willclose || s->chunksize < 0) &&
1161 s->filesize >= 0 && s->off < s->filesize) {
1162 av_log(h, AV_LOG_ERROR,
1163 "Stream ends prematurely at %"PRId64", should be %"PRId64"\n",
1166 return AVERROR(EIO);
1171 if (s->chunksize > 0)
1172 s->chunksize -= len;
1178 #define DECOMPRESS_BUF_SIZE (256 * 1024)
1179 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
1181 HTTPContext *s = h->priv_data;
1184 if (!s->inflate_buffer) {
1185 s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
1186 if (!s->inflate_buffer)
1187 return AVERROR(ENOMEM);
1190 if (s->inflate_stream.avail_in == 0) {
1191 int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
1194 s->inflate_stream.next_in = s->inflate_buffer;
1195 s->inflate_stream.avail_in = read;
1198 s->inflate_stream.avail_out = size;
1199 s->inflate_stream.next_out = buf;
1201 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
1202 if (ret != Z_OK && ret != Z_STREAM_END)
1203 av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
1204 ret, s->inflate_stream.msg);
1206 return size - s->inflate_stream.avail_out;
1208 #endif /* CONFIG_ZLIB */
1210 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect);
1212 static int http_read_stream(URLContext *h, uint8_t *buf, int size)
1214 HTTPContext *s = h->priv_data;
1215 int err, new_location, read_ret, seek_ret;
1220 if (s->end_chunked_post && !s->end_header) {
1221 err = http_read_header(h, &new_location);
1226 if (s->chunksize >= 0) {
1227 if (!s->chunksize) {
1231 if ((err = http_get_line(s, line, sizeof(line))) < 0)
1233 } while (!*line); /* skip CR LF from last chunk */
1235 s->chunksize = strtoll(line, NULL, 16);
1237 av_log(NULL, AV_LOG_TRACE, "Chunked encoding data size: %"PRId64"'\n",
1243 size = FFMIN(size, s->chunksize);
1247 return http_buf_read_compressed(h, buf, size);
1248 #endif /* CONFIG_ZLIB */
1249 read_ret = http_buf_read(h, buf, size);
1250 if ( (read_ret < 0 && s->reconnect && (!h->is_streamed || s->reconnect_streamed) && s->filesize > 0 && s->off < s->filesize)
1251 || (read_ret == 0 && s->reconnect_at_eof && (!h->is_streamed || s->reconnect_streamed))) {
1252 int64_t target = h->is_streamed ? 0 : s->off;
1253 av_log(h, AV_LOG_INFO, "Will reconnect at %"PRId64" error=%s.\n", s->off, av_err2str(read_ret));
1254 av_usleep(1000U*1000*s->reconnect_delay);
1255 s->reconnect_delay = 1 + 2*s->reconnect_delay;
1256 seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
1257 if (seek_ret != target) {
1258 av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRId64".\n", target);
1262 read_ret = http_buf_read(h, buf, size);
1264 s->reconnect_delay = 0;
1269 // Like http_read_stream(), but no short reads.
1270 // Assumes partial reads are an error.
1271 static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
1274 while (pos < size) {
1275 int len = http_read_stream(h, buf + pos, size - pos);
1283 static void update_metadata(HTTPContext *s, char *data)
1292 val = strstr(key, "='");
1295 end = strstr(val, "';");
1303 av_dict_set(&s->metadata, key, val, 0);
1309 static int store_icy(URLContext *h, int size)
1311 HTTPContext *s = h->priv_data;
1312 /* until next metadata packet */
1313 int remaining = s->icy_metaint - s->icy_data_read;
1316 return AVERROR_INVALIDDATA;
1319 /* The metadata packet is variable sized. It has a 1 byte header
1320 * which sets the length of the packet (divided by 16). If it's 0,
1321 * the metadata doesn't change. After the packet, icy_metaint bytes
1322 * of normal data follows. */
1324 int len = http_read_stream_all(h, &ch, 1);
1328 char data[255 * 16 + 1];
1331 ret = http_read_stream_all(h, data, len);
1335 if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
1337 update_metadata(s, data);
1339 s->icy_data_read = 0;
1340 remaining = s->icy_metaint;
1343 return FFMIN(size, remaining);
1346 static int http_read(URLContext *h, uint8_t *buf, int size)
1348 HTTPContext *s = h->priv_data;
1350 if (s->icy_metaint > 0) {
1351 size = store_icy(h, size);
1356 size = http_read_stream(h, buf, size);
1358 s->icy_data_read += size;
1362 /* used only when posting data */
1363 static int http_write(URLContext *h, const uint8_t *buf, int size)
1365 char temp[11] = ""; /* 32-bit hex + CRLF + nul */
1367 char crlf[] = "\r\n";
1368 HTTPContext *s = h->priv_data;
1370 if (!s->chunked_post) {
1371 /* non-chunked data is sent without any special encoding */
1372 return ffurl_write(s->hd, buf, size);
1375 /* silently ignore zero-size data since chunk encoding that would
1378 /* upload data using chunked encoding */
1379 snprintf(temp, sizeof(temp), "%x\r\n", size);
1381 if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
1382 (ret = ffurl_write(s->hd, buf, size)) < 0 ||
1383 (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
1389 static int http_shutdown(URLContext *h, int flags)
1392 char footer[] = "0\r\n\r\n";
1393 HTTPContext *s = h->priv_data;
1395 /* signal end of chunked encoding if used */
1396 if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
1397 ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
1398 ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
1399 ret = ret > 0 ? 0 : ret;
1400 s->end_chunked_post = 1;
1406 static int http_close(URLContext *h)
1409 HTTPContext *s = h->priv_data;
1412 inflateEnd(&s->inflate_stream);
1413 av_freep(&s->inflate_buffer);
1414 #endif /* CONFIG_ZLIB */
1416 if (!s->end_chunked_post)
1417 /* Close the write direction by sending the end of chunked encoding. */
1418 ret = http_shutdown(h, h->flags);
1421 ffurl_closep(&s->hd);
1422 av_dict_free(&s->chained_options);
1426 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
1428 HTTPContext *s = h->priv_data;
1429 URLContext *old_hd = s->hd;
1430 int64_t old_off = s->off;
1431 uint8_t old_buf[BUFFER_SIZE];
1432 int old_buf_size, ret;
1433 AVDictionary *options = NULL;
1435 if (whence == AVSEEK_SIZE)
1437 else if (!force_reconnect &&
1438 ((whence == SEEK_CUR && off == 0) ||
1439 (whence == SEEK_SET && off == s->off)))
1441 else if ((s->filesize == -1 && whence == SEEK_END))
1442 return AVERROR(ENOSYS);
1444 if (whence == SEEK_CUR)
1446 else if (whence == SEEK_END)
1448 else if (whence != SEEK_SET)
1449 return AVERROR(EINVAL);
1451 return AVERROR(EINVAL);
1454 if (s->off && h->is_streamed)
1455 return AVERROR(ENOSYS);
1457 /* we save the old context in case the seek fails */
1458 old_buf_size = s->buf_end - s->buf_ptr;
1459 memcpy(old_buf, s->buf_ptr, old_buf_size);
1462 /* if it fails, continue on old connection */
1463 if ((ret = http_open_cnx(h, &options)) < 0) {
1464 av_dict_free(&options);
1465 memcpy(s->buffer, old_buf, old_buf_size);
1466 s->buf_ptr = s->buffer;
1467 s->buf_end = s->buffer + old_buf_size;
1472 av_dict_free(&options);
1473 ffurl_close(old_hd);
1477 static int64_t http_seek(URLContext *h, int64_t off, int whence)
1479 return http_seek_internal(h, off, whence, 0);
1482 static int http_get_file_handle(URLContext *h)
1484 HTTPContext *s = h->priv_data;
1485 return ffurl_get_file_handle(s->hd);
1488 #define HTTP_CLASS(flavor) \
1489 static const AVClass flavor ## _context_class = { \
1490 .class_name = # flavor, \
1491 .item_name = av_default_item_name, \
1492 .option = options, \
1493 .version = LIBAVUTIL_VERSION_INT, \
1496 #if CONFIG_HTTP_PROTOCOL
1499 URLProtocol ff_http_protocol = {
1501 .url_open2 = http_open,
1502 .url_accept = http_accept,
1503 .url_handshake = http_handshake,
1504 .url_read = http_read,
1505 .url_write = http_write,
1506 .url_seek = http_seek,
1507 .url_close = http_close,
1508 .url_get_file_handle = http_get_file_handle,
1509 .url_shutdown = http_shutdown,
1510 .priv_data_size = sizeof(HTTPContext),
1511 .priv_data_class = &http_context_class,
1512 .flags = URL_PROTOCOL_FLAG_NETWORK,
1514 #endif /* CONFIG_HTTP_PROTOCOL */
1516 #if CONFIG_HTTPS_PROTOCOL
1519 URLProtocol ff_https_protocol = {
1521 .url_open2 = http_open,
1522 .url_read = http_read,
1523 .url_write = http_write,
1524 .url_seek = http_seek,
1525 .url_close = http_close,
1526 .url_get_file_handle = http_get_file_handle,
1527 .url_shutdown = http_shutdown,
1528 .priv_data_size = sizeof(HTTPContext),
1529 .priv_data_class = &https_context_class,
1530 .flags = URL_PROTOCOL_FLAG_NETWORK,
1532 #endif /* CONFIG_HTTPS_PROTOCOL */
1534 #if CONFIG_HTTPPROXY_PROTOCOL
1535 static int http_proxy_close(URLContext *h)
1537 HTTPContext *s = h->priv_data;
1539 ffurl_closep(&s->hd);
1543 static int http_proxy_open(URLContext *h, const char *uri, int flags)
1545 HTTPContext *s = h->priv_data;
1546 char hostname[1024], hoststr[1024];
1547 char auth[1024], pathbuf[1024], *path;
1548 char lower_url[100];
1549 int port, ret = 0, attempts = 0;
1550 HTTPAuthType cur_auth_type;
1554 if( s->seekable == 1 )
1559 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
1560 pathbuf, sizeof(pathbuf), uri);
1561 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1566 ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
1569 ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
1570 &h->interrupt_callback, NULL);
1574 authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
1576 snprintf(s->buffer, sizeof(s->buffer),
1577 "CONNECT %s HTTP/1.1\r\n"
1579 "Connection: close\r\n"
1584 authstr ? "Proxy-" : "", authstr ? authstr : "");
1587 if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1590 s->buf_ptr = s->buffer;
1591 s->buf_end = s->buffer;
1594 cur_auth_type = s->proxy_auth_state.auth_type;
1596 /* Note: This uses buffering, potentially reading more than the
1597 * HTTP header. If tunneling a protocol where the server starts
1598 * the conversation, we might buffer part of that here, too.
1599 * Reading that requires using the proper ffurl_read() function
1600 * on this URLContext, not using the fd directly (as the tls
1601 * protocol does). This shouldn't be an issue for tls though,
1602 * since the client starts the conversation there, so there
1603 * is no extra data that we might buffer up here.
1605 ret = http_read_header(h, &new_loc);
1610 if (s->http_code == 407 &&
1611 (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
1612 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
1613 ffurl_closep(&s->hd);
1617 if (s->http_code < 400)
1619 ret = ff_http_averror(s->http_code, AVERROR(EIO));
1622 http_proxy_close(h);
1626 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
1628 HTTPContext *s = h->priv_data;
1629 return ffurl_write(s->hd, buf, size);
1632 URLProtocol ff_httpproxy_protocol = {
1633 .name = "httpproxy",
1634 .url_open = http_proxy_open,
1635 .url_read = http_buf_read,
1636 .url_write = http_proxy_write,
1637 .url_close = http_proxy_close,
1638 .url_get_file_handle = http_get_file_handle,
1639 .priv_data_size = sizeof(HTTPContext),
1640 .flags = URL_PROTOCOL_FLAG_NETWORK,
1642 #endif /* CONFIG_HTTPPROXY_PROTOCOL */