45 #define BUFFER_SIZE MAX_URL_SIZE
46 #define MAX_REDIRECTS 8
92 z_stream inflate_stream;
100 #define OFFSET(x) offsetof(HTTPContext, x)
101 #define D AV_OPT_FLAG_DECODING_PARAM
102 #define E AV_OPT_FLAG_ENCODING_PARAM
103 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
106 {
"seekable",
"control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
D },
107 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
E },
108 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D |
E },
109 {
"content_type",
"set a specific content type for the POST messages",
OFFSET(content_type),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D |
E },
112 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
D |
E },
115 {
"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 },
121 {
"none",
"No auth method set, autodetect", 0,
AV_OPT_TYPE_CONST, { .i64 =
HTTP_AUTH_NONE }, 0, 0,
D |
E,
"auth_type"},
123 {
"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 },
126 {
"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 },
132 const char *hoststr,
const char *auth,
133 const char *proxyauth,
int *new_location);
147 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
148 char hostname[1024], hoststr[1024], proto[10];
149 char auth[1024], proxyauth[1024] =
"";
152 int port, use_proxy, err, location_changed = 0;
156 hostname,
sizeof(hostname), &port,
160 proxy_path = getenv(
"http_proxy");
164 if (!strcmp(proto,
"https")) {
173 if (path1[0] ==
'\0')
185 hostname,
sizeof(hostname), &port,
NULL, 0, proxy_path);
198 auth, proxyauth, &location_changed);
202 return location_changed;
210 int location_changed, attempts = 0, redirects = 0;
218 if (location_changed < 0)
240 location_changed == 1) {
249 location_changed = 0;
257 if (location_changed < 0)
258 return location_changed;
282 switch (status_code) {
289 if (status_code >= 400 && status_code <= 499)
291 else if (status_code >= 500)
294 return default_averror;
317 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
319 "No trailing CRLF found in HTTP header.\n");
335 }
else if (len == 0) {
357 if (q > line && q[-1] ==
'\r')
363 if ((q - line) < line_size - 1)
374 if (http_code >= 400 && http_code < 600 &&
389 new_loc =
av_strdup(redirected_location);
403 if (!strncmp(p,
"bytes ", 6)) {
406 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
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)) {
429 "Your zlib was compiled without gzip support.\n");
434 "Compressed (%s) content, need zlib with gzip support\n", p);
449 int len = 4 + strlen(p) + strlen(tag);
474 if (!(eql = strchr(p,
'=')))
return AVERROR(EINVAL);
490 len += strlen(e->
key) + strlen(e->
value) + 1;
494 if (*cookies)
av_free(*cookies);
496 if (!cookies)
return AVERROR(ENOMEM);
514 if (line[0] ==
'\0') {
520 if (line_count == 0) {
532 while (*p !=
'\0' && *p !=
':')
551 !strncmp(p,
"bytes", 5) &&
565 if (!strcmp(p,
"close"))
605 char *next, *cookie, *set_cookies =
av_strdup(s->
cookies), *cset_cookies = set_cookies;
607 if (!set_cookies)
return AVERROR(EINVAL);
613 while ((cookie =
av_strtok(set_cookies,
"\n", &next))) {
614 int domain_offset = 0;
615 char *param, *next_param, *cdomain =
NULL, *cpath =
NULL, *cvalue =
NULL;
622 while ((param =
av_strtok(cookie,
"; ", &next_param))) {
633 int leading_dot = (param[7] ==
'.');
635 cdomain =
av_strdup(¶m[7+leading_dot]);
644 if (!cdomain || !cpath || !cvalue) {
646 "Invalid cookie found, no value, path or domain specified\n");
655 domain_offset = strlen(domain) - strlen(cdomain);
656 if (domain_offset < 0)
670 char *tmp = *cookies;
671 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
676 snprintf(*cookies, str_size,
"%s; %s", tmp, cvalue);
737 const char *hoststr,
const char *auth,
738 const char *proxyauth,
int *new_location)
743 char *authstr =
NULL, *proxyauthstr =
NULL;
744 int64_t off = s->
off;
747 int send_expect_100 = 0;
762 method = post ?
"POST" :
"GET";
782 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
785 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
786 sizeof(headers) - len);
791 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
792 "Range: bytes=%"PRId64
"-", s->
off);
794 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
797 sizeof(headers) - len);
800 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
801 "Expect: 100-continue\r\n");
805 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
806 sizeof(headers) - len);
808 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
809 sizeof(headers) - len);
813 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
814 "Host: %s\r\n", hoststr);
816 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
820 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
823 char *cookies =
NULL;
824 if (!
get_cookies(s, &cookies, path, hoststr) && cookies) {
825 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
826 "Cookie: %s\r\n", cookies);
831 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
832 "Icy-MetaData: %d\r\n", 1);
847 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
849 authstr ? authstr :
"",
850 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
871 if (post && !s->
post_data && !send_expect_100) {
885 err = (off == s->
off) ? 0 : -1;
918 #define DECOMPRESS_BUF_SIZE (256 * 1024)
924 if (!s->inflate_buffer) {
925 s->inflate_buffer =
av_malloc(DECOMPRESS_BUF_SIZE);
926 if (!s->inflate_buffer)
930 if (s->inflate_stream.avail_in == 0) {
931 int read =
http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
934 s->inflate_stream.next_in = s->inflate_buffer;
935 s->inflate_stream.avail_in = read;
938 s->inflate_stream.avail_out =
size;
939 s->inflate_stream.next_out =
buf;
941 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
942 if (ret != Z_OK && ret != Z_STREAM_END)
944 ret, s->inflate_stream.msg);
946 return size - s->inflate_stream.avail_out;
953 int err, new_location;
975 av_dlog(
NULL,
"Chunked encoding data size: %"PRId64
"'\n",
985 return http_buf_read_compressed(h, buf, size);
1013 val = strstr(key,
"='");
1016 end = strstr(val,
"';");
1049 char data[255 * 16 + 1];
1056 if ((ret =
av_opt_set(s,
"icy_metadata_packet", data, 0)) < 0)
1064 return FFMIN(size, remaining);
1088 char crlf[] =
"\r\n";
1100 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
1113 char footer[] =
"0\r\n\r\n";
1119 ret = ret > 0 ? 0 :
ret;
1132 inflateEnd(&s->inflate_stream);
1150 int64_t old_off = s->
off;
1152 int old_buf_size,
ret;
1157 else if ((whence == SEEK_CUR && off == 0) ||
1158 (whence == SEEK_SET && off == s->
off))
1163 if (whence == SEEK_CUR)
1165 else if (whence == SEEK_END)
1167 else if (whence != SEEK_SET)
1175 memcpy(old_buf, s->
buf_ptr, old_buf_size);
1181 memcpy(s->
buffer, old_buf, old_buf_size);
1199 #define HTTP_CLASS(flavor) \
1200 static const AVClass flavor ## _context_class = { \
1201 .class_name = # flavor, \
1202 .item_name = av_default_item_name, \
1203 .option = options, \
1204 .version = LIBAVUTIL_VERSION_INT, \
1207 #if CONFIG_HTTP_PROTOCOL
1220 .priv_data_class = &http_context_class,
1225 #if CONFIG_HTTPS_PROTOCOL
1238 .priv_data_class = &https_context_class,
1243 #if CONFIG_HTTPPROXY_PROTOCOL
1255 char hostname[1024], hoststr[1024];
1256 char auth[1024], pathbuf[1024], *path;
1257 char lower_url[100];
1258 int port, ret = 0, attempts = 0;
1268 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
1269 pathbuf,
sizeof(pathbuf), uri);
1275 ff_url_join(lower_url,
sizeof(lower_url),
"tcp",
NULL, hostname, port,
1286 "CONNECT %s HTTP/1.1\r\n"
1288 "Connection: close\r\n"
1293 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
1331 http_proxy_close(h);
1342 .
name =
"httpproxy",
1343 .url_open = http_proxy_open,
1345 .url_write = http_proxy_write,
1346 .url_close = http_proxy_close,