39 #define BUFFER_SIZE MAX_URL_SIZE
40 #define MAX_REDIRECTS 8
51 int64_t
off, filesize;
70 #define OFFSET(x) offsetof(HTTPContext, x)
71 #define D AV_OPT_FLAG_DECODING_PARAM
72 #define E AV_OPT_FLAG_ENCODING_PARAM
73 #define DEC AV_OPT_FLAG_DECODING_PARAM
75 {
"seekable",
"control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1,
D },
76 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
77 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
80 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
82 {
"timeout",
"set timeout of socket I/O operations",
OFFSET(rw_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D|
E },
84 {
"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 },
87 #define HTTP_CLASS(flavor)\
88 static const AVClass flavor ## _context_class = {\
89 .class_name = #flavor,\
90 .item_name = av_default_item_name,\
92 .version = LIBAVUTIL_VERSION_INT,\
99 const char *hoststr,
const char *auth,
100 const char *proxyauth,
int *new_location);
114 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
115 char hostname[1024], hoststr[1024], proto[10];
116 char auth[1024], proxyauth[1024] =
"";
119 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
127 hostname,
sizeof(hostname), &port,
131 proxy_path = getenv(
"http_proxy");
135 if (!strcmp(proto,
"https")) {
144 if (path1[0] ==
'\0')
156 hostname,
sizeof(hostname), &port,
NULL, 0, proxy_path);
163 char opts_format[20];
177 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
197 && location_changed == 1) {
206 location_changed = 0;
240 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
253 }
else if (len == 0) {
275 if (q > line && q[-1] ==
'\r')
281 if ((q - line) < line_size - 1)
294 if (line[0] ==
'\0') {
300 if (line_count == 0) {
320 while (*p !=
'\0' && *p !=
':')
338 if (!strncmp (p,
"bytes ", 6)) {
341 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
358 if (!strcmp(p,
"close"))
370 size_t str_size = strlen(tmp) + strlen(p) + 2;
396 char *next, *cookie, *set_cookies =
av_strdup(s->
cookies), *cset_cookies = set_cookies;
398 if (!set_cookies)
return AVERROR(EINVAL);
401 while ((cookie =
av_strtok(set_cookies,
"\n", &next))) {
402 int domain_offset = 0;
403 char *param, *next_param, *cdomain =
NULL, *cpath =
NULL, *cvalue =
NULL;
406 while ((param =
av_strtok(cookie,
"; ", &next_param))) {
426 if (!cdomain || !cpath || !cvalue) {
428 "Invalid cookie found, no value, path or domain specified\n");
437 domain_offset = strlen(domain) - strlen(cdomain);
438 if (domain_offset < 0)
452 char *tmp = *cookies;
453 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
458 snprintf(*cookies, str_size,
"%s; %s", tmp, cvalue);
478 static inline int has_header(
const char *str,
const char *header)
512 const char *hoststr,
const char *auth,
513 const char *proxyauth,
int *new_location)
517 char headers[4096] =
"";
518 char *authstr =
NULL, *proxyauthstr =
NULL;
534 method = post ?
"POST" :
"GET";
542 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
543 "User-Agent: %s\r\n",
546 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
547 sizeof(headers) - len);
552 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
553 "Range: bytes=%"PRId64
"-\r\n", s->
off);
557 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
558 sizeof(headers) - len);
560 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
561 sizeof(headers) - len);
566 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
567 "Host: %s\r\n", hoststr);
569 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
572 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
575 char *cookies =
NULL;
577 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
578 "Cookie: %s\r\n", cookies);
596 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
598 authstr ? authstr :
"",
599 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
632 return (off == s->
off) ? 0 : -1;
663 int err, new_location;
703 char crlf[] =
"\r\n";
715 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
728 char footer[] =
"0\r\n\r\n";
734 ret = ret > 0 ? 0 : ret;
760 int64_t old_off = s->
off;
771 memcpy(old_buf, s->
buf_ptr, old_buf_size);
773 if (whence == SEEK_CUR)
775 else if (whence == SEEK_END)
781 memcpy(s->
buffer, old_buf, old_buf_size);
799 #if CONFIG_HTTP_PROTOCOL
810 .priv_data_class = &http_context_class,
814 #if CONFIG_HTTPS_PROTOCOL
825 .priv_data_class = &https_context_class,
830 #if CONFIG_HTTPPROXY_PROTOCOL
842 char hostname[1024], hoststr[1024];
843 char auth[1024], pathbuf[1024], *path;
845 int port, ret = 0, attempts = 0;
850 char opts_format[20];
857 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
858 pathbuf,
sizeof(pathbuf), uri);
880 "CONNECT %s HTTP/1.1\r\n"
882 "Connection: close\r\n"
887 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
937 .url_open = http_proxy_open,
939 .url_write = http_proxy_write,
940 .url_close = http_proxy_close,