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