2 * HTTP protocol for avconv client
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/avstring.h"
28 #include "os_support.h"
31 #include "libavutil/opt.h"
33 /* XXX: POST protocol is not completely implemented because avconv uses
34 only a subset of it. */
36 /* used for protocol handling */
37 #define BUFFER_SIZE 1024
38 #define MAX_REDIRECTS 8
43 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
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;
51 int willclose; /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
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 },
63 #define HTTP_CLASS(flavor)\
64 static const AVClass flavor ## _context_class = {\
65 .class_name = #flavor,\
66 .item_name = av_default_item_name,\
68 .version = LIBAVUTIL_VERSION_INT,\
74 static int http_connect(URLContext *h, const char *path, const char *hoststr,
75 const char *auth, int *new_location);
77 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
79 memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
80 &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
83 /* return non zero if error */
84 static int http_open_cnx(URLContext *h)
86 const char *path, *proxy_path, *lower_proto = "tcp";
87 char hostname[1024], hoststr[1024], proto[10];
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;
96 proxy_path = getenv("http_proxy");
97 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
98 av_strstart(proxy_path, "http://", NULL);
100 /* fill the dest addr */
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);
109 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
110 NULL, 0, proxy_path);
113 if (path1[0] == '\0')
118 if (!strcmp(proto, "https")) {
126 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
127 err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE);
132 cur_auth_type = s->auth_state.auth_type;
133 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
135 if (s->http_code == 401) {
136 if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
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 */
146 if (redirects++ >= MAX_REDIRECTS)
148 location_changed = 0;
159 static int http_open(URLContext *h, const char *uri, int flags)
161 HTTPContext *s = h->priv_data;
166 av_strlcpy(s->location, uri, sizeof(s->location));
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");
174 return http_open_cnx(h);
176 static int http_getc(HTTPContext *s)
179 if (s->buf_ptr >= s->buf_end) {
180 len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
183 } else if (len == 0) {
186 s->buf_ptr = s->buffer;
187 s->buf_end = s->buffer + len;
190 return *s->buf_ptr++;
193 static int http_get_line(HTTPContext *s, char *line, int line_size)
205 if (q > line && q[-1] == '\r')
211 if ((q - line) < line_size - 1)
217 static int process_line(URLContext *h, char *line, int line_count,
220 HTTPContext *s = h->priv_data;
228 if (line_count == 0) {
229 while (!isspace(*p) && *p != '\0')
233 s->http_code = strtol(p, &end, 10);
235 av_dlog(NULL, "http_code=%d\n", s->http_code);
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",
246 while (*p != '\0' && *p != ':')
256 if (!av_strcasecmp(tag, "Location")) {
257 strcpy(s->location, p);
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" */
264 if (!strncmp (p, "bytes ", 6)) {
267 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
268 s->filesize = atoll(slash+1);
270 h->is_streamed = 0; /* we _can_ in fact seek */
271 } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5)) {
273 } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
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"))
288 static inline int has_header(const char *str, const char *header)
290 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
293 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
296 static int http_connect(URLContext *h, const char *path, const char *hoststr,
297 const char *auth, int *new_location)
299 HTTPContext *s = h->priv_data;
302 char headers[1024] = "";
303 char *authstr = NULL;
304 int64_t off = s->off;
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");
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);
330 /* now add in custom headers */
332 av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
334 snprintf(s->buffer, sizeof(s->buffer),
340 post ? "POST" : "GET",
342 post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
344 authstr ? authstr : "");
347 if (ffurl_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
350 /* init input buffer */
351 s->buf_ptr = s->buffer;
352 s->buf_end = s->buffer;
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. */
366 /* wait for header */
368 if (http_get_line(s, line, sizeof(line)) < 0)
371 av_dlog(NULL, "header='%s'\n", line);
373 err = process_line(h, line, s->line_count, new_location);
381 return (off == s->off) ? 0 : -1;
385 static int http_read(URLContext *h, uint8_t *buf, int size)
387 HTTPContext *s = h->priv_data;
390 if (s->chunksize >= 0) {
396 if (http_get_line(s, line, sizeof(line)) < 0)
398 } while (!*line); /* skip CR LF from last chunk */
400 s->chunksize = strtoll(line, NULL, 16);
402 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
409 size = FFMIN(size, s->chunksize);
411 /* read bytes from input buffer first */
412 len = s->buf_end - s->buf_ptr;
416 memcpy(buf, s->buf_ptr, len);
419 if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
421 len = ffurl_read(s->hd, buf, size);
425 if (s->chunksize > 0)
431 /* used only when posting data */
432 static int http_write(URLContext *h, const uint8_t *buf, int size)
434 char temp[11] = ""; /* 32-bit hex + CRLF + nul */
436 char crlf[] = "\r\n";
437 HTTPContext *s = h->priv_data;
439 if (!s->chunked_post) {
440 /* non-chunked data is sent without any special encoding */
441 return ffurl_write(s->hd, buf, size);
444 /* silently ignore zero-size data since chunk encoding that would
447 /* upload data using chunked encoding */
448 snprintf(temp, sizeof(temp), "%x\r\n", size);
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)
458 static int http_close(URLContext *h)
461 char footer[] = "0\r\n\r\n";
462 HTTPContext *s = h->priv_data;
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;
475 static int64_t http_seek(URLContext *h, int64_t off, int whence)
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];
483 if (whence == AVSEEK_SIZE)
485 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
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);
492 if (whence == SEEK_CUR)
494 else if (whence == SEEK_END)
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;
512 http_get_file_handle(URLContext *h)
514 HTTPContext *s = h->priv_data;
515 return ffurl_get_file_handle(s->hd);
518 #if CONFIG_HTTP_PROTOCOL
519 URLProtocol ff_http_protocol = {
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,
531 #if CONFIG_HTTPS_PROTOCOL
532 URLProtocol ff_https_protocol = {
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,