37 #define BUFFER_SIZE 1024
38 #define MAX_REDIRECTS 8
47 int64_t
off, filesize;
56 #define OFFSET(x) offsetof(HTTPContext, x)
57 #define D AV_OPT_FLAG_DECODING_PARAM
58 #define E AV_OPT_FLAG_ENCODING_PARAM
60 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1,
E },
61 {
"headers",
"custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
64 #define HTTP_CLASS(flavor)\
65 static const AVClass flavor ## _context_class = {\
66 .class_name = #flavor,\
67 .item_name = av_default_item_name,\
69 .version = LIBAVUTIL_VERSION_INT,\
76 const char *hoststr,
const char *auth,
77 const char *proxyauth,
int *new_location);
91 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
92 char hostname[1024], hoststr[1024], proto[10];
93 char auth[1024], proxyauth[1024] =
"";
95 char buf[1024], urlbuf[1024];
96 int port, use_proxy, err, location_changed = 0, redirects = 0;
101 proxy_path = getenv(
"http_proxy");
102 use_proxy = (proxy_path !=
NULL) && !getenv(
"no_proxy") &&
109 hostname,
sizeof(hostname), &port,
113 if (!strcmp(proto,
"https")) {
122 if (path1[0] ==
'\0')
134 hostname,
sizeof(hostname), &port,
NULL, 0, proxy_path);
146 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
164 && location_changed == 1) {
169 location_changed = 0;
191 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
204 }
else if (len == 0) {
226 if (q > line && q[-1] ==
'\r')
232 if ((q - line) < line_size - 1)
249 if (line_count == 0) {
250 while (!isspace(*p) && *p !=
'\0')
269 while (*p !=
'\0' && *p !=
':')
287 if (!strncmp (p,
"bytes ", 6)) {
290 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
294 }
else if (!
av_strcasecmp(tag,
"Accept-Ranges") && !strncmp(p,
"bytes", 5)) {
306 if (!strcmp(p,
"close"))
313 static inline int has_header(
const char *str,
const char *header)
322 const char *hoststr,
const char *auth,
323 const char *proxyauth,
int *new_location)
328 char headers[1024] =
"";
329 char *authstr =
NULL, *proxyauthstr =
NULL;
337 method = post ?
"POST" :
"GET";
345 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
348 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
349 sizeof(headers) - len);
351 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
352 "Range: bytes=%"PRId64
"-\r\n", s->
off);
354 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
355 sizeof(headers)-len);
357 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
358 "Host: %s\r\n", hoststr);
373 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
375 authstr ? authstr :
"",
376 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
414 return (off == s->
off) ? 0 : -1;
475 char crlf[] =
"\r\n";
487 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
500 char footer[] =
"0\r\n\r\n";
506 ret = ret > 0 ? 0 : ret;
518 int64_t old_off = s->
off;
529 memcpy(old_buf, s->
buf_ptr, old_buf_size);
531 if (whence == SEEK_CUR)
533 else if (whence == SEEK_END)
539 memcpy(s->
buffer, old_buf, old_buf_size);
557 #if CONFIG_HTTP_PROTOCOL
567 .priv_data_class = &http_context_class,
571 #if CONFIG_HTTPS_PROTOCOL
581 .priv_data_class = &https_context_class,
586 #if CONFIG_HTTPPROXY_PROTOCOL
598 char hostname[1024], hoststr[1024];
599 char auth[1024], pathbuf[1024], *path;
600 char line[1024], lower_url[100];
607 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
608 pathbuf,
sizeof(pathbuf), uri);
625 "CONNECT %s HTTP/1.1\r\n"
627 "Connection: close\r\n"
632 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
659 av_dlog(h,
"header='%s'\n", line);
684 static int http_proxy_write(
URLContext *h,
const uint8_t *buf,
int size)
692 .url_open = http_proxy_open,
694 .url_write = http_proxy_write,
695 .url_close = http_proxy_close,