2 * HTTP protocol for avconv client
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avstring.h"
27 #include "os_support.h"
30 #include "libavutil/opt.h"
36 /* XXX: POST protocol is not completely implemented because avconv uses
37 only a subset of it. */
39 /* The IO buffer size is unrelated to the max URL size in itself, but needs
40 * to be large enough to fit the full request headers (including long
43 #define BUFFER_SIZE MAX_URL_SIZE
44 #define MAX_REDIRECTS 8
49 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
52 /* Used if "Transfer-Encoding: chunked" otherwise -1. */
54 int64_t off, filesize;
56 HTTPAuthState auth_state;
57 HTTPAuthState proxy_auth_state;
60 /* Set if the server correctly handles Connection: close and will close
61 * the connection after feeding us the content. */
64 /* A flag which indicates if the end of chunked encoding has been sent. */
66 /* A flag which indicates we have finished to read POST reply. */
68 /* A flag which indicates if we use persistent connections. */
69 int multiple_requests;
73 /* how much data was read since the last ICY metadata packet */
75 /* after how many bytes of read data a new metadata packet will be found */
77 char *icy_metadata_headers;
78 char *icy_metadata_packet;
81 z_stream inflate_stream;
82 uint8_t *inflate_buffer;
84 AVDictionary *chained_options;
88 #define OFFSET(x) offsetof(HTTPContext, x)
89 #define D AV_OPT_FLAG_DECODING_PARAM
90 #define E AV_OPT_FLAG_ENCODING_PARAM
91 static const AVOption options[] = {
92 {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
93 {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
94 {"multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
95 {"post_data", "custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D|E },
96 {"mime_type", "export the MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, {0}, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
97 {"icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D },
98 {"icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
99 {"icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
100 {"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" },
101 {"none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, {.i64 = HTTP_AUTH_NONE}, 0, 0, D|E, "auth_type" },
102 {"basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, {.i64 = HTTP_AUTH_BASIC}, 0, 0, D|E, "auth_type" },
103 {"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 },
104 {"location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
107 #define HTTP_CLASS(flavor)\
108 static const AVClass flavor ## _context_class = {\
109 .class_name = #flavor,\
110 .item_name = av_default_item_name,\
112 .version = LIBAVUTIL_VERSION_INT,\
118 static int http_connect(URLContext *h, const char *path, const char *local_path,
119 const char *hoststr, const char *auth,
120 const char *proxyauth, int *new_location);
122 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
124 memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
125 &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
126 memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state,
127 &((HTTPContext*)src->priv_data)->proxy_auth_state,
128 sizeof(HTTPAuthState));
131 /* return non zero if error */
132 static int http_open_cnx(URLContext *h, AVDictionary **options)
134 const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
135 char hostname[1024], hoststr[1024], proto[10];
136 char auth[1024], proxyauth[1024] = "";
137 char path1[MAX_URL_SIZE];
138 char buf[1024], urlbuf[MAX_URL_SIZE];
139 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
140 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
141 HTTPContext *s = h->priv_data;
143 /* fill the dest addr */
145 /* needed in any case to build the host string */
146 av_url_split(proto, sizeof(proto), auth, sizeof(auth),
147 hostname, sizeof(hostname), &port,
148 path1, sizeof(path1), s->location);
149 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
151 proxy_path = getenv("http_proxy");
152 use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
153 proxy_path != NULL && av_strstart(proxy_path, "http://", NULL);
155 if (!strcmp(proto, "https")) {
164 if (path1[0] == '\0')
170 /* Reassemble the request URL without auth string - we don't
171 * want to leak the auth to the proxy. */
172 ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
175 av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
176 hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
179 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
182 err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE,
183 &h->interrupt_callback, options);
188 cur_auth_type = s->auth_state.auth_type;
189 cur_proxy_auth_type = s->auth_state.auth_type;
190 if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
193 if (s->http_code == 401) {
194 if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
195 s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
202 if (s->http_code == 407) {
203 if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
204 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
211 if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
212 && location_changed == 1) {
213 /* url moved, get next */
216 if (redirects++ >= MAX_REDIRECTS)
218 /* Restart the authentication process with the new target, which
219 * might use a different auth mechanism. */
220 memset(&s->auth_state, 0, sizeof(s->auth_state));
222 location_changed = 0;
233 int ff_http_do_new_request(URLContext *h, const char *uri)
235 HTTPContext *s = h->priv_data;
236 AVDictionary *options = NULL;
240 s->icy_data_read = 0;
241 av_free(s->location);
242 s->location = av_strdup(uri);
244 return AVERROR(ENOMEM);
246 av_dict_copy(&options, s->chained_options, 0);
247 ret = http_open_cnx(h, &options);
248 av_dict_free(&options);
252 static int http_open(URLContext *h, const char *uri, int flags,
253 AVDictionary **options)
255 HTTPContext *s = h->priv_data;
261 s->location = av_strdup(uri);
263 return AVERROR(ENOMEM);
265 av_dict_copy(&s->chained_options, *options, 0);
268 int len = strlen(s->headers);
269 if (len < 2 || strcmp("\r\n", s->headers + len - 2))
270 av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
273 ret = http_open_cnx(h, options);
275 av_dict_free(&s->chained_options);
278 static int http_getc(HTTPContext *s)
281 if (s->buf_ptr >= s->buf_end) {
282 len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
285 } else if (len == 0) {
288 s->buf_ptr = s->buffer;
289 s->buf_end = s->buffer + len;
292 return *s->buf_ptr++;
295 static int http_get_line(HTTPContext *s, char *line, int line_size)
307 if (q > line && q[-1] == '\r')
313 if ((q - line) < line_size - 1)
319 static int check_http_code(URLContext *h, int http_code, const char *end)
321 HTTPContext *s = h->priv_data;
322 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
323 * don't abort until all headers have been parsed. */
324 if (http_code >= 400 && http_code < 600 &&
325 (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
326 (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
327 end += strspn(end, SPACE_CHARS);
328 av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
334 static int parse_location(HTTPContext *s, const char *p)
336 char redirected_location[MAX_URL_SIZE], *new_loc;
337 ff_make_absolute_url(redirected_location, sizeof(redirected_location),
339 new_loc = av_strdup(redirected_location);
341 return AVERROR(ENOMEM);
342 av_free(s->location);
343 s->location = new_loc;
347 /* "bytes $from-$to/$document_size" */
348 static void parse_content_range(URLContext *h, char *p)
350 HTTPContext *s = h->priv_data;
353 if (!strncmp(p, "bytes ", 6)) {
355 s->off = strtoll(p, NULL, 10);
356 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
357 s->filesize = strtoll(slash+1, NULL, 10);
359 h->is_streamed = 0; /* we _can_ in fact seek */
362 static int parse_content_encoding(URLContext *h, char *p)
364 HTTPContext *s = h->priv_data;
366 if (!av_strncasecmp(p, "gzip", 4) ||
367 !av_strncasecmp(p, "deflate", 7)) {
370 inflateEnd(&s->inflate_stream);
371 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
372 av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
373 s->inflate_stream.msg);
374 return AVERROR(ENOSYS);
376 if (zlibCompileFlags() & (1 << 17)) {
377 av_log(h, AV_LOG_WARNING,
378 "Your zlib was compiled without gzip support.\n");
379 return AVERROR(ENOSYS);
382 av_log(h, AV_LOG_WARNING,
383 "Compressed (%s) content, need zlib with gzip support\n", p);
384 return AVERROR(ENOSYS);
386 } else if (!av_strncasecmp(p, "identity", 8)) {
387 // The normal, no-encoding case (although servers shouldn't include
388 // the header at all if this is the case).
390 av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
391 return AVERROR(ENOSYS);
396 // Concat all Icy- header lines
397 static int parse_icy(HTTPContext *s, const char *tag, const char *p)
399 int len = 4 + strlen(p) + strlen(tag);
402 if (s->icy_metadata_headers)
403 len += strlen(s->icy_metadata_headers);
405 if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
408 av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
413 static int process_line(URLContext *h, char *line, int line_count,
416 HTTPContext *s = h->priv_data;
421 if (line[0] == '\0') {
427 if (line_count == 0) {
428 while (!av_isspace(*p) && *p != '\0')
430 while (av_isspace(*p))
432 s->http_code = strtol(p, &end, 10);
434 av_dlog(NULL, "http_code=%d\n", s->http_code);
436 if ((ret = check_http_code(h, s->http_code, end)) < 0)
439 while (*p != '\0' && *p != ':')
447 while (av_isspace(*p))
449 if (!av_strcasecmp(tag, "Location")) {
450 if ((ret = parse_location(s, p)) < 0)
453 } else if (!av_strcasecmp(tag, "Content-Length") && s->filesize == -1) {
454 s->filesize = strtoll(p, NULL, 10);
455 } else if (!av_strcasecmp(tag, "Content-Range")) {
456 parse_content_range(h, p);
457 } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
458 !strncmp(p, "bytes", 5)) {
460 } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
461 !av_strncasecmp(p, "chunked", 7)) {
464 } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
465 ff_http_auth_handle_header(&s->auth_state, tag, p);
466 } else if (!av_strcasecmp(tag, "Authentication-Info")) {
467 ff_http_auth_handle_header(&s->auth_state, tag, p);
468 } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
469 ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
470 } else if (!av_strcasecmp(tag, "Connection")) {
471 if (!strcmp(p, "close"))
473 } else if (!av_strcasecmp (tag, "Content-Type")) {
474 av_free(s->mime_type);
475 s->mime_type = av_strdup(p);
476 } else if (!av_strcasecmp (tag, "Icy-MetaInt")) {
477 s->icy_metaint = strtoll(p, NULL, 10);
478 } else if (!av_strncasecmp(tag, "Icy-", 4)) {
479 if ((ret = parse_icy(s, tag, p)) < 0)
481 } else if (!av_strcasecmp(tag, "Content-Encoding")) {
482 if ((ret = parse_content_encoding(h, p)) < 0)
489 static inline int has_header(const char *str, const char *header)
491 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
494 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
497 static int http_read_header(URLContext *h, int *new_location)
499 HTTPContext *s = h->priv_data;
500 char line[MAX_URL_SIZE];
506 if ((err = http_get_line(s, line, sizeof(line))) < 0)
509 av_dlog(NULL, "header='%s'\n", line);
511 err = process_line(h, line, s->line_count, new_location);
522 static int http_connect(URLContext *h, const char *path, const char *local_path,
523 const char *hoststr, const char *auth,
524 const char *proxyauth, int *new_location)
526 HTTPContext *s = h->priv_data;
528 char headers[1024] = "";
529 char *authstr = NULL, *proxyauthstr = NULL;
530 int64_t off = s->off;
533 int send_expect_100 = 0;
536 /* send http header */
537 post = h->flags & AVIO_FLAG_WRITE;
540 /* force POST method and disable chunked encoding when
541 * custom HTTP post data is set */
546 method = post ? "POST" : "GET";
547 authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
549 proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
551 if (post && !s->post_data) {
552 send_expect_100 = s->send_expect_100;
553 /* The user has supplied authentication but we don't know the auth type,
554 * send Expect: 100-continue to get the 401 response including the
555 * WWW-Authenticate header, or an 100 continue if no auth actually
558 s->auth_state.auth_type == HTTP_AUTH_NONE &&
563 /* set default headers if needed */
564 if (!has_header(s->headers, "\r\nUser-Agent: "))
565 len += av_strlcatf(headers + len, sizeof(headers) - len,
566 "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
567 if (!has_header(s->headers, "\r\nAccept: "))
568 len += av_strlcpy(headers + len, "Accept: */*\r\n",
569 sizeof(headers) - len);
570 if (!has_header(s->headers, "\r\nRange: ") && !post)
571 len += av_strlcatf(headers + len, sizeof(headers) - len,
572 "Range: bytes=%"PRId64"-\r\n", s->off);
573 if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
574 len += av_strlcatf(headers + len, sizeof(headers) - len,
575 "Expect: 100-continue\r\n");
577 if (!has_header(s->headers, "\r\nConnection: ")) {
578 if (s->multiple_requests) {
579 len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
580 sizeof(headers) - len);
582 len += av_strlcpy(headers + len, "Connection: close\r\n",
583 sizeof(headers) - len);
587 if (!has_header(s->headers, "\r\nHost: "))
588 len += av_strlcatf(headers + len, sizeof(headers) - len,
589 "Host: %s\r\n", hoststr);
590 if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
591 len += av_strlcatf(headers + len, sizeof(headers) - len,
592 "Content-Length: %d\r\n", s->post_datalen);
593 if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy) {
594 len += av_strlcatf(headers + len, sizeof(headers) - len,
595 "Icy-MetaData: %d\r\n", 1);
598 /* now add in custom headers */
600 av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
602 snprintf(s->buffer, sizeof(s->buffer),
611 post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
613 authstr ? authstr : "",
614 proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
617 av_freep(&proxyauthstr);
618 if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
622 if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
625 /* init input buffer */
626 s->buf_ptr = s->buffer;
627 s->buf_end = s->buffer;
630 s->icy_data_read = 0;
633 s->end_chunked_post = 0;
635 if (post && !s->post_data && !send_expect_100) {
636 /* Pretend that it did work. We didn't read any header yet, since
637 * we've still to send the POST data, but the code calling this
638 * function will check http_code after we return. */
643 /* wait for header */
644 err = http_read_header(h, new_location);
648 return (off == s->off) ? 0 : -1;
652 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
654 HTTPContext *s = h->priv_data;
656 /* read bytes from input buffer first */
657 len = s->buf_end - s->buf_ptr;
661 memcpy(buf, s->buf_ptr, len);
664 if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
666 len = ffurl_read(s->hd, buf, size);
670 if (s->chunksize > 0)
677 #define DECOMPRESS_BUF_SIZE (256 * 1024)
678 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
680 HTTPContext *s = h->priv_data;
683 if (!s->inflate_buffer) {
684 s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
685 if (!s->inflate_buffer)
686 return AVERROR(ENOMEM);
689 if (s->inflate_stream.avail_in == 0) {
690 int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
693 s->inflate_stream.next_in = s->inflate_buffer;
694 s->inflate_stream.avail_in = read;
697 s->inflate_stream.avail_out = size;
698 s->inflate_stream.next_out = buf;
700 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
701 if (ret != Z_OK && ret != Z_STREAM_END)
702 av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n", ret, s->inflate_stream.msg);
704 return size - s->inflate_stream.avail_out;
708 static int http_read_stream(URLContext *h, uint8_t *buf, int size)
710 HTTPContext *s = h->priv_data;
711 int err, new_location;
716 if (s->end_chunked_post && !s->end_header) {
717 err = http_read_header(h, &new_location);
722 if (s->chunksize >= 0) {
728 if ((err = http_get_line(s, line, sizeof(line))) < 0)
730 } while (!*line); /* skip CR LF from last chunk */
732 s->chunksize = strtoll(line, NULL, 16);
734 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
741 size = FFMIN(size, s->chunksize);
745 return http_buf_read_compressed(h, buf, size);
747 return http_buf_read(h, buf, size);
750 static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
754 int len = http_read_stream(h, buf + pos, size - pos);
762 static int store_icy(URLContext *h, int size)
764 HTTPContext *s = h->priv_data;
765 /* until next metadata packet */
766 int remaining = s->icy_metaint - s->icy_data_read;
769 return AVERROR_INVALIDDATA;
772 // The metadata packet is variable sized. It has a 1 byte header
773 // which sets the length of the packet (divided by 16). If it's 0,
774 // the metadata doesn't change. After the packet, icy_metaint bytes
775 // of normal data follow.
777 int len = http_read_stream_all(h, &ch, 1);
781 char data[255 * 16 + 1];
784 ret = http_read_stream_all(h, data, len);
788 if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
791 s->icy_data_read = 0;
792 remaining = s->icy_metaint;
795 return FFMIN(size, remaining);
798 static int http_read(URLContext *h, uint8_t *buf, int size)
800 HTTPContext *s = h->priv_data;
802 if (s->icy_metaint > 0) {
803 size = store_icy(h, size);
808 size = http_read_stream(h, buf, size);
810 s->icy_data_read += size;
814 /* used only when posting data */
815 static int http_write(URLContext *h, const uint8_t *buf, int size)
817 char temp[11] = ""; /* 32-bit hex + CRLF + nul */
819 char crlf[] = "\r\n";
820 HTTPContext *s = h->priv_data;
822 if (!s->chunked_post) {
823 /* non-chunked data is sent without any special encoding */
824 return ffurl_write(s->hd, buf, size);
827 /* silently ignore zero-size data since chunk encoding that would
830 /* upload data using chunked encoding */
831 snprintf(temp, sizeof(temp), "%x\r\n", size);
833 if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
834 (ret = ffurl_write(s->hd, buf, size)) < 0 ||
835 (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
841 static int http_shutdown(URLContext *h, int flags)
844 char footer[] = "0\r\n\r\n";
845 HTTPContext *s = h->priv_data;
847 /* signal end of chunked encoding if used */
848 if ((flags & AVIO_FLAG_WRITE) && s->chunked_post) {
849 ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
850 ret = ret > 0 ? 0 : ret;
851 s->end_chunked_post = 1;
857 static int http_close(URLContext *h)
860 HTTPContext *s = h->priv_data;
863 inflateEnd(&s->inflate_stream);
864 av_freep(&s->inflate_buffer);
867 if (!s->end_chunked_post) {
868 /* Close the write direction by sending the end of chunked encoding. */
869 ret = http_shutdown(h, h->flags);
874 av_dict_free(&s->chained_options);
878 static int64_t http_seek(URLContext *h, int64_t off, int whence)
880 HTTPContext *s = h->priv_data;
881 URLContext *old_hd = s->hd;
882 int64_t old_off = s->off;
883 uint8_t old_buf[BUFFER_SIZE];
884 int old_buf_size, ret;
885 AVDictionary *options = NULL;
887 if (whence == AVSEEK_SIZE)
889 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
890 return AVERROR(ENOSYS);
892 /* we save the old context in case the seek fails */
893 old_buf_size = s->buf_end - s->buf_ptr;
894 memcpy(old_buf, s->buf_ptr, old_buf_size);
896 if (whence == SEEK_CUR)
898 else if (whence == SEEK_END)
902 /* if it fails, continue on old connection */
903 av_dict_copy(&options, s->chained_options, 0);
904 if ((ret = http_open_cnx(h, &options)) < 0) {
905 av_dict_free(&options);
906 memcpy(s->buffer, old_buf, old_buf_size);
907 s->buf_ptr = s->buffer;
908 s->buf_end = s->buffer + old_buf_size;
913 av_dict_free(&options);
919 http_get_file_handle(URLContext *h)
921 HTTPContext *s = h->priv_data;
922 return ffurl_get_file_handle(s->hd);
925 #if CONFIG_HTTP_PROTOCOL
926 URLProtocol ff_http_protocol = {
928 .url_open2 = http_open,
929 .url_read = http_read,
930 .url_write = http_write,
931 .url_seek = http_seek,
932 .url_close = http_close,
933 .url_get_file_handle = http_get_file_handle,
934 .url_shutdown = http_shutdown,
935 .priv_data_size = sizeof(HTTPContext),
936 .priv_data_class = &http_context_class,
937 .flags = URL_PROTOCOL_FLAG_NETWORK,
940 #if CONFIG_HTTPS_PROTOCOL
941 URLProtocol ff_https_protocol = {
943 .url_open2 = http_open,
944 .url_read = http_read,
945 .url_write = http_write,
946 .url_seek = http_seek,
947 .url_close = http_close,
948 .url_get_file_handle = http_get_file_handle,
949 .url_shutdown = http_shutdown,
950 .priv_data_size = sizeof(HTTPContext),
951 .priv_data_class = &https_context_class,
952 .flags = URL_PROTOCOL_FLAG_NETWORK,
956 #if CONFIG_HTTPPROXY_PROTOCOL
957 static int http_proxy_close(URLContext *h)
959 HTTPContext *s = h->priv_data;
965 static int http_proxy_open(URLContext *h, const char *uri, int flags)
967 HTTPContext *s = h->priv_data;
968 char hostname[1024], hoststr[1024];
969 char auth[1024], pathbuf[1024], *path;
971 int port, ret = 0, attempts = 0;
972 HTTPAuthType cur_auth_type;
978 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
979 pathbuf, sizeof(pathbuf), uri);
980 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
985 ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
988 ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
989 &h->interrupt_callback, NULL);
993 authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
995 snprintf(s->buffer, sizeof(s->buffer),
996 "CONNECT %s HTTP/1.1\r\n"
998 "Connection: close\r\n"
1003 authstr ? "Proxy-" : "", authstr ? authstr : "");
1006 if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1009 s->buf_ptr = s->buffer;
1010 s->buf_end = s->buffer;
1013 cur_auth_type = s->proxy_auth_state.auth_type;
1015 /* Note: This uses buffering, potentially reading more than the
1016 * HTTP header. If tunneling a protocol where the server starts
1017 * the conversation, we might buffer part of that here, too.
1018 * Reading that requires using the proper ffurl_read() function
1019 * on this URLContext, not using the fd directly (as the tls
1020 * protocol does). This shouldn't be an issue for tls though,
1021 * since the client starts the conversation there, so there
1022 * is no extra data that we might buffer up here.
1024 ret = http_read_header(h, &new_loc);
1029 if (s->http_code == 407 &&
1030 (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
1031 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
1037 if (s->http_code < 400)
1042 http_proxy_close(h);
1046 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
1048 HTTPContext *s = h->priv_data;
1049 return ffurl_write(s->hd, buf, size);
1052 URLProtocol ff_httpproxy_protocol = {
1053 .name = "httpproxy",
1054 .url_open = http_proxy_open,
1055 .url_read = http_buf_read,
1056 .url_write = http_proxy_write,
1057 .url_close = http_proxy_close,
1058 .url_get_file_handle = http_get_file_handle,
1059 .priv_data_size = sizeof(HTTPContext),
1060 .flags = URL_PROTOCOL_FLAG_NETWORK,