43 #define BUFFER_SIZE MAX_URL_SIZE
44 #define MAX_REDIRECTS 8
55 int64_t
off, filesize;
78 z_stream inflate_stream;
85 #define OFFSET(x) offsetof(HTTPContext, x)
86 #define D AV_OPT_FLAG_DECODING_PARAM
87 #define E AV_OPT_FLAG_ENCODING_PARAM
88 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
90 {
"seekable",
"control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1,
D },
91 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
92 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
95 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
98 {
"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 },
100 {
"icy_metadata_headers",
"return ICY metadata headers",
OFFSET(icy_metadata_headers),
AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
101 {
"icy_metadata_packet",
"return current ICY metadata packet",
OFFSET(icy_metadata_packet),
AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
103 {
"none",
"No auth method set, autodetect", 0,
AV_OPT_TYPE_CONST, {.i64 =
HTTP_AUTH_NONE}, 0, 0,
D|
E,
"auth_type" },
105 {
"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,
"auth_type" },
108 #define HTTP_CLASS(flavor)\
109 static const AVClass flavor ## _context_class = {\
110 .class_name = #flavor,\
111 .item_name = av_default_item_name,\
113 .version = LIBAVUTIL_VERSION_INT,\
120 const char *hoststr,
const char *auth,
121 const char *proxyauth,
int *new_location);
135 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
136 char hostname[1024], hoststr[1024], proto[10];
137 char auth[1024], proxyauth[1024] =
"";
140 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
148 hostname,
sizeof(hostname), &port,
150 ff_url_join(hoststr,
sizeof(hoststr), NULL, NULL, hostname, port, NULL);
152 proxy_path = getenv(
"http_proxy");
154 proxy_path != NULL &&
av_strstart(proxy_path,
"http://", NULL);
156 if (!strcmp(proto,
"https")) {
165 if (path1[0] ==
'\0')
173 ff_url_join(urlbuf,
sizeof(urlbuf), proto, NULL, hostname, port,
"%s",
177 hostname,
sizeof(hostname), &port, NULL, 0, proxy_path);
180 ff_url_join(buf,
sizeof(buf), lower_proto, NULL, hostname, port, NULL);
191 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
211 && location_changed == 1) {
220 location_changed = 0;
264 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
280 }
else if (len == 0) {
302 if (q > line && q[-1] ==
'\r')
308 if ((q - line) < line_size - 1)
322 if (line[0] ==
'\0') {
328 if (line_count == 0) {
348 while (*p !=
'\0' && *p !=
':')
367 if (!strncmp (p,
"bytes ", 6)) {
369 s->
off = strtoll(p, NULL, 10);
370 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
371 s->
filesize = strtoll(slash+1, NULL, 10);
387 if (!strcmp(p,
"close"))
399 size_t str_size = strlen(tmp) + strlen(p) + 2;
421 inflateEnd(&s->inflate_stream);
422 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
424 s->inflate_stream.msg);
427 if (zlibCompileFlags() & (1 << 17)) {
459 char *next, *cookie, *set_cookies =
av_strdup(s->
cookies), *cset_cookies = set_cookies;
461 if (!set_cookies)
return AVERROR(EINVAL);
464 while ((cookie =
av_strtok(set_cookies,
"\n", &next))) {
465 int domain_offset = 0;
466 char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
469 while ((param =
av_strtok(cookie,
"; ", &next_param))) {
491 if (!cdomain || !cpath || !cvalue) {
493 "Invalid cookie found, no value, path or domain specified\n");
502 domain_offset = strlen(domain) - strlen(cdomain);
503 if (domain_offset < 0)
517 char *tmp = *cookies;
518 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
523 snprintf(*cookies, str_size,
"%s; %s", tmp, cvalue);
543 static inline int has_header(
const char *str,
const char *header)
563 av_dlog(NULL,
"header='%s'\n", line);
577 const char *hoststr,
const char *auth,
578 const char *proxyauth,
int *new_location)
582 char headers[4096] =
"";
583 char *authstr = NULL, *proxyauthstr = NULL;
587 int send_expect_100 = 0;
600 method = post ?
"POST" :
"GET";
619 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
622 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
623 sizeof(headers) - len);
628 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
629 "Range: bytes=%"PRId64
"-\r\n", s->
off);
631 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
632 "Expect: 100-continue\r\n");
636 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
637 sizeof(headers) - len);
639 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
640 sizeof(headers) - len);
645 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
646 "Host: %s\r\n", hoststr);
648 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
651 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
654 char *cookies = NULL;
656 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
657 "Cookie: %s\r\n", cookies);
662 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
663 "Icy-MetaData: %d\r\n", 1);
679 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
681 authstr ? authstr :
"",
682 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
703 if (post && !s->
post_data && !send_expect_100) {
716 return (off == s->
off) ? 0 : -1;
746 #define DECOMPRESS_BUF_SIZE (256 * 1024)
752 if (!s->inflate_buffer) {
753 s->inflate_buffer =
av_malloc(DECOMPRESS_BUF_SIZE);
754 if (!s->inflate_buffer)
758 if (s->inflate_stream.avail_in == 0) {
759 int read =
http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
762 s->inflate_stream.next_in = s->inflate_buffer;
763 s->inflate_stream.avail_in = read;
766 s->inflate_stream.avail_out =
size;
767 s->inflate_stream.next_out =
buf;
769 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
770 if (ret != Z_OK && ret != Z_STREAM_END)
773 return size - s->inflate_stream.avail_out;
780 int err, new_location;
823 char data[255 * 16 + 1];
827 for (n = 0; n < ch; n++)
830 if ((ret =
av_opt_set(s,
"icy_metadata_packet", data, 0)) < 0)
836 size =
FFMIN(size, remaining);
840 return http_buf_read_compressed(h, buf, size);
850 char crlf[] =
"\r\n";
862 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
875 char footer[] =
"0\r\n\r\n";
881 ret = ret > 0 ? 0 :
ret;
894 inflateEnd(&s->inflate_stream);
913 int64_t old_off = s->
off;
925 memcpy(old_buf, s->
buf_ptr, old_buf_size);
927 if (whence == SEEK_CUR)
929 else if (whence == SEEK_END)
937 memcpy(s->
buffer, old_buf, old_buf_size);
956 #if CONFIG_HTTP_PROTOCOL
967 .priv_data_class = &http_context_class,
971 #if CONFIG_HTTPS_PROTOCOL
982 .priv_data_class = &https_context_class,
987 #if CONFIG_HTTPPROXY_PROTOCOL
999 char hostname[1024], hoststr[1024];
1000 char auth[1024], pathbuf[1024], *path;
1001 char lower_url[100];
1002 int port, ret = 0, attempts = 0;
1012 av_url_split(NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
1013 pathbuf,
sizeof(pathbuf), uri);
1014 ff_url_join(hoststr,
sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1019 ff_url_join(lower_url,
sizeof(lower_url),
"tcp", NULL, hostname, port,
1030 "CONNECT %s HTTP/1.1\r\n"
1032 "Connection: close\r\n"
1037 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
1075 http_proxy_close(h);
1086 .
name =
"httpproxy",
1087 .url_open = http_proxy_open,
1089 .url_write = http_proxy_write,
1090 .url_close = http_proxy_close,