Properly initialize filesize during input buffer initialization.
[libav.git] / libavformat / http.c
CommitLineData
558b86a5
LB
1/*
2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
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 */
21#include "avformat.h"
22#include <unistd.h>
23#include "network.h"
24
25#include "base64.h"
26
27/* XXX: POST protocol is not completly implemented because ffmpeg use
28 only a subset of it */
29
30//#define DEBUG
31
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;
42 offset_t off, filesize;
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") &&
65 strstart(proxy_path, "http://", NULL);
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 {
75 pstrcpy(hoststr, sizeof(hoststr), hostname);
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)
103 return AVERROR_IO;
104 location_changed = 0;
105 goto redo;
106 }
107 return 0;
108 fail:
109 if (hd)
110 url_close(hd);
111 return AVERROR_IO;
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;
128 pstrcpy (s->location, URL_SIZE, uri);
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) {
141 return AVERROR_IO;
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
152static int process_line(URLContext *h, char *line, int line_count,
153 int *new_location)
154{
155 HTTPContext *s = h->priv_data;
156 char *tag, *p;
157
158 /* end of header */
159 if (line[0] == '\0')
160 return 0;
161
162 p = line;
163 if (line_count == 0) {
164 while (!isspace(*p) && *p != '\0')
165 p++;
166 while (isspace(*p))
167 p++;
168 s->http_code = strtol(p, NULL, 10);
169#ifdef DEBUG
170 printf("http_code=%d\n", s->http_code);
171#endif
7b19aa64
AB
172 /* error codes are 4xx and 5xx */
173 if (s->http_code >= 400 && s->http_code < 600)
174 return -1;
558b86a5
LB
175 } else {
176 while (*p != '\0' && *p != ':')
177 p++;
178 if (*p != ':')
179 return 1;
180
181 *p = '\0';
182 tag = line;
183 p++;
184 while (isspace(*p))
185 p++;
186 if (!strcmp(tag, "Location")) {
187 strcpy(s->location, p);
188 *new_location = 1;
189 } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
190 s->filesize = atoll(p);
191 } else if (!strcmp (tag, "Content-Range")) {
192 /* "bytes $from-$to/$document_size" */
193 const char *slash;
194 if (!strncmp (p, "bytes ", 6)) {
195 p += 6;
196 s->off = atoll(p);
197 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
198 s->filesize = atoll(slash+1);
199 }
200 h->is_streamed = 0; /* we _can_ in fact seek */
201 }
202 }
203 return 1;
204}
205
206static int http_connect(URLContext *h, const char *path, const char *hoststr,
207 const char *auth, int *new_location)
208{
209 HTTPContext *s = h->priv_data;
210 int post, err, ch;
211 char line[1024], *q;
212 char *auth_b64;
213 offset_t off = s->off;
214
215
216 /* send http header */
217 post = h->flags & URL_WRONLY;
218
219 auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
220 snprintf(s->buffer, sizeof(s->buffer),
221 "%s %s HTTP/1.1\r\n"
222 "User-Agent: %s\r\n"
223 "Accept: */*\r\n"
224 "Range: bytes=%"PRId64"-\r\n"
225 "Host: %s\r\n"
226 "Authorization: Basic %s\r\n"
167c5531 227 "Connection: close\r\n"
558b86a5
LB
228 "\r\n",
229 post ? "POST" : "GET",
230 path,
231 LIBAVFORMAT_IDENT,
232 s->off,
233 hoststr,
234 auth_b64);
235
236 av_freep(&auth_b64);
237 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
238 return AVERROR_IO;
239
240 /* init input buffer */
241 s->buf_ptr = s->buffer;
242 s->buf_end = s->buffer;
243 s->line_count = 0;
244 s->off = 0;
ecb4a895 245 s->filesize = -1;
558b86a5
LB
246 if (post) {
247 sleep(1);
248 return 0;
249 }
250
251 /* wait for header */
252 q = line;
253 for(;;) {
254 ch = http_getc(s);
255 if (ch < 0)
256 return AVERROR_IO;
257 if (ch == '\n') {
258 /* process line */
259 if (q > line && q[-1] == '\r')
260 q--;
261 *q = '\0';
262#ifdef DEBUG
263 printf("header='%s'\n", line);
264#endif
265 err = process_line(h, line, s->line_count, new_location);
266 if (err < 0)
267 return err;
268 if (err == 0)
269 break;
270 s->line_count++;
271 q = line;
272 } else {
273 if ((q - line) < sizeof(line) - 1)
274 *q++ = ch;
275 }
276 }
277
278 return (off == s->off) ? 0 : -1;
279}
280
281
282static int http_read(URLContext *h, uint8_t *buf, int size)
283{
284 HTTPContext *s = h->priv_data;
285 int len;
286
287 /* read bytes from input buffer first */
288 len = s->buf_end - s->buf_ptr;
289 if (len > 0) {
290 if (len > size)
291 len = size;
292 memcpy(buf, s->buf_ptr, len);
293 s->buf_ptr += len;
294 } else {
295 len = url_read(s->hd, buf, size);
296 }
297 if (len > 0)
298 s->off += len;
299 return len;
300}
301
302/* used only when posting data */
303static int http_write(URLContext *h, uint8_t *buf, int size)
304{
305 HTTPContext *s = h->priv_data;
306 return url_write(s->hd, buf, size);
307}
308
309static int http_close(URLContext *h)
310{
311 HTTPContext *s = h->priv_data;
312 url_close(s->hd);
313 av_free(s);
314 return 0;
315}
316
317static offset_t http_seek(URLContext *h, offset_t off, int whence)
318{
319 HTTPContext *s = h->priv_data;
320 URLContext *old_hd = s->hd;
321 offset_t old_off = s->off;
322
323 if (whence == AVSEEK_SIZE)
324 return s->filesize;
325 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
326 return -1;
327
328 /* we save the old context in case the seek fails */
329 s->hd = NULL;
330 if (whence == SEEK_CUR)
331 off += s->off;
332 else if (whence == SEEK_END)
333 off += s->filesize;
334 s->off = off;
335
336 /* if it fails, continue on old connection */
337 if (http_open_cnx(h) < 0) {
338 s->hd = old_hd;
339 s->off = old_off;
340 return -1;
341 }
342 url_close(old_hd);
343 return off;
344}
345
346URLProtocol http_protocol = {
347 "http",
348 http_open,
349 http_read,
350 http_write,
351 http_seek,
352 http_close,
353};