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 DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
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))) {
428 if (!cdomain || !cpath || !cvalue) {
430 "Invalid cookie found, no value, path or domain specified\n");
439 domain_offset = strlen(domain) - strlen(cdomain);
440 if (domain_offset < 0)
454 char *tmp = *cookies;
455 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
460 snprintf(*cookies, str_size,
"%s; %s", tmp, cvalue);
480 static inline int has_header(
const char *str,
const char *header)
514 const char *hoststr,
const char *auth,
515 const char *proxyauth,
int *new_location)
519 char headers[4096] =
"";
520 char *authstr =
NULL, *proxyauthstr =
NULL;
536 method = post ?
"POST" :
"GET";
544 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
547 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
548 sizeof(headers) - len);
553 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
554 "Range: bytes=%"PRId64
"-\r\n", s->
off);
558 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
559 sizeof(headers) - len);
561 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
562 sizeof(headers) - len);
567 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
568 "Host: %s\r\n", hoststr);
570 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
573 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
576 char *cookies =
NULL;
578 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
579 "Cookie: %s\r\n", cookies);
597 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
599 authstr ? authstr :
"",
600 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
633 return (off == s->
off) ? 0 : -1;
664 int err, new_location;
704 char crlf[] =
"\r\n";
716 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
729 char footer[] =
"0\r\n\r\n";
735 ret = ret > 0 ? 0 :
ret;
761 int64_t old_off = s->
off;
772 memcpy(old_buf, s->
buf_ptr, old_buf_size);
774 if (whence == SEEK_CUR)
776 else if (whence == SEEK_END)
782 memcpy(s->
buffer, old_buf, old_buf_size);
800 #if CONFIG_HTTP_PROTOCOL
811 .priv_data_class = &http_context_class,
815 #if CONFIG_HTTPS_PROTOCOL
826 .priv_data_class = &https_context_class,
831 #if CONFIG_HTTPPROXY_PROTOCOL
843 char hostname[1024], hoststr[1024];
844 char auth[1024], pathbuf[1024], *path;
846 int port,
ret = 0, attempts = 0;
851 char opts_format[20];
858 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
859 pathbuf,
sizeof(pathbuf), uri);
881 "CONNECT %s HTTP/1.1\r\n"
883 "Connection: close\r\n"
888 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
938 .url_open = http_proxy_open,
940 .url_write = http_proxy_write,
941 .url_close = http_proxy_close,