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