39 #define BUFFER_SIZE MAX_URL_SIZE
40 #define MAX_REDIRECTS 8
51 int64_t
off, filesize;
75 #define OFFSET(x) offsetof(HTTPContext, x)
76 #define D AV_OPT_FLAG_DECODING_PARAM
77 #define E AV_OPT_FLAG_ENCODING_PARAM
78 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
80 {
"seekable",
"control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1,
D },
81 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
82 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
85 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
87 {
"timeout",
"set timeout of socket I/O operations",
OFFSET(rw_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D|
E },
89 {
"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, 0 },
91 {
"icy_metadata_headers",
"return ICY metadata headers",
OFFSET(icy_metadata_headers),
AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
92 {
"icy_metadata_packet",
"return current ICY metadata packet",
OFFSET(icy_metadata_packet),
AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
95 #define HTTP_CLASS(flavor)\
96 static const AVClass flavor ## _context_class = {\
97 .class_name = #flavor,\
98 .item_name = av_default_item_name,\
100 .version = LIBAVUTIL_VERSION_INT,\
107 const char *hoststr,
const char *auth,
108 const char *proxyauth,
int *new_location);
122 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
123 char hostname[1024], hoststr[1024], proto[10];
124 char auth[1024], proxyauth[1024] =
"";
127 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
135 hostname,
sizeof(hostname), &port,
137 ff_url_join(hoststr,
sizeof(hoststr), NULL, NULL, hostname, port, NULL);
139 proxy_path = getenv(
"http_proxy");
141 proxy_path != NULL &&
av_strstart(proxy_path,
"http://", NULL);
143 if (!strcmp(proto,
"https")) {
152 if (path1[0] ==
'\0')
160 ff_url_join(urlbuf,
sizeof(urlbuf), proto, NULL, hostname, port,
"%s",
164 hostname,
sizeof(hostname), &port, NULL, 0, proxy_path);
167 ff_url_join(buf,
sizeof(buf), lower_proto, NULL, hostname, port, NULL);
171 char opts_format[20];
185 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
205 && location_changed == 1) {
214 location_changed = 0;
249 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
262 }
else if (len == 0) {
284 if (q > line && q[-1] ==
'\r')
290 if ((q - line) < line_size - 1)
304 if (line[0] ==
'\0') {
310 if (line_count == 0) {
330 while (*p !=
'\0' && *p !=
':')
349 if (!strncmp (p,
"bytes ", 6)) {
351 s->
off = strtoll(p, NULL, 10);
352 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
353 s->
filesize = strtoll(slash+1, NULL, 10);
369 if (!strcmp(p,
"close"))
381 size_t str_size = strlen(tmp) + strlen(p) + 2;
417 char *next, *cookie, *set_cookies =
av_strdup(s->
cookies), *cset_cookies = set_cookies;
419 if (!set_cookies)
return AVERROR(EINVAL);
422 while ((cookie =
av_strtok(set_cookies,
"\n", &next))) {
423 int domain_offset = 0;
424 char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
427 while ((param =
av_strtok(cookie,
"; ", &next_param))) {
449 if (!cdomain || !cpath || !cvalue) {
451 "Invalid cookie found, no value, path or domain specified\n");
460 domain_offset = strlen(domain) - strlen(cdomain);
461 if (domain_offset < 0)
475 char *tmp = *cookies;
476 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
481 snprintf(*cookies, str_size,
"%s; %s", tmp, cvalue);
501 static inline int has_header(
const char *str,
const char *header)
521 av_dlog(NULL,
"header='%s'\n", line);
535 const char *hoststr,
const char *auth,
536 const char *proxyauth,
int *new_location)
540 char headers[4096] =
"";
541 char *authstr = NULL, *proxyauthstr = NULL;
557 method = post ?
"POST" :
"GET";
565 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
568 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
569 sizeof(headers) - len);
574 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
575 "Range: bytes=%"PRId64
"-\r\n", s->
off);
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);
588 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
589 "Host: %s\r\n", hoststr);
591 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
594 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
597 char *cookies = NULL;
599 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
600 "Cookie: %s\r\n", cookies);
605 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
606 "Icy-MetaData: %d\r\n", 1);
622 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
624 authstr ? authstr :
"",
625 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
659 return (off == s->
off) ? 0 : -1;
691 int err, new_location;
734 char data[255 * 16 + 1];
738 for (n = 0; n < ch; n++)
741 if ((ret =
av_opt_set(s,
"icy_metadata_packet", data, 0)) < 0)
747 size =
FFMIN(size, remaining);
757 char crlf[] =
"\r\n";
769 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
782 char footer[] =
"0\r\n\r\n";
788 ret = ret > 0 ? 0 :
ret;
814 int64_t old_off = s->
off;
825 memcpy(old_buf, s->
buf_ptr, old_buf_size);
827 if (whence == SEEK_CUR)
829 else if (whence == SEEK_END)
835 memcpy(s->
buffer, old_buf, old_buf_size);
853 #if CONFIG_HTTP_PROTOCOL
864 .priv_data_class = &http_context_class,
868 #if CONFIG_HTTPS_PROTOCOL
879 .priv_data_class = &https_context_class,
884 #if CONFIG_HTTPPROXY_PROTOCOL
896 char hostname[1024], hoststr[1024];
897 char auth[1024], pathbuf[1024], *path;
899 int port,
ret = 0, attempts = 0;
904 char opts_format[20];
911 av_url_split(NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
912 pathbuf,
sizeof(pathbuf), uri);
913 ff_url_join(hoststr,
sizeof(hoststr), NULL, NULL, hostname, port, NULL);
918 ff_url_join(lower_url,
sizeof(lower_url),
"tcp", NULL, hostname, port,
934 "CONNECT %s HTTP/1.1\r\n"
936 "Connection: close\r\n"
941 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
991 .url_open = http_proxy_open,
993 .url_write = http_proxy_write,
994 .url_close = http_proxy_close,