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