25#include "config_components.h"
58#define BUFFER_SIZE (MAX_URL_SIZE + HTTP_HEADERS_SIZE)
59#define MAX_REDIRECTS 8
60#define MAX_CACHED_REDIRECTS 32
63#define MAX_DATE_LEN 19
64#define WHITESPACES " \n\t\r"
168 z_stream inflate_stream;
169 uint8_t *inflate_buffer;
189#define OFFSET(x) offsetof(HTTPContext, x)
190#define D AV_OPT_FLAG_DECODING_PARAM
191#define E AV_OPT_FLAG_ENCODING_PARAM
192#define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
195 {
"seekable",
"control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1,
D },
196 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
E },
198 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { .str =
NULL }, 0, 0,
D |
E },
202 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1,
D |
E },
204 {
"initial_request_size",
"size (in bytes) of initial requests made during probing / header parsing",
OFFSET(initial_request_size),
AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX,
D },
208 {
"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, { .str =
NULL }, 0, 0,
D },
214 {
"none",
"No auth method set, autodetect", 0,
AV_OPT_TYPE_CONST, { .i64 =
HTTP_AUTH_NONE }, 0, 0,
D |
E, .unit =
"auth_type"},
216 {
"send_expect_100",
"Force sending an Expect: 100-continue header for POST",
OFFSET(send_expect_100),
AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1,
E },
219 {
"end_offset",
"try to limit the request to bytes preceding this offset",
OFFSET(end_off),
AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX,
D },
220 {
"method",
"Override the HTTP method or set the expected HTTP method from a client",
OFFSET(method),
AV_OPT_TYPE_STRING, { .str =
NULL }, 0, 0,
D |
E },
221 {
"reconnect",
"auto reconnect after disconnect before EOF",
OFFSET(reconnect),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
D },
222 {
"reconnect_at_eof",
"auto reconnect at EOF",
OFFSET(reconnect_at_eof),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
D },
223 {
"reconnect_on_network_error",
"auto reconnect in case of tcp/tls error during connect",
OFFSET(reconnect_on_network_error),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
D },
224 {
"reconnect_on_http_error",
"list of http status codes to reconnect on",
OFFSET(reconnect_on_http_error),
AV_OPT_TYPE_STRING, { .str =
NULL }, 0, 0,
D },
225 {
"reconnect_streamed",
"auto reconnect streamed / non seekable streams",
OFFSET(reconnect_streamed),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
D },
226 {
"reconnect_delay_max",
"max reconnect delay in seconds after which to give up",
OFFSET(reconnect_delay_max),
AV_OPT_TYPE_INT, { .i64 = 120 }, 0, UINT_MAX/1000/1000,
D },
227 {
"reconnect_max_retries",
"the max number of times to retry a connection",
OFFSET(reconnect_max_retries),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
D },
228 {
"reconnect_delay_total_max",
"max total reconnect delay in seconds after which to give up",
OFFSET(reconnect_delay_total_max),
AV_OPT_TYPE_INT, { .i64 = 256 }, 0, UINT_MAX/1000/1000,
D },
229 {
"respect_retry_after",
"respect the Retry-After header when retrying connections",
OFFSET(respect_retry_after),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
D },
232 {
"reply_code",
"The http status code to return to a client",
OFFSET(reply_code),
AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599,
E},
233 {
"short_seek_size",
"Threshold to favor readahead over seek.",
OFFSET(short_seek_size),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
D },
239 const char *hoststr,
const char *auth,
240 const char *proxyauth);
257 const char *
path, *proxy_path, *lower_proto =
"tcp", *local_path;
258 char *env_http_proxy, *env_no_proxy;
260 char hostname[1024], hoststr[1024], proto[10], tmp_host[1024];
261 char auth[1024], proxyauth[1024] =
"";
264 int port, use_proxy, err = 0;
268 hostname,
sizeof(hostname), &port,
269 path1,
sizeof(path1),
s->location);
270 if (strpbrk(hostname,
"\r\n"))
278 av_strlcpy(tmp_host, hostname,
sizeof(tmp_host));
282 if (strchr(tmp_host,
':'))
283 tmp_host[strcspn(tmp_host,
"%")] =
'\0';
287 proxy_path =
s->http_proxy ?
s->http_proxy : env_http_proxy;
294 if (
h->protocol_whitelist &&
av_match_list(proto,
h->protocol_whitelist,
',') <= 0) {
295 av_log(
h,
AV_LOG_ERROR,
"Protocol '%s' not on whitelist '%s'!\n", proto,
h->protocol_whitelist);
299 if (
h->protocol_blacklist &&
av_match_list(proto,
h->protocol_blacklist,
',') > 0) {
304 if (!strcmp(proto,
"https")) {
315 }
else if (strcmp(proto,
"http")) {
323 hashmark = strchr(path1,
'#');
327 if (path1[0] ==
'\0') {
329 }
else if (path1[0] ==
'?') {
330 snprintf(sanitized_path,
sizeof(sanitized_path),
"/%s", path1);
331 path = sanitized_path;
343 hostname,
sizeof(hostname), &port,
NULL, 0, proxy_path);
348 if (
s->hd && strcmp(
s->hd->filename, buf))
355 h->protocol_whitelist,
h->protocol_blacklist,
h);
361 h,
path, local_path, hoststr, auth, proxyauth);
366 const char *status_group;
376 status_group =
"4xx";
380 status_group =
"5xx";
384 return s->reconnect_on_network_error;
387 if (!
s->reconnect_on_http_error)
390 if (
av_match_list(status_group,
s->reconnect_on_http_error,
',') > 0)
393 snprintf(http_code,
sizeof(http_code),
"%d",
s->http_code);
395 return av_match_list(http_code,
s->reconnect_on_http_error,
',') > 0;
409 delim = strchr(re->
value,
';');
415 if (time(
NULL) > expiry) {
442 return http_code == 301 || http_code == 302 || http_code == 303 ||
443 http_code == 307 || http_code == 308;
451 s->oc_filesize == UINT64_MAX &&
452 (
s->filesize_from_content_range == UINT64_MAX ||
453 s->filesize_from_content_range <= off);
463 int ret, conn_attempts = 1, auth_attempts = 0, redirects = 0;
464 int reconnect_delay = 0;
465 int reconnect_delay_total = 0;
466 uint64_t off =
s->off;
468 uint64_t oc_filesize =
s->oc_filesize;
473 s->oc_filesize = oc_filesize;
477 if (redirects++ >=
s->max_redirects) {
493 cur_auth_type =
s->auth_state.auth_type;
494 cur_proxy_auth_type =
s->auth_state.auth_type;
503 reconnect_delay >
s->reconnect_delay_max ||
504 (
s->reconnect_max_retries >= 0 && conn_attempts >
s->reconnect_max_retries) ||
505 reconnect_delay_total >
s->reconnect_delay_total_max)
509 if (
s->respect_retry_after &&
s->retry_after > 0) {
510 reconnect_delay =
s->retry_after;
511 if (reconnect_delay >
s->reconnect_delay_max)
518 s->willclose ?
"reconnect" :
"retry", off, reconnect_delay);
522 reconnect_delay_total += reconnect_delay;
523 reconnect_delay = 1 + 2 * reconnect_delay;
535 if (
s->http_code == 401) {
543 if (
s->http_code == 407) {
544 if ((cur_proxy_auth_type ==
HTTP_AUTH_NONE ||
s->proxy_auth_state.stale) &&
545 s->proxy_auth_state.auth_type !=
HTTP_AUTH_NONE && auth_attempts < 4) {
554 if (redirects++ >=
s->max_redirects) {
560 s->expires = (
s->http_code == 301 ||
s->http_code == 308) ? INT64_MAX : -1;
568 s->location =
s->new_location;
569 s->new_location =
NULL;
574 memset(&
s->auth_state, 0,
sizeof(
s->auth_state));
583 s->oc_filesize = oc_filesize;
600 char hostname1[1024], hostname2[1024], proto1[10], proto2[10];
604 !(!strcmp(
h->prot->name,
"http") ||
605 !strcmp(
h->prot->name,
"https")))
609 hostname1,
sizeof(hostname1), &port1,
610 NULL, 0,
s->location);
612 hostname2,
sizeof(hostname2), &port2,
614 if (strcmp(proto1, proto2) != 0) {
615 av_log(
h,
AV_LOG_INFO,
"Cannot reuse HTTP connection for different protocol %s vs %s\n",
619 if (port1 != port2 || strncmp(hostname1, hostname2,
sizeof(hostname2)) != 0) {
620 av_log(
h,
AV_LOG_INFO,
"Cannot reuse HTTP connection for different host: %s:%d != %s:%d\n",
627 if (!
s->end_chunked_post) {
636 s->end_chunked_post = 0;
640 s->oc_filesize = UINT64_MAX;
641 s->end_of_resource = 0;
642 s->icy_data_read = 0;
666 return s->new_location;
671 int ret,
body = 0, reply_code, message_len;
672 const char *reply_text, *content_type;
675 content_type =
"text/plain";
679 switch (status_code) {
683 reply_text =
"Bad Request";
688 reply_text =
"Forbidden";
693 reply_text =
"Not Found";
698 reply_text =
"Too Many Requests";
703 content_type =
s->content_type ?
s->content_type :
"application/octet-stream";
708 reply_text =
"Internal server error";
716 "HTTP/1.1 %03d %s\r\n"
717 "Content-Type: %s\r\n"
718 "Content-Length: %zu\r\n"
725 strlen(reply_text) + 6,
726 s->headers ?
s->headers :
"",
732 "HTTP/1.1 %03d %s\r\n"
733 "Content-Type: %s\r\n"
734 "Transfer-Encoding: chunked\r\n"
740 s->headers ?
s->headers :
"");
794 char hostname[1024], proto[10];
796 const char *lower_proto =
"tcp";
798 av_url_split(proto,
sizeof(proto),
NULL, 0, hostname,
sizeof(hostname), &port,
800 if (!strcmp(proto,
"https"))
802 ff_url_join(lower_url,
sizeof(lower_url), lower_proto,
NULL, hostname, port,
808 h->protocol_whitelist,
h->protocol_blacklist,
h
828 if(
s->seekable == 1 )
833 s->initial_requests =
s->seekable != 0 &&
s->initial_request_size > 0;
834 s->filesize = UINT64_MAX;
835 s->oc_filesize = UINT64_MAX;
836 s->end_of_resource = 0;
853 int len = strlen(
s->headers);
854 if (
len < 2 || strcmp(
"\r\n",
s->headers +
len - 2)) {
856 "No trailing CRLF found in HTTP header. Adding it.\n");
860 s->headers[
len] =
'\r';
861 s->headers[
len + 1] =
'\n';
862 s->headers[
len + 2] =
'\0';
873 inflateEnd(&
s->inflate_stream);
896 cc = (*c)->priv_data;
912 if (
s->buf_ptr >=
s->buf_end) {
916 }
else if (
len == 0) {
919 s->buf_ptr =
s->buffer;
920 s->buf_end =
s->buffer +
len;
923 return *
s->buf_ptr++;
938 if (q >
line && q[-1] ==
'\r')
944 if ((q -
line) < line_size - 1)
954 memset(st, 0,
sizeof(*st));
978 st->
code = 100 * (p[0] -
'0') + 10 * (p[1] -
'0') + p[2] -
'0';
979 if (st->
code < 100 || st->
code > 599) {
999 if (http_code >= 400 && http_code < 600 &&
1001 (http_code != 407 ||
s->proxy_auth_state.auth_type !=
HTTP_AUTH_NONE)) {
1014 s->new_location =
av_strdup(redirected_location);
1015 if (!
s->new_location)
1024 const char *slash, *end;
1026 if (!strncmp(p,
"bytes ", 6)) {
1028 s->off = strtoull(p,
NULL, 10);
1030 if ((end = strchr(p,
'-')) && strlen(end) > 0) {
1031 s->range_end = strtoull(end + 1,
NULL, 10) + 1;
1032 if (
s->range_end <=
s->off) {
1034 s->range_end - 1,
s->off);
1038 if ((slash = strchr(p,
'/')) && strlen(slash) > 0) {
1040 s->filesize_from_content_range = strtoull(slash + 1,
NULL, 10);
1042 s->content_range_unknown_size = 1;
1045 if (
s->seekable == -1 && (!
s->is_akamai ||
s->filesize != 2147483647))
1058 inflateEnd(&
s->inflate_stream);
1059 if (inflateInit2(&
s->inflate_stream, 32 + 15) != Z_OK) {
1061 s->inflate_stream.msg);
1064 if (zlibCompileFlags() & (1 << 17)) {
1066 "Your zlib was compiled without gzip support.\n");
1071 "Compressed (%s) content, need zlib with gzip support\n", p);
1086 int len = 4 + strlen(p) + strlen(
tag);
1087 int is_first = !
s->icy_metadata_headers;
1092 if (
s->icy_metadata_headers)
1093 len += strlen(
s->icy_metadata_headers);
1099 *
s->icy_metadata_headers =
'\0';
1113 for (
i = 0, j = 0; date_str[
i] !=
'\0' && j < date_buf_len;
i++) {
1114 if ((date_str[
i] >=
'0' && date_str[
i] <=
'9') ||
1115 (date_str[
i] >=
'A' && date_str[
i] <=
'Z') ||
1116 (date_str[
i] >=
'a' && date_str[
i] <=
'z')) {
1117 date_buf[j] = date_str[
i];
1125 while ((*date <
'0' || *date >
'9') && *date !=
'\0')
1133 char *param, *next_param, *cstr, *back;
1134 char *saveptr =
NULL;
1143 back = &cstr[strlen(cstr)-1];
1152 while ((param =
av_strtok(next_param,
";", &saveptr))) {
1174 const char *domain = e ? e->
value + (e->
value[0] ==
'.') :
"";
1176 return *domain ? domain :
NULL;
1181 return !
host[strspn(
host,
"0123456789.")] || strchr(
host,
':');
1209 if (!cookie_entry || !cookie_entry->
value) {
1216 struct tm new_tm = {0};
1228 if (e2 && e2->
value) {
1232 if (e2 && e2->
value) {
1233 struct tm old_tm = {0};
1248 int host_only =
host && !domain;
1257 if (!(eql = strchr(p,
'=')))
return AVERROR(EINVAL);
1277 char host[1024], *cookies, *cookie, *next, *saveptr =
NULL, *
bound;
1288 while ((cookie =
av_strtok(next,
"\n", &saveptr))) {
1290 int len = strlen(cookie), host_only;
1301 av_bprintf(&bp,
"%.*s%s%s\n",
len, cookie, host_only ?
"; @hostonly=" :
"", host_only ?
host :
"");
1328 if (*cookies)
av_free(*cookies);
1330 if (!*cookies)
return AVERROR(ENOMEM);
1384 if (
line[0] ==
'\0') {
1390 if (line_count == 0) {
1391 if (
s->is_connected_server) {
1402 av_log(
h,
AV_LOG_ERROR,
"Received and expected HTTP method do not match. (%s expected, %s received)\n",
1411 "(%s autodetected %s received)\n", auto_method, method);
1457 s->http_code = st.
code;
1459 *parsed_http_code = 1;
1465 while (*p !=
'\0' && *p !=
':')
1479 s->filesize == UINT64_MAX) {
1480 s->filesize = strtoull(p,
NULL, 10);
1485 !strncmp(p,
"bytes", 5) &&
1486 s->seekable == -1) {
1490 s->filesize = UINT64_MAX;
1505 s->is_mediagateway = 1;
1514 s->icy_metaint = strtoull(p,
NULL, 10);
1533 s->retry_after = (
unsigned int)
FFMAX(0,
diff);
1535 s->retry_after = strtoul(p,
NULL, 10);
1555 char *cookie, *set_cookies, *next;
1556 char *saveptr =
NULL;
1569 while ((cookie =
av_strtok(next,
"\n", &saveptr)) && !
ret) {
1572 const char *domain, *eql;
1585 eql = strchr(cookie,
'=');
1586 if (!cookie_entry || !eql || eql == cookie + strspn(cookie,
WHITESPACES) ||
1587 memchr(cookie,
';', eql - cookie))
1596 struct tm tm_buf = {0};
1625 char *
tmp = *cookies;
1653 int err = 0, http_err = 0;
1657 s->chunksize = UINT64_MAX;
1658 s->filesize_from_content_range = UINT64_MAX;
1659 s->content_range_unknown_size = 0;
1662 int parsed_http_code = 0;
1674 if (parsed_http_code) {
1692 if (
s->filesize_from_content_range != UINT64_MAX)
1693 s->filesize =
s->filesize_from_content_range;
1694 else if (
s->content_range_unknown_size)
1695 s->filesize =
s->oc_filesize;
1698 if (
s->filesize_from_content_range != UINT64_MAX)
1699 s->oc_filesize =
s->filesize_from_content_range;
1700 else if (
s->http_code == 200 &&
s->filesize != UINT64_MAX)
1701 s->oc_filesize =
s->filesize;
1703 if (
s->seekable == -1 &&
s->is_mediagateway &&
s->filesize == 2000000000)
1707 s->initial_requests = 0;
1725#define NEEDS_ESCAPE(ch) \
1726 ((ch) <= ' ' || (ch) >= '\x7f' || \
1727 (ch) == '"' || (ch) == '%' || (ch) == '<' || (ch) == '>' || (ch) == '\\' || \
1728 (ch) == '^' || (ch) == '`' || (ch) == '{' || (ch) == '}' || (ch) == '|')
1732 while (*path && q - buf < sizeof(buf) - 4) {
1733 if (path[0] == '%' && av_isxdigit(path[1]) && av_isxdigit(path[2])) {
1737 } else if (NEEDS_ESCAPE(*path)) {
1738 q += snprintf(q, 4, "%%%02X", (uint8_t)*path++);
1743 av_bprint_append_data(bp, buf, q - buf);
1747static uint64_t request_size(URLContext *h)
1749 HTTPContext *s = h->priv_data;
1750 if (s->initial_requests)
1751 return s->initial_request_size;
1752 return s->request_size;
1755static int http_connect(URLContext *h, const char *path, const char *local_path,
1756 const char *hoststr, const char *auth,
1757 const char *proxyauth)
1759 HTTPContext *s = h->priv_data;
1762 char *authstr = NULL, *proxyauthstr = NULL;
1763 uint64_t off = s->off;
1765 int send_expect_100 = 0;
1768 av_bprint_init_for_buffer(&request, s->buffer, sizeof(s->buffer));
1770 /* send http header */
1771 post = h->flags & AVIO_FLAG_WRITE;
1774 /* force POST method and disable chunked encoding when
1775 * custom HTTP post data is set */
1777 s->chunked_post = 0;
1783 method = post ? "POST" : "GET";
1785 authstr = ff_http_auth_create_response(&s->auth_state, auth,
1786 local_path, method);
1787 proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
1788 local_path, method);
1790 if (post && !s->post_data) {
1791 if (s->send_expect_100 != -1) {
1792 send_expect_100 = s->send_expect_100;
1794 send_expect_100 = 0;
1795 /* The user has supplied authentication but we don't know the auth type,
1796 * send Expect: 100-continue to get the 401 response including the
1797 * WWW-Authenticate header, or an 100 continue if no auth actually
1799 if (auth && *auth &&
1800 s->auth_state.auth_type == HTTP_AUTH_NONE)
1801 send_expect_100 = 1;
1805 av_bprintf(&request, "%s ", method);
1806 bprint_escaped_path(&request, path);
1807 av_bprintf(&request, " HTTP/1.1\r\n");
1809 if (post && s->chunked_post)
1810 av_bprintf(&request, "Transfer-Encoding: chunked\r\n");
1811 /* set default headers if needed */
1812 if (!has_header(s->headers, "\r\nUser-Agent: "))
1813 av_bprintf(&request, "User-Agent: %s\r\n", s->user_agent);
1815 /* set default headers if needed */
1816 if (!has_header(s->headers, "\r\nReferer: "))
1817 av_bprintf(&request, "Referer: %s\r\n", s->referer);
1819 if (!has_header(s->headers, "\r\nAccept: "))
1820 av_bprintf(&request, "Accept: *
1831 target_off = UINT64_MAX;
1833 target_off =
FFMIN(target_off,
s->end_off);
1834 if (target_off != UINT64_MAX) {
1835 av_bprintf(&request,
"%"PRId64, target_off - 1);
1836 is_partial_request = 1;
1838 }
else if (
s->end_off)
1842 if (send_expect_100 && !
has_header(
s->headers,
"\r\nExpect: "))
1843 av_bprintf(&request,
"Expect: 100-continue\r\n");
1845 if (!
has_header(
s->headers,
"\r\nConnection: ")) {
1846 keep_alive =
s->multiple_requests > 0;
1847 if (
s->multiple_requests < 0 && is_partial_request)
1849 av_bprintf(&request,
"Connection: %s\r\n", keep_alive ?
"keep-alive" :
"close");
1853 av_bprintf(&request,
"Host: %s\r\n", hoststr);
1854 if (!
has_header(
s->headers,
"\r\nContent-Length: ") &&
s->post_data)
1855 av_bprintf(&request,
"Content-Length: %d\r\n",
s->post_datalen);
1857 if (!
has_header(
s->headers,
"\r\nContent-Type: ") &&
s->content_type)
1858 av_bprintf(&request,
"Content-Type: %s\r\n",
s->content_type);
1859 if (!
has_header(
s->headers,
"\r\nCookie: ") &&
s->cookies) {
1860 char *cookies =
NULL;
1861 if (!
get_cookies(
s, &cookies, local_path) && cookies) {
1862 av_bprintf(&request,
"Cookie: %s\r\n", cookies);
1866 if (!
has_header(
s->headers,
"\r\nIcy-MetaData: ") &&
s->icy)
1876 av_bprintf(&request,
"Proxy-%s", proxyauthstr);
1887 if ((err =
ffurl_write(
s->hd, request.str, request.len)) < 0)
1891 if ((err =
ffurl_write(
s->hd,
s->post_data,
s->post_datalen)) < 0)
1895 s->buf_ptr =
s->buffer;
1896 s->buf_end =
s->buffer;
1899 s->icy_data_read = 0;
1901 s->filesize = UINT64_MAX;
1903 s->willclose = !keep_alive;
1904 s->end_chunked_post = 0;
1909 if (post && !
s->post_data && !send_expect_100) {
1926 s->sum_latency += latency;
1927 s->max_latency =
FFMAX(
s->max_latency, latency);
1932 s->http_code == 401 ||
s->http_code == 407)
1935 if (off !=
s->off) {
1937 "Unexpected offset: expected %"PRIu64
", got %"PRIu64
"\n",
1952 return s->end_off ?
FFMIN(
s->end_off,
s->filesize) :
s->filesize;
1963 if (
s->chunksize != UINT64_MAX) {
1967 if (!
s->chunksize) {
1976 s->chunksize = strtoull(
line,
NULL, 16);
1979 "Chunked encoding data size: %"PRIu64
"\n",
1982 if (!
s->chunksize) {
1984 int more =
s->range_end &&
s->off ==
s->range_end &&
1986 if (
s->multiple_requests) {
1991 s->chunkend = !more;
1996 return more ?
AVERROR(EAGAIN) : 0;
1998 else if (
s->chunksize == UINT64_MAX) {
2008 len =
s->buf_end -
s->buf_ptr;
2012 memcpy(buf,
s->buf_ptr,
len);
2016 uint64_t target_end =
s->range_end ?
s->range_end : file_end;
2017 if ((!
s->willclose ||
s->chunksize == UINT64_MAX) &&
s->off >= file_end)
2019 if (
s->off == target_end && target_end < file_end)
2023 (!
s->willclose ||
s->chunksize == UINT64_MAX) &&
s->off < target_end) {
2025 "Stream ends prematurely at %"PRIu64
", should be %"PRIu64
"\n",
2033 if (
s->chunksize > 0 &&
s->chunksize != UINT64_MAX) {
2035 s->chunksize -=
len;
2042#define DECOMPRESS_BUF_SIZE (256 * 1024)
2043static int http_buf_read_compressed(
URLContext *
h, uint8_t *buf,
int size)
2048 if (!
s->inflate_buffer) {
2049 s->inflate_buffer =
av_malloc(DECOMPRESS_BUF_SIZE);
2050 if (!
s->inflate_buffer)
2054 if (
s->inflate_stream.avail_in == 0) {
2062 s->inflate_stream.next_in =
s->inflate_buffer;
2063 s->inflate_stream.avail_in =
read;
2066 s->inflate_stream.avail_out =
size;
2067 s->inflate_stream.next_out = buf;
2070 if (
ret != Z_OK &&
ret != Z_STREAM_END)
2072 ret,
s->inflate_stream.msg);
2074 return size -
s->inflate_stream.avail_out;
2083 s->oc_filesize =
s->filesize_from_content_range;
2084 s->filesize =
s->oc_filesize;
2085 s->end_of_resource = 1;
2096 int reconnect_delay = 0;
2097 int reconnect_delay_total = 0;
2098 int conn_attempts = 1;
2100 if (
s->end_of_resource)
2103 if (
s->hd &&
s->end_chunked_post && !
s->end_header) {
2110 if (!
s->is_connected_server &&
s->method && !
av_strcasecmp(
s->method,
"HEAD"))
2118 return http_buf_read_compressed(
h, buf,
size);
2123 while (read_ret < 0) {
2124 uint64_t target =
h->is_streamed ? 0 :
s->off;
2126 bool is_premature = file_end > 0 &&
s->off < file_end;
2130 else if (read_ret ==
AVERROR(EAGAIN)) {
2135 s->initial_requests = 0;
2146 if (
h->is_streamed && !
s->reconnect_streamed)
2149 if (!(
s->reconnect && is_premature) &&
2157 if (reconnect_delay >
s->reconnect_delay_max || (
s->reconnect_max_retries >= 0 && conn_attempts >
s->reconnect_max_retries) ||
2158 reconnect_delay_total >
s->reconnect_delay_total_max)
2161 av_log(
h,
AV_LOG_WARNING,
"Will %s at %"PRIu64
" in %d second(s), error=%s.\n",
s->willclose ?
"reconnect" :
"retry",
2164 if (err !=
AVERROR(ETIMEDOUT))
2166 reconnect_delay_total += reconnect_delay;
2167 reconnect_delay = 1 + 2*reconnect_delay;
2170 if (seek_ret >= 0 && seek_ret != target) {
2175 if (
s->end_of_resource) {
2206 if (
s->icy_metaint <
s->icy_data_read)
2208 remaining =
s->icy_metaint -
s->icy_data_read;
2220 char data[255 * 16 + 1];
2231 s->icy_data_read = 0;
2232 remaining =
s->icy_metaint;
2242 if (
s->icy_metaint > 0) {
2250 s->icy_data_read +=
size;
2259 char crlf[] =
"\r\n";
2262 if (!
s->chunked_post) {
2284 char footer[] =
"0\r\n\r\n";
2299 if (read_ret < 0 && read_ret !=
AVERROR(EAGAIN)) {
2304 s->end_chunked_post = 1;
2316 inflateEnd(&
s->inflate_stream);
2320 if (
s->hd && !
s->end_chunked_post)
2333 av_log(
h,
AV_LOG_DEBUG,
"Statistics: %d connection%s, %d request%s, %d retr%s, %d reconnection%s, %d redirect%s\n",
2334 s->nb_connections,
s->nb_connections == 1 ?
"" :
"s",
2335 s->nb_requests,
s->nb_requests == 1 ?
"" :
"s",
2336 s->nb_retries,
s->nb_retries == 1 ?
"y" :
"ies",
2337 s->nb_reconnects,
s->nb_reconnects == 1 ?
"" :
"s",
2338 s->nb_redirects,
s->nb_redirects == 1 ?
"" :
"s");
2340 if (
s->nb_requests > 0) {
2342 1e-3 *
s->sum_latency /
s->nb_requests,
2343 1e-3 *
s->max_latency);
2352 uint64_t old_off =
s->off;
2353 uint64_t old_range_end =
s->range_end;
2354 uint64_t old_chunksize =
s->chunksize;
2355 int old_chunkend =
s->chunkend;
2356 int old_willclose =
s->willclose;
2358 int old_compressed =
s->compressed;
2360 int old_end_of_resource =
s->end_of_resource;
2362 int old_buf_size,
ret;
2364 uint8_t discard[4096];
2367 return s->filesize == UINT64_MAX ?
AVERROR(ENOSYS) :
s->filesize;
2368 else if ((
s->filesize == UINT64_MAX && whence == SEEK_END))
2371 if (whence == SEEK_CUR)
2373 else if (whence == SEEK_END)
2375 else if (whence != SEEK_SET)
2379 if (!force_reconnect && off ==
s->off &&
s->hd)
2381 if (off &&
h->is_streamed)
2384 s->end_of_resource = 0;
2390 s->buf_ptr =
s->buf_end =
s->buffer;
2395 if (strcmp(
s->uri,
s->location)) {
2401 s->location = new_uri;
2405 old_buf_size =
s->buf_end -
s->buf_ptr;
2406 memcpy(old_buf,
s->buf_ptr, old_buf_size);
2410 uint64_t old_read_pos = old_off + old_buf_size;
2411 if (
s->hd && !
s->willclose &&
s->range_end && short_seek > 0 &&
2412 old_read_pos + short_seek >=
s->range_end)
2414 uint64_t remaining =
s->range_end - old_read_pos;
2419 "%"PRIu64
" remaining byte(s)\n",
s->off, remaining);
2448 memcpy(
s->buffer, old_buf, old_buf_size);
2449 s->buf_ptr =
s->buffer;
2450 s->buf_end =
s->buffer + old_buf_size;
2452 s->range_end = old_range_end;
2453 s->chunksize = old_chunksize;
2454 s->chunkend = old_chunkend;
2455 s->willclose = old_willclose;
2457 s->compressed = old_compressed;
2461 s->end_of_resource = old_end_of_resource;
2486 if (
s->short_seek_size >= 1)
2487 return s->short_seek_size;
2491#define HTTP_CLASS(flavor) \
2492static const AVClass flavor ## _context_class = { \
2493 .class_name = # flavor, \
2494 .item_name = av_default_item_name, \
2495 .option = http_options, \
2496 .version = LIBAVUTIL_VERSION_INT, \
2499#if CONFIG_HTTP_PROTOCOL
2515 .priv_data_class = &http_context_class,
2517 .default_whitelist =
"http,https,tls,rtp,tcp,udp,crypto,httpproxy,data"
2521#if CONFIG_HTTPS_PROTOCOL
2535 .priv_data_class = &https_context_class,
2537 .default_whitelist =
"http,https,tls,rtp,tcp,udp,crypto,httpproxy"
2541#if CONFIG_HTTPPROXY_PROTOCOL
2553 char hostname[1024], hoststr[1024];
2554 char auth[1024], pathbuf[1024], *
path;
2555 char lower_url[100];
2556 int port,
ret = 0, auth_attempts = 0;
2560 if(
s->seekable == 1 )
2565 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
2566 pathbuf,
sizeof(pathbuf), uri);
2571 for (
const char *p =
path; *
p;
p++)
2572 if ((uint8_t)*p <=
' ' || *p == 0x7f)
2575 ff_url_join(lower_url,
sizeof(lower_url),
"tcp",
NULL, hostname, port,
2579 &
h->interrupt_callback,
NULL,
2580 h->protocol_whitelist,
h->protocol_blacklist,
h);
2586 snprintf(
s->buffer,
sizeof(
s->buffer),
2587 "CONNECT %s HTTP/1.1\r\n"
2589 "Connection: close\r\n"
2594 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
2600 s->buf_ptr =
s->buffer;
2601 s->buf_end =
s->buffer;
2603 s->filesize = UINT64_MAX;
2604 cur_auth_type =
s->proxy_auth_state.auth_type;
2620 if (
s->http_code == 407 &&
2622 s->proxy_auth_state.auth_type !=
HTTP_AUTH_NONE && auth_attempts < 2) {
2627 if (
s->http_code < 400)
2632 http_proxy_close(
h);
2636static int http_proxy_write(
URLContext *
h,
const uint8_t *buf,
int size)
2654 len =
s->buf_end -
s->buf_ptr;
2658 memcpy(buf,
s->buf_ptr,
len);
2666 .name =
"httpproxy",
2667 .url_open = http_proxy_open,
2668 .url_read = http_proxy_read,
2669 .url_write = http_proxy_write,
2670 .url_close = http_proxy_close,
SwsAArch64OpImplParams params
static double bound(const double threshold, const double val)
static AVDictionary * opts
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
int ffurl_alloc(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Create a URLContext for accessing to the resource indicated by url, but do not initiate the connectio...
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
int ffurl_close(URLContext *h)
int ffurl_get_short_seek(void *urlcontext)
Return the current short seek threshold value for this URL.
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
#define AVIO_FLAG_READ
read-only
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
#define AVIO_FLAG_WRITE
write-only
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
char * av_asprintf(const char *fmt,...)
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_BPRINT_SIZE_UNLIMITED
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static int64_t filesize(AVIOContext *pb)
static char * getenv_utf8(const char *varname)
static void freeenv_utf8(char *var)
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
#define AV_OPT_FLAG_EXPORT
The option is intended for exporting values to the caller.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_BINARY
Underlying C type is a uint8_t* that is either NULL or points to an array allocated with the av_mallo...
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function.
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
#define AV_DICT_MATCH_CASE
Only get an entry with exact-case key match.
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
#define AVERROR_HTTP_FORBIDDEN
#define AVERROR_HTTP_BAD_REQUEST
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
#define AVERROR_HTTP_SERVER_ERROR
#define AVERROR_HTTP_OTHER_4XX
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AVERROR_HTTP_TOO_MANY_REQUESTS
#define AVERROR_EOF
End of file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AVERROR_HTTP_UNAUTHORIZED
#define AVERROR_HTTP_NOT_FOUND
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle.
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
int av_match_list(const char *name, const char *list, char separator)
Check if a name is in a list.
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
int av_stristart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str independent of case.
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
static int http_handshake(URLContext *c)
static int parse_content_range(URLContext *h, const char *p)
static int http_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
static int cookie_string(AVDictionary *dict, char **cookies)
static int redirect_cache_set(HTTPContext *s, const char *source, const char *dest, int64_t expiry)
static const char * cookie_domain(const AVDictionary *cookie_params)
static int store_icy(URLContext *h, int size)
static int parse_content_encoding(URLContext *h, const char *p)
static uint64_t requested_end(HTTPContext *s)
static int host_is_ip_literal(const char *host)
static const AVOption http_options[]
static int http_shutdown(URLContext *h, int flags)
int ff_http_do_new_request2(URLContext *h, const char *uri, AVDictionary **opts)
Send a new HTTP request, reusing the old connection.
static int parse_set_cookie(const char *set_cookie, AVDictionary **dict)
static int http_read_header(URLContext *h)
static int http_read(URLContext *h, uint8_t *buf, int size)
static int http_getc(HTTPContext *s)
static uint64_t request_size(URLContext *h)
static int get_cookies(HTTPContext *s, char **cookies, const char *path)
Create a string containing cookie values for use as a HTTP cookie header field value for a particular...
static int process_line(URLContext *h, char *line, int line_count, int *parsed_http_code)
static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
static int http_get_line(HTTPContext *s, char *line, int line_size)
static int http_buf_read(URLContext *h, uint8_t *buf, int size)
static int parse_icy(HTTPContext *s, const char *tag, const char *p)
static int has_header(const char *str, const char *header)
static int http_listen(URLContext *h, const char *uri, int flags, AVDictionary **options)
static int is_redirect(int http_code)
static int check_http_code(URLContext *h, int http_code, const char *end)
static int host_in_cookie_domain(const char *host, const char *domain)
static int http_get_short_seek(URLContext *h)
static int http_get_file_handle(URLContext *h)
void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
Initialize the authentication state based on another HTTP URLContext.
static int http_write_reply(URLContext *h, int status_code)
static int http_should_reconnect(HTTPContext *s, int err)
static char * redirect_cache_get(HTTPContext *s)
static void handle_http_errors(URLContext *h, int error)
static int is_end_of_resource(HTTPContext *s, int err, uint64_t off)
const char * ff_http_get_new_location(URLContext *h)
#define DEFAULT_USER_AGENT
#define HTTP_CLASS(flavor)
static void parse_cache_control(HTTPContext *s, const char *p)
static void parse_expires(HTTPContext *s, const char *p)
static int http_connect(URLContext *h, const char *path, const char *local_path, const char *hoststr, const char *auth, const char *proxyauth)
static int http_write(URLContext *h, const uint8_t *buf, int size)
static int http_end_of_resource(HTTPContext *s, int err)
static void bprint_escaped_path(AVBPrint *bp, const char *path)
Escape unsafe characters in path in order to pass them safely to the HTTP request.
int ff_http_parse_status_line(void *logctx, const char *line, HTTPStatusLine *st)
Parse the status line of an HTTP response.
static int64_t http_seek(URLContext *h, int64_t off, int whence)
static int parse_http_date(const char *date_str, struct tm *buf)
static int bind_cookies_to_host(HTTPContext *s)
static int http_close(URLContext *h)
int ff_http_do_new_request(URLContext *h, const char *uri)
Send a new HTTP request, reusing the old connection.
static int http_open_cnx(URLContext *h, AVDictionary **options, int end_is_eof)
#define MAX_CACHED_REDIRECTS
static int http_accept(URLContext *s, URLContext **c)
static int http_read_stream(URLContext *h, uint8_t *buf, int size)
static int parse_cookie(HTTPContext *s, const char *p, const char *host, AVDictionary **cookies)
static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
static int parse_location(HTTPContext *s, const char *p)
static void ff_http_parse_icy_packet(void *logctx, AVDictionary **metadata, char *data)
Split an in-band ICY metadata packet into its "Key='Value';" pairs.
static int ff_http_averror(int status_code, int default_averror)
char * ff_http_auth_create_response(HTTPAuthState *state, const char *auth, const char *path, const char *method)
void ff_http_auth_handle_header(HTTPAuthState *state, const char *key, const char *value)
HTTPAuthType
Authentication types, ordered from weakest to strongest.
@ HTTP_AUTH_NONE
No authentication specified.
@ HTTP_AUTH_BASIC
HTTP 1.0 Basic auth from RFC 1945 (also in RFC 2617)
Utility Preprocessor macros.
static void body(uint32_t ABCD[4], const uint8_t *src, size_t nblocks)
Memory handling functions.
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
Waits for up to 'timeout' microseconds.
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
miscellaneous OS support macros and functions.
char * av_small_strptime(const char *p, const char *fmt, struct tm *dt)
Simplified version of strptime.
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value.
const URLProtocol ff_https_protocol
const URLProtocol ff_http_protocol
const URLProtocol ff_httpproxy_protocol
static const uint8_t header[24]
Describe the class of an AVClass context structure.
HTTP Authentication state structure.
HandshakeState handshake_step
AVDictionary * chained_options
uint64_t filesize_from_content_range
char * icy_metadata_packet
char * reconnect_on_http_error
int seekable
Control seekability, 0 = disable, 1 = enable, -1 = probe.
int reconnect_delay_total_max
unsigned char buffer[BUFFER_SIZE]
AVDictionary * redirect_cache
int reconnect_on_network_error
char * icy_metadata_headers
int multiple_requests
A flag which indicates if we use persistent connections.
HTTPAuthState proxy_auth_state
char * cookies
holds newline ( ) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
AVDictionary * cookie_dict
int content_range_unknown_size
int reconnect_max_retries
uint64_t initial_request_size
Parsed form of a response status line.
int code
response status code
const char * reason
reason phrase, points into the parsed line
int willclose
the version implies the connection will close
char version[4]
protocol version, e.g.
AVIOInterruptCB interrupt_callback
static void error(const char *err)
int64_t av_gettime(void)
Get the current time in microseconds.
int ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
unbuffered private I/O API
static int ffurl_write(URLContext *h, const uint8_t *buf, int size)
Write size bytes from buf to the resource accessed by h.
static int ffurl_read(URLContext *h, uint8_t *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
#define URL_PROTOCOL_FLAG_NETWORK
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)