]> git.sesse.net Git - ffmpeg/blob - libavformat/http.c
avformat/mxfdec; parse random index pack to get last partition offset and use it...
[ffmpeg] / libavformat / http.c
1 /*
2  * HTTP protocol for ffmpeg client
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/avstring.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "network.h"
26 #include "http.h"
27 #include "os_support.h"
28 #include "httpauth.h"
29 #include "url.h"
30 #include "libavutil/opt.h"
31
32 #if CONFIG_ZLIB
33 #include <zlib.h>
34 #endif
35
36 /* XXX: POST protocol is not completely implemented because ffmpeg uses
37    only a subset of it. */
38
39 /* The IO buffer size is unrelated to the max URL size in itself, but needs
40  * to be large enough to fit the full request headers (including long
41  * path names).
42  */
43 #define BUFFER_SIZE MAX_URL_SIZE
44 #define MAX_REDIRECTS 8
45
46 typedef struct {
47     const AVClass *class;
48     URLContext *hd;
49     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
50     int line_count;
51     int http_code;
52     int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
53     char *content_type;
54     char *user_agent;
55     int64_t off, filesize;
56     int icy_data_read;      ///< how much data was read since last ICY metadata packet
57     int icy_metaint;        ///< after how many bytes of read data a new metadata packet will be found
58     char location[MAX_URL_SIZE];
59     HTTPAuthState auth_state;
60     HTTPAuthState proxy_auth_state;
61     char *headers;
62     int willclose;          /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
63     int seekable;           /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
64     int chunked_post;
65     int end_chunked_post;   /**< A flag which indicates if the end of chunked encoding has been sent. */
66     int end_header;         /**< A flag which indicates we have finished to read POST reply. */
67     int multiple_requests;  /**< A flag which indicates if we use persistent connections. */
68     uint8_t *post_data;
69     int post_datalen;
70     int is_akamai;
71     char *mime_type;
72     char *cookies;          ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
73     int icy;
74     char *icy_metadata_headers;
75     char *icy_metadata_packet;
76 #if CONFIG_ZLIB
77     int compressed;
78     z_stream inflate_stream;
79     uint8_t *inflate_buffer;
80 #endif
81     AVDictionary *chained_options;
82     int send_expect_100;
83 } HTTPContext;
84
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)
89 static const AVOption options[] = {
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 },
93 {"content_type", "force a content type", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
94 {"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = DEFAULT_USER_AGENT}, 0, 0, D },
95 {"multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
96 {"post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D|E },
97 {"mime_type", "set MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
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, 0 },
99 {"icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 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 },
102 {"auth_type", "HTTP authentication type", OFFSET(auth_state.auth_type), AV_OPT_TYPE_INT, {.i64 = HTTP_AUTH_NONE}, HTTP_AUTH_NONE, HTTP_AUTH_BASIC, D|E, "auth_type" },
103 {"none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, {.i64 = HTTP_AUTH_NONE}, 0, 0, D|E, "auth_type" },
104 {"basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, {.i64 = HTTP_AUTH_BASIC}, 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" },
106 {NULL}
107 };
108 #define HTTP_CLASS(flavor)\
109 static const AVClass flavor ## _context_class = {\
110     .class_name     = #flavor,\
111     .item_name      = av_default_item_name,\
112     .option         = options,\
113     .version        = LIBAVUTIL_VERSION_INT,\
114 }
115
116 HTTP_CLASS(http);
117 HTTP_CLASS(https);
118
119 static int http_connect(URLContext *h, const char *path, const char *local_path,
120                         const char *hoststr, const char *auth,
121                         const char *proxyauth, int *new_location);
122
123 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
124 {
125     memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
126            &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
127     memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state,
128            &((HTTPContext*)src->priv_data)->proxy_auth_state,
129            sizeof(HTTPAuthState));
130 }
131
132 /* return non zero if error */
133 static int http_open_cnx(URLContext *h, AVDictionary **options)
134 {
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] = "";
138     char path1[MAX_URL_SIZE];
139     char buf[1024], urlbuf[MAX_URL_SIZE];
140     int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
141     HTTPAuthType cur_auth_type, cur_proxy_auth_type;
142     HTTPContext *s = h->priv_data;
143
144     /* fill the dest addr */
145  redo:
146     /* needed in any case to build the host string */
147     av_url_split(proto, sizeof(proto), auth, sizeof(auth),
148                  hostname, sizeof(hostname), &port,
149                  path1, sizeof(path1), s->location);
150     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
151
152     proxy_path = getenv("http_proxy");
153     use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
154                 proxy_path != NULL && av_strstart(proxy_path, "http://", NULL);
155
156     if (!strcmp(proto, "https")) {
157         lower_proto = "tls";
158         use_proxy = 0;
159         if (port < 0)
160             port = 443;
161     }
162     if (port < 0)
163         port = 80;
164
165     if (path1[0] == '\0')
166         path = "/";
167     else
168         path = path1;
169     local_path = path;
170     if (use_proxy) {
171         /* Reassemble the request URL without auth string - we don't
172          * want to leak the auth to the proxy. */
173         ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
174                     path1);
175         path = urlbuf;
176         av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
177                      hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
178     }
179
180     ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
181
182     if (!s->hd) {
183         err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE,
184                          &h->interrupt_callback, options);
185         if (err < 0)
186             goto fail;
187     }
188
189     cur_auth_type = s->auth_state.auth_type;
190     cur_proxy_auth_type = s->auth_state.auth_type;
191     if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
192         goto fail;
193     attempts++;
194     if (s->http_code == 401) {
195         if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
196             s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
197             ffurl_closep(&s->hd);
198             goto redo;
199         } else
200             goto fail;
201     }
202     if (s->http_code == 407) {
203         if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
204             s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
205             ffurl_closep(&s->hd);
206             goto redo;
207         } else
208             goto fail;
209     }
210     if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
211         && location_changed == 1) {
212         /* url moved, get next */
213         ffurl_closep(&s->hd);
214         if (redirects++ >= MAX_REDIRECTS)
215             return AVERROR(EIO);
216         /* Restart the authentication process with the new target, which
217          * might use a different auth mechanism. */
218         memset(&s->auth_state, 0, sizeof(s->auth_state));
219         attempts = 0;
220         location_changed = 0;
221         goto redo;
222     }
223     return 0;
224  fail:
225     if (s->hd)
226         ffurl_closep(&s->hd);
227     return AVERROR(EIO);
228 }
229
230 int ff_http_do_new_request(URLContext *h, const char *uri)
231 {
232     HTTPContext *s = h->priv_data;
233     AVDictionary *options = NULL;
234     int ret;
235
236     s->off = 0;
237     s->icy_data_read = 0;
238     av_strlcpy(s->location, uri, sizeof(s->location));
239
240     av_dict_copy(&options, s->chained_options, 0);
241     ret = http_open_cnx(h, &options);
242     av_dict_free(&options);
243     return ret;
244 }
245
246 static int http_open(URLContext *h, const char *uri, int flags,
247                      AVDictionary **options)
248 {
249     HTTPContext *s = h->priv_data;
250     int ret;
251
252     if( s->seekable == 1 )
253         h->is_streamed = 0;
254     else
255         h->is_streamed = 1;
256
257     s->filesize = -1;
258     av_strlcpy(s->location, uri, sizeof(s->location));
259     if (options)
260         av_dict_copy(&s->chained_options, *options, 0);
261
262     if (s->headers) {
263         int len = strlen(s->headers);
264         if (len < 2 || strcmp("\r\n", s->headers + len - 2))
265             av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
266     }
267
268     ret = http_open_cnx(h, options);
269     if (ret < 0)
270         av_dict_free(&s->chained_options);
271     return ret;
272 }
273 static int http_getc(HTTPContext *s)
274 {
275     int len;
276     if (s->buf_ptr >= s->buf_end) {
277         len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
278         if (len < 0) {
279             return len;
280         } else if (len == 0) {
281             return -1;
282         } else {
283             s->buf_ptr = s->buffer;
284             s->buf_end = s->buffer + len;
285         }
286     }
287     return *s->buf_ptr++;
288 }
289
290 static int http_get_line(HTTPContext *s, char *line, int line_size)
291 {
292     int ch;
293     char *q;
294
295     q = line;
296     for(;;) {
297         ch = http_getc(s);
298         if (ch < 0)
299             return ch;
300         if (ch == '\n') {
301             /* process line */
302             if (q > line && q[-1] == '\r')
303                 q--;
304             *q = '\0';
305
306             return 0;
307         } else {
308             if ((q - line) < line_size - 1)
309                 *q++ = ch;
310         }
311     }
312 }
313
314 static int process_line(URLContext *h, char *line, int line_count,
315                         int *new_location)
316 {
317     HTTPContext *s = h->priv_data;
318     char *tag, *p, *end;
319     char redirected_location[MAX_URL_SIZE];
320
321     /* end of header */
322     if (line[0] == '\0') {
323         s->end_header = 1;
324         return 0;
325     }
326
327     p = line;
328     if (line_count == 0) {
329         while (!av_isspace(*p) && *p != '\0')
330             p++;
331         while (av_isspace(*p))
332             p++;
333         s->http_code = strtol(p, &end, 10);
334
335         av_dlog(NULL, "http_code=%d\n", s->http_code);
336
337         /* error codes are 4xx and 5xx, but regard 401 as a success, so we
338          * don't abort until all headers have been parsed. */
339         if (s->http_code >= 400 && s->http_code < 600 && (s->http_code != 401
340             || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
341             (s->http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
342             end += strspn(end, SPACE_CHARS);
343             av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
344                    s->http_code, end);
345             return -1;
346         }
347     } else {
348         while (*p != '\0' && *p != ':')
349             p++;
350         if (*p != ':')
351             return 1;
352
353         *p = '\0';
354         tag = line;
355         p++;
356         while (av_isspace(*p))
357             p++;
358         if (!av_strcasecmp(tag, "Location")) {
359             ff_make_absolute_url(redirected_location, sizeof(redirected_location), s->location, p);
360             av_strlcpy(s->location, redirected_location, sizeof(s->location));
361             *new_location = 1;
362         } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
363             s->filesize = strtoll(p, NULL, 10);
364         } else if (!av_strcasecmp (tag, "Content-Range")) {
365             /* "bytes $from-$to/$document_size" */
366             const char *slash;
367             if (!strncmp (p, "bytes ", 6)) {
368                 p += 6;
369                 s->off = strtoll(p, NULL, 10);
370                 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
371                     s->filesize = strtoll(slash+1, NULL, 10);
372             }
373             if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
374                 h->is_streamed = 0; /* we _can_ in fact seek */
375         } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5) && s->seekable == -1) {
376             h->is_streamed = 0;
377         } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
378             s->filesize = -1;
379             s->chunksize = 0;
380         } else if (!av_strcasecmp (tag, "WWW-Authenticate")) {
381             ff_http_auth_handle_header(&s->auth_state, tag, p);
382         } else if (!av_strcasecmp (tag, "Authentication-Info")) {
383             ff_http_auth_handle_header(&s->auth_state, tag, p);
384         } else if (!av_strcasecmp (tag, "Proxy-Authenticate")) {
385             ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
386         } else if (!av_strcasecmp (tag, "Connection")) {
387             if (!strcmp(p, "close"))
388                 s->willclose = 1;
389         } else if (!av_strcasecmp (tag, "Server") && !av_strcasecmp (p, "AkamaiGHost")) {
390             s->is_akamai = 1;
391         } else if (!av_strcasecmp (tag, "Content-Type")) {
392             av_free(s->mime_type); s->mime_type = av_strdup(p);
393         } else if (!av_strcasecmp (tag, "Set-Cookie")) {
394             if (!s->cookies) {
395                 if (!(s->cookies = av_strdup(p)))
396                     return AVERROR(ENOMEM);
397             } else {
398                 char *tmp = s->cookies;
399                 size_t str_size = strlen(tmp) + strlen(p) + 2;
400                 if (!(s->cookies = av_malloc(str_size))) {
401                     s->cookies = tmp;
402                     return AVERROR(ENOMEM);
403                 }
404                 snprintf(s->cookies, str_size, "%s\n%s", tmp, p);
405                 av_free(tmp);
406             }
407         } else if (!av_strcasecmp (tag, "Icy-MetaInt")) {
408             s->icy_metaint = strtoll(p, NULL, 10);
409         } else if (!av_strncasecmp(tag, "Icy-", 4)) {
410             // Concat all Icy- header lines
411             char *buf = av_asprintf("%s%s: %s\n",
412                 s->icy_metadata_headers ? s->icy_metadata_headers : "", tag, p);
413             if (!buf)
414                 return AVERROR(ENOMEM);
415             av_freep(&s->icy_metadata_headers);
416             s->icy_metadata_headers = buf;
417         } else if (!av_strcasecmp (tag, "Content-Encoding")) {
418             if (!av_strncasecmp(p, "gzip", 4) || !av_strncasecmp(p, "deflate", 7)) {
419 #if CONFIG_ZLIB
420                 s->compressed = 1;
421                 inflateEnd(&s->inflate_stream);
422                 if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
423                     av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
424                            s->inflate_stream.msg);
425                     return AVERROR(ENOSYS);
426                 }
427                 if (zlibCompileFlags() & (1 << 17)) {
428                     av_log(h, AV_LOG_WARNING, "Your zlib was compiled without gzip support.\n");
429                     return AVERROR(ENOSYS);
430                 }
431 #else
432                 av_log(h, AV_LOG_WARNING, "Compressed (%s) content, need zlib with gzip support\n", p);
433                 return AVERROR(ENOSYS);
434 #endif
435             } else if (!av_strncasecmp(p, "identity", 8)) {
436                 // The normal, no-encoding case (although servers shouldn't include
437                 // the header at all if this is the case).
438             } else {
439                 av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
440                 return AVERROR(ENOSYS);
441             }
442         }
443     }
444     return 1;
445 }
446
447 /**
448  * Create a string containing cookie values for use as a HTTP cookie header
449  * field value for a particular path and domain from the cookie values stored in
450  * the HTTP protocol context. The cookie string is stored in *cookies.
451  *
452  * @return a negative value if an error condition occurred, 0 otherwise
453  */
454 static int get_cookies(HTTPContext *s, char **cookies, const char *path,
455                        const char *domain)
456 {
457     // cookie strings will look like Set-Cookie header field values.  Multiple
458     // Set-Cookie fields will result in multiple values delimited by a newline
459     int ret = 0;
460     char *next, *cookie, *set_cookies = av_strdup(s->cookies), *cset_cookies = set_cookies;
461
462     if (!set_cookies) return AVERROR(EINVAL);
463
464     *cookies = NULL;
465     while ((cookie = av_strtok(set_cookies, "\n", &next))) {
466         int domain_offset = 0;
467         char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
468         set_cookies = NULL;
469
470         while ((param = av_strtok(cookie, "; ", &next_param))) {
471             cookie = NULL;
472             if        (!av_strncasecmp("path=",   param, 5)) {
473                 av_free(cpath);
474                 cpath = av_strdup(&param[5]);
475             } else if (!av_strncasecmp("domain=", param, 7)) {
476                 av_free(cdomain);
477                 cdomain = av_strdup(&param[7]);
478             } else if (!av_strncasecmp("secure",  param, 6) ||
479                        !av_strncasecmp("comment", param, 7) ||
480                        !av_strncasecmp("max-age", param, 7) ||
481                        !av_strncasecmp("version", param, 7)) {
482                 // ignore Comment, Max-Age, Secure and Version
483             } else {
484                 av_free(cvalue);
485                 cvalue = av_strdup(param);
486             }
487         }
488         if (!cdomain)
489             cdomain = av_strdup(domain);
490
491         // ensure all of the necessary values are valid
492         if (!cdomain || !cpath || !cvalue) {
493             av_log(s, AV_LOG_WARNING,
494                    "Invalid cookie found, no value, path or domain specified\n");
495             goto done_cookie;
496         }
497
498         // check if the request path matches the cookie path
499         if (av_strncasecmp(path, cpath, strlen(cpath)))
500             goto done_cookie;
501
502         // the domain should be at least the size of our cookie domain
503         domain_offset = strlen(domain) - strlen(cdomain);
504         if (domain_offset < 0)
505             goto done_cookie;
506
507         // match the cookie domain
508         if (av_strcasecmp(&domain[domain_offset], cdomain))
509             goto done_cookie;
510
511         // cookie parameters match, so copy the value
512         if (!*cookies) {
513             if (!(*cookies = av_strdup(cvalue))) {
514                 ret = AVERROR(ENOMEM);
515                 goto done_cookie;
516             }
517         } else {
518             char *tmp = *cookies;
519             size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
520             if (!(*cookies = av_malloc(str_size))) {
521                 ret = AVERROR(ENOMEM);
522                 goto done_cookie;
523             }
524             snprintf(*cookies, str_size, "%s; %s", tmp, cvalue);
525             av_free(tmp);
526         }
527
528         done_cookie:
529         av_free(cdomain);
530         av_free(cpath);
531         av_free(cvalue);
532         if (ret < 0) {
533             if (*cookies) av_freep(cookies);
534             av_free(cset_cookies);
535             return ret;
536         }
537     }
538
539     av_free(cset_cookies);
540
541     return 0;
542 }
543
544 static inline int has_header(const char *str, const char *header)
545 {
546     /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
547     if (!str)
548         return 0;
549     return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
550 }
551
552 static int http_read_header(URLContext *h, int *new_location)
553 {
554     HTTPContext *s = h->priv_data;
555     char line[MAX_URL_SIZE];
556     int err = 0;
557
558     s->chunksize = -1;
559
560     for (;;) {
561         if ((err = http_get_line(s, line, sizeof(line))) < 0)
562             return err;
563
564         av_dlog(NULL, "header='%s'\n", line);
565
566         err = process_line(h, line, s->line_count, new_location);
567         if (err < 0)
568             return err;
569         if (err == 0)
570             break;
571         s->line_count++;
572     }
573
574     return err;
575 }
576
577 static int http_connect(URLContext *h, const char *path, const char *local_path,
578                         const char *hoststr, const char *auth,
579                         const char *proxyauth, int *new_location)
580 {
581     HTTPContext *s = h->priv_data;
582     int post, err;
583     char headers[4096] = "";
584     char *authstr = NULL, *proxyauthstr = NULL;
585     int64_t off = s->off;
586     int len = 0;
587     const char *method;
588     int send_expect_100 = 0;
589
590
591     /* send http header */
592     post = h->flags & AVIO_FLAG_WRITE;
593
594     if (s->post_data) {
595         /* force POST method and disable chunked encoding when
596          * custom HTTP post data is set */
597         post = 1;
598         s->chunked_post = 0;
599     }
600
601     method = post ? "POST" : "GET";
602     authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
603                                            method);
604     proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
605                                                 local_path, method);
606     if (post && !s->post_data) {
607         send_expect_100 = s->send_expect_100;
608         /* The user has supplied authentication but we don't know the auth type,
609          * send Expect: 100-continue to get the 401 response including the
610          * WWW-Authenticate header, or an 100 continue if no auth actually
611          * is needed. */
612         if (auth && *auth &&
613             s->auth_state.auth_type == HTTP_AUTH_NONE &&
614             s->http_code != 401)
615             send_expect_100 = 1;
616     }
617
618     /* set default headers if needed */
619     if (!has_header(s->headers, "\r\nUser-Agent: "))
620         len += av_strlcatf(headers + len, sizeof(headers) - len,
621                            "User-Agent: %s\r\n", s->user_agent);
622     if (!has_header(s->headers, "\r\nAccept: "))
623         len += av_strlcpy(headers + len, "Accept: */*\r\n",
624                           sizeof(headers) - len);
625     // Note: we send this on purpose even when s->off is 0 when we're probing,
626     // since it allows us to detect more reliably if a (non-conforming)
627     // server supports seeking by analysing the reply headers.
628     if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->seekable == -1))
629         len += av_strlcatf(headers + len, sizeof(headers) - len,
630                            "Range: bytes=%"PRId64"-\r\n", s->off);
631     if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
632         len += av_strlcatf(headers + len, sizeof(headers) - len,
633                            "Expect: 100-continue\r\n");
634
635     if (!has_header(s->headers, "\r\nConnection: ")) {
636         if (s->multiple_requests) {
637             len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
638                               sizeof(headers) - len);
639         } else {
640             len += av_strlcpy(headers + len, "Connection: close\r\n",
641                               sizeof(headers) - len);
642         }
643     }
644
645     if (!has_header(s->headers, "\r\nHost: "))
646         len += av_strlcatf(headers + len, sizeof(headers) - len,
647                            "Host: %s\r\n", hoststr);
648     if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
649         len += av_strlcatf(headers + len, sizeof(headers) - len,
650                            "Content-Length: %d\r\n", s->post_datalen);
651     if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
652         len += av_strlcatf(headers + len, sizeof(headers) - len,
653                            "Content-Type: %s\r\n", s->content_type);
654     if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
655         char *cookies = NULL;
656         if (!get_cookies(s, &cookies, path, hoststr)) {
657             len += av_strlcatf(headers + len, sizeof(headers) - len,
658                                "Cookie: %s\r\n", cookies);
659             av_free(cookies);
660         }
661     }
662     if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy) {
663         len += av_strlcatf(headers + len, sizeof(headers) - len,
664                            "Icy-MetaData: %d\r\n", 1);
665     }
666
667     /* now add in custom headers */
668     if (s->headers)
669         av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
670
671     snprintf(s->buffer, sizeof(s->buffer),
672              "%s %s HTTP/1.1\r\n"
673              "%s"
674              "%s"
675              "%s"
676              "%s%s"
677              "\r\n",
678              method,
679              path,
680              post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
681              headers,
682              authstr ? authstr : "",
683              proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
684
685     av_freep(&authstr);
686     av_freep(&proxyauthstr);
687     if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
688         return err;
689
690     if (s->post_data)
691         if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
692             return err;
693
694     /* init input buffer */
695     s->buf_ptr = s->buffer;
696     s->buf_end = s->buffer;
697     s->line_count = 0;
698     s->off = 0;
699     s->icy_data_read = 0;
700     s->filesize = -1;
701     s->willclose = 0;
702     s->end_chunked_post = 0;
703     s->end_header = 0;
704     if (post && !s->post_data && !send_expect_100) {
705         /* Pretend that it did work. We didn't read any header yet, since
706          * we've still to send the POST data, but the code calling this
707          * function will check http_code after we return. */
708         s->http_code = 200;
709         return 0;
710     }
711
712     /* wait for header */
713     err = http_read_header(h, new_location);
714     if (err < 0)
715         return err;
716
717     return (off == s->off) ? 0 : -1;
718 }
719
720
721 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
722 {
723     HTTPContext *s = h->priv_data;
724     int len;
725     /* read bytes from input buffer first */
726     len = s->buf_end - s->buf_ptr;
727     if (len > 0) {
728         if (len > size)
729             len = size;
730         memcpy(buf, s->buf_ptr, len);
731         s->buf_ptr += len;
732     } else {
733         if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
734             return AVERROR_EOF;
735         len = ffurl_read(s->hd, buf, size);
736     }
737     if (len > 0) {
738         s->off += len;
739         s->icy_data_read += len;
740         if (s->chunksize > 0)
741             s->chunksize -= len;
742     }
743     return len;
744 }
745
746 #if CONFIG_ZLIB
747 #define DECOMPRESS_BUF_SIZE (256 * 1024)
748 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
749 {
750     HTTPContext *s = h->priv_data;
751     int ret;
752
753     if (!s->inflate_buffer) {
754         s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
755         if (!s->inflate_buffer)
756             return AVERROR(ENOMEM);
757     }
758
759     if (s->inflate_stream.avail_in == 0) {
760         int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
761         if (read <= 0)
762             return read;
763         s->inflate_stream.next_in  = s->inflate_buffer;
764         s->inflate_stream.avail_in = read;
765     }
766
767     s->inflate_stream.avail_out = size;
768     s->inflate_stream.next_out  = buf;
769
770     ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
771     if (ret != Z_OK && ret != Z_STREAM_END)
772         av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n", ret, s->inflate_stream.msg);
773
774     return size - s->inflate_stream.avail_out;
775 }
776 #endif
777
778 static int http_read(URLContext *h, uint8_t *buf, int size)
779 {
780     HTTPContext *s = h->priv_data;
781     int err, new_location;
782
783     if (!s->hd)
784         return AVERROR_EOF;
785
786     if (s->end_chunked_post && !s->end_header) {
787         err = http_read_header(h, &new_location);
788         if (err < 0)
789             return err;
790     }
791
792     if (s->chunksize >= 0) {
793         if (!s->chunksize) {
794             char line[32];
795
796             for(;;) {
797                 do {
798                     if ((err = http_get_line(s, line, sizeof(line))) < 0)
799                         return err;
800                 } while (!*line);    /* skip CR LF from last chunk */
801
802                 s->chunksize = strtoll(line, NULL, 16);
803
804                 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
805
806                 if (!s->chunksize)
807                     return 0;
808                 break;
809             }
810         }
811         size = FFMIN(size, s->chunksize);
812     }
813     if (s->icy_metaint > 0) {
814         int remaining = s->icy_metaint - s->icy_data_read; /* until next metadata packet */
815         if (!remaining) {
816             // The metadata packet is variable sized. It has a 1 byte header
817             // which sets the length of the packet (divided by 16). If it's 0,
818             // the metadata doesn't change. After the packet, icy_metaint bytes
819             // of normal data follow.
820             int ch = http_getc(s);
821             if (ch < 0)
822                 return ch;
823             if (ch > 0) {
824                 char data[255 * 16 + 1];
825                 int n;
826                 int ret;
827                 ch *= 16;
828                 for (n = 0; n < ch; n++)
829                     data[n] = http_getc(s);
830                 data[ch + 1] = 0;
831                 if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
832                     return ret;
833             }
834             s->icy_data_read = 0;
835             remaining = s->icy_metaint;
836         }
837         size = FFMIN(size, remaining);
838     }
839 #if CONFIG_ZLIB
840     if (s->compressed)
841         return http_buf_read_compressed(h, buf, size);
842 #endif
843     return http_buf_read(h, buf, size);
844 }
845
846 /* used only when posting data */
847 static int http_write(URLContext *h, const uint8_t *buf, int size)
848 {
849     char temp[11] = "";  /* 32-bit hex + CRLF + nul */
850     int ret;
851     char crlf[] = "\r\n";
852     HTTPContext *s = h->priv_data;
853
854     if (!s->chunked_post) {
855         /* non-chunked data is sent without any special encoding */
856         return ffurl_write(s->hd, buf, size);
857     }
858
859     /* silently ignore zero-size data since chunk encoding that would
860      * signal EOF */
861     if (size > 0) {
862         /* upload data using chunked encoding */
863         snprintf(temp, sizeof(temp), "%x\r\n", size);
864
865         if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
866             (ret = ffurl_write(s->hd, buf, size)) < 0 ||
867             (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
868             return ret;
869     }
870     return size;
871 }
872
873 static int http_shutdown(URLContext *h, int flags)
874 {
875     int ret = 0;
876     char footer[] = "0\r\n\r\n";
877     HTTPContext *s = h->priv_data;
878
879     /* signal end of chunked encoding if used */
880     if ((flags & AVIO_FLAG_WRITE) && s->chunked_post) {
881         ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
882         ret = ret > 0 ? 0 : ret;
883         s->end_chunked_post = 1;
884     }
885
886     return ret;
887 }
888
889 static int http_close(URLContext *h)
890 {
891     int ret = 0;
892     HTTPContext *s = h->priv_data;
893
894 #if CONFIG_ZLIB
895     inflateEnd(&s->inflate_stream);
896     av_freep(&s->inflate_buffer);
897 #endif
898
899     if (!s->end_chunked_post) {
900         /* Close the write direction by sending the end of chunked encoding. */
901         ret = http_shutdown(h, h->flags);
902     }
903
904     if (s->hd)
905         ffurl_closep(&s->hd);
906     av_dict_free(&s->chained_options);
907     return ret;
908 }
909
910 static int64_t http_seek(URLContext *h, int64_t off, int whence)
911 {
912     HTTPContext *s = h->priv_data;
913     URLContext *old_hd = s->hd;
914     int64_t old_off = s->off;
915     uint8_t old_buf[BUFFER_SIZE];
916     int old_buf_size;
917     AVDictionary *options = NULL;
918
919     if (whence == AVSEEK_SIZE)
920         return s->filesize;
921     else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
922         return -1;
923
924     /* we save the old context in case the seek fails */
925     old_buf_size = s->buf_end - s->buf_ptr;
926     memcpy(old_buf, s->buf_ptr, old_buf_size);
927     s->hd = NULL;
928     if (whence == SEEK_CUR)
929         off += s->off;
930     else if (whence == SEEK_END)
931         off += s->filesize;
932     s->off = off;
933
934     /* if it fails, continue on old connection */
935     av_dict_copy(&options, s->chained_options, 0);
936     if (http_open_cnx(h, &options) < 0) {
937         av_dict_free(&options);
938         memcpy(s->buffer, old_buf, old_buf_size);
939         s->buf_ptr = s->buffer;
940         s->buf_end = s->buffer + old_buf_size;
941         s->hd = old_hd;
942         s->off = old_off;
943         return -1;
944     }
945     av_dict_free(&options);
946     ffurl_close(old_hd);
947     return off;
948 }
949
950 static int
951 http_get_file_handle(URLContext *h)
952 {
953     HTTPContext *s = h->priv_data;
954     return ffurl_get_file_handle(s->hd);
955 }
956
957 #if CONFIG_HTTP_PROTOCOL
958 URLProtocol ff_http_protocol = {
959     .name                = "http",
960     .url_open2           = http_open,
961     .url_read            = http_read,
962     .url_write           = http_write,
963     .url_seek            = http_seek,
964     .url_close           = http_close,
965     .url_get_file_handle = http_get_file_handle,
966     .url_shutdown        = http_shutdown,
967     .priv_data_size      = sizeof(HTTPContext),
968     .priv_data_class     = &http_context_class,
969     .flags               = URL_PROTOCOL_FLAG_NETWORK,
970 };
971 #endif
972 #if CONFIG_HTTPS_PROTOCOL
973 URLProtocol ff_https_protocol = {
974     .name                = "https",
975     .url_open2           = http_open,
976     .url_read            = http_read,
977     .url_write           = http_write,
978     .url_seek            = http_seek,
979     .url_close           = http_close,
980     .url_get_file_handle = http_get_file_handle,
981     .url_shutdown        = http_shutdown,
982     .priv_data_size      = sizeof(HTTPContext),
983     .priv_data_class     = &https_context_class,
984     .flags               = URL_PROTOCOL_FLAG_NETWORK,
985 };
986 #endif
987
988 #if CONFIG_HTTPPROXY_PROTOCOL
989 static int http_proxy_close(URLContext *h)
990 {
991     HTTPContext *s = h->priv_data;
992     if (s->hd)
993         ffurl_closep(&s->hd);
994     return 0;
995 }
996
997 static int http_proxy_open(URLContext *h, const char *uri, int flags)
998 {
999     HTTPContext *s = h->priv_data;
1000     char hostname[1024], hoststr[1024];
1001     char auth[1024], pathbuf[1024], *path;
1002     char lower_url[100];
1003     int port, ret = 0, attempts = 0;
1004     HTTPAuthType cur_auth_type;
1005     char *authstr;
1006     int new_loc;
1007
1008     if( s->seekable == 1 )
1009         h->is_streamed = 0;
1010     else
1011         h->is_streamed = 1;
1012
1013     av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
1014                  pathbuf, sizeof(pathbuf), uri);
1015     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1016     path = pathbuf;
1017     if (*path == '/')
1018         path++;
1019
1020     ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
1021                 NULL);
1022 redo:
1023     ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
1024                      &h->interrupt_callback, NULL);
1025     if (ret < 0)
1026         return ret;
1027
1028     authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
1029                                            path, "CONNECT");
1030     snprintf(s->buffer, sizeof(s->buffer),
1031              "CONNECT %s HTTP/1.1\r\n"
1032              "Host: %s\r\n"
1033              "Connection: close\r\n"
1034              "%s%s"
1035              "\r\n",
1036              path,
1037              hoststr,
1038              authstr ? "Proxy-" : "", authstr ? authstr : "");
1039     av_freep(&authstr);
1040
1041     if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1042         goto fail;
1043
1044     s->buf_ptr = s->buffer;
1045     s->buf_end = s->buffer;
1046     s->line_count = 0;
1047     s->filesize = -1;
1048     cur_auth_type = s->proxy_auth_state.auth_type;
1049
1050     /* Note: This uses buffering, potentially reading more than the
1051      * HTTP header. If tunneling a protocol where the server starts
1052      * the conversation, we might buffer part of that here, too.
1053      * Reading that requires using the proper ffurl_read() function
1054      * on this URLContext, not using the fd directly (as the tls
1055      * protocol does). This shouldn't be an issue for tls though,
1056      * since the client starts the conversation there, so there
1057      * is no extra data that we might buffer up here.
1058      */
1059     ret = http_read_header(h, &new_loc);
1060     if (ret < 0)
1061         goto fail;
1062
1063     attempts++;
1064     if (s->http_code == 407 &&
1065         (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
1066         s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
1067         ffurl_closep(&s->hd);
1068         goto redo;
1069     }
1070
1071     if (s->http_code < 400)
1072         return 0;
1073     ret = AVERROR(EIO);
1074
1075 fail:
1076     http_proxy_close(h);
1077     return ret;
1078 }
1079
1080 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
1081 {
1082     HTTPContext *s = h->priv_data;
1083     return ffurl_write(s->hd, buf, size);
1084 }
1085
1086 URLProtocol ff_httpproxy_protocol = {
1087     .name                = "httpproxy",
1088     .url_open            = http_proxy_open,
1089     .url_read            = http_buf_read,
1090     .url_write           = http_proxy_write,
1091     .url_close           = http_proxy_close,
1092     .url_get_file_handle = http_get_file_handle,
1093     .priv_data_size      = sizeof(HTTPContext),
1094     .flags               = URL_PROTOCOL_FLAG_NETWORK,
1095 };
1096 #endif