43 #define BUFFER_SIZE MAX_URL_SIZE
44 #define MAX_REDIRECTS 8
54 int64_t
off, end_off, filesize;
83 z_stream inflate_stream;
90 #define OFFSET(x) offsetof(HTTPContext, x)
91 #define D AV_OPT_FLAG_DECODING_PARAM
92 #define E AV_OPT_FLAG_ENCODING_PARAM
93 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
95 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
96 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
97 {
"content_type",
"set a specific content type for the POST messages",
OFFSET(content_type),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
100 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
104 {
"icy_metadata_headers",
"return ICY metadata headers",
OFFSET(icy_metadata_headers),
AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
105 {
"icy_metadata_packet",
"return current ICY metadata packet",
OFFSET(icy_metadata_packet),
AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
107 {
"none",
"No auth method set, autodetect", 0,
AV_OPT_TYPE_CONST, {.i64 =
HTTP_AUTH_NONE}, 0, 0,
D|
E,
"auth_type" },
109 {
"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 },
112 {
"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 },
115 #define HTTP_CLASS(flavor)\
116 static const AVClass flavor ## _context_class = {\
117 .class_name = #flavor,\
118 .item_name = av_default_item_name,\
120 .version = LIBAVUTIL_VERSION_INT,\
127 const char *hoststr,
const char *auth,
128 const char *proxyauth,
int *new_location);
142 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
143 char hostname[1024], hoststr[1024], proto[10];
144 char auth[1024], proxyauth[1024] =
"";
147 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
155 hostname,
sizeof(hostname), &port,
159 proxy_path = getenv(
"http_proxy");
163 if (!strcmp(proto,
"https")) {
172 if (path1[0] ==
'\0')
184 hostname,
sizeof(hostname), &port,
NULL, 0, proxy_path);
198 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
220 && location_changed == 1) {
230 location_changed = 0;
277 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
293 }
else if (len == 0) {
315 if (q > line && q[-1] ==
'\r')
321 if ((q - line) < line_size - 1)
332 if (http_code >= 400 && http_code < 600 &&
347 new_loc =
av_strdup(redirected_location);
361 if (!strncmp(p,
"bytes ", 6)) {
364 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
378 inflateEnd(&s->inflate_stream);
379 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
381 s->inflate_stream.msg);
384 if (zlibCompileFlags() & (1 << 17)) {
386 "Your zlib was compiled without gzip support.\n");
391 "Compressed (%s) content, need zlib with gzip support\n", p);
407 int len = 4 + strlen(p) + strlen(tag);
433 if (line[0] ==
'\0') {
439 if (line_count == 0) {
451 while (*p !=
'\0' && *p !=
':')
470 !strncmp(p,
"bytes", 5)) {
483 if (!strcmp(p,
"close"))
501 static inline int has_header(
const char *str,
const char *header)
535 const char *hoststr,
const char *auth,
536 const char *proxyauth,
int *new_location)
540 char headers[1024] =
"";
541 char *authstr =
NULL, *proxyauthstr =
NULL;
545 int send_expect_100 = 0;
558 method = post ?
"POST" :
"GET";
577 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
580 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
581 sizeof(headers) - len);
586 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
587 "Range: bytes=%"PRId64
"-", s->
off);
589 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
592 sizeof(headers) - len);
595 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
596 "Expect: 100-continue\r\n");
600 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
601 sizeof(headers) - len);
603 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
604 sizeof(headers) - len);
609 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
610 "Host: %s\r\n", hoststr);
612 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
616 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
619 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
620 "Icy-MetaData: %d\r\n", 1);
636 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
638 authstr ? authstr :
"",
639 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
660 if (post && !s->
post_data && !send_expect_100) {
673 return (off == s->
off) ? 0 : -1;
702 #define DECOMPRESS_BUF_SIZE (256 * 1024)
708 if (!s->inflate_buffer) {
709 s->inflate_buffer =
av_malloc(DECOMPRESS_BUF_SIZE);
710 if (!s->inflate_buffer)
714 if (s->inflate_stream.avail_in == 0) {
715 int read =
http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
718 s->inflate_stream.next_in = s->inflate_buffer;
719 s->inflate_stream.avail_in = read;
722 s->inflate_stream.avail_out =
size;
723 s->inflate_stream.next_out = buf;
725 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
726 if (ret != Z_OK && ret != Z_STREAM_END)
729 return size - s->inflate_stream.avail_out;
736 int err, new_location;
770 return http_buf_read_compressed(h, buf, size);
806 char data[255 * 16 + 1];
813 if ((ret =
av_opt_set(s,
"icy_metadata_packet", data, 0)) < 0)
820 return FFMIN(size, remaining);
844 char crlf[] =
"\r\n";
856 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
869 char footer[] =
"0\r\n\r\n";
875 ret = ret > 0 ? 0 : ret;
888 inflateEnd(&s->inflate_stream);
907 int64_t old_off = s->
off;
909 int old_buf_size, ret;
914 else if ((whence == SEEK_CUR && off == 0) ||
915 (whence == SEEK_SET && off == s->
off))
922 memcpy(old_buf, s->
buf_ptr, old_buf_size);
924 if (whence == SEEK_CUR)
926 else if (whence == SEEK_END)
934 memcpy(s->
buffer, old_buf, old_buf_size);
953 #if CONFIG_HTTP_PROTOCOL
964 .priv_data_class = &http_context_class,
968 #if CONFIG_HTTPS_PROTOCOL
979 .priv_data_class = &https_context_class,
984 #if CONFIG_HTTPPROXY_PROTOCOL
996 char hostname[1024], hoststr[1024];
997 char auth[1024], pathbuf[1024], *path;
999 int port, ret = 0, attempts = 0;
1006 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
1007 pathbuf,
sizeof(pathbuf), uri);
1013 ff_url_join(lower_url,
sizeof(lower_url),
"tcp",
NULL, hostname, port,
1024 "CONNECT %s HTTP/1.1\r\n"
1026 "Connection: close\r\n"
1031 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
1070 http_proxy_close(h);
1081 .
name =
"httpproxy",
1082 .url_open = http_proxy_open,
1084 .url_write = http_proxy_write,
1085 .url_close = http_proxy_close,