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