http: Don't add a Range: bytes=0- header for POST
[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 int chunked_post;
53 } HTTPContext;
54
55 #define OFFSET(x) offsetof(HTTPContext, x)
56 #define D AV_OPT_FLAG_DECODING_PARAM
57 #define E AV_OPT_FLAG_ENCODING_PARAM
58 static const AVOption options[] = {
59 {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1, E },
60 {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
61 {NULL}
62 };
63 #define HTTP_CLASS(flavor)\
64 static const AVClass flavor ## _context_class = {\
65 .class_name = #flavor,\
66 .item_name = av_default_item_name,\
67 .option = options,\
68 .version = LIBAVUTIL_VERSION_INT,\
69 };
70
71 HTTP_CLASS(http);
72 HTTP_CLASS(https);
73
74 static int http_connect(URLContext *h, const char *path, const char *hoststr,
75 const char *auth, int *new_location);
76
77 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
78 {
79 memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
80 &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
81 }
82
83 /* return non zero if error */
84 static int http_open_cnx(URLContext *h)
85 {
86 const char *path, *proxy_path, *lower_proto = "tcp";
87 char hostname[1024], hoststr[1024], proto[10];
88 char auth[1024];
89 char path1[1024];
90 char buf[1024];
91 int port, use_proxy, err, location_changed = 0, redirects = 0;
92 HTTPAuthType cur_auth_type;
93 HTTPContext *s = h->priv_data;
94 URLContext *hd = NULL;
95
96 proxy_path = getenv("http_proxy");
97 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
98 av_strstart(proxy_path, "http://", NULL);
99
100 /* fill the dest addr */
101 redo:
102 /* needed in any case to build the host string */
103 av_url_split(proto, sizeof(proto), auth, sizeof(auth),
104 hostname, sizeof(hostname), &port,
105 path1, sizeof(path1), s->location);
106 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
107
108 if (use_proxy) {
109 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
110 NULL, 0, proxy_path);
111 path = s->location;
112 } else {
113 if (path1[0] == '\0')
114 path = "/";
115 else
116 path = path1;
117 }
118 if (!strcmp(proto, "https")) {
119 lower_proto = "tls";
120 if (port < 0)
121 port = 443;
122 }
123 if (port < 0)
124 port = 80;
125
126 ff_url_join(buf, sizeof(buf), lower_proto, 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 if (s->headers) {
169 int len = strlen(s->headers);
170 if (len < 2 || strcmp("\r\n", s->headers + len - 2))
171 av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
172 }
173
174 return http_open_cnx(h);
175 }
176 static int http_getc(HTTPContext *s)
177 {
178 int len;
179 if (s->buf_ptr >= s->buf_end) {
180 len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
181 if (len < 0) {
182 return AVERROR(EIO);
183 } else if (len == 0) {
184 return -1;
185 } else {
186 s->buf_ptr = s->buffer;
187 s->buf_end = s->buffer + len;
188 }
189 }
190 return *s->buf_ptr++;
191 }
192
193 static int http_get_line(HTTPContext *s, char *line, int line_size)
194 {
195 int ch;
196 char *q;
197
198 q = line;
199 for(;;) {
200 ch = http_getc(s);
201 if (ch < 0)
202 return AVERROR(EIO);
203 if (ch == '\n') {
204 /* process line */
205 if (q > line && q[-1] == '\r')
206 q--;
207 *q = '\0';
208
209 return 0;
210 } else {
211 if ((q - line) < line_size - 1)
212 *q++ = ch;
213 }
214 }
215 }
216
217 static int process_line(URLContext *h, char *line, int line_count,
218 int *new_location)
219 {
220 HTTPContext *s = h->priv_data;
221 char *tag, *p, *end;
222
223 /* end of header */
224 if (line[0] == '\0')
225 return 0;
226
227 p = line;
228 if (line_count == 0) {
229 while (!isspace(*p) && *p != '\0')
230 p++;
231 while (isspace(*p))
232 p++;
233 s->http_code = strtol(p, &end, 10);
234
235 av_dlog(NULL, "http_code=%d\n", s->http_code);
236
237 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
238 * don't abort until all headers have been parsed. */
239 if (s->http_code >= 400 && s->http_code < 600 && s->http_code != 401) {
240 end += strspn(end, SPACE_CHARS);
241 av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
242 s->http_code, end);
243 return -1;
244 }
245 } else {
246 while (*p != '\0' && *p != ':')
247 p++;
248 if (*p != ':')
249 return 1;
250
251 *p = '\0';
252 tag = line;
253 p++;
254 while (isspace(*p))
255 p++;
256 if (!av_strcasecmp(tag, "Location")) {
257 strcpy(s->location, p);
258 *new_location = 1;
259 } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
260 s->filesize = atoll(p);
261 } else if (!av_strcasecmp (tag, "Content-Range")) {
262 /* "bytes $from-$to/$document_size" */
263 const char *slash;
264 if (!strncmp (p, "bytes ", 6)) {
265 p += 6;
266 s->off = atoll(p);
267 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
268 s->filesize = atoll(slash+1);
269 }
270 h->is_streamed = 0; /* we _can_ in fact seek */
271 } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5)) {
272 h->is_streamed = 0;
273 } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
274 s->filesize = -1;
275 s->chunksize = 0;
276 } else if (!av_strcasecmp (tag, "WWW-Authenticate")) {
277 ff_http_auth_handle_header(&s->auth_state, tag, p);
278 } else if (!av_strcasecmp (tag, "Authentication-Info")) {
279 ff_http_auth_handle_header(&s->auth_state, tag, p);
280 } else if (!av_strcasecmp (tag, "Connection")) {
281 if (!strcmp(p, "close"))
282 s->willclose = 1;
283 }
284 }
285 return 1;
286 }
287
288 static inline int has_header(const char *str, const char *header)
289 {
290 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
291 if (!str)
292 return 0;
293 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
294 }
295
296 static int http_connect(URLContext *h, const char *path, const char *hoststr,
297 const char *auth, int *new_location)
298 {
299 HTTPContext *s = h->priv_data;
300 int post, err;
301 char line[1024];
302 char headers[1024] = "";
303 char *authstr = NULL;
304 int64_t off = s->off;
305 int len = 0;
306
307
308 /* send http header */
309 post = h->flags & AVIO_FLAG_WRITE;
310 authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
311 post ? "POST" : "GET");
312
313 /* set default headers if needed */
314 if (!has_header(s->headers, "\r\nUser-Agent: "))
315 len += av_strlcatf(headers + len, sizeof(headers) - len,
316 "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
317 if (!has_header(s->headers, "\r\nAccept: "))
318 len += av_strlcpy(headers + len, "Accept: */*\r\n",
319 sizeof(headers) - len);
320 if (!has_header(s->headers, "\r\nRange: ") && !post)
321 len += av_strlcatf(headers + len, sizeof(headers) - len,
322 "Range: bytes=%"PRId64"-\r\n", s->off);
323 if (!has_header(s->headers, "\r\nConnection: "))
324 len += av_strlcpy(headers + len, "Connection: close\r\n",
325 sizeof(headers)-len);
326 if (!has_header(s->headers, "\r\nHost: "))
327 len += av_strlcatf(headers + len, sizeof(headers) - len,
328 "Host: %s\r\n", hoststr);
329
330 /* now add in custom headers */
331 if (s->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->chunked_post ? "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->chunked_post) {
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->chunked_post) {
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 = &http_context_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 = &https_context_class,
542 };
543 #endif