46 #define BUFFER_SIZE MAX_URL_SIZE
47 #define MAX_REDIRECTS 8
101 z_stream inflate_stream;
116 #define OFFSET(x) offsetof(HTTPContext, x)
117 #define D AV_OPT_FLAG_DECODING_PARAM
118 #define E AV_OPT_FLAG_ENCODING_PARAM
119 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
122 {
"seekable",
"control seekability of connection",
OFFSET(seekable),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1,
D },
123 {
"chunked_post",
"use chunked transfer-encoding for posts",
OFFSET(chunked_post),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
E },
124 {
"headers",
"set custom HTTP headers, can override built in default headers",
OFFSET(headers),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D |
E },
125 {
"content_type",
"set a specific content type for the POST messages",
OFFSET(content_type),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
D |
E },
128 {
"multiple_requests",
"use persistent connections",
OFFSET(multiple_requests),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
D |
E },
131 {
"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 },
137 {
"none",
"No auth method set, autodetect", 0,
AV_OPT_TYPE_CONST, { .i64 =
HTTP_AUTH_NONE }, 0, 0,
D |
E,
"auth_type"},
139 {
"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 },
142 {
"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 },
143 {
"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 },
144 {
"reconnect",
"auto reconnect after disconnect before EOF",
OFFSET(reconnect),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
D },
147 {
"reply_code",
"The http status code to return to a client",
OFFSET(reply_code),
AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599,
E},
152 const char *hoststr,
const char *auth,
153 const char *proxyauth,
int *new_location);
168 const char *path, *proxy_path, *lower_proto =
"tcp", *local_path;
169 char hostname[1024], hoststr[1024], proto[10];
170 char auth[1024], proxyauth[1024] =
"";
173 int port, use_proxy, err, location_changed = 0;
177 hostname,
sizeof(hostname), &port,
181 proxy_path = getenv(
"http_proxy");
185 if (!strcmp(proto,
"https")) {
194 if (path1[0] ==
'\0')
206 hostname,
sizeof(hostname), &port,
NULL, 0, proxy_path);
219 auth, proxyauth, &location_changed);
223 return location_changed;
231 int location_changed, attempts = 0, redirects = 0;
239 if (location_changed < 0)
261 location_changed == 1) {
270 location_changed = 0;
278 if (location_changed < 0)
279 return location_changed;
303 switch (status_code) {
310 if (status_code >= 400 && status_code <= 499)
312 else if (status_code >= 500)
315 return default_averror;
320 int ret,
body = 0, reply_code, message_len;
321 const char *reply_text, *content_type;
324 content_type =
"text/plain";
328 switch (status_code) {
332 reply_text =
"Bad Request";
337 reply_text =
"Forbidden";
342 reply_text =
"Not Found";
347 content_type =
"application/octet-stream";
352 reply_text =
"Internal server error";
359 message_len =
snprintf(message,
sizeof(message),
360 "HTTP/1.1 %03d %s\r\n"
361 "Content-Type: %s\r\n"
362 "Content-Length: %zu\r\n"
368 strlen(reply_text) + 6,
373 message_len =
snprintf(message,
sizeof(message),
374 "HTTP/1.1 %03d %s\r\n"
375 "Content-Type: %s\r\n"
376 "Transfer-Encoding: chunked\r\n"
396 int ret, err, new_location;
434 char hostname[1024], proto[10];
436 const char *lower_proto =
"tcp";
438 av_url_split(proto,
sizeof(proto),
NULL, 0, hostname,
sizeof(hostname), &port,
440 if (!strcmp(proto,
"https"))
442 ff_url_join(lower_url,
sizeof(lower_url), lower_proto,
NULL, hostname, port,
479 if (len < 2 || strcmp(
"\r\n", s->
headers + len - 2)) {
481 "No trailing CRLF found in HTTP header.\n");
511 cc = (*c)->priv_data;
531 }
else if (len == 0) {
553 if (q > line && q[-1] ==
'\r')
559 if ((q - line) < line_size - 1)
570 if (http_code >= 400 && http_code < 600 &&
585 new_loc =
av_strdup(redirected_location);
599 if (!strncmp(p,
"bytes ", 6)) {
601 s->
off = strtoull(p,
NULL, 10);
602 if ((slash = strchr(p,
'/')) && strlen(slash) > 0)
617 inflateEnd(&s->inflate_stream);
618 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
620 s->inflate_stream.msg);
623 if (zlibCompileFlags() & (1 << 17)) {
625 "Your zlib was compiled without gzip support.\n");
630 "Compressed (%s) content, need zlib with gzip support\n", p);
645 int len = 4 + strlen(p) + strlen(tag);
670 if (!(eql = strchr(p,
'=')))
return AVERROR(EINVAL);
686 len += strlen(e->
key) + strlen(e->
value) + 1;
690 if (*cookies)
av_free(*cookies);
692 if (!*cookies)
return AVERROR(ENOMEM);
711 if (line[0] ==
'\0') {
717 if (line_count == 0) {
727 av_log(h,
AV_LOG_ERROR,
"Received and expected HTTP method do not match. (%s expected, %s received)\n",
736 "(%s autodetected %s received)\n", auto_method, method);
779 while (*p !=
'\0' && *p !=
':')
799 !strncmp(p,
"bytes", 5) &&
813 if (!strcmp(p,
"close"))
853 char *next, *cookie, *set_cookies =
av_strdup(s->
cookies), *cset_cookies = set_cookies;
855 if (!set_cookies)
return AVERROR(EINVAL);
861 while ((cookie =
av_strtok(set_cookies,
"\n", &next))) {
862 int domain_offset = 0;
863 char *param, *next_param, *cdomain =
NULL, *cpath =
NULL, *cvalue =
NULL;
870 while ((param =
av_strtok(cookie,
"; ", &next_param))) {
881 int leading_dot = (param[7] ==
'.');
883 cdomain =
av_strdup(¶m[7+leading_dot]);
892 if (!cdomain || !cpath || !cvalue) {
894 "Invalid cookie found, no value, path or domain specified\n");
903 domain_offset = strlen(domain) - strlen(cdomain);
904 if (domain_offset < 0)
918 char *tmp = *cookies;
919 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
924 snprintf(*cookies, str_size,
"%s; %s", tmp, cvalue);
985 const char *hoststr,
const char *auth,
986 const char *proxyauth,
int *new_location)
991 char *authstr =
NULL, *proxyauthstr =
NULL;
992 uint64_t off = s->
off;
995 int send_expect_100 = 0;
1011 method = post ?
"POST" :
"GET";
1014 local_path, method);
1016 local_path, method);
1023 if (auth && *auth &&
1026 send_expect_100 = 1;
1031 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1034 len +=
av_strlcpy(headers + len,
"Accept: */*\r\n",
1035 sizeof(headers) - len);
1040 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1041 "Range: bytes=%"PRIu64
"-", s->
off);
1043 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1046 sizeof(headers) - len);
1049 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1050 "Expect: 100-continue\r\n");
1054 len +=
av_strlcpy(headers + len,
"Connection: keep-alive\r\n",
1055 sizeof(headers) - len);
1057 len +=
av_strlcpy(headers + len,
"Connection: close\r\n",
1058 sizeof(headers) - len);
1062 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1063 "Host: %s\r\n", hoststr);
1065 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1069 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1072 char *cookies =
NULL;
1073 if (!
get_cookies(s, &cookies, path, hoststr) && cookies) {
1074 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1075 "Cookie: %s\r\n", cookies);
1080 len +=
av_strlcatf(headers + len,
sizeof(headers) - len,
1081 "Icy-MetaData: %d\r\n", 1);
1088 "%s %s HTTP/1.1\r\n"
1096 post && s->
chunked_post ?
"Transfer-Encoding: chunked\r\n" :
"",
1098 authstr ? authstr :
"",
1099 proxyauthstr ?
"Proxy-" :
"", proxyauthstr ? proxyauthstr :
"");
1103 if (strlen(headers) + 1 ==
sizeof(headers) ||
1104 ret >=
sizeof(s->
buffer)) {
1128 if (post && !s->
post_data && !send_expect_100) {
1145 err = (off == s->
off) ? 0 : -1;
1170 "Chunked encoding data size: %"PRIu64
"'\n",
1197 "Stream ends prematurely at %"PRIu64
", should be %"PRIu64
"\n",
1214 #define DECOMPRESS_BUF_SIZE (256 * 1024)
1220 if (!s->inflate_buffer) {
1221 s->inflate_buffer =
av_malloc(DECOMPRESS_BUF_SIZE);
1222 if (!s->inflate_buffer)
1226 if (s->inflate_stream.avail_in == 0) {
1227 int read =
http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
1230 s->inflate_stream.next_in = s->inflate_buffer;
1231 s->inflate_stream.avail_in = read;
1234 s->inflate_stream.avail_out =
size;
1235 s->inflate_stream.next_out =
buf;
1237 ret =
inflate(&s->inflate_stream, Z_SYNC_FLUSH);
1238 if (ret != Z_OK && ret != Z_STREAM_END)
1240 ret, s->inflate_stream.msg);
1242 return size - s->inflate_stream.avail_out;
1251 int err, new_location, read_ret, seek_ret;
1264 return http_buf_read_compressed(h, buf, size);
1270 if (seek_ret != s->
off) {
1286 while (pos < size) {
1304 val = strstr(key,
"='");
1307 end = strstr(val,
"';");
1341 char data[255 * 16 + 1];
1348 if ((ret =
av_opt_set(s,
"icy_metadata_packet", data, 0)) < 0)
1356 return FFMIN(size, remaining);
1380 char crlf[] =
"\r\n";
1392 snprintf(temp,
sizeof(temp),
"%x\r\n", size);
1405 char footer[] =
"0\r\n\r\n";
1412 ret = ret > 0 ? 0 : ret;
1425 inflateEnd(&s->inflate_stream);
1443 uint64_t old_off = s->
off;
1445 int old_buf_size, ret;
1450 else if (!force_reconnect &&
1451 ((whence == SEEK_CUR && off == 0) ||
1452 (whence == SEEK_SET && off == s->
off)))
1457 if (whence == SEEK_CUR)
1459 else if (whence == SEEK_END)
1461 else if (whence != SEEK_SET)
1469 memcpy(old_buf, s->
buf_ptr, old_buf_size);
1475 memcpy(s->
buffer, old_buf, old_buf_size);
1498 #define HTTP_CLASS(flavor) \
1499 static const AVClass flavor ## _context_class = { \
1500 .class_name = # flavor, \
1501 .item_name = av_default_item_name, \
1502 .option = options, \
1503 .version = LIBAVUTIL_VERSION_INT, \
1506 #if CONFIG_HTTP_PROTOCOL
1521 .priv_data_class = &http_context_class,
1526 #if CONFIG_HTTPS_PROTOCOL
1539 .priv_data_class = &https_context_class,
1544 #if CONFIG_HTTPPROXY_PROTOCOL
1556 char hostname[1024], hoststr[1024];
1557 char auth[1024], pathbuf[1024], *path;
1558 char lower_url[100];
1559 int port, ret = 0, attempts = 0;
1569 av_url_split(
NULL, 0, auth,
sizeof(auth), hostname,
sizeof(hostname), &port,
1570 pathbuf,
sizeof(pathbuf), uri);
1576 ff_url_join(lower_url,
sizeof(lower_url),
"tcp",
NULL, hostname, port,
1587 "CONNECT %s HTTP/1.1\r\n"
1589 "Connection: close\r\n"
1594 authstr ?
"Proxy-" :
"", authstr ? authstr :
"");
1632 http_proxy_close(h);
1643 .
name =
"httpproxy",
1644 .url_open = http_proxy_open,
1646 .url_write = http_proxy_write,
1647 .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 void parse_content_range(URLContext *h, 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, int *new_location)
const char const char void * val
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.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define URL_PROTOCOL_FLAG_NETWORK
ptrdiff_t const GLvoid * data
#define AV_OPT_FLAG_EXPORT
The option is inteded for exporting values to the caller.
HTTPAuthType
Authentication types, ordered from weakest to strongest.
#define AV_LOG_WARNING
Something somehow does not look correct.
static int http_close(URLContext *h)
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle...
int is_streamed
true if streamed (no seek possible), default = false
AVIOInterruptCB interrupt_callback
HTTPAuthState proxy_auth_state
#define AVIO_FLAG_READ
read-only
char * icy_metadata_headers
#define AVIO_FLAG_WRITE
write-only
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
#define AVERROR_HTTP_NOT_FOUND
static int http_listen(URLContext *h, const char *uri, int flags, AVDictionary **options)
static int http_getc(HTTPContext *s)
HTTP Authentication state structure.
int auth_type
The currently chosen auth type.
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function...
static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
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)
int ff_http_averror(int status_code, int default_averror)
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
miscellaneous OS support macros and functions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
static int http_read_header(URLContext *h, int *new_location)
static av_cold int end(AVCodecContext *avctx)
#define HTTP_HEADERS_SIZE
static int http_open_cnx(URLContext *h, AVDictionary **options)
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord)
#define DEFAULT_USER_AGENT
char * av_strndup(const char *s, size_t len)
Duplicate a substring of the string s.
#define AVERROR_EOF
End of file.
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)
static const uint8_t header[24]
static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
static void update_metadata(HTTPContext *s, char *data)
static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
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...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int http_write(URLContext *h, const uint8_t *buf, int size)
static const AVOption options[]
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
static int http_handshake(URLContext *c)
simple assert() macros that are a bit more flexible than ISO C assert().
static void body(uint32_t ABCD[4], uint32_t *src, int nblocks)
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
#define AVERROR_HTTP_SERVER_ERROR
static int store_icy(URLContext *h, int size)
#define AVERROR_HTTP_UNAUTHORIZED
char * icy_metadata_packet
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
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.
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
AVDictionary * cookie_dict
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)
#define AV_LOG_INFO
Standard information.
char * av_strdup(const char *s)
Duplicate the string s.
HandshakeState handshake_step
int seekable
Control seekability, 0 = disable, 1 = enable, -1 = probe.
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Describe the class of an AVClass context structure.
#define AVERROR_HTTP_OTHER_4XX
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
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)
#define AVERROR_HTTP_FORBIDDEN
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.
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_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) ...
static int parse_content_encoding(URLContext *h, const char *p)
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. ...
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...
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...
char * filename
specified URL
static int http_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
void ff_http_auth_handle_header(HTTPAuthState *state, const char *key, const char *value)
static int cookie_string(AVDictionary *dict, char **cookies)
static int parse_location(HTTPContext *s, const char *p)
static int http_shutdown(URLContext *h, int flags)
static int http_write_reply(URLContext *h, int status_code)
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
#define AVERROR_HTTP_BAD_REQUEST
static int parse_icy(HTTPContext *s, const char *tag, const char *p)
static int http_read_stream(URLContext *h, uint8_t *buf, int size)
static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
unbuffered private I/O API
static void handle_http_errors(URLContext *h, int error)
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...
static int http_accept(URLContext *s, URLContext **c)
#define HTTP_CLASS(flavor)
No authentication specified.
static int check_http_code(URLContext *h, int http_code, const char *end)