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"
37 #include "os_support.h"
40 /* XXX: POST protocol is not completely implemented because ffmpeg uses
41 * only a subset of it. */
43 /* The IO buffer size is unrelated to the max URL size in itself, but needs
44 * to be large enough to fit the full request headers (including long
46 #define BUFFER_SIZE MAX_URL_SIZE
47 #define MAX_REDIRECTS 8
57 typedef struct HTTPContext {
60 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
63 /* Used if "Transfer-Encoding: chunked" otherwise -1. */
65 int64_t off, end_off, filesize;
67 HTTPAuthState auth_state;
68 HTTPAuthState proxy_auth_state;
73 /* Set if the server correctly handles Connection: close and will close
74 * the connection after feeding us the content. */
76 int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
78 /* A flag which indicates if the end of chunked encoding has been sent. */
80 /* A flag which indicates we have finished to read POST reply. */
82 /* A flag which indicates if we use persistent connections. */
83 int multiple_requests;
88 char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
89 /* A dictionary containing cookies keyed by cookie name */
90 AVDictionary *cookie_dict;
92 /* how much data was read since the last ICY metadata packet */
94 /* after how many bytes of read data a new metadata packet will be found */
96 char *icy_metadata_headers;
97 char *icy_metadata_packet;
98 AVDictionary *metadata;
101 z_stream inflate_stream;
102 uint8_t *inflate_buffer;
103 #endif /* CONFIG_ZLIB */
104 AVDictionary *chained_options;
108 int reconnect_at_eof;
109 int reconnect_streamed;
114 HandshakeState handshake_step;
115 int is_connected_server;
118 #define OFFSET(x) offsetof(HTTPContext, x)
119 #define D AV_OPT_FLAG_DECODING_PARAM
120 #define E AV_OPT_FLAG_ENCODING_PARAM
121 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
123 static const AVOption options[] = {
124 { "seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, D },
125 { "chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
126 { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
127 { "content_type", "set a specific content type for the POST messages", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
128 { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
129 { "user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
130 { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D | E },
131 { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
132 { "mime_type", "export the MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
133 { "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 },
134 { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
135 { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT },
136 { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT },
137 { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
138 { "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"},
139 { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, "auth_type"},
140 { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, "auth_type"},
141 { "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 },
142 { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
143 { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
144 { "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 },
145 { "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 },
146 { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
147 { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
148 { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
149 { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
150 { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
151 { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
155 static int http_connect(URLContext *h, const char *path, const char *local_path,
156 const char *hoststr, const char *auth,
157 const char *proxyauth, int *new_location);
158 static int http_read_header(URLContext *h, int *new_location);
160 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
162 memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
163 &((HTTPContext *)src->priv_data)->auth_state,
164 sizeof(HTTPAuthState));
165 memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
166 &((HTTPContext *)src->priv_data)->proxy_auth_state,
167 sizeof(HTTPAuthState));
170 static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
172 const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
173 char hostname[1024], hoststr[1024], proto[10];
174 char auth[1024], proxyauth[1024] = "";
175 char path1[MAX_URL_SIZE];
176 char buf[1024], urlbuf[MAX_URL_SIZE];
177 int port, use_proxy, err, location_changed = 0;
178 HTTPContext *s = h->priv_data;
180 av_url_split(proto, sizeof(proto), auth, sizeof(auth),
181 hostname, sizeof(hostname), &port,
182 path1, sizeof(path1), s->location);
183 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
185 proxy_path = getenv("http_proxy");
186 use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
187 proxy_path && av_strstart(proxy_path, "http://", NULL);
189 if (!strcmp(proto, "https")) {
198 if (path1[0] == '\0')
204 /* Reassemble the request URL without auth string - we don't
205 * want to leak the auth to the proxy. */
206 ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
209 av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
210 hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
213 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
216 err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE,
217 &h->interrupt_callback, options);
222 err = http_connect(h, path, local_path, hoststr,
223 auth, proxyauth, &location_changed);
227 return location_changed;
230 /* return non zero if error */
231 static int http_open_cnx(URLContext *h, AVDictionary **options)
233 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
234 HTTPContext *s = h->priv_data;
235 int location_changed, attempts = 0, redirects = 0;
237 av_dict_copy(options, s->chained_options, 0);
239 cur_auth_type = s->auth_state.auth_type;
240 cur_proxy_auth_type = s->auth_state.auth_type;
242 location_changed = http_open_cnx_internal(h, options);
243 if (location_changed < 0)
247 if (s->http_code == 401) {
248 if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
249 s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
250 ffurl_closep(&s->hd);
255 if (s->http_code == 407) {
256 if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
257 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
258 ffurl_closep(&s->hd);
263 if ((s->http_code == 301 || s->http_code == 302 ||
264 s->http_code == 303 || s->http_code == 307) &&
265 location_changed == 1) {
266 /* url moved, get next */
267 ffurl_closep(&s->hd);
268 if (redirects++ >= MAX_REDIRECTS)
270 /* Restart the authentication process with the new target, which
271 * might use a different auth mechanism. */
272 memset(&s->auth_state, 0, sizeof(s->auth_state));
274 location_changed = 0;
281 ffurl_closep(&s->hd);
282 if (location_changed < 0)
283 return location_changed;
284 return ff_http_averror(s->http_code, AVERROR(EIO));
287 int ff_http_do_new_request(URLContext *h, const char *uri)
289 HTTPContext *s = h->priv_data;
290 AVDictionary *options = NULL;
294 s->icy_data_read = 0;
295 av_free(s->location);
296 s->location = av_strdup(uri);
298 return AVERROR(ENOMEM);
300 ret = http_open_cnx(h, &options);
301 av_dict_free(&options);
305 int ff_http_averror(int status_code, int default_averror)
307 switch (status_code) {
308 case 400: return AVERROR_HTTP_BAD_REQUEST;
309 case 401: return AVERROR_HTTP_UNAUTHORIZED;
310 case 403: return AVERROR_HTTP_FORBIDDEN;
311 case 404: return AVERROR_HTTP_NOT_FOUND;
314 if (status_code >= 400 && status_code <= 499)
315 return AVERROR_HTTP_OTHER_4XX;
316 else if (status_code >= 500)
317 return AVERROR_HTTP_SERVER_ERROR;
319 return default_averror;
322 static int http_write_reply(URLContext* h, int status_code)
324 int ret, body = 0, reply_code, message_len;
325 const char *reply_text, *content_type;
326 HTTPContext *s = h->priv_data;
327 char message[BUFFER_SIZE];
328 content_type = "text/plain";
332 switch (status_code) {
333 case AVERROR_HTTP_BAD_REQUEST:
336 reply_text = "Bad Request";
338 case AVERROR_HTTP_FORBIDDEN:
341 reply_text = "Forbidden";
343 case AVERROR_HTTP_NOT_FOUND:
346 reply_text = "Not Found";
351 content_type = "application/octet-stream";
353 case AVERROR_HTTP_SERVER_ERROR:
356 reply_text = "Internal server error";
359 return AVERROR(EINVAL);
363 message_len = snprintf(message, sizeof(message),
364 "HTTP/1.1 %03d %s\r\n"
365 "Content-Type: %s\r\n"
366 "Content-Length: %zu\r\n"
372 strlen(reply_text) + 6, // 3 digit status code + space + \r\n
377 message_len = snprintf(message, sizeof(message),
378 "HTTP/1.1 %03d %s\r\n"
379 "Content-Type: %s\r\n"
380 "Transfer-Encoding: chunked\r\n"
386 av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
387 if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
392 static void handle_http_errors(URLContext *h, int error)
394 av_assert0(error < 0);
395 http_write_reply(h, error);
398 static int http_handshake(URLContext *c)
400 int ret, err, new_location;
401 HTTPContext *ch = c->priv_data;
402 URLContext *cl = ch->hd;
403 switch (ch->handshake_step) {
405 av_log(c, AV_LOG_TRACE, "Lower protocol\n");
406 if ((ret = ffurl_handshake(cl)) > 0)
410 ch->handshake_step = READ_HEADERS;
411 ch->is_connected_server = 1;
414 av_log(c, AV_LOG_TRACE, "Read headers\n");
415 if ((err = http_read_header(c, &new_location)) < 0) {
416 handle_http_errors(c, err);
419 ch->handshake_step = WRITE_REPLY_HEADERS;
421 case WRITE_REPLY_HEADERS:
422 av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
423 if ((err = http_write_reply(c, ch->reply_code)) < 0)
425 ch->handshake_step = FINISH;
430 // this should never be reached.
431 return AVERROR(EINVAL);
434 static int http_listen(URLContext *h, const char *uri, int flags,
435 AVDictionary **options) {
436 HTTPContext *s = h->priv_data;
438 char hostname[1024], proto[10];
440 const char *lower_proto = "tcp";
442 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
444 if (!strcmp(proto, "https"))
446 ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
448 if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
450 if ((ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
451 &h->interrupt_callback, options)) < 0)
453 s->handshake_step = LOWER_PROTO;
454 if (s->listen == HTTP_SINGLE) { /* single client */
456 while ((ret = http_handshake(h)) > 0);
459 av_dict_free(&s->chained_options);
463 static int http_open(URLContext *h, const char *uri, int flags,
464 AVDictionary **options)
466 HTTPContext *s = h->priv_data;
469 if( s->seekable == 1 )
475 s->location = av_strdup(uri);
477 return AVERROR(ENOMEM);
479 av_dict_copy(&s->chained_options, *options, 0);
482 int len = strlen(s->headers);
483 if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
484 av_log(h, AV_LOG_WARNING,
485 "No trailing CRLF found in HTTP header.\n");
486 ret = av_reallocp(&s->headers, len + 3);
489 s->headers[len] = '\r';
490 s->headers[len + 1] = '\n';
491 s->headers[len + 2] = '\0';
496 return http_listen(h, uri, flags, options);
498 ret = http_open_cnx(h, options);
500 av_dict_free(&s->chained_options);
504 static int http_accept(URLContext *s, URLContext **c)
507 HTTPContext *sc = s->priv_data;
509 URLContext *sl = sc->hd;
510 URLContext *cl = NULL;
512 av_assert0(sc->listen);
513 if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
515 cc = (*c)->priv_data;
516 if ((ret = ffurl_accept(sl, &cl)) < 0)
519 cc->is_multi_client = 1;
524 static int http_getc(HTTPContext *s)
527 if (s->buf_ptr >= s->buf_end) {
528 len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
531 } else if (len == 0) {
534 s->buf_ptr = s->buffer;
535 s->buf_end = s->buffer + len;
538 return *s->buf_ptr++;
541 static int http_get_line(HTTPContext *s, char *line, int line_size)
553 if (q > line && q[-1] == '\r')
559 if ((q - line) < line_size - 1)
565 static int check_http_code(URLContext *h, int http_code, const char *end)
567 HTTPContext *s = h->priv_data;
568 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
569 * don't abort until all headers have been parsed. */
570 if (http_code >= 400 && http_code < 600 &&
571 (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
572 (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
573 end += strspn(end, SPACE_CHARS);
574 av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
575 return ff_http_averror(http_code, AVERROR(EIO));
580 static int parse_location(HTTPContext *s, const char *p)
582 char redirected_location[MAX_URL_SIZE], *new_loc;
583 ff_make_absolute_url(redirected_location, sizeof(redirected_location),
585 new_loc = av_strdup(redirected_location);
587 return AVERROR(ENOMEM);
588 av_free(s->location);
589 s->location = new_loc;
593 /* "bytes $from-$to/$document_size" */
594 static void parse_content_range(URLContext *h, const char *p)
596 HTTPContext *s = h->priv_data;
599 if (!strncmp(p, "bytes ", 6)) {
601 s->off = strtoll(p, NULL, 10);
602 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
603 s->filesize = strtoll(slash + 1, NULL, 10);
605 if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
606 h->is_streamed = 0; /* we _can_ in fact seek */
609 static int parse_content_encoding(URLContext *h, const char *p)
611 if (!av_strncasecmp(p, "gzip", 4) ||
612 !av_strncasecmp(p, "deflate", 7)) {
614 HTTPContext *s = h->priv_data;
617 inflateEnd(&s->inflate_stream);
618 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
619 av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
620 s->inflate_stream.msg);
621 return AVERROR(ENOSYS);
623 if (zlibCompileFlags() & (1 << 17)) {
624 av_log(h, AV_LOG_WARNING,
625 "Your zlib was compiled without gzip support.\n");
626 return AVERROR(ENOSYS);
629 av_log(h, AV_LOG_WARNING,
630 "Compressed (%s) content, need zlib with gzip support\n", p);
631 return AVERROR(ENOSYS);
632 #endif /* CONFIG_ZLIB */
633 } else if (!av_strncasecmp(p, "identity", 8)) {
634 // The normal, no-encoding case (although servers shouldn't include
635 // the header at all if this is the case).
637 av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
642 // Concat all Icy- header lines
643 static int parse_icy(HTTPContext *s, const char *tag, const char *p)
645 int len = 4 + strlen(p) + strlen(tag);
646 int is_first = !s->icy_metadata_headers;
649 av_dict_set(&s->metadata, tag, p, 0);
651 if (s->icy_metadata_headers)
652 len += strlen(s->icy_metadata_headers);
654 if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
658 *s->icy_metadata_headers = '\0';
660 av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
665 static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)
669 // duplicate the cookie name (dict will dupe the value)
670 if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);
671 if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);
673 // add the cookie to the dictionary
674 av_dict_set(cookies, name, eql, AV_DICT_DONT_STRDUP_KEY);
679 static int cookie_string(AVDictionary *dict, char **cookies)
681 AVDictionaryEntry *e = NULL;
684 // determine how much memory is needed for the cookies string
685 while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
686 len += strlen(e->key) + strlen(e->value) + 1;
688 // reallocate the cookies
690 if (*cookies) av_free(*cookies);
691 *cookies = av_malloc(len);
692 if (!*cookies) return AVERROR(ENOMEM);
695 // write out the cookies
696 while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
697 av_strlcatf(*cookies, len, "%s%s\n", e->key, e->value);
702 static int process_line(URLContext *h, char *line, int line_count,
705 HTTPContext *s = h->priv_data;
706 const char *auto_method = h->flags & AVIO_FLAG_READ ? "POST" : "GET";
707 char *tag, *p, *end, *method, *resource, *version;
711 if (line[0] == '\0') {
717 if (line_count == 0) {
718 if (s->is_connected_server) {
721 while (*p && !av_isspace(*p))
724 av_log(h, AV_LOG_TRACE, "Received method: %s\n", method);
726 if (av_strcasecmp(s->method, method)) {
727 av_log(h, AV_LOG_ERROR, "Received and expected HTTP method do not match. (%s expected, %s received)\n",
729 return ff_http_averror(400, AVERROR(EIO));
732 // use autodetected HTTP method to expect
733 av_log(h, AV_LOG_TRACE, "Autodetected %s HTTP method\n", auto_method);
734 if (av_strcasecmp(auto_method, method)) {
735 av_log(h, AV_LOG_ERROR, "Received and autodetected HTTP method did not match "
736 "(%s autodetected %s received)\n", auto_method, method);
737 return ff_http_averror(400, AVERROR(EIO));
739 if (!(s->method = av_strdup(method)))
740 return AVERROR(ENOMEM);
744 while (av_isspace(*p))
747 while (!av_isspace(*p))
750 av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
751 if (!(s->resource = av_strdup(resource)))
752 return AVERROR(ENOMEM);
755 while (av_isspace(*p))
758 while (*p && !av_isspace(*p))
761 if (av_strncasecmp(version, "HTTP/", 5)) {
762 av_log(h, AV_LOG_ERROR, "Malformed HTTP version string.\n");
763 return ff_http_averror(400, AVERROR(EIO));
765 av_log(h, AV_LOG_TRACE, "HTTP version string: %s\n", version);
767 while (!av_isspace(*p) && *p != '\0')
769 while (av_isspace(*p))
771 s->http_code = strtol(p, &end, 10);
773 av_log(h, AV_LOG_TRACE, "http_code=%d\n", s->http_code);
775 if ((ret = check_http_code(h, s->http_code, end)) < 0)
779 while (*p != '\0' && *p != ':')
787 while (av_isspace(*p))
789 if (!av_strcasecmp(tag, "Location")) {
790 if ((ret = parse_location(s, p)) < 0)
793 } else if (!av_strcasecmp(tag, "Content-Length") && s->filesize == -1) {
794 s->filesize = strtoll(p, NULL, 10);
795 } else if (!av_strcasecmp(tag, "Content-Range")) {
796 parse_content_range(h, p);
797 } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
798 !strncmp(p, "bytes", 5) &&
801 } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
802 !av_strncasecmp(p, "chunked", 7)) {
805 } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
806 ff_http_auth_handle_header(&s->auth_state, tag, p);
807 } else if (!av_strcasecmp(tag, "Authentication-Info")) {
808 ff_http_auth_handle_header(&s->auth_state, tag, p);
809 } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
810 ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
811 } else if (!av_strcasecmp(tag, "Connection")) {
812 if (!strcmp(p, "close"))
814 } else if (!av_strcasecmp(tag, "Server")) {
815 if (!av_strcasecmp(p, "AkamaiGHost")) {
817 } else if (!av_strncasecmp(p, "MediaGateway", 12)) {
818 s->is_mediagateway = 1;
820 } else if (!av_strcasecmp(tag, "Content-Type")) {
821 av_free(s->mime_type);
822 s->mime_type = av_strdup(p);
823 } else if (!av_strcasecmp(tag, "Set-Cookie")) {
824 if (parse_cookie(s, p, &s->cookie_dict))
825 av_log(h, AV_LOG_WARNING, "Unable to parse '%s'\n", p);
826 } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
827 s->icy_metaint = strtoll(p, NULL, 10);
828 } else if (!av_strncasecmp(tag, "Icy-", 4)) {
829 if ((ret = parse_icy(s, tag, p)) < 0)
831 } else if (!av_strcasecmp(tag, "Content-Encoding")) {
832 if ((ret = parse_content_encoding(h, p)) < 0)
840 * Create a string containing cookie values for use as a HTTP cookie header
841 * field value for a particular path and domain from the cookie values stored in
842 * the HTTP protocol context. The cookie string is stored in *cookies.
844 * @return a negative value if an error condition occurred, 0 otherwise
846 static int get_cookies(HTTPContext *s, char **cookies, const char *path,
849 // cookie strings will look like Set-Cookie header field values. Multiple
850 // Set-Cookie fields will result in multiple values delimited by a newline
852 char *next, *cookie, *set_cookies = av_strdup(s->cookies), *cset_cookies = set_cookies;
854 if (!set_cookies) return AVERROR(EINVAL);
856 // destroy any cookies in the dictionary.
857 av_dict_free(&s->cookie_dict);
860 while ((cookie = av_strtok(set_cookies, "\n", &next))) {
861 int domain_offset = 0;
862 char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
865 // store the cookie in a dict in case it is updated in the response
866 if (parse_cookie(s, cookie, &s->cookie_dict))
867 av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);
869 while ((param = av_strtok(cookie, "; ", &next_param))) {
871 // first key-value pair is the actual cookie value
872 cvalue = av_strdup(param);
874 } else if (!av_strncasecmp("path=", param, 5)) {
876 cpath = av_strdup(¶m[5]);
877 } else if (!av_strncasecmp("domain=", param, 7)) {
878 // if the cookie specifies a sub-domain, skip the leading dot thereby
879 // supporting URLs that point to sub-domains and the master domain
880 int leading_dot = (param[7] == '.');
882 cdomain = av_strdup(¶m[7+leading_dot]);
884 // ignore unknown attributes
888 cdomain = av_strdup(domain);
890 // ensure all of the necessary values are valid
891 if (!cdomain || !cpath || !cvalue) {
892 av_log(s, AV_LOG_WARNING,
893 "Invalid cookie found, no value, path or domain specified\n");
897 // check if the request path matches the cookie path
898 if (av_strncasecmp(path, cpath, strlen(cpath)))
901 // the domain should be at least the size of our cookie domain
902 domain_offset = strlen(domain) - strlen(cdomain);
903 if (domain_offset < 0)
906 // match the cookie domain
907 if (av_strcasecmp(&domain[domain_offset], cdomain))
910 // cookie parameters match, so copy the value
912 if (!(*cookies = av_strdup(cvalue))) {
913 ret = AVERROR(ENOMEM);
917 char *tmp = *cookies;
918 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
919 if (!(*cookies = av_malloc(str_size))) {
920 ret = AVERROR(ENOMEM);
923 snprintf(*cookies, str_size, "%s; %s", tmp, cvalue);
932 if (*cookies) av_freep(cookies);
933 av_free(cset_cookies);
938 av_free(cset_cookies);
943 static inline int has_header(const char *str, const char *header)
945 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
948 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
951 static int http_read_header(URLContext *h, int *new_location)
953 HTTPContext *s = h->priv_data;
954 char line[MAX_URL_SIZE];
960 if ((err = http_get_line(s, line, sizeof(line))) < 0)
963 av_log(h, AV_LOG_TRACE, "header='%s'\n", line);
965 err = process_line(h, line, s->line_count, new_location);
973 if (s->seekable == -1 && s->is_mediagateway && s->filesize == 2000000000)
974 h->is_streamed = 1; /* we can in fact _not_ seek */
976 // add any new cookies into the existing cookie string
977 cookie_string(s->cookie_dict, &s->cookies);
978 av_dict_free(&s->cookie_dict);
983 static int http_connect(URLContext *h, const char *path, const char *local_path,
984 const char *hoststr, const char *auth,
985 const char *proxyauth, int *new_location)
987 HTTPContext *s = h->priv_data;
989 char headers[HTTP_HEADERS_SIZE] = "";
990 char *authstr = NULL, *proxyauthstr = NULL;
991 int64_t off = s->off;
994 int send_expect_100 = 0;
996 /* send http header */
997 post = h->flags & AVIO_FLAG_WRITE;
1000 /* force POST method and disable chunked encoding when
1001 * custom HTTP post data is set */
1003 s->chunked_post = 0;
1009 method = post ? "POST" : "GET";
1011 authstr = ff_http_auth_create_response(&s->auth_state, auth,
1012 local_path, method);
1013 proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
1014 local_path, method);
1015 if (post && !s->post_data) {
1016 send_expect_100 = s->send_expect_100;
1017 /* The user has supplied authentication but we don't know the auth type,
1018 * send Expect: 100-continue to get the 401 response including the
1019 * WWW-Authenticate header, or an 100 continue if no auth actually
1021 if (auth && *auth &&
1022 s->auth_state.auth_type == HTTP_AUTH_NONE &&
1023 s->http_code != 401)
1024 send_expect_100 = 1;
1027 /* set default headers if needed */
1028 if (!has_header(s->headers, "\r\nUser-Agent: "))
1029 len += av_strlcatf(headers + len, sizeof(headers) - len,
1030 "User-Agent: %s\r\n", s->user_agent);
1031 if (!has_header(s->headers, "\r\nAccept: "))
1032 len += av_strlcpy(headers + len, "Accept: */*\r\n",
1033 sizeof(headers) - len);
1034 // Note: we send this on purpose even when s->off is 0 when we're probing,
1035 // since it allows us to detect more reliably if a (non-conforming)
1036 // server supports seeking by analysing the reply headers.
1037 if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable == -1)) {
1038 len += av_strlcatf(headers + len, sizeof(headers) - len,
1039 "Range: bytes=%"PRId64"-", s->off);
1041 len += av_strlcatf(headers + len, sizeof(headers) - len,
1042 "%"PRId64, s->end_off - 1);
1043 len += av_strlcpy(headers + len, "\r\n",
1044 sizeof(headers) - len);
1046 if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
1047 len += av_strlcatf(headers + len, sizeof(headers) - len,
1048 "Expect: 100-continue\r\n");
1050 if (!has_header(s->headers, "\r\nConnection: ")) {
1051 if (s->multiple_requests)
1052 len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
1053 sizeof(headers) - len);
1055 len += av_strlcpy(headers + len, "Connection: close\r\n",
1056 sizeof(headers) - len);
1059 if (!has_header(s->headers, "\r\nHost: "))
1060 len += av_strlcatf(headers + len, sizeof(headers) - len,
1061 "Host: %s\r\n", hoststr);
1062 if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
1063 len += av_strlcatf(headers + len, sizeof(headers) - len,
1064 "Content-Length: %d\r\n", s->post_datalen);
1066 if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
1067 len += av_strlcatf(headers + len, sizeof(headers) - len,
1068 "Content-Type: %s\r\n", s->content_type);
1069 if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
1070 char *cookies = NULL;
1071 if (!get_cookies(s, &cookies, path, hoststr) && cookies) {
1072 len += av_strlcatf(headers + len, sizeof(headers) - len,
1073 "Cookie: %s\r\n", cookies);
1077 if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
1078 len += av_strlcatf(headers + len, sizeof(headers) - len,
1079 "Icy-MetaData: %d\r\n", 1);
1081 /* now add in custom headers */
1083 av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
1085 snprintf(s->buffer, sizeof(s->buffer),
1086 "%s %s HTTP/1.1\r\n"
1094 post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
1096 authstr ? authstr : "",
1097 proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
1099 av_log(h, AV_LOG_DEBUG, "request: %s\n", s->buffer);
1101 if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1105 if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
1108 /* init input buffer */
1109 s->buf_ptr = s->buffer;
1110 s->buf_end = s->buffer;
1113 s->icy_data_read = 0;
1116 s->end_chunked_post = 0;
1118 if (post && !s->post_data && !send_expect_100) {
1119 /* Pretend that it did work. We didn't read any header yet, since
1120 * we've still to send the POST data, but the code calling this
1121 * function will check http_code after we return. */
1127 /* wait for header */
1128 err = http_read_header(h, new_location);
1135 err = (off == s->off) ? 0 : -1;
1138 av_freep(&proxyauthstr);
1142 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
1144 HTTPContext *s = h->priv_data;
1146 /* read bytes from input buffer first */
1147 len = s->buf_end - s->buf_ptr;
1151 memcpy(buf, s->buf_ptr, len);
1154 if ((!s->willclose || s->chunksize < 0) &&
1155 s->filesize >= 0 && s->off >= s->filesize)
1157 len = ffurl_read(s->hd, buf, size);
1158 if (!len && (!s->willclose || s->chunksize < 0) &&
1159 s->filesize >= 0 && s->off < s->filesize) {
1160 av_log(h, AV_LOG_ERROR,
1161 "Stream ends prematurely at %"PRId64", should be %"PRId64"\n",
1164 return AVERROR(EIO);
1169 if (s->chunksize > 0)
1170 s->chunksize -= len;
1176 #define DECOMPRESS_BUF_SIZE (256 * 1024)
1177 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
1179 HTTPContext *s = h->priv_data;
1182 if (!s->inflate_buffer) {
1183 s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
1184 if (!s->inflate_buffer)
1185 return AVERROR(ENOMEM);
1188 if (s->inflate_stream.avail_in == 0) {
1189 int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
1192 s->inflate_stream.next_in = s->inflate_buffer;
1193 s->inflate_stream.avail_in = read;
1196 s->inflate_stream.avail_out = size;
1197 s->inflate_stream.next_out = buf;
1199 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
1200 if (ret != Z_OK && ret != Z_STREAM_END)
1201 av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
1202 ret, s->inflate_stream.msg);
1204 return size - s->inflate_stream.avail_out;
1206 #endif /* CONFIG_ZLIB */
1208 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect);
1210 static int http_read_stream(URLContext *h, uint8_t *buf, int size)
1212 HTTPContext *s = h->priv_data;
1213 int err, new_location, read_ret, seek_ret;
1218 if (s->end_chunked_post && !s->end_header) {
1219 err = http_read_header(h, &new_location);
1224 if (s->chunksize >= 0) {
1225 if (!s->chunksize) {
1229 if ((err = http_get_line(s, line, sizeof(line))) < 0)
1231 } while (!*line); /* skip CR LF from last chunk */
1233 s->chunksize = strtoll(line, NULL, 16);
1235 av_log(NULL, AV_LOG_TRACE, "Chunked encoding data size: %"PRId64"'\n",
1241 size = FFMIN(size, s->chunksize);
1245 return http_buf_read_compressed(h, buf, size);
1246 #endif /* CONFIG_ZLIB */
1247 read_ret = http_buf_read(h, buf, size);
1248 if ( (read_ret < 0 && s->reconnect && (!h->is_streamed || s->reconnect_streamed) && s->filesize > 0 && s->off < s->filesize)
1249 || (read_ret == 0 && s->reconnect_at_eof && (!h->is_streamed || s->reconnect_streamed))) {
1250 int64_t target = h->is_streamed ? 0 : s->off;
1251 av_log(h, AV_LOG_INFO, "Will reconnect at %"PRId64" error=%s.\n", s->off, av_err2str(read_ret));
1252 seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
1253 if (seek_ret != target) {
1254 av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRId64".\n", target);
1258 read_ret = http_buf_read(h, buf, size);
1264 // Like http_read_stream(), but no short reads.
1265 // Assumes partial reads are an error.
1266 static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
1269 while (pos < size) {
1270 int len = http_read_stream(h, buf + pos, size - pos);
1278 static void update_metadata(HTTPContext *s, char *data)
1287 val = strstr(key, "='");
1290 end = strstr(val, "';");
1298 av_dict_set(&s->metadata, key, val, 0);
1304 static int store_icy(URLContext *h, int size)
1306 HTTPContext *s = h->priv_data;
1307 /* until next metadata packet */
1308 int remaining = s->icy_metaint - s->icy_data_read;
1311 return AVERROR_INVALIDDATA;
1314 /* The metadata packet is variable sized. It has a 1 byte header
1315 * which sets the length of the packet (divided by 16). If it's 0,
1316 * the metadata doesn't change. After the packet, icy_metaint bytes
1317 * of normal data follows. */
1319 int len = http_read_stream_all(h, &ch, 1);
1323 char data[255 * 16 + 1];
1326 ret = http_read_stream_all(h, data, len);
1330 if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
1332 update_metadata(s, data);
1334 s->icy_data_read = 0;
1335 remaining = s->icy_metaint;
1338 return FFMIN(size, remaining);
1341 static int http_read(URLContext *h, uint8_t *buf, int size)
1343 HTTPContext *s = h->priv_data;
1345 if (s->icy_metaint > 0) {
1346 size = store_icy(h, size);
1351 size = http_read_stream(h, buf, size);
1353 s->icy_data_read += size;
1357 /* used only when posting data */
1358 static int http_write(URLContext *h, const uint8_t *buf, int size)
1360 char temp[11] = ""; /* 32-bit hex + CRLF + nul */
1362 char crlf[] = "\r\n";
1363 HTTPContext *s = h->priv_data;
1365 if (!s->chunked_post) {
1366 /* non-chunked data is sent without any special encoding */
1367 return ffurl_write(s->hd, buf, size);
1370 /* silently ignore zero-size data since chunk encoding that would
1373 /* upload data using chunked encoding */
1374 snprintf(temp, sizeof(temp), "%x\r\n", size);
1376 if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
1377 (ret = ffurl_write(s->hd, buf, size)) < 0 ||
1378 (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
1384 static int http_shutdown(URLContext *h, int flags)
1387 char footer[] = "0\r\n\r\n";
1388 HTTPContext *s = h->priv_data;
1390 /* signal end of chunked encoding if used */
1391 if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
1392 ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
1393 ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
1394 ret = ret > 0 ? 0 : ret;
1395 s->end_chunked_post = 1;
1401 static int http_close(URLContext *h)
1404 HTTPContext *s = h->priv_data;
1407 inflateEnd(&s->inflate_stream);
1408 av_freep(&s->inflate_buffer);
1409 #endif /* CONFIG_ZLIB */
1411 if (!s->end_chunked_post)
1412 /* Close the write direction by sending the end of chunked encoding. */
1413 ret = http_shutdown(h, h->flags);
1416 ffurl_closep(&s->hd);
1417 av_dict_free(&s->chained_options);
1421 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
1423 HTTPContext *s = h->priv_data;
1424 URLContext *old_hd = s->hd;
1425 int64_t old_off = s->off;
1426 uint8_t old_buf[BUFFER_SIZE];
1427 int old_buf_size, ret;
1428 AVDictionary *options = NULL;
1430 if (whence == AVSEEK_SIZE)
1432 else if (!force_reconnect &&
1433 ((whence == SEEK_CUR && off == 0) ||
1434 (whence == SEEK_SET && off == s->off)))
1436 else if ((s->filesize == -1 && whence == SEEK_END))
1437 return AVERROR(ENOSYS);
1439 if (whence == SEEK_CUR)
1441 else if (whence == SEEK_END)
1443 else if (whence != SEEK_SET)
1444 return AVERROR(EINVAL);
1446 return AVERROR(EINVAL);
1449 if (s->off && h->is_streamed)
1450 return AVERROR(ENOSYS);
1452 /* we save the old context in case the seek fails */
1453 old_buf_size = s->buf_end - s->buf_ptr;
1454 memcpy(old_buf, s->buf_ptr, old_buf_size);
1457 /* if it fails, continue on old connection */
1458 if ((ret = http_open_cnx(h, &options)) < 0) {
1459 av_dict_free(&options);
1460 memcpy(s->buffer, old_buf, old_buf_size);
1461 s->buf_ptr = s->buffer;
1462 s->buf_end = s->buffer + old_buf_size;
1467 av_dict_free(&options);
1468 ffurl_close(old_hd);
1472 static int64_t http_seek(URLContext *h, int64_t off, int whence)
1474 return http_seek_internal(h, off, whence, 0);
1477 static int http_get_file_handle(URLContext *h)
1479 HTTPContext *s = h->priv_data;
1480 return ffurl_get_file_handle(s->hd);
1483 #define HTTP_CLASS(flavor) \
1484 static const AVClass flavor ## _context_class = { \
1485 .class_name = # flavor, \
1486 .item_name = av_default_item_name, \
1487 .option = options, \
1488 .version = LIBAVUTIL_VERSION_INT, \
1491 #if CONFIG_HTTP_PROTOCOL
1494 URLProtocol ff_http_protocol = {
1496 .url_open2 = http_open,
1497 .url_accept = http_accept,
1498 .url_handshake = http_handshake,
1499 .url_read = http_read,
1500 .url_write = http_write,
1501 .url_seek = http_seek,
1502 .url_close = http_close,
1503 .url_get_file_handle = http_get_file_handle,
1504 .url_shutdown = http_shutdown,
1505 .priv_data_size = sizeof(HTTPContext),
1506 .priv_data_class = &http_context_class,
1507 .flags = URL_PROTOCOL_FLAG_NETWORK,
1509 #endif /* CONFIG_HTTP_PROTOCOL */
1511 #if CONFIG_HTTPS_PROTOCOL
1514 URLProtocol ff_https_protocol = {
1516 .url_open2 = http_open,
1517 .url_read = http_read,
1518 .url_write = http_write,
1519 .url_seek = http_seek,
1520 .url_close = http_close,
1521 .url_get_file_handle = http_get_file_handle,
1522 .url_shutdown = http_shutdown,
1523 .priv_data_size = sizeof(HTTPContext),
1524 .priv_data_class = &https_context_class,
1525 .flags = URL_PROTOCOL_FLAG_NETWORK,
1527 #endif /* CONFIG_HTTPS_PROTOCOL */
1529 #if CONFIG_HTTPPROXY_PROTOCOL
1530 static int http_proxy_close(URLContext *h)
1532 HTTPContext *s = h->priv_data;
1534 ffurl_closep(&s->hd);
1538 static int http_proxy_open(URLContext *h, const char *uri, int flags)
1540 HTTPContext *s = h->priv_data;
1541 char hostname[1024], hoststr[1024];
1542 char auth[1024], pathbuf[1024], *path;
1543 char lower_url[100];
1544 int port, ret = 0, attempts = 0;
1545 HTTPAuthType cur_auth_type;
1549 if( s->seekable == 1 )
1554 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
1555 pathbuf, sizeof(pathbuf), uri);
1556 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1561 ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
1564 ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
1565 &h->interrupt_callback, NULL);
1569 authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
1571 snprintf(s->buffer, sizeof(s->buffer),
1572 "CONNECT %s HTTP/1.1\r\n"
1574 "Connection: close\r\n"
1579 authstr ? "Proxy-" : "", authstr ? authstr : "");
1582 if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1585 s->buf_ptr = s->buffer;
1586 s->buf_end = s->buffer;
1589 cur_auth_type = s->proxy_auth_state.auth_type;
1591 /* Note: This uses buffering, potentially reading more than the
1592 * HTTP header. If tunneling a protocol where the server starts
1593 * the conversation, we might buffer part of that here, too.
1594 * Reading that requires using the proper ffurl_read() function
1595 * on this URLContext, not using the fd directly (as the tls
1596 * protocol does). This shouldn't be an issue for tls though,
1597 * since the client starts the conversation there, so there
1598 * is no extra data that we might buffer up here.
1600 ret = http_read_header(h, &new_loc);
1605 if (s->http_code == 407 &&
1606 (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
1607 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
1608 ffurl_closep(&s->hd);
1612 if (s->http_code < 400)
1614 ret = ff_http_averror(s->http_code, AVERROR(EIO));
1617 http_proxy_close(h);
1621 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
1623 HTTPContext *s = h->priv_data;
1624 return ffurl_write(s->hd, buf, size);
1627 URLProtocol ff_httpproxy_protocol = {
1628 .name = "httpproxy",
1629 .url_open = http_proxy_open,
1630 .url_read = http_buf_read,
1631 .url_write = http_proxy_write,
1632 .url_close = http_proxy_close,
1633 .url_get_file_handle = http_get_file_handle,
1634 .priv_data_size = sizeof(HTTPContext),
1635 .flags = URL_PROTOCOL_FLAG_NETWORK,
1637 #endif /* CONFIG_HTTPPROXY_PROTOCOL */