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/base64.h"
23 #include "libavutil/avstring.h"
28 #include "os_support.h"
30 /* XXX: POST protocol is not completely implemented because ffmpeg uses
31 only a subset of it. */
33 /* used for protocol handling */
34 #define BUFFER_SIZE 1024
36 #define MAX_REDIRECTS 8
40 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
43 int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
44 int64_t off, filesize;
45 char location[URL_SIZE];
48 static int http_connect(URLContext *h, const char *path, const char *hoststr,
49 const char *auth, int *new_location);
50 static int http_write(URLContext *h, uint8_t *buf, int size);
53 /* return non zero if error */
54 static int http_open_cnx(URLContext *h)
56 const char *path, *proxy_path;
57 char hostname[1024], hoststr[1024];
61 int port, use_proxy, err, location_changed = 0, redirects = 0;
62 HTTPContext *s = h->priv_data;
63 URLContext *hd = NULL;
65 proxy_path = getenv("http_proxy");
66 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
67 av_strstart(proxy_path, "http://", NULL);
69 /* fill the dest addr */
71 /* needed in any case to build the host string */
72 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
73 path1, sizeof(path1), s->location);
75 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
77 av_strlcpy(hoststr, hostname, sizeof(hoststr));
81 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
93 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
94 err = url_open(&hd, buf, URL_RDWR);
99 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
101 if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
102 /* url moved, get next */
104 if (redirects++ >= MAX_REDIRECTS)
106 location_changed = 0;
116 static int http_open(URLContext *h, const char *uri, int flags)
123 s = av_malloc(sizeof(HTTPContext));
125 return AVERROR(ENOMEM);
131 av_strlcpy(s->location, uri, URL_SIZE);
133 ret = http_open_cnx(h);
138 static int http_getc(HTTPContext *s)
141 if (s->buf_ptr >= s->buf_end) {
142 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
145 } else if (len == 0) {
148 s->buf_ptr = s->buffer;
149 s->buf_end = s->buffer + len;
152 return *s->buf_ptr++;
155 static int http_get_line(HTTPContext *s, char *line, int line_size)
167 if (q > line && q[-1] == '\r')
173 if ((q - line) < line_size - 1)
179 static int process_line(URLContext *h, char *line, int line_count,
182 HTTPContext *s = h->priv_data;
190 if (line_count == 0) {
191 while (!isspace(*p) && *p != '\0')
195 s->http_code = strtol(p, NULL, 10);
197 dprintf(NULL, "http_code=%d\n", s->http_code);
199 /* error codes are 4xx and 5xx */
200 if (s->http_code >= 400 && s->http_code < 600)
203 while (*p != '\0' && *p != ':')
213 if (!strcmp(tag, "Location")) {
214 strcpy(s->location, p);
216 } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
217 s->filesize = atoll(p);
218 } else if (!strcmp (tag, "Content-Range")) {
219 /* "bytes $from-$to/$document_size" */
221 if (!strncmp (p, "bytes ", 6)) {
224 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
225 s->filesize = atoll(slash+1);
227 h->is_streamed = 0; /* we _can_ in fact seek */
228 } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
236 static int http_connect(URLContext *h, const char *path, const char *hoststr,
237 const char *auth, int *new_location)
239 HTTPContext *s = h->priv_data;
243 int auth_b64_len = (strlen(auth) + 2) / 3 * 4 + 1;
244 int64_t off = s->off;
247 /* send http header */
248 post = h->flags & URL_WRONLY;
249 auth_b64 = av_malloc(auth_b64_len);
250 av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth));
251 snprintf(s->buffer, sizeof(s->buffer),
255 "Range: bytes=%"PRId64"-\r\n"
257 "Authorization: Basic %s\r\n"
258 "Connection: close\r\n"
261 post ? "POST" : "GET",
267 post ? "Transfer-Encoding: chunked\r\n" : "");
270 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
273 /* init input buffer */
274 s->buf_ptr = s->buffer;
275 s->buf_end = s->buffer;
280 /* always use chunked encoding for upload data */
285 /* wait for header */
287 if (http_get_line(s, line, sizeof(line)) < 0)
290 dprintf(NULL, "header='%s'\n", line);
292 err = process_line(h, line, s->line_count, new_location);
300 return (off == s->off) ? 0 : -1;
304 static int http_read(URLContext *h, uint8_t *buf, int size)
306 HTTPContext *s = h->priv_data;
309 if (s->chunksize >= 0) {
315 if (http_get_line(s, line, sizeof(line)) < 0)
317 } while (!*line); /* skip CR LF from last chunk */
319 s->chunksize = strtoll(line, NULL, 16);
321 dprintf(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
328 size = FFMIN(size, s->chunksize);
330 /* read bytes from input buffer first */
331 len = s->buf_end - s->buf_ptr;
335 memcpy(buf, s->buf_ptr, len);
338 len = url_read(s->hd, buf, size);
342 if (s->chunksize > 0)
348 /* used only when posting data */
349 static int http_write(URLContext *h, uint8_t *buf, int size)
351 char temp[11]; /* 32-bit hex + CRLF + nul */
353 char crlf[] = "\r\n";
354 HTTPContext *s = h->priv_data;
356 if (s->chunksize == -1) {
357 /* headers are sent without any special encoding */
358 return url_write(s->hd, buf, size);
361 /* silently ignore zero-size data since chunk encoding that would
364 /* upload data using chunked encoding */
365 snprintf(temp, sizeof(temp), "%x\r\n", size);
367 if ((ret = url_write(s->hd, temp, strlen(temp))) < 0 ||
368 (ret = url_write(s->hd, buf, size)) < 0 ||
369 (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
375 static int http_close(URLContext *h)
378 char footer[] = "0\r\n\r\n";
379 HTTPContext *s = h->priv_data;
381 /* signal end of chunked encoding if used */
382 if ((h->flags & URL_WRONLY) && s->chunksize != -1) {
383 ret = url_write(s->hd, footer, sizeof(footer) - 1);
384 ret = ret > 0 ? 0 : ret;
392 static int64_t http_seek(URLContext *h, int64_t off, int whence)
394 HTTPContext *s = h->priv_data;
395 URLContext *old_hd = s->hd;
396 int64_t old_off = s->off;
397 uint8_t old_buf[BUFFER_SIZE];
400 if (whence == AVSEEK_SIZE)
402 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
405 /* we save the old context in case the seek fails */
406 old_buf_size = s->buf_end - s->buf_ptr;
407 memcpy(old_buf, s->buf_ptr, old_buf_size);
409 if (whence == SEEK_CUR)
411 else if (whence == SEEK_END)
415 /* if it fails, continue on old connection */
416 if (http_open_cnx(h) < 0) {
417 memcpy(s->buffer, old_buf, old_buf_size);
418 s->buf_ptr = s->buffer;
419 s->buf_end = s->buffer + old_buf_size;
429 http_get_file_handle(URLContext *h)
431 HTTPContext *s = h->priv_data;
432 return url_get_file_handle(s->hd);
435 URLProtocol http_protocol = {
442 .url_get_file_handle = http_get_file_handle,