39 #define BUFFER_SIZE MAX_URL_SIZE
40 #define MAX_REDIRECTS 8
49 int64_t
off, filesize;
63 #define OFFSET(x) offsetof(HTTPContext, x)
64 #define D AV_OPT_FLAG_DECODING_PARAM
65 #define E AV_OPT_FLAG_ENCODING_PARAM
67 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
68 {
"headers",
"custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
69 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
73 #define HTTP_CLASS(flavor)\
74 static const AVClass flavor ## _context_class = {\
75 .class_name = #flavor,\
76 .item_name = av_default_item_name,\
78 .version = LIBAVUTIL_VERSION_INT,\
85 const char *hoststr,
const char *auth,
86 const char *proxyauth,
int *new_location);
100 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
101 char hostname[1024], hoststr[1024], proto[10];
102 char auth[1024], proxyauth[1024] =
"";
105 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
109 proxy_path = getenv(
"http_proxy");
110 use_proxy = (proxy_path !=
NULL) && !getenv(
"no_proxy") &&
117 hostname,
sizeof(hostname), &port,
121 if (!strcmp(proto,
"https")) {
130 if (path1[0] ==
'\0')
142 hostname,
sizeof(hostname), &port,
NULL, 0, proxy_path);
156 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
178 && location_changed == 1) {
188 location_changed = 0;
220 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
233 }
else if (len == 0) {
255 if (q > line && q[-1] ==
'\r')
261 if ((q - line) < line_size - 1)
274 if (line[0] ==
'\0') {
280 if (line_count == 0) {
281 while (!isspace(*p) && *p !=
'\0')
300 while (*p !=
'\0' && *p !=
':')
318 if (!strncmp (p,
"bytes ", 6)) {
321 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
325 }
else if (!
av_strcasecmp(tag,
"Accept-Ranges") && !strncmp(p,
"bytes", 5)) {
337 if (!strcmp(p,
"close"))
344 static inline int has_header(
const char *str,
const char *header)
378 const char *hoststr,
const char *auth,
379 const char *proxyauth,
int *new_location)
383 char headers[1024] =
"";
384 char *authstr =
NULL, *proxyauthstr =
NULL;
400 method = post ?
"POST" :
"GET";
408 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
411 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
412 sizeof(headers) - len);
414 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
415 "Range: bytes=%"PRId64
"-\r\n", s->
off);
419 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
420 sizeof(headers) - len);
422 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
423 sizeof(headers) - len);
428 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
429 "Host: %s\r\n", hoststr);
431 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
447 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
449 authstr ? authstr :
"",
450 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
483 return (off == s->
off) ? 0 : -1;
514 int err, new_location;
554 char crlf[] =
"\r\n";
566 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
579 char footer[] =
"0\r\n\r\n";
585 ret = ret > 0 ? 0 : ret;
611 int64_t old_off = s->
off;
622 memcpy(old_buf, s->
buf_ptr, old_buf_size);
624 if (whence == SEEK_CUR)
626 else if (whence == SEEK_END)
632 memcpy(s->
buffer, old_buf, old_buf_size);
650 #if CONFIG_HTTP_PROTOCOL
661 .priv_data_class = &http_context_class,
665 #if CONFIG_HTTPS_PROTOCOL
676 .priv_data_class = &https_context_class,
681 #if CONFIG_HTTPPROXY_PROTOCOL
693 char hostname[1024], hoststr[1024];
694 char auth[1024], pathbuf[1024], *path;
696 int port, ret = 0, attempts = 0;
703 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
704 pathbuf,
sizeof(pathbuf), uri);
713 ret =
ffurl_open(&s->
hd, lower_url, AVIO_FLAG_READ_WRITE,
721 "CONNECT %s HTTP/1.1\r\n"
723 "Connection: close\r\n"
728 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
779 .url_open = http_proxy_open,
781 .url_write = http_proxy_write,
782 .url_close = http_proxy_close,