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