39 #define BUFFER_SIZE MAX_URL_SIZE
40 #define MAX_REDIRECTS 8
51 int64_t
off, filesize;
69 #define OFFSET(x) offsetof(HTTPContext, x)
70 #define D AV_OPT_FLAG_DECODING_PARAM
71 #define E AV_OPT_FLAG_ENCODING_PARAM
72 #define DEC AV_OPT_FLAG_DECODING_PARAM
74 {
"seekable",
"Control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1,
D },
75 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
76 {
"headers",
"custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
79 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
81 {
"timeout",
"timeout of socket i/o operations",
OFFSET(rw_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX,
D|
E },
85 #define HTTP_CLASS(flavor)\
86 static const AVClass flavor ## _context_class = {\
87 .class_name = #flavor,\
88 .item_name = av_default_item_name,\
90 .version = LIBAVUTIL_VERSION_INT,\
97 const char *hoststr,
const char *auth,
98 const char *proxyauth,
int *new_location);
112 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
113 char hostname[1024], hoststr[1024], proto[10];
114 char auth[1024], proxyauth[1024] =
"";
117 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
121 proxy_path = getenv(
"http_proxy");
122 use_proxy = (proxy_path !=
NULL) && !getenv(
"no_proxy") &&
129 hostname,
sizeof(hostname), &port,
133 if (!strcmp(proto,
"https")) {
142 if (path1[0] ==
'\0')
154 hostname,
sizeof(hostname), &port,
NULL, 0, proxy_path);
161 char opts_format[20];
175 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
195 && location_changed == 1) {
204 location_changed = 0;
238 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
251 }
else if (len == 0) {
273 if (q > line && q[-1] ==
'\r')
279 if ((q - line) < line_size - 1)
292 if (line[0] ==
'\0') {
298 if (line_count == 0) {
299 while (!isspace(*p) && *p !=
'\0')
318 while (*p !=
'\0' && *p !=
':')
336 if (!strncmp (p,
"bytes ", 6)) {
339 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
356 if (!strcmp(p,
"close"))
367 static inline int has_header(
const char *str,
const char *header)
401 const char *hoststr,
const char *auth,
402 const char *proxyauth,
int *new_location)
406 char headers[4096] =
"";
407 char *authstr =
NULL, *proxyauthstr =
NULL;
423 method = post ?
"POST" :
"GET";
431 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
432 "User-Agent: %s\r\n",
435 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
436 sizeof(headers) - len);
441 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
442 "Range: bytes=%"PRId64
"-\r\n", s->
off);
446 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
447 sizeof(headers) - len);
449 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
450 sizeof(headers) - len);
455 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
456 "Host: %s\r\n", hoststr);
458 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
461 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
477 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
479 authstr ? authstr :
"",
480 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
513 return (off == s->
off) ? 0 : -1;
544 int err, new_location;
584 char crlf[] =
"\r\n";
596 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
609 char footer[] =
"0\r\n\r\n";
615 ret = ret > 0 ? 0 : ret;
641 int64_t old_off = s->
off;
652 memcpy(old_buf, s->
buf_ptr, old_buf_size);
654 if (whence == SEEK_CUR)
656 else if (whence == SEEK_END)
662 memcpy(s->
buffer, old_buf, old_buf_size);
680 #if CONFIG_HTTP_PROTOCOL
691 .priv_data_class = &http_context_class,
695 #if CONFIG_HTTPS_PROTOCOL
706 .priv_data_class = &https_context_class,
711 #if CONFIG_HTTPPROXY_PROTOCOL
723 char hostname[1024], hoststr[1024];
724 char auth[1024], pathbuf[1024], *path;
726 int port, ret = 0, attempts = 0;
731 char opts_format[20];
738 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
739 pathbuf,
sizeof(pathbuf), uri);
761 "CONNECT %s HTTP/1.1\r\n"
763 "Connection: close\r\n"
768 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
818 .url_open = http_proxy_open,
820 .url_write = http_proxy_write,
821 .url_close = http_proxy_close,