2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avstring.h"
28 #include "os_support.h"
31 /* XXX: POST protocol is not completely implemented because ffmpeg uses
32 only a subset of it. */
34 /* used for protocol handling */
35 #define BUFFER_SIZE 1024
37 #define MAX_REDIRECTS 8
41 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
44 int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
45 int64_t off, filesize;
46 char location[URL_SIZE];
47 HTTPAuthState auth_state;
51 static int http_connect(URLContext *h, const char *path, const char *hoststr,
52 const char *auth, int *new_location);
53 static int http_write(URLContext *h, const uint8_t *buf, int size);
56 /* return non zero if error */
57 static int http_open_cnx(URLContext *h)
59 const char *path, *proxy_path;
60 char hostname[1024], hoststr[1024];
64 int port, use_proxy, err, location_changed = 0, redirects = 0;
65 HTTPAuthType cur_auth_type;
66 HTTPContext *s = h->priv_data;
67 URLContext *hd = NULL;
70 proxy_path = getenv("http_proxy");
71 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
72 av_strstart(proxy_path, "http://", NULL);
74 /* fill the dest addr */
76 /* needed in any case to build the host string */
77 ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
78 path1, sizeof(path1), s->location);
79 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
82 ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
94 ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
95 err = url_open(&hd, buf, URL_RDWR);
100 cur_auth_type = s->auth_state.auth_type;
101 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
103 if (s->http_code == 401) {
104 if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
110 if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
111 /* url moved, get next */
113 if (redirects++ >= MAX_REDIRECTS)
115 location_changed = 0;
125 static int http_open(URLContext *h, const char *uri, int flags)
131 s = av_malloc(sizeof(HTTPContext));
133 return AVERROR(ENOMEM);
140 memset(&s->auth_state, 0, sizeof(s->auth_state));
141 av_strlcpy(s->location, uri, URL_SIZE);
145 static int http_getc(HTTPContext *s)
148 if (s->buf_ptr >= s->buf_end) {
149 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
152 } else if (len == 0) {
155 s->buf_ptr = s->buffer;
156 s->buf_end = s->buffer + len;
159 return *s->buf_ptr++;
162 static int http_get_line(HTTPContext *s, char *line, int line_size)
174 if (q > line && q[-1] == '\r')
180 if ((q - line) < line_size - 1)
186 static int process_line(URLContext *h, char *line, int line_count,
189 HTTPContext *s = h->priv_data;
197 if (line_count == 0) {
198 while (!isspace(*p) && *p != '\0')
202 s->http_code = strtol(p, NULL, 10);
204 dprintf(NULL, "http_code=%d\n", s->http_code);
206 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
207 * don't abort until all headers have been parsed. */
208 if (s->http_code >= 400 && s->http_code < 600 && s->http_code != 401)
211 while (*p != '\0' && *p != ':')
221 if (!strcmp(tag, "Location")) {
222 strcpy(s->location, p);
224 } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
225 s->filesize = atoll(p);
226 } else if (!strcmp (tag, "Content-Range")) {
227 /* "bytes $from-$to/$document_size" */
229 if (!strncmp (p, "bytes ", 6)) {
232 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
233 s->filesize = atoll(slash+1);
235 h->is_streamed = 0; /* we _can_ in fact seek */
236 } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
239 } else if (!strcmp (tag, "WWW-Authenticate")) {
240 ff_http_auth_handle_header(&s->auth_state, tag, p);
241 } else if (!strcmp (tag, "Authentication-Info")) {
242 ff_http_auth_handle_header(&s->auth_state, tag, p);
248 static int http_connect(URLContext *h, const char *path, const char *hoststr,
249 const char *auth, int *new_location)
251 HTTPContext *s = h->priv_data;
254 char *authstr = NULL;
255 int64_t off = s->off;
258 /* send http header */
259 post = h->flags & URL_WRONLY;
260 authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
261 post ? "POST" : "GET");
262 snprintf(s->buffer, sizeof(s->buffer),
266 "Range: bytes=%"PRId64"-\r\n"
269 "Connection: close\r\n"
272 post ? "POST" : "GET",
277 authstr ? authstr : "",
278 post ? "Transfer-Encoding: chunked\r\n" : "");
281 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
284 /* init input buffer */
285 s->buf_ptr = s->buffer;
286 s->buf_end = s->buffer;
291 /* always use chunked encoding for upload data */
293 /* Pretend that it did work. We didn't read any header yet, since
294 * we've still to send the POST data, but the code calling this
295 * function will check http_code after we return. */
300 /* wait for header */
302 if (http_get_line(s, line, sizeof(line)) < 0)
305 dprintf(NULL, "header='%s'\n", line);
307 err = process_line(h, line, s->line_count, new_location);
315 return (off == s->off) ? 0 : -1;
319 static int http_read(URLContext *h, uint8_t *buf, int size)
321 HTTPContext *s = h->priv_data;
325 int ret = http_open_cnx(h);
330 /* A size of zero can be used to force
331 * initializaton of the connection. */
335 if (s->chunksize >= 0) {
341 if (http_get_line(s, line, sizeof(line)) < 0)
343 } while (!*line); /* skip CR LF from last chunk */
345 s->chunksize = strtoll(line, NULL, 16);
347 dprintf(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
354 size = FFMIN(size, s->chunksize);
356 /* read bytes from input buffer first */
357 len = s->buf_end - s->buf_ptr;
361 memcpy(buf, s->buf_ptr, len);
364 len = url_read(s->hd, buf, size);
368 if (s->chunksize > 0)
374 /* used only when posting data */
375 static int http_write(URLContext *h, const uint8_t *buf, int size)
377 char temp[11]; /* 32-bit hex + CRLF + nul */
379 char crlf[] = "\r\n";
380 HTTPContext *s = h->priv_data;
383 int ret = http_open_cnx(h);
388 if (s->chunksize == -1) {
389 /* headers are sent without any special encoding */
390 return url_write(s->hd, buf, size);
393 /* silently ignore zero-size data since chunk encoding that would
396 /* upload data using chunked encoding */
397 snprintf(temp, sizeof(temp), "%x\r\n", size);
399 if ((ret = url_write(s->hd, temp, strlen(temp))) < 0 ||
400 (ret = url_write(s->hd, buf, size)) < 0 ||
401 (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
407 static int http_close(URLContext *h)
410 char footer[] = "0\r\n\r\n";
411 HTTPContext *s = h->priv_data;
413 /* signal end of chunked encoding if used */
414 if ((h->flags & URL_WRONLY) && s->chunksize != -1) {
415 ret = url_write(s->hd, footer, sizeof(footer) - 1);
416 ret = ret > 0 ? 0 : ret;
424 static int64_t http_seek(URLContext *h, int64_t off, int whence)
426 HTTPContext *s = h->priv_data;
427 URLContext *old_hd = s->hd;
428 int64_t old_off = s->off;
429 uint8_t old_buf[BUFFER_SIZE];
432 if (whence == AVSEEK_SIZE)
434 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
437 /* we save the old context in case the seek fails */
438 old_buf_size = s->buf_end - s->buf_ptr;
439 memcpy(old_buf, s->buf_ptr, old_buf_size);
441 if (whence == SEEK_CUR)
443 else if (whence == SEEK_END)
447 /* if it fails, continue on old connection */
448 if (http_open_cnx(h) < 0) {
449 memcpy(s->buffer, old_buf, old_buf_size);
450 s->buf_ptr = s->buffer;
451 s->buf_end = s->buffer + old_buf_size;
461 http_get_file_handle(URLContext *h)
463 HTTPContext *s = h->priv_data;
464 return url_get_file_handle(s->hd);
467 URLProtocol http_protocol = {
474 .url_get_file_handle = http_get_file_handle,