]> git.sesse.net Git - ffmpeg/blob - libavformat/http.c
7d59152409ba19fe8699d5db167a444c6c0eb25a
[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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 <unistd.h>
25 #include <strings.h>
26 #include "internal.h"
27 #include "network.h"
28 #include "http.h"
29 #include "os_support.h"
30 #include "httpauth.h"
31 #include "url.h"
32 #include "libavutil/opt.h"
33
34 /* XXX: POST protocol is not completely implemented because ffmpeg uses
35    only a subset of it. */
36
37 /* used for protocol handling */
38 #define BUFFER_SIZE 1024
39 #define MAX_REDIRECTS 8
40
41 typedef struct {
42     const AVClass *class;
43     URLContext *hd;
44     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
45     int line_count;
46     int http_code;
47     int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
48     int64_t off, filesize;
49     char location[MAX_URL_SIZE];
50     HTTPAuthState auth_state;
51     unsigned char headers[BUFFER_SIZE];
52     int willclose;          /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
53 } HTTPContext;
54
55 #define OFFSET(x) offsetof(HTTPContext, x)
56 static const AVOption options[] = {
57 {"chunksize", "use chunked transfer-encoding for posts, -1 disables it, 0 enables it", OFFSET(chunksize), FF_OPT_TYPE_INT64, 0, -1, 0 }, /* Default to 0, for chunked POSTs */
58 {NULL}
59 };
60 static const AVClass httpcontext_class = {
61     "HTTP", av_default_item_name, options, LIBAVUTIL_VERSION_INT
62 };
63
64 static int http_connect(URLContext *h, const char *path, const char *hoststr,
65                         const char *auth, int *new_location);
66
67 void ff_http_set_headers(URLContext *h, const char *headers)
68 {
69     HTTPContext *s = h->priv_data;
70     int len = strlen(headers);
71
72     if (len && strcmp("\r\n", headers + len - 2))
73         av_log(h, AV_LOG_ERROR, "No trailing CRLF found in HTTP header.\n");
74
75     av_strlcpy(s->headers, headers, sizeof(s->headers));
76 }
77
78 void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked)
79 {
80     ((HTTPContext*)h->priv_data)->chunksize = is_chunked ? 0 : -1;
81 }
82
83 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
84 {
85     memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
86            &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
87 }
88
89 /* return non zero if error */
90 static int http_open_cnx(URLContext *h)
91 {
92     const char *path, *proxy_path;
93     char hostname[1024], hoststr[1024];
94     char auth[1024];
95     char path1[1024];
96     char buf[1024];
97     int port, use_proxy, err, location_changed = 0, redirects = 0;
98     HTTPAuthType cur_auth_type;
99     HTTPContext *s = h->priv_data;
100     URLContext *hd = NULL;
101
102     proxy_path = getenv("http_proxy");
103     use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
104         av_strstart(proxy_path, "http://", NULL);
105
106     /* fill the dest addr */
107  redo:
108     /* needed in any case to build the host string */
109     av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
110                  path1, sizeof(path1), s->location);
111     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
112
113     if (use_proxy) {
114         av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
115                      NULL, 0, proxy_path);
116         path = s->location;
117     } else {
118         if (path1[0] == '\0')
119             path = "/";
120         else
121             path = path1;
122     }
123     if (port < 0)
124         port = 80;
125
126     ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
127     err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE);
128     if (err < 0)
129         goto fail;
130
131     s->hd = hd;
132     cur_auth_type = s->auth_state.auth_type;
133     if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
134         goto fail;
135     if (s->http_code == 401) {
136         if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
137             ffurl_close(hd);
138             goto redo;
139         } else
140             goto fail;
141     }
142     if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
143         && location_changed == 1) {
144         /* url moved, get next */
145         ffurl_close(hd);
146         if (redirects++ >= MAX_REDIRECTS)
147             return AVERROR(EIO);
148         location_changed = 0;
149         goto redo;
150     }
151     return 0;
152  fail:
153     if (hd)
154         ffurl_close(hd);
155     s->hd = NULL;
156     return AVERROR(EIO);
157 }
158
159 static int http_open(URLContext *h, const char *uri, int flags)
160 {
161     HTTPContext *s = h->priv_data;
162
163     h->is_streamed = 1;
164
165     s->filesize = -1;
166     av_strlcpy(s->location, uri, sizeof(s->location));
167
168     return http_open_cnx(h);
169 }
170 static int http_getc(HTTPContext *s)
171 {
172     int len;
173     if (s->buf_ptr >= s->buf_end) {
174         len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
175         if (len < 0) {
176             return AVERROR(EIO);
177         } else if (len == 0) {
178             return -1;
179         } else {
180             s->buf_ptr = s->buffer;
181             s->buf_end = s->buffer + len;
182         }
183     }
184     return *s->buf_ptr++;
185 }
186
187 static int http_get_line(HTTPContext *s, char *line, int line_size)
188 {
189     int ch;
190     char *q;
191
192     q = line;
193     for(;;) {
194         ch = http_getc(s);
195         if (ch < 0)
196             return AVERROR(EIO);
197         if (ch == '\n') {
198             /* process line */
199             if (q > line && q[-1] == '\r')
200                 q--;
201             *q = '\0';
202
203             return 0;
204         } else {
205             if ((q - line) < line_size - 1)
206                 *q++ = ch;
207         }
208     }
209 }
210
211 static int process_line(URLContext *h, char *line, int line_count,
212                         int *new_location)
213 {
214     HTTPContext *s = h->priv_data;
215     char *tag, *p, *end;
216
217     /* end of header */
218     if (line[0] == '\0')
219         return 0;
220
221     p = line;
222     if (line_count == 0) {
223         while (!isspace(*p) && *p != '\0')
224             p++;
225         while (isspace(*p))
226             p++;
227         s->http_code = strtol(p, &end, 10);
228
229         av_dlog(NULL, "http_code=%d\n", s->http_code);
230
231         /* error codes are 4xx and 5xx, but regard 401 as a success, so we
232          * don't abort until all headers have been parsed. */
233         if (s->http_code >= 400 && s->http_code < 600 && s->http_code != 401) {
234             end += strspn(end, SPACE_CHARS);
235             av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
236                    s->http_code, end);
237             return -1;
238         }
239     } else {
240         while (*p != '\0' && *p != ':')
241             p++;
242         if (*p != ':')
243             return 1;
244
245         *p = '\0';
246         tag = line;
247         p++;
248         while (isspace(*p))
249             p++;
250         if (!strcasecmp(tag, "Location")) {
251             strcpy(s->location, p);
252             *new_location = 1;
253         } else if (!strcasecmp (tag, "Content-Length") && s->filesize == -1) {
254             s->filesize = atoll(p);
255         } else if (!strcasecmp (tag, "Content-Range")) {
256             /* "bytes $from-$to/$document_size" */
257             const char *slash;
258             if (!strncmp (p, "bytes ", 6)) {
259                 p += 6;
260                 s->off = atoll(p);
261                 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
262                     s->filesize = atoll(slash+1);
263             }
264             h->is_streamed = 0; /* we _can_ in fact seek */
265         } else if (!strcasecmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
266             s->filesize = -1;
267             s->chunksize = 0;
268         } else if (!strcasecmp (tag, "WWW-Authenticate")) {
269             ff_http_auth_handle_header(&s->auth_state, tag, p);
270         } else if (!strcasecmp (tag, "Authentication-Info")) {
271             ff_http_auth_handle_header(&s->auth_state, tag, p);
272         } else if (!strcasecmp (tag, "Connection")) {
273             if (!strcmp(p, "close"))
274                 s->willclose = 1;
275         }
276     }
277     return 1;
278 }
279
280 static inline int has_header(const char *str, const char *header)
281 {
282     /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
283     return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
284 }
285
286 static int http_connect(URLContext *h, const char *path, const char *hoststr,
287                         const char *auth, int *new_location)
288 {
289     HTTPContext *s = h->priv_data;
290     int post, err;
291     char line[1024];
292     char headers[1024] = "";
293     char *authstr = NULL;
294     int64_t off = s->off;
295     int len = 0;
296
297
298     /* send http header */
299     post = h->flags & AVIO_FLAG_WRITE;
300     authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
301                                         post ? "POST" : "GET");
302
303     /* set default headers if needed */
304     if (!has_header(s->headers, "\r\nUser-Agent: "))
305        len += av_strlcatf(headers + len, sizeof(headers) - len,
306                           "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
307     if (!has_header(s->headers, "\r\nAccept: "))
308         len += av_strlcpy(headers + len, "Accept: */*\r\n",
309                           sizeof(headers) - len);
310     if (!has_header(s->headers, "\r\nRange: "))
311         len += av_strlcatf(headers + len, sizeof(headers) - len,
312                            "Range: bytes=%"PRId64"-\r\n", s->off);
313     if (!has_header(s->headers, "\r\nConnection: "))
314         len += av_strlcpy(headers + len, "Connection: close\r\n",
315                           sizeof(headers)-len);
316     if (!has_header(s->headers, "\r\nHost: "))
317         len += av_strlcatf(headers + len, sizeof(headers) - len,
318                            "Host: %s\r\n", hoststr);
319
320     /* now add in custom headers */
321     av_strlcpy(headers+len, s->headers, sizeof(headers)-len);
322
323     snprintf(s->buffer, sizeof(s->buffer),
324              "%s %s HTTP/1.1\r\n"
325              "%s"
326              "%s"
327              "%s"
328              "\r\n",
329              post ? "POST" : "GET",
330              path,
331              post && s->chunksize >= 0 ? "Transfer-Encoding: chunked\r\n" : "",
332              headers,
333              authstr ? authstr : "");
334
335     av_freep(&authstr);
336     if (ffurl_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
337         return AVERROR(EIO);
338
339     /* init input buffer */
340     s->buf_ptr = s->buffer;
341     s->buf_end = s->buffer;
342     s->line_count = 0;
343     s->off = 0;
344     s->filesize = -1;
345     s->willclose = 0;
346     if (post) {
347         /* Pretend that it did work. We didn't read any header yet, since
348          * we've still to send the POST data, but the code calling this
349          * function will check http_code after we return. */
350         s->http_code = 200;
351         return 0;
352     }
353     s->chunksize = -1;
354
355     /* wait for header */
356     for(;;) {
357         if (http_get_line(s, line, sizeof(line)) < 0)
358             return AVERROR(EIO);
359
360         av_dlog(NULL, "header='%s'\n", line);
361
362         err = process_line(h, line, s->line_count, new_location);
363         if (err < 0)
364             return err;
365         if (err == 0)
366             break;
367         s->line_count++;
368     }
369
370     return (off == s->off) ? 0 : -1;
371 }
372
373
374 static int http_read(URLContext *h, uint8_t *buf, int size)
375 {
376     HTTPContext *s = h->priv_data;
377     int len;
378
379     if (s->chunksize >= 0) {
380         if (!s->chunksize) {
381             char line[32];
382
383             for(;;) {
384                 do {
385                     if (http_get_line(s, line, sizeof(line)) < 0)
386                         return AVERROR(EIO);
387                 } while (!*line);    /* skip CR LF from last chunk */
388
389                 s->chunksize = strtoll(line, NULL, 16);
390
391                 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
392
393                 if (!s->chunksize)
394                     return 0;
395                 break;
396             }
397         }
398         size = FFMIN(size, s->chunksize);
399     }
400     /* read bytes from input buffer first */
401     len = s->buf_end - s->buf_ptr;
402     if (len > 0) {
403         if (len > size)
404             len = size;
405         memcpy(buf, s->buf_ptr, len);
406         s->buf_ptr += len;
407     } else {
408         if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
409             return AVERROR_EOF;
410         len = ffurl_read(s->hd, buf, size);
411     }
412     if (len > 0) {
413         s->off += len;
414         if (s->chunksize > 0)
415             s->chunksize -= len;
416     }
417     return len;
418 }
419
420 /* used only when posting data */
421 static int http_write(URLContext *h, const uint8_t *buf, int size)
422 {
423     char temp[11] = "";  /* 32-bit hex + CRLF + nul */
424     int ret;
425     char crlf[] = "\r\n";
426     HTTPContext *s = h->priv_data;
427
428     if (s->chunksize == -1) {
429         /* non-chunked data is sent without any special encoding */
430         return ffurl_write(s->hd, buf, size);
431     }
432
433     /* silently ignore zero-size data since chunk encoding that would
434      * signal EOF */
435     if (size > 0) {
436         /* upload data using chunked encoding */
437         snprintf(temp, sizeof(temp), "%x\r\n", size);
438
439         if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
440             (ret = ffurl_write(s->hd, buf, size)) < 0 ||
441             (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
442             return ret;
443     }
444     return size;
445 }
446
447 static int http_close(URLContext *h)
448 {
449     int ret = 0;
450     char footer[] = "0\r\n\r\n";
451     HTTPContext *s = h->priv_data;
452
453     /* signal end of chunked encoding if used */
454     if ((h->flags & AVIO_FLAG_WRITE) && s->chunksize != -1) {
455         ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
456         ret = ret > 0 ? 0 : ret;
457     }
458
459     if (s->hd)
460         ffurl_close(s->hd);
461     return ret;
462 }
463
464 static int64_t http_seek(URLContext *h, int64_t off, int whence)
465 {
466     HTTPContext *s = h->priv_data;
467     URLContext *old_hd = s->hd;
468     int64_t old_off = s->off;
469     uint8_t old_buf[BUFFER_SIZE];
470     int old_buf_size;
471
472     if (whence == AVSEEK_SIZE)
473         return s->filesize;
474     else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
475         return -1;
476
477     /* we save the old context in case the seek fails */
478     old_buf_size = s->buf_end - s->buf_ptr;
479     memcpy(old_buf, s->buf_ptr, old_buf_size);
480     s->hd = NULL;
481     if (whence == SEEK_CUR)
482         off += s->off;
483     else if (whence == SEEK_END)
484         off += s->filesize;
485     s->off = off;
486
487     /* if it fails, continue on old connection */
488     if (http_open_cnx(h) < 0) {
489         memcpy(s->buffer, old_buf, old_buf_size);
490         s->buf_ptr = s->buffer;
491         s->buf_end = s->buffer + old_buf_size;
492         s->hd = old_hd;
493         s->off = old_off;
494         return -1;
495     }
496     ffurl_close(old_hd);
497     return off;
498 }
499
500 static int
501 http_get_file_handle(URLContext *h)
502 {
503     HTTPContext *s = h->priv_data;
504     return ffurl_get_file_handle(s->hd);
505 }
506
507 URLProtocol ff_http_protocol = {
508     .name                = "http",
509     .url_open            = http_open,
510     .url_read            = http_read,
511     .url_write           = http_write,
512     .url_seek            = http_seek,
513     .url_close           = http_close,
514     .url_get_file_handle = http_get_file_handle,
515     .priv_data_size      = sizeof(HTTPContext),
516     .priv_data_class     = &httpcontext_class,
517 };