]> git.sesse.net Git - ffmpeg/blob - libavformat/http.c
TIFF decoder
[ffmpeg] / 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 #ifndef __BEOS__
27 # include <arpa/inet.h>
28 #else
29 # include "barpainet.h"
30 #endif
31 #include <netdb.h>
32
33
34 /* XXX: POST protocol is not completly implemented because ffmpeg use
35    only a subset of it */
36
37 //#define DEBUG
38
39 /* used for protocol handling */
40 #define BUFFER_SIZE 1024
41 #define URL_SIZE    4096
42
43 typedef struct {
44     URLContext *hd;
45     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
46     int line_count;
47     int http_code;
48     char location[URL_SIZE];
49 } HTTPContext;
50
51 static int http_connect(URLContext *h, const char *path, const char *hoststr,
52                         const char *auth);
53 static int http_write(URLContext *h, uint8_t *buf, int size);
54 static char *b64_encode(const unsigned char *src );
55
56
57 /* return non zero if error */
58 static int http_open(URLContext *h, const char *uri, int flags)
59 {
60     const char *path, *proxy_path;
61     char hostname[1024], hoststr[1024];
62     char auth[1024];
63     char path1[1024];
64     char buf[1024];
65     int port, use_proxy, err;
66     HTTPContext *s;
67     URLContext *hd = NULL;
68
69     h->is_streamed = 1;
70
71     s = av_malloc(sizeof(HTTPContext));
72     if (!s) {
73         return -ENOMEM;
74     }
75     h->priv_data = s;
76
77     proxy_path = getenv("http_proxy");
78     use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
79         strstart(proxy_path, "http://", NULL);
80
81     /* fill the dest addr */
82  redo:
83     /* needed in any case to build the host string */
84     url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
85               path1, sizeof(path1), uri);
86     if (port > 0) {
87         snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
88     } else {
89         pstrcpy(hoststr, sizeof(hoststr), hostname);
90     }
91
92     if (use_proxy) {
93         url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
94                   NULL, 0, proxy_path);
95         path = uri;
96     } else {
97         if (path1[0] == '\0')
98             path = "/";
99         else
100             path = path1;
101     }
102     if (port < 0)
103         port = 80;
104
105     snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
106     err = url_open(&hd, buf, URL_RDWR);
107     if (err < 0)
108         goto fail;
109
110     s->hd = hd;
111     if (http_connect(h, path, hoststr, auth) < 0)
112         goto fail;
113     if (s->http_code == 303 && s->location[0] != '\0') {
114         /* url moved, get next */
115         uri = s->location;
116         url_close(hd);
117         goto redo;
118     }
119     return 0;
120  fail:
121     if (hd)
122         url_close(hd);
123     av_free(s);
124     return AVERROR_IO;
125 }
126
127 static int http_getc(HTTPContext *s)
128 {
129     int len;
130     if (s->buf_ptr >= s->buf_end) {
131         len = url_read(s->hd, s->buffer, BUFFER_SIZE);
132         if (len < 0) {
133             return AVERROR_IO;
134         } else if (len == 0) {
135             return -1;
136         } else {
137             s->buf_ptr = s->buffer;
138             s->buf_end = s->buffer + len;
139         }
140     }
141     return *s->buf_ptr++;
142 }
143
144 static int process_line(HTTPContext *s, char *line, int line_count)
145 {
146     char *tag, *p;
147
148     /* end of header */
149     if (line[0] == '\0')
150         return 0;
151
152     p = line;
153     if (line_count == 0) {
154         while (!isspace(*p) && *p != '\0')
155             p++;
156         while (isspace(*p))
157             p++;
158         s->http_code = strtol(p, NULL, 10);
159 #ifdef DEBUG
160         printf("http_code=%d\n", s->http_code);
161 #endif
162     } else {
163         while (*p != '\0' && *p != ':')
164             p++;
165         if (*p != ':')
166             return 1;
167
168         *p = '\0';
169         tag = line;
170         p++;
171         while (isspace(*p))
172             p++;
173         if (!strcmp(tag, "Location")) {
174             strcpy(s->location, p);
175         }
176     }
177     return 1;
178 }
179
180 static int http_connect(URLContext *h, const char *path, const char *hoststr,
181                         const char *auth)
182 {
183     HTTPContext *s = h->priv_data;
184     int post, err, ch;
185     char line[1024], *q;
186     char *auth_b64;
187
188
189     /* send http header */
190     post = h->flags & URL_WRONLY;
191
192     auth_b64 = b64_encode(auth);
193     snprintf(s->buffer, sizeof(s->buffer),
194              "%s %s HTTP/1.0\r\n"
195              "User-Agent: %s\r\n"
196              "Accept: */*\r\n"
197              "Host: %s\r\n"
198              "Authorization: Basic %s\r\n"
199              "\r\n",
200              post ? "POST" : "GET",
201              path,
202              LIBAVFORMAT_IDENT,
203              hoststr,
204              auth_b64);
205
206     av_freep(&auth_b64);
207     if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
208         return AVERROR_IO;
209
210     /* init input buffer */
211     s->buf_ptr = s->buffer;
212     s->buf_end = s->buffer;
213     s->line_count = 0;
214     s->location[0] = '\0';
215     if (post) {
216         sleep(1);
217         return 0;
218     }
219
220     /* wait for header */
221     q = line;
222     for(;;) {
223         ch = http_getc(s);
224         if (ch < 0)
225             return AVERROR_IO;
226         if (ch == '\n') {
227             /* process line */
228             if (q > line && q[-1] == '\r')
229                 q--;
230             *q = '\0';
231 #ifdef DEBUG
232             printf("header='%s'\n", line);
233 #endif
234             err = process_line(s, line, s->line_count);
235             if (err < 0)
236                 return err;
237             if (err == 0)
238                 return 0;
239             s->line_count++;
240             q = line;
241         } else {
242             if ((q - line) < sizeof(line) - 1)
243                 *q++ = ch;
244         }
245     }
246 }
247
248
249 static int http_read(URLContext *h, uint8_t *buf, int size)
250 {
251     HTTPContext *s = h->priv_data;
252     int len;
253
254     /* read bytes from input buffer first */
255     len = s->buf_end - s->buf_ptr;
256     if (len > 0) {
257         if (len > size)
258             len = size;
259         memcpy(buf, s->buf_ptr, len);
260         s->buf_ptr += len;
261     } else {
262         len = url_read(s->hd, buf, size);
263     }
264     return len;
265 }
266
267 /* used only when posting data */
268 static int http_write(URLContext *h, uint8_t *buf, int size)
269 {
270     HTTPContext *s = h->priv_data;
271     return url_write(s->hd, buf, size);
272 }
273
274 static int http_close(URLContext *h)
275 {
276     HTTPContext *s = h->priv_data;
277     url_close(s->hd);
278     av_free(s);
279     return 0;
280 }
281
282 URLProtocol http_protocol = {
283     "http",
284     http_open,
285     http_read,
286     http_write,
287     NULL, /* seek */
288     http_close,
289 };
290
291 /*****************************************************************************
292  * b64_encode: stolen from VLC's http.c
293  * simplified by michael
294  *****************************************************************************/
295
296 static char *b64_encode( const unsigned char *src )
297 {
298     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
299     unsigned int len= strlen(src);
300     char *ret, *dst;
301     unsigned i_bits = 0;
302     unsigned i_shift = 0;
303
304     if(len < UINT_MAX/4){
305         ret=dst= av_malloc( len * 4 / 3 + 12 );
306     }else
307         return NULL;
308
309     while(*src){
310         i_bits = (i_bits << 8) + *src++;
311         i_shift += 8;
312
313         do{
314             *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
315             i_shift -= 6;
316         }while( i_shift > 6 || (*src == 0 && i_shift>0));
317     }
318     *dst++ = '=';
319     *dst   = '\0';
320
321     return ret;
322 }
323