22 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
43 #define BUFFER_SIZE MAX_URL_SIZE
44 #define MAX_REDIRECTS 8
55 int64_t
off, filesize;
78 z_stream inflate_stream;
85 #define OFFSET(x) offsetof(HTTPContext, x)
86 #define D AV_OPT_FLAG_DECODING_PARAM
87 #define E AV_OPT_FLAG_ENCODING_PARAM
88 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
90 {
"seekable",
"control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1,
D },
91 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
92 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D|
E },
95 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
D|
E },
98 {
"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,
D },
100 {
"icy_metadata_headers",
"return ICY metadata headers",
OFFSET(icy_metadata_headers),
AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
101 {
"icy_metadata_packet",
"return current ICY metadata packet",
OFFSET(icy_metadata_packet),
AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
103 {
"none",
"No auth method set, autodetect", 0,
AV_OPT_TYPE_CONST, {.i64 =
HTTP_AUTH_NONE}, 0, 0,
D|
E,
"auth_type" },
105 {
"send_expect_100",
"Force sending an Expect: 100-continue header for POST",
OFFSET(send_expect_100),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
E,
"auth_type" },
108 #define HTTP_CLASS(flavor)\
109 static const AVClass flavor ## _context_class = {\
110 .class_name = #flavor,\
111 .item_name = av_default_item_name,\
113 .version = LIBAVUTIL_VERSION_INT,\
120 const char *hoststr,
const char *auth,
121 const char *proxyauth,
int *new_location);
135 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
136 char hostname[1024], hoststr[1024], proto[10];
137 char auth[1024], proxyauth[1024] =
"";
140 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
148 hostname,
sizeof(hostname), &port,
150 ff_url_join(hoststr,
sizeof(hoststr), NULL, NULL, hostname, port, NULL);
152 proxy_path = getenv(
"http_proxy");
154 proxy_path != NULL &&
av_strstart(proxy_path,
"http://", NULL);
156 if (!strcmp(proto,
"https")) {
165 if (path1[0] ==
'\0')
173 ff_url_join(urlbuf,
sizeof(urlbuf), proto, NULL, hostname, port,
"%s",
177 hostname,
sizeof(hostname), &port, NULL, 0, proxy_path);
180 ff_url_join(buf,
sizeof(buf), lower_proto, NULL, hostname, port, NULL);
191 if (
http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
211 && location_changed == 1) {
220 location_changed = 0;
264 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2))
280 }
else if (len == 0) {
302 if (q > line && q[-1] ==
'\r')
308 if ((q - line) < line_size - 1)
322 if (line[0] ==
'\0') {
328 if (line_count == 0) {
348 while (*p !=
'\0' && *p !=
':')
367 if (!strncmp (p,
"bytes ", 6)) {
369 s->
off = strtoll(p, NULL, 10);
370 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
371 s->
filesize = strtoll(slash+1, NULL, 10);
387 if (!strcmp(p,
"close"))
399 size_t str_size = strlen(tmp) + strlen(p) + 2;
421 inflateEnd(&s->inflate_stream);
422 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
424 s->inflate_stream.msg);
427 if (zlibCompileFlags() & (1 << 17)) {
459 char *next, *cookie, *set_cookies =
av_strdup(s->
cookies), *cset_cookies = set_cookies;
461 if (!set_cookies)
return AVERROR(EINVAL);
464 while ((cookie =
av_strtok(set_cookies,
"\n", &next))) {
465 int domain_offset = 0;
466 char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
469 while ((param =
av_strtok(cookie,
"; ", &next_param))) {
491 if (!cdomain || !cpath || !cvalue) {
493 "Invalid cookie found, no value, path or domain specified\n");
502 domain_offset = strlen(domain) - strlen(cdomain);
503 if (domain_offset < 0)
517 char *tmp = *cookies;
518 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
523 snprintf(*cookies, str_size,
"%s; %s", tmp, cvalue);
543 static inline int has_header(
const char *str,
const char *header)
563 av_dlog(NULL,
"header='%s'\n", line);
577 const char *hoststr,
const char *auth,
578 const char *proxyauth,
int *new_location)
582 char headers[4096] =
"";
583 char *authstr = NULL, *proxyauthstr = NULL;
587 int send_expect_100 = 0;
600 method = post ?
"POST" :
"GET";
619 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
622 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
623 sizeof(headers) - len);
628 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
629 "Range: bytes=%"PRId64
"-\r\n", s->
off);
631 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
632 "Expect: 100-continue\r\n");
636 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
637 sizeof(headers) - len);
639 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
640 sizeof(headers) - len);
645 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
646 "Host: %s\r\n", hoststr);
648 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
651 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
654 char *cookies = NULL;
656 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
657 "Cookie: %s\r\n", cookies);
662 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
663 "Icy-MetaData: %d\r\n", 1);
679 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
681 authstr ? authstr :
"",
682 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
703 if (post && !s->
post_data && !send_expect_100) {
716 return (off == s->
off) ? 0 : -1;
746 #define DECOMPRESS_BUF_SIZE (256 * 1024)
752 if (!s->inflate_buffer) {
753 s->inflate_buffer =
av_malloc(DECOMPRESS_BUF_SIZE);
754 if (!s->inflate_buffer)
758 if (s->inflate_stream.avail_in == 0) {
759 int read =
http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
762 s->inflate_stream.next_in = s->inflate_buffer;
763 s->inflate_stream.avail_in = read;
766 s->inflate_stream.avail_out =
size;
767 s->inflate_stream.next_out =
buf;
769 ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
770 if (ret != Z_OK && ret != Z_STREAM_END)
773 return size - s->inflate_stream.avail_out;
780 int err, new_location;
823 char data[255 * 16 + 1];
827 for (n = 0; n < ch; n++)
830 if ((ret =
av_opt_set(s,
"icy_metadata_packet", data, 0)) < 0)
836 size =
FFMIN(size, remaining);
840 return http_buf_read_compressed(h, buf, size);
850 char crlf[] =
"\r\n";
862 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
875 char footer[] =
"0\r\n\r\n";
881 ret = ret > 0 ? 0 :
ret;
894 inflateEnd(&s->inflate_stream);
913 int64_t old_off = s->
off;
925 memcpy(old_buf, s->
buf_ptr, old_buf_size);
927 if (whence == SEEK_CUR)
929 else if (whence == SEEK_END)
937 memcpy(s->
buffer, old_buf, old_buf_size);
956 #if CONFIG_HTTP_PROTOCOL
967 .priv_data_class = &http_context_class,
971 #if CONFIG_HTTPS_PROTOCOL
982 .priv_data_class = &https_context_class,
987 #if CONFIG_HTTPPROXY_PROTOCOL
999 char hostname[1024], hoststr[1024];
1000 char auth[1024], pathbuf[1024], *path;
1001 char lower_url[100];
1002 int port, ret = 0, attempts = 0;
1012 av_url_split(NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
1013 pathbuf,
sizeof(pathbuf), uri);
1014 ff_url_join(hoststr,
sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1019 ff_url_join(lower_url,
sizeof(lower_url),
"tcp", NULL, hostname, port,
1030 "CONNECT %s HTTP/1.1\r\n"
1032 "Connection: close\r\n"
1037 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
1075 http_proxy_close(h);
1086 .
name =
"httpproxy",
1087 .url_open = http_proxy_open,
1089 .url_write = http_proxy_write,
1090 .url_close = http_proxy_close,
static int http_get_line(HTTPContext *s, char *line, int line_size)
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 http_connect(URLContext *h, const char *path, const char *local_path, const char *hoststr, const char *auth, const char *proxyauth, int *new_location)
void 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 icy_data_read
how much data was read since last ICY metadata packet
#define URL_PROTOCOL_FLAG_NETWORK
char location[MAX_URL_SIZE]
HTTPAuthType
Authentication types, ordered from weakest to strongest.
#define AV_LOG_WARNING
Something somehow does not look correct.
static int http_close(URLContext *h)
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
int is_streamed
true if streamed (no seek possible), default = false
AVIOInterruptCB interrupt_callback
HTTPAuthState proxy_auth_state
char * icy_metadata_headers
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static int http_getc(HTTPContext *s)
HTTP Authentication state structure.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
int end_chunked_post
A flag which indicates if the end of chunked encoding has been sent.
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.
static int http_get_file_handle(URLContext *h)
static int http_buf_read(URLContext *h, uint8_t *buf, int size)
miscellaneous OS support macros and functions.
static int http_read_header(URLContext *h, int *new_location)
static av_cold int end(AVCodecContext *avctx)
static int http_open_cnx(URLContext *h, AVDictionary **options)
#define DEFAULT_USER_AGENT
void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
Initialize the authentication state based on another HTTP URLContext.
HTTP 1.0 Basic auth from RFC 1945 (also in RFC 2617)
const OptionDef options[]
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
char * av_stristr(const char *haystack, const char *needle)
Locate the first case-independent occurrence in the string haystack of the string needle...
static int http_write(URLContext *h, const uint8_t *buf, int size)
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
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.
AVDictionary * chained_options
char * icy_metadata_packet
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int willclose
Set if the server correctly handles Connection: close and will close the connection after feeding us ...
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
size_t char * av_asprintf(const char *fmt,...) av_printf_format(1
Print arguments following specified format into a large enough auto allocated buffer.
int stale
Auth ok, but needs to be resent with a new nonce.
offset must point to a pointer immediately followed by an int for the length
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
static int http_read(URLContext *h, uint8_t *buf, int size)
int seekable
Control seekability, 0 = disable, 1 = enable, -1 = probe.
Describe the class of an AVClass context structure.
char * ff_http_auth_create_response(HTTPAuthState *state, const char *auth, const char *path, const char *method)
static int has_header(const char *str, const char *header)
int ffurl_close(URLContext *h)
static int process_line(URLContext *h, char *line, int line_count, int *new_location)
int ff_http_do_new_request(URLContext *h, const char *uri)
Send a new HTTP request, reusing the old connection.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
char * cookies
holds newline ( ) delimited Set-Cookie header field values (without the "Set-Cookie: " field name) ...
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
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()...
static int get_cookies(HTTPContext *s, char **cookies, const char *path, const char *domain)
Create a string containing cookie values for use as a HTTP cookie header field value for a particular...
static int http_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
void ff_http_auth_handle_header(HTTPAuthState *state, const char *key, const char *value)
int multiple_requests
A flag which indicates if we use persistent connections.
static int http_shutdown(URLContext *h, int flags)
int icy_metaint
after how many bytes of read data a new metadata packet will be found
int end_header
A flag which indicates we have finished to read POST reply.
HTTPAuthType auth_type
The currently chosen auth type.
unbuffered private I/O API
static int64_t http_seek(URLContext *h, int64_t off, int whence)
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
int64_t chunksize
Used if "Transfer-Encoding: chunked" otherwise -1.
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...) av_printf_format(3
Append output to a string, according to a format.
#define HTTP_CLASS(flavor)
No authentication specified.