Split direct mode (macro)block decoding off h264.c.
[libav.git] / libavformat / http.c
CommitLineData
558b86a5
LB
1/*
2 * HTTP protocol for ffmpeg client
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
558b86a5
LB
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 */
245976da
DB
21
22#include "libavutil/base64.h"
23#include "libavutil/avstring.h"
558b86a5
LB
24#include "avformat.h"
25#include <unistd.h>
7995ed8e 26#include <strings.h>
558b86a5 27#include "network.h"
a5e979f4 28#include "os_support.h"
558b86a5 29
e42dba48
DB
30/* XXX: POST protocol is not completely implemented because ffmpeg uses
31 only a subset of it. */
558b86a5 32
558b86a5
LB
33/* used for protocol handling */
34#define BUFFER_SIZE 1024
35#define URL_SIZE 4096
36#define MAX_REDIRECTS 8
37
38typedef struct {
39 URLContext *hd;
40 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
41 int line_count;
42 int http_code;
7995ed8e 43 int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
bc5c918e 44 int64_t off, filesize;
558b86a5
LB
45 char location[URL_SIZE];
46} HTTPContext;
47
48static int http_connect(URLContext *h, const char *path, const char *hoststr,
49 const char *auth, int *new_location);
50static int http_write(URLContext *h, uint8_t *buf, int size);
51
52
53/* return non zero if error */
54static int http_open_cnx(URLContext *h)
55{
56 const char *path, *proxy_path;
57 char hostname[1024], hoststr[1024];
58 char auth[1024];
59 char path1[1024];
60 char buf[1024];
61 int port, use_proxy, err, location_changed = 0, redirects = 0;
62 HTTPContext *s = h->priv_data;
63 URLContext *hd = NULL;
64
65 proxy_path = getenv("http_proxy");
66 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
f7d78f36 67 av_strstart(proxy_path, "http://", NULL);
558b86a5
LB
68
69 /* fill the dest addr */
70 redo:
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);
74 if (port > 0) {
75 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
76 } else {
75e61b0e 77 av_strlcpy(hoststr, hostname, sizeof(hoststr));
558b86a5
LB
78 }
79
80 if (use_proxy) {
81 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
82 NULL, 0, proxy_path);
83 path = s->location;
84 } else {
85 if (path1[0] == '\0')
86 path = "/";
87 else
88 path = path1;
89 }
90 if (port < 0)
91 port = 80;
92
93 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
94 err = url_open(&hd, buf, URL_RDWR);
95 if (err < 0)
96 goto fail;
97
98 s->hd = hd;
99 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
100 goto fail;
a3fd2bd8 101 if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
558b86a5
LB
102 /* url moved, get next */
103 url_close(hd);
104 if (redirects++ >= MAX_REDIRECTS)
6f3e0b21 105 return AVERROR(EIO);
558b86a5
LB
106 location_changed = 0;
107 goto redo;
108 }
109 return 0;
110 fail:
111 if (hd)
112 url_close(hd);
6f3e0b21 113 return AVERROR(EIO);
558b86a5
LB
114}
115
116static int http_open(URLContext *h, const char *uri, int flags)
117{
118 HTTPContext *s;
119 int ret;
120
121 h->is_streamed = 1;
122
123 s = av_malloc(sizeof(HTTPContext));
124 if (!s) {
125 return AVERROR(ENOMEM);
126 }
127 h->priv_data = s;
128 s->filesize = -1;
7995ed8e 129 s->chunksize = -1;
558b86a5 130 s->off = 0;
f7d78f36 131 av_strlcpy(s->location, uri, URL_SIZE);
558b86a5
LB
132
133 ret = http_open_cnx(h);
134 if (ret != 0)
135 av_free (s);
136 return ret;
137}
138static int http_getc(HTTPContext *s)
139{
140 int len;
141 if (s->buf_ptr >= s->buf_end) {
142 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
143 if (len < 0) {
6f3e0b21 144 return AVERROR(EIO);
558b86a5
LB
145 } else if (len == 0) {
146 return -1;
147 } else {
148 s->buf_ptr = s->buffer;
149 s->buf_end = s->buffer + len;
150 }
151 }
152 return *s->buf_ptr++;
153}
154
682d49f4
PH
155static int http_get_line(HTTPContext *s, char *line, int line_size)
156{
157 int ch;
158 char *q;
159
160 q = line;
161 for(;;) {
162 ch = http_getc(s);
163 if (ch < 0)
164 return AVERROR(EIO);
165 if (ch == '\n') {
166 /* process line */
167 if (q > line && q[-1] == '\r')
168 q--;
169 *q = '\0';
170
171 return 0;
172 } else {
173 if ((q - line) < line_size - 1)
174 *q++ = ch;
175 }
176 }
177}
178
558b86a5
LB
179static int process_line(URLContext *h, char *line, int line_count,
180 int *new_location)
181{
182 HTTPContext *s = h->priv_data;
183 char *tag, *p;
184
185 /* end of header */
186 if (line[0] == '\0')
187 return 0;
188
189 p = line;
190 if (line_count == 0) {
191 while (!isspace(*p) && *p != '\0')
192 p++;
193 while (isspace(*p))
194 p++;
195 s->http_code = strtol(p, NULL, 10);
a52dc730
PH
196
197 dprintf(NULL, "http_code=%d\n", s->http_code);
198
7b19aa64
AB
199 /* error codes are 4xx and 5xx */
200 if (s->http_code >= 400 && s->http_code < 600)
201 return -1;
558b86a5
LB
202 } else {
203 while (*p != '\0' && *p != ':')
204 p++;
205 if (*p != ':')
206 return 1;
207
208 *p = '\0';
209 tag = line;
210 p++;
211 while (isspace(*p))
212 p++;
213 if (!strcmp(tag, "Location")) {
214 strcpy(s->location, p);
215 *new_location = 1;
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" */
220 const char *slash;
221 if (!strncmp (p, "bytes ", 6)) {
222 p += 6;
223 s->off = atoll(p);
224 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
225 s->filesize = atoll(slash+1);
226 }
227 h->is_streamed = 0; /* we _can_ in fact seek */
7995ed8e
PH
228 } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
229 s->filesize = -1;
230 s->chunksize = 0;
558b86a5
LB
231 }
232 }
233 return 1;
234}
235
236static int http_connect(URLContext *h, const char *path, const char *hoststr,
237 const char *auth, int *new_location)
238{
239 HTTPContext *s = h->priv_data;
682d49f4
PH
240 int post, err;
241 char line[1024];
558b86a5 242 char *auth_b64;
d176f903 243 int auth_b64_len = (strlen(auth) + 2) / 3 * 4 + 1;
bc5c918e 244 int64_t off = s->off;
558b86a5
LB
245
246
247 /* send http header */
248 post = h->flags & URL_WRONLY;
bd03c380 249 auth_b64 = av_malloc(auth_b64_len);
2907abed 250 av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth));
558b86a5
LB
251 snprintf(s->buffer, sizeof(s->buffer),
252 "%s %s HTTP/1.1\r\n"
253 "User-Agent: %s\r\n"
254 "Accept: */*\r\n"
255 "Range: bytes=%"PRId64"-\r\n"
256 "Host: %s\r\n"
257 "Authorization: Basic %s\r\n"
167c5531 258 "Connection: close\r\n"
558b86a5
LB
259 "\r\n",
260 post ? "POST" : "GET",
261 path,
262 LIBAVFORMAT_IDENT,
263 s->off,
264 hoststr,
265 auth_b64);
266
267 av_freep(&auth_b64);
268 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
6f3e0b21 269 return AVERROR(EIO);
558b86a5
LB
270
271 /* init input buffer */
272 s->buf_ptr = s->buffer;
273 s->buf_end = s->buffer;
274 s->line_count = 0;
275 s->off = 0;
ecb4a895 276 s->filesize = -1;
558b86a5 277 if (post) {
558b86a5
LB
278 return 0;
279 }
280
281 /* wait for header */
558b86a5 282 for(;;) {
682d49f4 283 if (http_get_line(s, line, sizeof(line)) < 0)
6f3e0b21 284 return AVERROR(EIO);
a52dc730
PH
285
286 dprintf(NULL, "header='%s'\n", line);
287
2b01a520
JM
288 err = process_line(h, line, s->line_count, new_location);
289 if (err < 0)
290 return err;
291 if (err == 0)
292 break;
293 s->line_count++;
558b86a5
LB
294 }
295
296 return (off == s->off) ? 0 : -1;
297}
298
299
300static int http_read(URLContext *h, uint8_t *buf, int size)
301{
302 HTTPContext *s = h->priv_data;
303 int len;
304
7995ed8e
PH
305 if (s->chunksize >= 0) {
306 if (!s->chunksize) {
307 char line[32];
308
309 for(;;) {
310 do {
311 if (http_get_line(s, line, sizeof(line)) < 0)
312 return AVERROR(EIO);
313 } while (!*line); /* skip CR LF from last chunk */
314
315 s->chunksize = strtoll(line, NULL, 16);
316
317 dprintf(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
318
319 if (!s->chunksize)
320 return 0;
321 break;
322 }
323 }
324 size = FFMIN(size, s->chunksize);
325 }
558b86a5
LB
326 /* read bytes from input buffer first */
327 len = s->buf_end - s->buf_ptr;
328 if (len > 0) {
329 if (len > size)
330 len = size;
331 memcpy(buf, s->buf_ptr, len);
332 s->buf_ptr += len;
333 } else {
334 len = url_read(s->hd, buf, size);
335 }
7995ed8e 336 if (len > 0) {
558b86a5 337 s->off += len;
7995ed8e
PH
338 if (s->chunksize > 0)
339 s->chunksize -= len;
340 }
558b86a5
LB
341 return len;
342}
343
344/* used only when posting data */
345static int http_write(URLContext *h, uint8_t *buf, int size)
346{
347 HTTPContext *s = h->priv_data;
348 return url_write(s->hd, buf, size);
349}
350
351static int http_close(URLContext *h)
352{
353 HTTPContext *s = h->priv_data;
354 url_close(s->hd);
355 av_free(s);
356 return 0;
357}
358
bc5c918e 359static int64_t http_seek(URLContext *h, int64_t off, int whence)
558b86a5
LB
360{
361 HTTPContext *s = h->priv_data;
362 URLContext *old_hd = s->hd;
bc5c918e 363 int64_t old_off = s->off;
558b86a5
LB
364
365 if (whence == AVSEEK_SIZE)
366 return s->filesize;
367 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
368 return -1;
369
370 /* we save the old context in case the seek fails */
371 s->hd = NULL;
372 if (whence == SEEK_CUR)
373 off += s->off;
374 else if (whence == SEEK_END)
375 off += s->filesize;
376 s->off = off;
377
378 /* if it fails, continue on old connection */
379 if (http_open_cnx(h) < 0) {
380 s->hd = old_hd;
381 s->off = old_off;
382 return -1;
383 }
384 url_close(old_hd);
385 return off;
386}
387
f0a80394
RB
388static int
389http_get_file_handle(URLContext *h)
390{
391 HTTPContext *s = h->priv_data;
392 return url_get_file_handle(s->hd);
393}
394
558b86a5
LB
395URLProtocol http_protocol = {
396 "http",
397 http_open,
398 http_read,
399 http_write,
400 http_seek,
401 http_close,
f0a80394 402 .url_get_file_handle = http_get_file_handle,
558b86a5 403};