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. */
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
},
60 static const AVClass httpcontext_class
= {
62 .item_name
= av_default_item_name
,
64 .version
= LIBAVUTIL_VERSION_INT
,
67 static int http_connect(URLContext
*h
, const char *path
, const char *hoststr
,
68 const char *auth
, int *new_location
);
70 void ff_http_set_headers(URLContext
*h
, const char *headers
)
72 HTTPContext
*s
= h
->priv_data
;
74 av_freep(&s
->headers
);
75 s
->headers
= av_strdup(headers
);
78 void ff_http_init_auth_state(URLContext
*dest
, const URLContext
*src
)
80 memcpy(&((HTTPContext
*)dest
->priv_data
)->auth_state
,
81 &((HTTPContext
*)src
->priv_data
)->auth_state
, sizeof(HTTPAuthState
));
84 /* return non zero if error */
85 static int http_open_cnx(URLContext
*h
)
87 const char *path
, *proxy_path
, *lower_proto
= "tcp";
88 char hostname
[1024], hoststr
[1024], proto
[10];
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
;
97 proxy_path
= getenv("http_proxy");
98 use_proxy
= (proxy_path
!= NULL
) && !getenv("no_proxy") &&
99 av_strstart(proxy_path
, "http://", NULL
);
101 /* fill the dest addr */
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
);
110 av_url_split(NULL
, 0, auth
, sizeof(auth
), hostname
, sizeof(hostname
), &port
,
111 NULL
, 0, proxy_path
);
114 if (path1
[0] == '\0')
119 if (!strcmp(proto
, "https")) {
127 ff_url_join(buf
, sizeof(buf
), lower_proto
, NULL
, hostname
, port
, NULL
);
128 err
= ffurl_open(&hd
, buf
, AVIO_FLAG_READ_WRITE
);
133 cur_auth_type
= s
->auth_state
.auth_type
;
134 if (http_connect(h
, path
, hoststr
, auth
, &location_changed
) < 0)
136 if (s
->http_code
== 401) {
137 if (cur_auth_type
== HTTP_AUTH_NONE
&& s
->auth_state
.auth_type
!= HTTP_AUTH_NONE
) {
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 */
147 if (redirects
++ >= MAX_REDIRECTS
)
149 location_changed
= 0;
160 static int http_open(URLContext
*h
, const char *uri
, int flags
)
162 HTTPContext
*s
= h
->priv_data
;
167 av_strlcpy(s
->location
, uri
, sizeof(s
->location
));
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");
175 return http_open_cnx(h
);
177 static int http_getc(HTTPContext
*s
)
180 if (s
->buf_ptr
>= s
->buf_end
) {
181 len
= ffurl_read(s
->hd
, s
->buffer
, BUFFER_SIZE
);
184 } else if (len
== 0) {
187 s
->buf_ptr
= s
->buffer
;
188 s
->buf_end
= s
->buffer
+ len
;
191 return *s
->buf_ptr
++;
194 static int http_get_line(HTTPContext
*s
, char *line
, int line_size
)
206 if (q
> line
&& q
[-1] == '\r')
212 if ((q
- line
) < line_size
- 1)
218 static int process_line(URLContext
*h
, char *line
, int line_count
,
221 HTTPContext
*s
= h
->priv_data
;
229 if (line_count
== 0) {
230 while (!isspace(*p
) && *p
!= '\0')
234 s
->http_code
= strtol(p
, &end
, 10);
236 av_dlog(NULL
, "http_code=%d\n", s
->http_code
);
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",
247 while (*p
!= '\0' && *p
!= ':')
257 if (!av_strcasecmp(tag
, "Location")) {
258 strcpy(s
->location
, p
);
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" */
265 if (!strncmp (p
, "bytes ", 6)) {
268 if ((slash
= strchr(p
, '/')) && strlen(slash
) > 0)
269 s
->filesize
= atoll(slash
+1);
271 h
->is_streamed
= 0; /* we _can_ in fact seek */
272 } else if (!av_strcasecmp(tag
, "Accept-Ranges") && !strncmp(p
, "bytes", 5)) {
274 } else if (!av_strcasecmp (tag
, "Transfer-Encoding") && !av_strncasecmp(p
, "chunked", 7)) {
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"))
289 static inline int has_header(const char *str
, const char *header
)
291 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
294 return av_stristart(str
, header
+ 2, NULL
) || av_stristr(str
, header
);
297 static int http_connect(URLContext
*h
, const char *path
, const char *hoststr
,
298 const char *auth
, int *new_location
)
300 HTTPContext
*s
= h
->priv_data
;
303 char headers
[1024] = "";
304 char *authstr
= NULL
;
305 int64_t off
= s
->off
;
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");
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
);
331 /* now add in custom headers */
333 av_strlcpy(headers
+ len
, s
->headers
, sizeof(headers
) - len
);
335 snprintf(s
->buffer
, sizeof(s
->buffer
),
341 post ?
"POST" : "GET",
343 post
&& s
->chunksize
>= 0 ?
"Transfer-Encoding: chunked\r\n" : "",
345 authstr ? authstr
: "");
348 if (ffurl_write(s
->hd
, s
->buffer
, strlen(s
->buffer
)) < 0)
351 /* init input buffer */
352 s
->buf_ptr
= s
->buffer
;
353 s
->buf_end
= s
->buffer
;
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. */
367 /* wait for header */
369 if (http_get_line(s
, line
, sizeof(line
)) < 0)
372 av_dlog(NULL
, "header='%s'\n", line
);
374 err
= process_line(h
, line
, s
->line_count
, new_location
);
382 return (off
== s
->off
) ?
0 : -1;
386 static int http_read(URLContext
*h
, uint8_t *buf
, int size
)
388 HTTPContext
*s
= h
->priv_data
;
391 if (s
->chunksize
>= 0) {
397 if (http_get_line(s
, line
, sizeof(line
)) < 0)
399 } while (!*line
); /* skip CR LF from last chunk */
401 s
->chunksize
= strtoll(line
, NULL
, 16);
403 av_dlog(NULL
, "Chunked encoding data size: %"PRId64
"'\n", s
->chunksize
);
410 size
= FFMIN(size
, s
->chunksize
);
412 /* read bytes from input buffer first */
413 len
= s
->buf_end
- s
->buf_ptr
;
417 memcpy(buf
, s
->buf_ptr
, len
);
420 if (!s
->willclose
&& s
->filesize
>= 0 && s
->off
>= s
->filesize
)
422 len
= ffurl_read(s
->hd
, buf
, size
);
426 if (s
->chunksize
> 0)
432 /* used only when posting data */
433 static int http_write(URLContext
*h
, const uint8_t *buf
, int size
)
435 char temp
[11] = ""; /* 32-bit hex + CRLF + nul */
437 char crlf
[] = "\r\n";
438 HTTPContext
*s
= h
->priv_data
;
440 if (s
->chunksize
== -1) {
441 /* non-chunked data is sent without any special encoding */
442 return ffurl_write(s
->hd
, buf
, size
);
445 /* silently ignore zero-size data since chunk encoding that would
448 /* upload data using chunked encoding */
449 snprintf(temp
, sizeof(temp
), "%x\r\n", size
);
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)
459 static int http_close(URLContext
*h
)
462 char footer
[] = "0\r\n\r\n";
463 HTTPContext
*s
= h
->priv_data
;
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
;
476 static int64_t http_seek(URLContext
*h
, int64_t off
, int whence
)
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
];
484 if (whence
== AVSEEK_SIZE
)
486 else if ((s
->filesize
== -1 && whence
== SEEK_END
) || h
->is_streamed
)
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
);
493 if (whence
== SEEK_CUR
)
495 else if (whence
== SEEK_END
)
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
;
513 http_get_file_handle(URLContext
*h
)
515 HTTPContext
*s
= h
->priv_data
;
516 return ffurl_get_file_handle(s
->hd
);
519 #if CONFIG_HTTP_PROTOCOL
520 URLProtocol ff_http_protocol
= {
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
,
532 #if CONFIG_HTTPS_PROTOCOL
533 URLProtocol ff_https_protocol
= {
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
,