]> git.sesse.net Git - ffmpeg/blob - libavformat/http.c
lavf/http: Implement server side network code.
[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 "config.h"
23
24 #if CONFIG_ZLIB
25 #include <zlib.h>
26 #endif /* CONFIG_ZLIB */
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
31
32 #include "avformat.h"
33 #include "http.h"
34 #include "httpauth.h"
35 #include "internal.h"
36 #include "network.h"
37 #include "os_support.h"
38 #include "url.h"
39
40 /* XXX: POST protocol is not completely implemented because ffmpeg uses
41  * only a subset of it. */
42
43 /* The IO buffer size is unrelated to the max URL size in itself, but needs
44  * to be large enough to fit the full request headers (including long
45  * path names). */
46 #define BUFFER_SIZE   MAX_URL_SIZE
47 #define MAX_REDIRECTS 8
48 #define HTTP_SINGLE   1
49 #define HTTP_MUTLI    2
50 typedef enum {
51     LOWER_PROTO,
52     READ_HEADERS,
53     WRITE_REPLY_HEADERS,
54     FINISH
55 }HandshakeState;
56
57 typedef struct HTTPContext {
58     const AVClass *class;
59     URLContext *hd;
60     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
61     int line_count;
62     int http_code;
63     /* Used if "Transfer-Encoding: chunked" otherwise -1. */
64     int64_t chunksize;
65     int64_t off, end_off, filesize;
66     char *location;
67     HTTPAuthState auth_state;
68     HTTPAuthState proxy_auth_state;
69     char *headers;
70     char *mime_type;
71     char *user_agent;
72     char *content_type;
73     /* Set if the server correctly handles Connection: close and will close
74      * the connection after feeding us the content. */
75     int willclose;
76     int seekable;           /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
77     int chunked_post;
78     /* A flag which indicates if the end of chunked encoding has been sent. */
79     int end_chunked_post;
80     /* A flag which indicates we have finished to read POST reply. */
81     int end_header;
82     /* A flag which indicates if we use persistent connections. */
83     int multiple_requests;
84     uint8_t *post_data;
85     int post_datalen;
86     int is_akamai;
87     int is_mediagateway;
88     char *cookies;          ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
89     /* A dictionary containing cookies keyed by cookie name */
90     AVDictionary *cookie_dict;
91     int icy;
92     /* how much data was read since the last ICY metadata packet */
93     int icy_data_read;
94     /* after how many bytes of read data a new metadata packet will be found */
95     int icy_metaint;
96     char *icy_metadata_headers;
97     char *icy_metadata_packet;
98     AVDictionary *metadata;
99 #if CONFIG_ZLIB
100     int compressed;
101     z_stream inflate_stream;
102     uint8_t *inflate_buffer;
103 #endif /* CONFIG_ZLIB */
104     AVDictionary *chained_options;
105     int send_expect_100;
106     char *method;
107     int reconnect;
108     int listen;
109     char *resource;
110     int reply_code;
111     int is_multi_client;
112     HandshakeState handshake_step;
113     int is_connected_server;
114 } HTTPContext;
115
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)
120
121 static const AVOption options[] = {
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 },
126     { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
127     { "user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
128     { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D | E },
129     { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
130     { "mime_type", "export the MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
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 },
132     { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
133     { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT },
134     { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT },
135     { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
136     { "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"},
137     { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, "auth_type"},
138     { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 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 },
140     { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
141     { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
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 },
145     { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
146     { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
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},
148     { NULL }
149 };
150
151 static int http_connect(URLContext *h, const char *path, const char *local_path,
152                         const char *hoststr, const char *auth,
153                         const char *proxyauth, int *new_location);
154 static int http_read_header(URLContext *h, int *new_location);
155
156 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
157 {
158     memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
159            &((HTTPContext *)src->priv_data)->auth_state,
160            sizeof(HTTPAuthState));
161     memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
162            &((HTTPContext *)src->priv_data)->proxy_auth_state,
163            sizeof(HTTPAuthState));
164 }
165
166 static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
167 {
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] = "";
171     char path1[MAX_URL_SIZE];
172     char buf[1024], urlbuf[MAX_URL_SIZE];
173     int port, use_proxy, err, location_changed = 0;
174     HTTPContext *s = h->priv_data;
175
176     av_url_split(proto, sizeof(proto), auth, sizeof(auth),
177                  hostname, sizeof(hostname), &port,
178                  path1, sizeof(path1), s->location);
179     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
180
181     proxy_path = getenv("http_proxy");
182     use_proxy  = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
183                  proxy_path && av_strstart(proxy_path, "http://", NULL);
184
185     if (!strcmp(proto, "https")) {
186         lower_proto = "tls";
187         use_proxy   = 0;
188         if (port < 0)
189             port = 443;
190     }
191     if (port < 0)
192         port = 80;
193
194     if (path1[0] == '\0')
195         path = "/";
196     else
197         path = path1;
198     local_path = path;
199     if (use_proxy) {
200         /* Reassemble the request URL without auth string - we don't
201          * want to leak the auth to the proxy. */
202         ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
203                     path1);
204         path = urlbuf;
205         av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
206                      hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
207     }
208
209     ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
210
211     if (!s->hd) {
212         err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE,
213                          &h->interrupt_callback, options);
214         if (err < 0)
215             return err;
216     }
217
218     err = http_connect(h, path, local_path, hoststr,
219                        auth, proxyauth, &location_changed);
220     if (err < 0)
221         return err;
222
223     return location_changed;
224 }
225
226 /* return non zero if error */
227 static int http_open_cnx(URLContext *h, AVDictionary **options)
228 {
229     HTTPAuthType cur_auth_type, cur_proxy_auth_type;
230     HTTPContext *s = h->priv_data;
231     int location_changed, attempts = 0, redirects = 0;
232 redo:
233     av_dict_copy(options, s->chained_options, 0);
234
235     cur_auth_type       = s->auth_state.auth_type;
236     cur_proxy_auth_type = s->auth_state.auth_type;
237
238     location_changed = http_open_cnx_internal(h, options);
239     if (location_changed < 0)
240         goto fail;
241
242     attempts++;
243     if (s->http_code == 401) {
244         if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
245             s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
246             ffurl_closep(&s->hd);
247             goto redo;
248         } else
249             goto fail;
250     }
251     if (s->http_code == 407) {
252         if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
253             s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
254             ffurl_closep(&s->hd);
255             goto redo;
256         } else
257             goto fail;
258     }
259     if ((s->http_code == 301 || s->http_code == 302 ||
260          s->http_code == 303 || s->http_code == 307) &&
261         location_changed == 1) {
262         /* url moved, get next */
263         ffurl_closep(&s->hd);
264         if (redirects++ >= MAX_REDIRECTS)
265             return AVERROR(EIO);
266         /* Restart the authentication process with the new target, which
267          * might use a different auth mechanism. */
268         memset(&s->auth_state, 0, sizeof(s->auth_state));
269         attempts         = 0;
270         location_changed = 0;
271         goto redo;
272     }
273     return 0;
274
275 fail:
276     if (s->hd)
277         ffurl_closep(&s->hd);
278     if (location_changed < 0)
279         return location_changed;
280     return ff_http_averror(s->http_code, AVERROR(EIO));
281 }
282
283 int ff_http_do_new_request(URLContext *h, const char *uri)
284 {
285     HTTPContext *s = h->priv_data;
286     AVDictionary *options = NULL;
287     int ret;
288
289     s->off           = 0;
290     s->icy_data_read = 0;
291     av_free(s->location);
292     s->location = av_strdup(uri);
293     if (!s->location)
294         return AVERROR(ENOMEM);
295
296     ret = http_open_cnx(h, &options);
297     av_dict_free(&options);
298     return ret;
299 }
300
301 int ff_http_averror(int status_code, int default_averror)
302 {
303     switch (status_code) {
304         case 400: return AVERROR_HTTP_BAD_REQUEST;
305         case 401: return AVERROR_HTTP_UNAUTHORIZED;
306         case 403: return AVERROR_HTTP_FORBIDDEN;
307         case 404: return AVERROR_HTTP_NOT_FOUND;
308         default: break;
309     }
310     if (status_code >= 400 && status_code <= 499)
311         return AVERROR_HTTP_OTHER_4XX;
312     else if (status_code >= 500)
313         return AVERROR_HTTP_SERVER_ERROR;
314     else
315         return default_averror;
316 }
317
318 static int http_write_reply(URLContext* h, int status_code)
319 {
320     int ret, body = 0, reply_code, message_len;
321     const char *reply_text, *content_type;
322     HTTPContext *s = h->priv_data;
323     char message[BUFFER_SIZE];
324     content_type = "text/plain";
325
326     if (status_code < 0)
327         body = 1;
328     switch (status_code) {
329     case AVERROR_HTTP_BAD_REQUEST:
330     case 400:
331         reply_code = 400;
332         reply_text = "Bad Request";
333         break;
334     case AVERROR_HTTP_FORBIDDEN:
335     case 403:
336         reply_code = 403;
337         reply_text = "Forbidden";
338         break;
339     case AVERROR_HTTP_NOT_FOUND:
340     case 404:
341         reply_code = 404;
342         reply_text = "Not Found";
343         break;
344     case 200:
345         reply_code = 200;
346         reply_text = "OK";
347         content_type = "application/octet-stream";
348         break;
349     case AVERROR_HTTP_SERVER_ERROR:
350     case 500:
351         reply_code = 500;
352         reply_text = "Internal server error";
353         break;
354     default:
355         return AVERROR(EINVAL);
356     }
357     if (body) {
358         s->chunked_post = 0;
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"
363                  "\r\n"
364                  "%03d %s\r\n",
365                  reply_code,
366                  reply_text,
367                  content_type,
368                  strlen(reply_text) + 6, // 3 digit status code + space + \r\n
369                  reply_code,
370                  reply_text);
371     } else {
372         s->chunked_post = 1;
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"
377                  "\r\n",
378                  reply_code,
379                  reply_text,
380                  content_type);
381     }
382     av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
383     if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
384         return ret;
385     return 0;
386 }
387
388 static void handle_http_errors(URLContext *h, int error)
389 {
390     av_assert0(error < 0);
391     http_write_reply(h, error);
392 }
393
394 static int http_handshake(URLContext *c)
395 {
396     int ret, err, new_location;
397     HTTPContext *ch = c->priv_data;
398     URLContext *cl = ch->hd;
399     switch (ch->handshake_step) {
400     case LOWER_PROTO:
401         av_log(c, AV_LOG_TRACE, "Lower protocol\n");
402         if ((ret = ffurl_handshake(cl) > 0))
403             return 2 + ret;
404         if ((ret < 0))
405             return ret;
406         ch->handshake_step = READ_HEADERS;
407         ch->is_connected_server = 1;
408         return 2;
409     case READ_HEADERS:
410         av_log(c, AV_LOG_TRACE, "Read headers\n");
411         if ((err = http_read_header(c, &new_location)) < 0) {
412             handle_http_errors(c, err);
413             return err;
414         }
415         ch->handshake_step = WRITE_REPLY_HEADERS;
416         return 1;
417     case WRITE_REPLY_HEADERS:
418         av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
419         if ((err = http_write_reply(c, ch->reply_code)) < 0)
420             return err;
421         ch->handshake_step = FINISH;
422         return 1;
423     case FINISH:
424         return 0;
425     }
426     // this should never be reached.
427     return AVERROR(EINVAL);
428 }
429
430 static int http_listen(URLContext *h, const char *uri, int flags,
431                        AVDictionary **options) {
432     HTTPContext *s = h->priv_data;
433     int ret;
434     char hostname[1024], proto[10];
435     char lower_url[100];
436     const char *lower_proto = "tcp";
437     int port;
438     av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
439                  NULL, 0, uri);
440     if (!strcmp(proto, "https"))
441         lower_proto = "tls";
442     ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
443                 NULL);
444     if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
445         goto fail;
446     if ((ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
447                           &h->interrupt_callback, options)) < 0)
448         goto fail;
449     s->handshake_step = LOWER_PROTO;
450     if (s->listen == HTTP_SINGLE) { /* single client */
451         s->reply_code = 200;
452         while ((ret = http_handshake(h)) > 0);
453     }
454 fail:
455     av_dict_free(&s->chained_options);
456     return ret;
457 }
458
459 static int http_open(URLContext *h, const char *uri, int flags,
460                      AVDictionary **options)
461 {
462     HTTPContext *s = h->priv_data;
463     int ret;
464
465     if( s->seekable == 1 )
466         h->is_streamed = 0;
467     else
468         h->is_streamed = 1;
469
470     s->filesize = -1;
471     s->location = av_strdup(uri);
472     if (!s->location)
473         return AVERROR(ENOMEM);
474     if (options)
475         av_dict_copy(&s->chained_options, *options, 0);
476
477     if (s->headers) {
478         int len = strlen(s->headers);
479         if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
480             av_log(h, AV_LOG_WARNING,
481                    "No trailing CRLF found in HTTP header.\n");
482             ret = av_reallocp(&s->headers, len + 3);
483             if (ret < 0)
484                 return ret;
485             s->headers[len]     = '\r';
486             s->headers[len + 1] = '\n';
487             s->headers[len + 2] = '\0';
488         }
489     }
490
491     if (s->listen) {
492         return http_listen(h, uri, flags, options);
493     }
494     ret = http_open_cnx(h, options);
495     if (ret < 0)
496         av_dict_free(&s->chained_options);
497     return ret;
498 }
499
500 static int http_accept(URLContext *s, URLContext **c)
501 {
502     int ret;
503     HTTPContext *sc = s->priv_data;
504     HTTPContext *cc;
505     URLContext *sl = sc->hd;
506     URLContext *cl = NULL;
507
508     av_assert0(sc->listen);
509     if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
510         goto fail;
511     cc = (*c)->priv_data;
512     if ((ret = ffurl_accept(sl, &cl)) < 0)
513         goto fail;
514     cc->hd = cl;
515     cc->is_multi_client = 1;
516 fail:
517     return ret;
518 }
519
520 static int http_getc(HTTPContext *s)
521 {
522     int len;
523     if (s->buf_ptr >= s->buf_end) {
524         len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
525         if (len < 0) {
526             return len;
527         } else if (len == 0) {
528             return AVERROR_EOF;
529         } else {
530             s->buf_ptr = s->buffer;
531             s->buf_end = s->buffer + len;
532         }
533     }
534     return *s->buf_ptr++;
535 }
536
537 static int http_get_line(HTTPContext *s, char *line, int line_size)
538 {
539     int ch;
540     char *q;
541
542     q = line;
543     for (;;) {
544         ch = http_getc(s);
545         if (ch < 0)
546             return ch;
547         if (ch == '\n') {
548             /* process line */
549             if (q > line && q[-1] == '\r')
550                 q--;
551             *q = '\0';
552
553             return 0;
554         } else {
555             if ((q - line) < line_size - 1)
556                 *q++ = ch;
557         }
558     }
559 }
560
561 static int check_http_code(URLContext *h, int http_code, const char *end)
562 {
563     HTTPContext *s = h->priv_data;
564     /* error codes are 4xx and 5xx, but regard 401 as a success, so we
565      * don't abort until all headers have been parsed. */
566     if (http_code >= 400 && http_code < 600 &&
567         (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
568         (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
569         end += strspn(end, SPACE_CHARS);
570         av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
571         return ff_http_averror(http_code, AVERROR(EIO));
572     }
573     return 0;
574 }
575
576 static int parse_location(HTTPContext *s, const char *p)
577 {
578     char redirected_location[MAX_URL_SIZE], *new_loc;
579     ff_make_absolute_url(redirected_location, sizeof(redirected_location),
580                          s->location, p);
581     new_loc = av_strdup(redirected_location);
582     if (!new_loc)
583         return AVERROR(ENOMEM);
584     av_free(s->location);
585     s->location = new_loc;
586     return 0;
587 }
588
589 /* "bytes $from-$to/$document_size" */
590 static void parse_content_range(URLContext *h, const char *p)
591 {
592     HTTPContext *s = h->priv_data;
593     const char *slash;
594
595     if (!strncmp(p, "bytes ", 6)) {
596         p     += 6;
597         s->off = strtoll(p, NULL, 10);
598         if ((slash = strchr(p, '/')) && strlen(slash) > 0)
599             s->filesize = strtoll(slash + 1, NULL, 10);
600     }
601     if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
602         h->is_streamed = 0; /* we _can_ in fact seek */
603 }
604
605 static int parse_content_encoding(URLContext *h, const char *p)
606 {
607     if (!av_strncasecmp(p, "gzip", 4) ||
608         !av_strncasecmp(p, "deflate", 7)) {
609 #if CONFIG_ZLIB
610         HTTPContext *s = h->priv_data;
611
612         s->compressed = 1;
613         inflateEnd(&s->inflate_stream);
614         if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
615             av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
616                    s->inflate_stream.msg);
617             return AVERROR(ENOSYS);
618         }
619         if (zlibCompileFlags() & (1 << 17)) {
620             av_log(h, AV_LOG_WARNING,
621                    "Your zlib was compiled without gzip support.\n");
622             return AVERROR(ENOSYS);
623         }
624 #else
625         av_log(h, AV_LOG_WARNING,
626                "Compressed (%s) content, need zlib with gzip support\n", p);
627         return AVERROR(ENOSYS);
628 #endif /* CONFIG_ZLIB */
629     } else if (!av_strncasecmp(p, "identity", 8)) {
630         // The normal, no-encoding case (although servers shouldn't include
631         // the header at all if this is the case).
632     } else {
633         av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
634     }
635     return 0;
636 }
637
638 // Concat all Icy- header lines
639 static int parse_icy(HTTPContext *s, const char *tag, const char *p)
640 {
641     int len = 4 + strlen(p) + strlen(tag);
642     int is_first = !s->icy_metadata_headers;
643     int ret;
644
645     av_dict_set(&s->metadata, tag, p, 0);
646
647     if (s->icy_metadata_headers)
648         len += strlen(s->icy_metadata_headers);
649
650     if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
651         return ret;
652
653     if (is_first)
654         *s->icy_metadata_headers = '\0';
655
656     av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
657
658     return 0;
659 }
660
661 static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)
662 {
663     char *eql, *name;
664
665     // duplicate the cookie name (dict will dupe the value)
666     if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);
667     if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);
668
669     // add the cookie to the dictionary
670     av_dict_set(cookies, name, eql, AV_DICT_DONT_STRDUP_KEY);
671
672     return 0;
673 }
674
675 static int cookie_string(AVDictionary *dict, char **cookies)
676 {
677     AVDictionaryEntry *e = NULL;
678     int len = 1;
679
680     // determine how much memory is needed for the cookies string
681     while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
682         len += strlen(e->key) + strlen(e->value) + 1;
683
684     // reallocate the cookies
685     e = NULL;
686     if (*cookies) av_free(*cookies);
687     *cookies = av_malloc(len);
688     if (!*cookies) return AVERROR(ENOMEM);
689     *cookies[0] = '\0';
690
691     // write out the cookies
692     while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
693         av_strlcatf(*cookies, len, "%s%s\n", e->key, e->value);
694
695     return 0;
696 }
697
698 static int process_line(URLContext *h, char *line, int line_count,
699                         int *new_location)
700 {
701     HTTPContext *s = h->priv_data;
702     const char *auto_method =  h->flags & AVIO_FLAG_READ ? "POST" : "GET";
703     char *tag, *p, *end, *method, *resource, *version;
704     int ret;
705
706     /* end of header */
707     if (line[0] == '\0') {
708         s->end_header = 1;
709         return 0;
710     }
711
712     p = line;
713     if (line_count == 0) {
714         if (s->is_connected_server) {
715             // HTTP method
716             method = p;
717             while (!av_isspace(*p))
718                 p++;
719             *(p++) = '\0';
720             av_log(h, AV_LOG_TRACE, "Received method: %s\n", method);
721             if (s->method) {
722                 if (av_strcasecmp(s->method, method)) {
723                     av_log(h, AV_LOG_ERROR, "Received and expected HTTP method do not match. (%s expected, %s received)\n",
724                            s->method, method);
725                     return ff_http_averror(400, AVERROR(EIO));
726                 }
727             } else {
728                 // use autodetected HTTP method to expect
729                 av_log(h, AV_LOG_TRACE, "Autodetected %s HTTP method\n", auto_method);
730                 if (av_strcasecmp(auto_method, method)) {
731                     av_log(h, AV_LOG_ERROR, "Received and autodetected HTTP method did not match "
732                            "(%s autodetected %s received)\n", auto_method, method);
733                     return ff_http_averror(400, AVERROR(EIO));
734                 }
735                 if (!(s->method = av_strdup(method)))
736                     return AVERROR(ENOMEM);
737             }
738
739             // HTTP resource
740             while (av_isspace(*p))
741                 p++;
742             resource = p;
743             while (!av_isspace(*p))
744                 p++;
745             *(p++) = '\0';
746             av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
747             if (!(s->resource = av_strdup(resource)))
748                 return AVERROR(ENOMEM);
749
750             // HTTP version
751             while (av_isspace(*p))
752                 p++;
753             version = p;
754             while (!av_isspace(*p))
755                 p++;
756             *p = '\0';
757             if (av_strncasecmp(version, "HTTP/", 5)) {
758                 av_log(h, AV_LOG_ERROR, "Malformed HTTP version string.\n");
759                 return ff_http_averror(400, AVERROR(EIO));
760             }
761             av_log(h, AV_LOG_TRACE, "HTTP version string: %s\n", version);
762         } else {
763             while (!av_isspace(*p) && *p != '\0')
764                 p++;
765             while (av_isspace(*p))
766                 p++;
767             s->http_code = strtol(p, &end, 10);
768
769             av_log(h, AV_LOG_TRACE, "http_code=%d\n", s->http_code);
770
771             if ((ret = check_http_code(h, s->http_code, end)) < 0)
772                 return ret;
773         }
774     } else {
775         while (*p != '\0' && *p != ':')
776             p++;
777         if (*p != ':')
778             return 1;
779
780         *p  = '\0';
781         tag = line;
782         p++;
783         while (av_isspace(*p))
784             p++;
785         if (!av_strcasecmp(tag, "Location")) {
786             if ((ret = parse_location(s, p)) < 0)
787                 return ret;
788             *new_location = 1;
789         } else if (!av_strcasecmp(tag, "Content-Length") && s->filesize == -1) {
790             s->filesize = strtoll(p, NULL, 10);
791         } else if (!av_strcasecmp(tag, "Content-Range")) {
792             parse_content_range(h, p);
793         } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
794                    !strncmp(p, "bytes", 5) &&
795                    s->seekable == -1) {
796             h->is_streamed = 0;
797         } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
798                    !av_strncasecmp(p, "chunked", 7)) {
799             s->filesize  = -1;
800             s->chunksize = 0;
801         } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
802             ff_http_auth_handle_header(&s->auth_state, tag, p);
803         } else if (!av_strcasecmp(tag, "Authentication-Info")) {
804             ff_http_auth_handle_header(&s->auth_state, tag, p);
805         } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
806             ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
807         } else if (!av_strcasecmp(tag, "Connection")) {
808             if (!strcmp(p, "close"))
809                 s->willclose = 1;
810         } else if (!av_strcasecmp(tag, "Server")) {
811             if (!av_strcasecmp(p, "AkamaiGHost")) {
812                 s->is_akamai = 1;
813             } else if (!av_strncasecmp(p, "MediaGateway", 12)) {
814                 s->is_mediagateway = 1;
815             }
816         } else if (!av_strcasecmp(tag, "Content-Type")) {
817             av_free(s->mime_type);
818             s->mime_type = av_strdup(p);
819         } else if (!av_strcasecmp(tag, "Set-Cookie")) {
820             if (parse_cookie(s, p, &s->cookie_dict))
821                 av_log(h, AV_LOG_WARNING, "Unable to parse '%s'\n", p);
822         } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
823             s->icy_metaint = strtoll(p, NULL, 10);
824         } else if (!av_strncasecmp(tag, "Icy-", 4)) {
825             if ((ret = parse_icy(s, tag, p)) < 0)
826                 return ret;
827         } else if (!av_strcasecmp(tag, "Content-Encoding")) {
828             if ((ret = parse_content_encoding(h, p)) < 0)
829                 return ret;
830         }
831     }
832     return 1;
833 }
834
835 /**
836  * Create a string containing cookie values for use as a HTTP cookie header
837  * field value for a particular path and domain from the cookie values stored in
838  * the HTTP protocol context. The cookie string is stored in *cookies.
839  *
840  * @return a negative value if an error condition occurred, 0 otherwise
841  */
842 static int get_cookies(HTTPContext *s, char **cookies, const char *path,
843                        const char *domain)
844 {
845     // cookie strings will look like Set-Cookie header field values.  Multiple
846     // Set-Cookie fields will result in multiple values delimited by a newline
847     int ret = 0;
848     char *next, *cookie, *set_cookies = av_strdup(s->cookies), *cset_cookies = set_cookies;
849
850     if (!set_cookies) return AVERROR(EINVAL);
851
852     // destroy any cookies in the dictionary.
853     av_dict_free(&s->cookie_dict);
854
855     *cookies = NULL;
856     while ((cookie = av_strtok(set_cookies, "\n", &next))) {
857         int domain_offset = 0;
858         char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
859         set_cookies = NULL;
860
861         // store the cookie in a dict in case it is updated in the response
862         if (parse_cookie(s, cookie, &s->cookie_dict))
863             av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);
864
865         while ((param = av_strtok(cookie, "; ", &next_param))) {
866             if (cookie) {
867                 // first key-value pair is the actual cookie value
868                 cvalue = av_strdup(param);
869                 cookie = NULL;
870             } else if (!av_strncasecmp("path=",   param, 5)) {
871                 av_free(cpath);
872                 cpath = av_strdup(&param[5]);
873             } else if (!av_strncasecmp("domain=", param, 7)) {
874                 // if the cookie specifies a sub-domain, skip the leading dot thereby
875                 // supporting URLs that point to sub-domains and the master domain
876                 int leading_dot = (param[7] == '.');
877                 av_free(cdomain);
878                 cdomain = av_strdup(&param[7+leading_dot]);
879             } else {
880                 // ignore unknown attributes
881             }
882         }
883         if (!cdomain)
884             cdomain = av_strdup(domain);
885
886         // ensure all of the necessary values are valid
887         if (!cdomain || !cpath || !cvalue) {
888             av_log(s, AV_LOG_WARNING,
889                    "Invalid cookie found, no value, path or domain specified\n");
890             goto done_cookie;
891         }
892
893         // check if the request path matches the cookie path
894         if (av_strncasecmp(path, cpath, strlen(cpath)))
895             goto done_cookie;
896
897         // the domain should be at least the size of our cookie domain
898         domain_offset = strlen(domain) - strlen(cdomain);
899         if (domain_offset < 0)
900             goto done_cookie;
901
902         // match the cookie domain
903         if (av_strcasecmp(&domain[domain_offset], cdomain))
904             goto done_cookie;
905
906         // cookie parameters match, so copy the value
907         if (!*cookies) {
908             if (!(*cookies = av_strdup(cvalue))) {
909                 ret = AVERROR(ENOMEM);
910                 goto done_cookie;
911             }
912         } else {
913             char *tmp = *cookies;
914             size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
915             if (!(*cookies = av_malloc(str_size))) {
916                 ret = AVERROR(ENOMEM);
917                 goto done_cookie;
918             }
919             snprintf(*cookies, str_size, "%s; %s", tmp, cvalue);
920             av_free(tmp);
921         }
922
923         done_cookie:
924         av_freep(&cdomain);
925         av_freep(&cpath);
926         av_freep(&cvalue);
927         if (ret < 0) {
928             if (*cookies) av_freep(cookies);
929             av_free(cset_cookies);
930             return ret;
931         }
932     }
933
934     av_free(cset_cookies);
935
936     return 0;
937 }
938
939 static inline int has_header(const char *str, const char *header)
940 {
941     /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
942     if (!str)
943         return 0;
944     return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
945 }
946
947 static int http_read_header(URLContext *h, int *new_location)
948 {
949     HTTPContext *s = h->priv_data;
950     char line[MAX_URL_SIZE];
951     int err = 0;
952
953     s->chunksize = -1;
954
955     for (;;) {
956         if ((err = http_get_line(s, line, sizeof(line))) < 0)
957             return err;
958
959         av_log(h, AV_LOG_TRACE, "header='%s'\n", line);
960
961         err = process_line(h, line, s->line_count, new_location);
962         if (err < 0)
963             return err;
964         if (err == 0)
965             break;
966         s->line_count++;
967     }
968
969     if (s->seekable == -1 && s->is_mediagateway && s->filesize == 2000000000)
970         h->is_streamed = 1; /* we can in fact _not_ seek */
971
972     // add any new cookies into the existing cookie string
973     cookie_string(s->cookie_dict, &s->cookies);
974     av_dict_free(&s->cookie_dict);
975
976     return err;
977 }
978
979 static int http_connect(URLContext *h, const char *path, const char *local_path,
980                         const char *hoststr, const char *auth,
981                         const char *proxyauth, int *new_location)
982 {
983     HTTPContext *s = h->priv_data;
984     int post, err;
985     char headers[HTTP_HEADERS_SIZE] = "";
986     char *authstr = NULL, *proxyauthstr = NULL;
987     int64_t off = s->off;
988     int len = 0;
989     const char *method;
990     int send_expect_100 = 0;
991
992     /* send http header */
993     post = h->flags & AVIO_FLAG_WRITE;
994
995     if (s->post_data) {
996         /* force POST method and disable chunked encoding when
997          * custom HTTP post data is set */
998         post            = 1;
999         s->chunked_post = 0;
1000     }
1001
1002     if (s->method)
1003         method = s->method;
1004     else
1005         method = post ? "POST" : "GET";
1006
1007     authstr      = ff_http_auth_create_response(&s->auth_state, auth,
1008                                                 local_path, method);
1009     proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
1010                                                 local_path, method);
1011     if (post && !s->post_data) {
1012         send_expect_100 = s->send_expect_100;
1013         /* The user has supplied authentication but we don't know the auth type,
1014          * send Expect: 100-continue to get the 401 response including the
1015          * WWW-Authenticate header, or an 100 continue if no auth actually
1016          * is needed. */
1017         if (auth && *auth &&
1018             s->auth_state.auth_type == HTTP_AUTH_NONE &&
1019             s->http_code != 401)
1020             send_expect_100 = 1;
1021     }
1022
1023     /* set default headers if needed */
1024     if (!has_header(s->headers, "\r\nUser-Agent: "))
1025         len += av_strlcatf(headers + len, sizeof(headers) - len,
1026                            "User-Agent: %s\r\n", s->user_agent);
1027     if (!has_header(s->headers, "\r\nAccept: "))
1028         len += av_strlcpy(headers + len, "Accept: */*\r\n",
1029                           sizeof(headers) - len);
1030     // Note: we send this on purpose even when s->off is 0 when we're probing,
1031     // since it allows us to detect more reliably if a (non-conforming)
1032     // server supports seeking by analysing the reply headers.
1033     if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable == -1)) {
1034         len += av_strlcatf(headers + len, sizeof(headers) - len,
1035                            "Range: bytes=%"PRId64"-", s->off);
1036         if (s->end_off)
1037             len += av_strlcatf(headers + len, sizeof(headers) - len,
1038                                "%"PRId64, s->end_off - 1);
1039         len += av_strlcpy(headers + len, "\r\n",
1040                           sizeof(headers) - len);
1041     }
1042     if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
1043         len += av_strlcatf(headers + len, sizeof(headers) - len,
1044                            "Expect: 100-continue\r\n");
1045
1046     if (!has_header(s->headers, "\r\nConnection: ")) {
1047         if (s->multiple_requests)
1048             len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
1049                               sizeof(headers) - len);
1050         else
1051             len += av_strlcpy(headers + len, "Connection: close\r\n",
1052                               sizeof(headers) - len);
1053     }
1054
1055     if (!has_header(s->headers, "\r\nHost: "))
1056         len += av_strlcatf(headers + len, sizeof(headers) - len,
1057                            "Host: %s\r\n", hoststr);
1058     if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
1059         len += av_strlcatf(headers + len, sizeof(headers) - len,
1060                            "Content-Length: %d\r\n", s->post_datalen);
1061
1062     if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
1063         len += av_strlcatf(headers + len, sizeof(headers) - len,
1064                            "Content-Type: %s\r\n", s->content_type);
1065     if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
1066         char *cookies = NULL;
1067         if (!get_cookies(s, &cookies, path, hoststr) && cookies) {
1068             len += av_strlcatf(headers + len, sizeof(headers) - len,
1069                                "Cookie: %s\r\n", cookies);
1070             av_free(cookies);
1071         }
1072     }
1073     if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
1074         len += av_strlcatf(headers + len, sizeof(headers) - len,
1075                            "Icy-MetaData: %d\r\n", 1);
1076
1077     /* now add in custom headers */
1078     if (s->headers)
1079         av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
1080
1081     snprintf(s->buffer, sizeof(s->buffer),
1082              "%s %s HTTP/1.1\r\n"
1083              "%s"
1084              "%s"
1085              "%s"
1086              "%s%s"
1087              "\r\n",
1088              method,
1089              path,
1090              post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
1091              headers,
1092              authstr ? authstr : "",
1093              proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
1094
1095     av_log(h, AV_LOG_DEBUG, "request: %s\n", s->buffer);
1096
1097     if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1098         goto done;
1099
1100     if (s->post_data)
1101         if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
1102             goto done;
1103
1104     /* init input buffer */
1105     s->buf_ptr          = s->buffer;
1106     s->buf_end          = s->buffer;
1107     s->line_count       = 0;
1108     s->off              = 0;
1109     s->icy_data_read    = 0;
1110     s->filesize         = -1;
1111     s->willclose        = 0;
1112     s->end_chunked_post = 0;
1113     s->end_header       = 0;
1114     if (post && !s->post_data && !send_expect_100) {
1115         /* Pretend that it did work. We didn't read any header yet, since
1116          * we've still to send the POST data, but the code calling this
1117          * function will check http_code after we return. */
1118         s->http_code = 200;
1119         err = 0;
1120         goto done;
1121     }
1122
1123     /* wait for header */
1124     err = http_read_header(h, new_location);
1125     if (err < 0)
1126         goto done;
1127
1128     if (*new_location)
1129         s->off = off;
1130
1131     err = (off == s->off) ? 0 : -1;
1132 done:
1133     av_freep(&authstr);
1134     av_freep(&proxyauthstr);
1135     return err;
1136 }
1137
1138 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
1139 {
1140     HTTPContext *s = h->priv_data;
1141     int len;
1142     /* read bytes from input buffer first */
1143     len = s->buf_end - s->buf_ptr;
1144     if (len > 0) {
1145         if (len > size)
1146             len = size;
1147         memcpy(buf, s->buf_ptr, len);
1148         s->buf_ptr += len;
1149     } else {
1150         if ((!s->willclose || s->chunksize < 0) &&
1151             s->filesize >= 0 && s->off >= s->filesize)
1152             return AVERROR_EOF;
1153         len = ffurl_read(s->hd, buf, size);
1154         if (!len && (!s->willclose || s->chunksize < 0) &&
1155             s->filesize >= 0 && s->off < s->filesize) {
1156             av_log(h, AV_LOG_ERROR,
1157                    "Stream ends prematurely at %"PRId64", should be %"PRId64"\n",
1158                    s->off, s->filesize
1159                   );
1160             return AVERROR(EIO);
1161         }
1162     }
1163     if (len > 0) {
1164         s->off += len;
1165         if (s->chunksize > 0)
1166             s->chunksize -= len;
1167     }
1168     return len;
1169 }
1170
1171 #if CONFIG_ZLIB
1172 #define DECOMPRESS_BUF_SIZE (256 * 1024)
1173 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
1174 {
1175     HTTPContext *s = h->priv_data;
1176     int ret;
1177
1178     if (!s->inflate_buffer) {
1179         s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
1180         if (!s->inflate_buffer)
1181             return AVERROR(ENOMEM);
1182     }
1183
1184     if (s->inflate_stream.avail_in == 0) {
1185         int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
1186         if (read <= 0)
1187             return read;
1188         s->inflate_stream.next_in  = s->inflate_buffer;
1189         s->inflate_stream.avail_in = read;
1190     }
1191
1192     s->inflate_stream.avail_out = size;
1193     s->inflate_stream.next_out  = buf;
1194
1195     ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
1196     if (ret != Z_OK && ret != Z_STREAM_END)
1197         av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
1198                ret, s->inflate_stream.msg);
1199
1200     return size - s->inflate_stream.avail_out;
1201 }
1202 #endif /* CONFIG_ZLIB */
1203
1204 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect);
1205
1206 static int http_read_stream(URLContext *h, uint8_t *buf, int size)
1207 {
1208     HTTPContext *s = h->priv_data;
1209     int err, new_location, read_ret, seek_ret;
1210
1211     if (!s->hd)
1212         return AVERROR_EOF;
1213
1214     if (s->end_chunked_post && !s->end_header) {
1215         err = http_read_header(h, &new_location);
1216         if (err < 0)
1217             return err;
1218     }
1219
1220     if (s->chunksize >= 0) {
1221         if (!s->chunksize) {
1222             char line[32];
1223
1224                 do {
1225                     if ((err = http_get_line(s, line, sizeof(line))) < 0)
1226                         return err;
1227                 } while (!*line);    /* skip CR LF from last chunk */
1228
1229                 s->chunksize = strtoll(line, NULL, 16);
1230
1231                 av_log(NULL, AV_LOG_TRACE, "Chunked encoding data size: %"PRId64"'\n",
1232                         s->chunksize);
1233
1234                 if (!s->chunksize)
1235                     return 0;
1236         }
1237         size = FFMIN(size, s->chunksize);
1238     }
1239 #if CONFIG_ZLIB
1240     if (s->compressed)
1241         return http_buf_read_compressed(h, buf, size);
1242 #endif /* CONFIG_ZLIB */
1243     read_ret = http_buf_read(h, buf, size);
1244     if (read_ret < 0 && s->reconnect && !h->is_streamed && s->filesize > 0 && s->off < s->filesize) {
1245         av_log(h, AV_LOG_INFO, "Will reconnect at %"PRId64".\n", s->off);
1246         seek_ret = http_seek_internal(h, s->off, SEEK_SET, 1);
1247         if (seek_ret != s->off) {
1248             av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRId64".\n", s->off);
1249             return read_ret;
1250         }
1251
1252         read_ret = http_buf_read(h, buf, size);
1253     }
1254
1255     return read_ret;
1256 }
1257
1258 // Like http_read_stream(), but no short reads.
1259 // Assumes partial reads are an error.
1260 static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
1261 {
1262     int pos = 0;
1263     while (pos < size) {
1264         int len = http_read_stream(h, buf + pos, size - pos);
1265         if (len < 0)
1266             return len;
1267         pos += len;
1268     }
1269     return pos;
1270 }
1271
1272 static void update_metadata(HTTPContext *s, char *data)
1273 {
1274     char *key;
1275     char *val;
1276     char *end;
1277     char *next = data;
1278
1279     while (*next) {
1280         key = next;
1281         val = strstr(key, "='");
1282         if (!val)
1283             break;
1284         end = strstr(val, "';");
1285         if (!end)
1286             break;
1287
1288         *val = '\0';
1289         *end = '\0';
1290         val += 2;
1291
1292         av_dict_set(&s->metadata, key, val, 0);
1293
1294         next = end + 2;
1295     }
1296 }
1297
1298 static int store_icy(URLContext *h, int size)
1299 {
1300     HTTPContext *s = h->priv_data;
1301     /* until next metadata packet */
1302     int remaining = s->icy_metaint - s->icy_data_read;
1303
1304     if (remaining < 0)
1305         return AVERROR_INVALIDDATA;
1306
1307     if (!remaining) {
1308         /* The metadata packet is variable sized. It has a 1 byte header
1309          * which sets the length of the packet (divided by 16). If it's 0,
1310          * the metadata doesn't change. After the packet, icy_metaint bytes
1311          * of normal data follows. */
1312         uint8_t ch;
1313         int len = http_read_stream_all(h, &ch, 1);
1314         if (len < 0)
1315             return len;
1316         if (ch > 0) {
1317             char data[255 * 16 + 1];
1318             int ret;
1319             len = ch * 16;
1320             ret = http_read_stream_all(h, data, len);
1321             if (ret < 0)
1322                 return ret;
1323             data[len + 1] = 0;
1324             if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
1325                 return ret;
1326             update_metadata(s, data);
1327         }
1328         s->icy_data_read = 0;
1329         remaining        = s->icy_metaint;
1330     }
1331
1332     return FFMIN(size, remaining);
1333 }
1334
1335 static int http_read(URLContext *h, uint8_t *buf, int size)
1336 {
1337     HTTPContext *s = h->priv_data;
1338
1339     if (s->icy_metaint > 0) {
1340         size = store_icy(h, size);
1341         if (size < 0)
1342             return size;
1343     }
1344
1345     size = http_read_stream(h, buf, size);
1346     if (size > 0)
1347         s->icy_data_read += size;
1348     return size;
1349 }
1350
1351 /* used only when posting data */
1352 static int http_write(URLContext *h, const uint8_t *buf, int size)
1353 {
1354     char temp[11] = "";  /* 32-bit hex + CRLF + nul */
1355     int ret;
1356     char crlf[] = "\r\n";
1357     HTTPContext *s = h->priv_data;
1358
1359     if (!s->chunked_post) {
1360         /* non-chunked data is sent without any special encoding */
1361         return ffurl_write(s->hd, buf, size);
1362     }
1363
1364     /* silently ignore zero-size data since chunk encoding that would
1365      * signal EOF */
1366     if (size > 0) {
1367         /* upload data using chunked encoding */
1368         snprintf(temp, sizeof(temp), "%x\r\n", size);
1369
1370         if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
1371             (ret = ffurl_write(s->hd, buf, size)) < 0          ||
1372             (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
1373             return ret;
1374     }
1375     return size;
1376 }
1377
1378 static int http_shutdown(URLContext *h, int flags)
1379 {
1380     int ret = 0;
1381     char footer[] = "0\r\n\r\n";
1382     HTTPContext *s = h->priv_data;
1383
1384     /* signal end of chunked encoding if used */
1385     if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
1386         ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
1387         ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
1388         ret = ret > 0 ? 0 : ret;
1389         s->end_chunked_post = 1;
1390     }
1391
1392     return ret;
1393 }
1394
1395 static int http_close(URLContext *h)
1396 {
1397     int ret = 0;
1398     HTTPContext *s = h->priv_data;
1399
1400 #if CONFIG_ZLIB
1401     inflateEnd(&s->inflate_stream);
1402     av_freep(&s->inflate_buffer);
1403 #endif /* CONFIG_ZLIB */
1404
1405     if (!s->end_chunked_post)
1406         /* Close the write direction by sending the end of chunked encoding. */
1407         ret = http_shutdown(h, h->flags);
1408
1409     if (s->hd)
1410         ffurl_closep(&s->hd);
1411     av_dict_free(&s->chained_options);
1412     return ret;
1413 }
1414
1415 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
1416 {
1417     HTTPContext *s = h->priv_data;
1418     URLContext *old_hd = s->hd;
1419     int64_t old_off = s->off;
1420     uint8_t old_buf[BUFFER_SIZE];
1421     int old_buf_size, ret;
1422     AVDictionary *options = NULL;
1423
1424     if (whence == AVSEEK_SIZE)
1425         return s->filesize;
1426     else if (!force_reconnect &&
1427              ((whence == SEEK_CUR && off == 0) ||
1428               (whence == SEEK_SET && off == s->off)))
1429         return s->off;
1430     else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
1431         return AVERROR(ENOSYS);
1432
1433     if (whence == SEEK_CUR)
1434         off += s->off;
1435     else if (whence == SEEK_END)
1436         off += s->filesize;
1437     else if (whence != SEEK_SET)
1438         return AVERROR(EINVAL);
1439     if (off < 0)
1440         return AVERROR(EINVAL);
1441     s->off = off;
1442
1443     /* we save the old context in case the seek fails */
1444     old_buf_size = s->buf_end - s->buf_ptr;
1445     memcpy(old_buf, s->buf_ptr, old_buf_size);
1446     s->hd = NULL;
1447
1448     /* if it fails, continue on old connection */
1449     if ((ret = http_open_cnx(h, &options)) < 0) {
1450         av_dict_free(&options);
1451         memcpy(s->buffer, old_buf, old_buf_size);
1452         s->buf_ptr = s->buffer;
1453         s->buf_end = s->buffer + old_buf_size;
1454         s->hd      = old_hd;
1455         s->off     = old_off;
1456         return ret;
1457     }
1458     av_dict_free(&options);
1459     ffurl_close(old_hd);
1460     return off;
1461 }
1462
1463 static int64_t http_seek(URLContext *h, int64_t off, int whence)
1464 {
1465     return http_seek_internal(h, off, whence, 0);
1466 }
1467
1468 static int http_get_file_handle(URLContext *h)
1469 {
1470     HTTPContext *s = h->priv_data;
1471     return ffurl_get_file_handle(s->hd);
1472 }
1473
1474 #define HTTP_CLASS(flavor)                          \
1475 static const AVClass flavor ## _context_class = {   \
1476     .class_name = # flavor,                         \
1477     .item_name  = av_default_item_name,             \
1478     .option     = options,                          \
1479     .version    = LIBAVUTIL_VERSION_INT,            \
1480 }
1481
1482 #if CONFIG_HTTP_PROTOCOL
1483 HTTP_CLASS(http);
1484
1485 URLProtocol ff_http_protocol = {
1486     .name                = "http",
1487     .url_open2           = http_open,
1488     .url_accept          = http_accept,
1489     .url_handshake       = http_handshake,
1490     .url_read            = http_read,
1491     .url_write           = http_write,
1492     .url_seek            = http_seek,
1493     .url_close           = http_close,
1494     .url_get_file_handle = http_get_file_handle,
1495     .url_shutdown        = http_shutdown,
1496     .priv_data_size      = sizeof(HTTPContext),
1497     .priv_data_class     = &http_context_class,
1498     .flags               = URL_PROTOCOL_FLAG_NETWORK,
1499 };
1500 #endif /* CONFIG_HTTP_PROTOCOL */
1501
1502 #if CONFIG_HTTPS_PROTOCOL
1503 HTTP_CLASS(https);
1504
1505 URLProtocol ff_https_protocol = {
1506     .name                = "https",
1507     .url_open2           = http_open,
1508     .url_read            = http_read,
1509     .url_write           = http_write,
1510     .url_seek            = http_seek,
1511     .url_close           = http_close,
1512     .url_get_file_handle = http_get_file_handle,
1513     .url_shutdown        = http_shutdown,
1514     .priv_data_size      = sizeof(HTTPContext),
1515     .priv_data_class     = &https_context_class,
1516     .flags               = URL_PROTOCOL_FLAG_NETWORK,
1517 };
1518 #endif /* CONFIG_HTTPS_PROTOCOL */
1519
1520 #if CONFIG_HTTPPROXY_PROTOCOL
1521 static int http_proxy_close(URLContext *h)
1522 {
1523     HTTPContext *s = h->priv_data;
1524     if (s->hd)
1525         ffurl_closep(&s->hd);
1526     return 0;
1527 }
1528
1529 static int http_proxy_open(URLContext *h, const char *uri, int flags)
1530 {
1531     HTTPContext *s = h->priv_data;
1532     char hostname[1024], hoststr[1024];
1533     char auth[1024], pathbuf[1024], *path;
1534     char lower_url[100];
1535     int port, ret = 0, attempts = 0;
1536     HTTPAuthType cur_auth_type;
1537     char *authstr;
1538     int new_loc;
1539
1540     if( s->seekable == 1 )
1541         h->is_streamed = 0;
1542     else
1543         h->is_streamed = 1;
1544
1545     av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
1546                  pathbuf, sizeof(pathbuf), uri);
1547     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1548     path = pathbuf;
1549     if (*path == '/')
1550         path++;
1551
1552     ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
1553                 NULL);
1554 redo:
1555     ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
1556                      &h->interrupt_callback, NULL);
1557     if (ret < 0)
1558         return ret;
1559
1560     authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
1561                                            path, "CONNECT");
1562     snprintf(s->buffer, sizeof(s->buffer),
1563              "CONNECT %s HTTP/1.1\r\n"
1564              "Host: %s\r\n"
1565              "Connection: close\r\n"
1566              "%s%s"
1567              "\r\n",
1568              path,
1569              hoststr,
1570              authstr ? "Proxy-" : "", authstr ? authstr : "");
1571     av_freep(&authstr);
1572
1573     if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1574         goto fail;
1575
1576     s->buf_ptr    = s->buffer;
1577     s->buf_end    = s->buffer;
1578     s->line_count = 0;
1579     s->filesize   = -1;
1580     cur_auth_type = s->proxy_auth_state.auth_type;
1581
1582     /* Note: This uses buffering, potentially reading more than the
1583      * HTTP header. If tunneling a protocol where the server starts
1584      * the conversation, we might buffer part of that here, too.
1585      * Reading that requires using the proper ffurl_read() function
1586      * on this URLContext, not using the fd directly (as the tls
1587      * protocol does). This shouldn't be an issue for tls though,
1588      * since the client starts the conversation there, so there
1589      * is no extra data that we might buffer up here.
1590      */
1591     ret = http_read_header(h, &new_loc);
1592     if (ret < 0)
1593         goto fail;
1594
1595     attempts++;
1596     if (s->http_code == 407 &&
1597         (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
1598         s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
1599         ffurl_closep(&s->hd);
1600         goto redo;
1601     }
1602
1603     if (s->http_code < 400)
1604         return 0;
1605     ret = ff_http_averror(s->http_code, AVERROR(EIO));
1606
1607 fail:
1608     http_proxy_close(h);
1609     return ret;
1610 }
1611
1612 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
1613 {
1614     HTTPContext *s = h->priv_data;
1615     return ffurl_write(s->hd, buf, size);
1616 }
1617
1618 URLProtocol ff_httpproxy_protocol = {
1619     .name                = "httpproxy",
1620     .url_open            = http_proxy_open,
1621     .url_read            = http_buf_read,
1622     .url_write           = http_proxy_write,
1623     .url_close           = http_proxy_close,
1624     .url_get_file_handle = http_get_file_handle,
1625     .priv_data_size      = sizeof(HTTPContext),
1626     .flags               = URL_PROTOCOL_FLAG_NETWORK,
1627 };
1628 #endif /* CONFIG_HTTPPROXY_PROTOCOL */