43 #define BUFFER_SIZE MAX_URL_SIZE
44 #define MAX_REDIRECTS 8
54 int64_t
off, end_off, filesize;
87 z_stream inflate_stream;
94 #define OFFSET(x) offsetof(HTTPContext, x)
95 #define D AV_OPT_FLAG_DECODING_PARAM
96 #define E AV_OPT_FLAG_ENCODING_PARAM
97 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
99 {
"seekable",
"control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1,
D },
100 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
101 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
102 {
"content_type",
"set a specific content type for the POST messages",
OFFSET(content_type),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
105 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
108 {
"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 },
113 {
"none",
"No auth method set, autodetect", 0,
AV_OPT_TYPE_CONST, {.i64 =
HTTP_AUTH_NONE}, 0, 0,
D|
E,
"auth_type" },
115 {
"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 },
118 {
"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 },
121 #define HTTP_CLASS(flavor)\
122 static const AVClass flavor ## _context_class = {\
123 .class_name = #flavor,\
124 .item_name = av_default_item_name,\
126 .version = LIBAVUTIL_VERSION_INT,\
133 const char *hoststr,
const char *auth,
134 const char *proxyauth,
int *new_location);
148 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
149 char hostname[1024], hoststr[1024], proto[10];
150 char auth[1024], proxyauth[1024] =
"";
153 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
161 hostname,
sizeof(hostname), &port,
163 ff_url_join(hoststr,
sizeof(hoststr), NULL, NULL, hostname, port, NULL);
165 proxy_path = getenv(
"http_proxy");
167 proxy_path != NULL &&
av_strstart(proxy_path,
"http://", NULL);
169 if (!strcmp(proto,
"https")) {
178 if (path1[0] ==
'\0')
186 ff_url_join(urlbuf,
sizeof(urlbuf), proto, NULL, hostname, port,
"%s",
190 hostname,
sizeof(hostname), &port, NULL, 0, proxy_path);
193 ff_url_join(buf,
sizeof(buf), lower_proto, NULL, hostname, port, NULL);
204 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
224 && location_changed == 1) {
233 location_changed = 0;
282 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
298 }
else if (len == 0) {
320 if (q > line && q[-1] ==
'\r')
326 if ((q - line) < line_size - 1)
337 if (http_code >= 400 && http_code < 600 &&
352 new_loc =
av_strdup(redirected_location);
366 if (!strncmp(p,
"bytes ", 6)) {
368 s->
off = strtoll(p, NULL, 10);
369 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
370 s->
filesize = strtoll(slash+1, NULL, 10);
384 inflateEnd(&s->inflate_stream);
385 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
387 s->inflate_stream.msg);
390 if (zlibCompileFlags() & (1 << 17)) {
392 "Your zlib was compiled without gzip support.\n");
397 "Compressed (%s) content, need zlib with gzip support\n", p);
412 int len = 4 + strlen(p) + strlen(tag);
438 if (line[0] ==
'\0') {
444 if (line_count == 0) {
456 while (*p !=
'\0' && *p !=
':')
475 !strncmp(p,
"bytes", 5) &&
489 if (!strcmp(p,
"close"))
506 size_t str_size = strlen(tmp) + strlen(p) + 2;
540 char *next, *cookie, *set_cookies =
av_strdup(s->
cookies), *cset_cookies = set_cookies;
542 if (!set_cookies)
return AVERROR(EINVAL);
545 while ((cookie =
av_strtok(set_cookies,
"\n", &next))) {
546 int domain_offset = 0;
547 char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
550 while ((param =
av_strtok(cookie,
"; ", &next_param))) {
558 int leading_dot = (param[7] ==
'.');
560 cdomain =
av_strdup(¶m[7+leading_dot]);
575 if (!cdomain || !cpath || !cvalue) {
577 "Invalid cookie found, no value, path or domain specified\n");
586 domain_offset = strlen(domain) - strlen(cdomain);
587 if (domain_offset < 0)
601 char *tmp = *cookies;
602 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
607 snprintf(*cookies, str_size,
"%s; %s", tmp, cvalue);
664 const char *hoststr,
const char *auth,
665 const char *proxyauth,
int *new_location)
669 char headers[4096] =
"";
670 char *authstr = NULL, *proxyauthstr = NULL;
674 int send_expect_100 = 0;
687 method = post ?
"POST" :
"GET";
706 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
709 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
710 sizeof(headers) - len);
715 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
716 "Range: bytes=%"PRId64
"-", s->
off);
718 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
721 sizeof(headers) - len);
724 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
725 "Expect: 100-continue\r\n");
729 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
730 sizeof(headers) - len);
732 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
733 sizeof(headers) - len);
738 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
739 "Host: %s\r\n", hoststr);
741 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
745 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
748 char *cookies = NULL;
750 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
751 "Cookie: %s\r\n", cookies);
756 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
757 "Icy-MetaData: %d\r\n", 1);
773 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
775 authstr ? authstr :
"",
776 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
800 if (post && !s->
post_data && !send_expect_100) {
813 return (off == s->
off) ? 0 : -1;
842 #define DECOMPRESS_BUF_SIZE (256 * 1024)
848 if (!s->inflate_buffer) {
849 s->inflate_buffer =
av_malloc(DECOMPRESS_BUF_SIZE);
850 if (!s->inflate_buffer)
854 if (s->inflate_stream.avail_in == 0) {
855 int read =
http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
858 s->inflate_stream.next_in = s->inflate_buffer;
859 s->inflate_stream.avail_in = read;
862 s->inflate_stream.avail_out =
size;
863 s->inflate_stream.next_out =
buf;
865 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
866 if (ret != Z_OK && ret != Z_STREAM_END)
869 return size - s->inflate_stream.avail_out;
876 int err, new_location;
910 return http_buf_read_compressed(h, buf, size);
948 char data[255 * 16 + 1];
955 if ((ret =
av_opt_set(s,
"icy_metadata_packet", data, 0)) < 0)
962 return FFMIN(size, remaining);
986 char crlf[] =
"\r\n";
998 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
1011 char footer[] =
"0\r\n\r\n";
1017 ret = ret > 0 ? 0 :
ret;
1030 inflateEnd(&s->inflate_stream);
1049 int64_t old_off = s->
off;
1051 int old_buf_size,
ret;
1056 else if ((whence == SEEK_CUR && off == 0) ||
1057 (whence == SEEK_SET && off == s->
off))
1064 memcpy(old_buf, s->
buf_ptr, old_buf_size);
1066 if (whence == SEEK_CUR)
1068 else if (whence == SEEK_END)
1076 memcpy(s->
buffer, old_buf, old_buf_size);
1095 #if CONFIG_HTTP_PROTOCOL
1106 .priv_data_class = &http_context_class,
1110 #if CONFIG_HTTPS_PROTOCOL
1121 .priv_data_class = &https_context_class,
1126 #if CONFIG_HTTPPROXY_PROTOCOL
1138 char hostname[1024], hoststr[1024];
1139 char auth[1024], pathbuf[1024], *path;
1140 char lower_url[100];
1141 int port, ret = 0, attempts = 0;
1151 av_url_split(NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
1152 pathbuf,
sizeof(pathbuf), uri);
1153 ff_url_join(hoststr,
sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1158 ff_url_join(lower_url,
sizeof(lower_url),
"tcp", NULL, hostname, port,
1169 "CONNECT %s HTTP/1.1\r\n"
1171 "Connection: close\r\n"
1176 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
1214 http_proxy_close(h);
1225 .
name =
"httpproxy",
1226 .url_open = http_proxy_open,
1228 .url_write = http_proxy_write,
1229 .url_close = http_proxy_close,