]> git.sesse.net Git - ffmpeg/blob - libav/http.c
removed incomplete seek patch
[ffmpeg] / libav / http.c
1 /*
2  * HTTP protocol for ffmpeg client
3  * Copyright (c) 2000, 2001 Gerard Lantau.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <netdb.h>
30
31 #include "avformat.h"
32
33 /* XXX: POST protocol is not completly implemented because ffmpeg use
34    only a subset of it */
35
36 //#define DEBUG
37
38 /* used for protocol handling */
39 #define BUFFER_SIZE 1024
40 #define URL_SIZE    4096
41
42 typedef struct {
43     int fd;
44     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
45     int line_count;
46     int http_code;
47     char location[URL_SIZE];
48 } HTTPContext;
49
50 static int http_connect(URLContext *h, const char *path);
51 static int http_write(URLContext *h, UINT8 *buf, int size);
52
53 /* return non zero if error */
54 static int http_open(URLContext *h, const char *uri, int flags)
55 {
56     struct sockaddr_in dest_addr;
57     const char *p, *path, *proxy_path;
58     char hostname[1024], *q;
59     int port, fd = -1, use_proxy;
60     struct hostent *hp;
61     HTTPContext *s;
62
63     h->is_streamed = 1;
64
65     s = malloc(sizeof(HTTPContext));
66     if (!s) {
67         return -ENOMEM;
68     }
69     h->priv_data = s;
70
71     proxy_path = getenv("http_proxy");
72     use_proxy = (proxy_path != NULL) && !getenv("no_proxy");
73
74     /* fill the dest addr */
75  redo:
76     if (use_proxy) {
77         p = proxy_path;
78     } else {
79         p = uri;
80     }
81     if (!strstart(p, "http://", &p))
82         goto fail;
83     q = hostname;
84     while (*p != ':' && *p != '/' && *p != '\0') {
85         if ((q - hostname) < sizeof(hostname) - 1)
86             *q++ = *p;
87         p++;
88     }
89     *q = '\0';
90     port = 80;
91     if (*p == ':') {
92         p++;
93         port = strtoul(p, (char **)&p, 10);
94     }
95     if (port <= 0)
96         goto fail;
97     if (use_proxy) {
98         path = uri;
99     } else {
100         if (*p == '\0')
101             path = "/";
102         else
103             path = p;
104     }
105
106     dest_addr.sin_family = AF_INET;
107     dest_addr.sin_port = htons(port);
108     if ((inet_aton(hostname, &dest_addr.sin_addr)) == 0) {
109         hp = gethostbyname(hostname);
110         if (!hp)
111             goto fail;
112         memcpy (&dest_addr.sin_addr, hp->h_addr, sizeof(dest_addr.sin_addr));
113     }
114     
115     fd = socket(PF_INET, SOCK_STREAM, 0);
116     if (fd < 0)
117         goto fail;
118
119     if (connect(fd, (struct sockaddr *)&dest_addr, 
120                 sizeof(dest_addr)) < 0)
121         goto fail;
122
123     s->fd = fd;
124     if (http_connect(h, path) < 0)
125         goto fail;
126     if (s->http_code == 303 && s->location[0] != '\0') {
127         /* url moved, get next */
128         uri = s->location;
129         goto redo;
130     }
131
132     return 0;
133  fail:
134     if (fd >= 0)
135         close(fd);
136     free(s);
137     return -EIO;
138 }
139
140 static int http_getc(HTTPContext *s)
141 {
142     int len;
143     if (s->buf_ptr >= s->buf_end) {
144     redo:
145         len = read(s->fd, s->buffer, BUFFER_SIZE);
146         if (len < 0) {
147             if (errno == EAGAIN || errno == EINTR)
148                 goto redo;
149             return -EIO;
150         } else if (len == 0) {
151             return -1;
152         } else {
153             s->buf_ptr = s->buffer;
154             s->buf_end = s->buffer + len;
155         }
156     }
157     return *s->buf_ptr++;
158 }
159
160 static int process_line(HTTPContext *s, char *line, int line_count)
161 {
162     char *tag, *p;
163     
164     /* end of header */
165     if (line[0] == '\0')
166         return 0;
167
168     p = line;
169     if (line_count == 0) {
170         while (!isspace(*p) && *p != '\0')
171             p++;
172         while (isspace(*p))
173             p++;
174         s->http_code = strtol(p, NULL, 10);
175 #ifdef DEBUG
176         printf("http_code=%d\n", s->http_code);
177 #endif
178     } else {
179         while (*p != '\0' && *p != ':')
180             p++;
181         if (*p != ':') 
182             return 1;
183         
184         *p = '\0';
185         tag = line;
186         p++;
187         while (isspace(*p))
188             p++;
189         if (!strcmp(tag, "Location")) {
190             strcpy(s->location, p);
191         }
192     }
193     return 1;
194 }
195
196 static int http_connect(URLContext *h, const char *path)
197 {
198     HTTPContext *s = h->priv_data;
199     int post, err, ch;
200     char line[1024], *q;
201
202
203     /* send http header */
204     post = h->flags & URL_WRONLY;
205
206     snprintf(s->buffer, sizeof(s->buffer),
207              "%s %s HTTP/1.0\n"
208              "User-Agent: FFmpeg %s\n"
209              "Accept: */*\n"
210              "\n",
211              post ? "POST" : "GET",
212              path,
213              FFMPEG_VERSION
214              );
215
216     if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
217         return -EIO;
218         
219     /* init input buffer */
220     s->buf_ptr = s->buffer;
221     s->buf_end = s->buffer;
222     s->line_count = 0;
223     s->location[0] = '\0';
224     if (post)
225         return 0;
226     
227     /* wait for header */
228     q = line;
229     for(;;) {
230         ch = http_getc(s);
231         if (ch < 0)
232             return -EIO;
233         if (ch == '\n') {
234             /* process line */
235             if (q > line && q[-1] == '\r')
236                 q--;
237             *q = '\0';
238 #ifdef DEBUG
239             printf("header='%s'\n", line);
240 #endif
241             err = process_line(s, line, s->line_count);
242             if (err < 0)
243                 return err;
244             if (err == 0)
245                 return 0;
246             s->line_count++;
247             q = line;
248         } else {
249             if ((q - line) < sizeof(line) - 1)
250                 *q++ = ch;
251         }
252     }
253 }
254
255
256 static int http_read(URLContext *h, UINT8 *buf, int size)
257 {
258     HTTPContext *s = h->priv_data;
259     int size1, len;
260
261     size1 = size;
262     while (size > 0) {
263         /* read bytes from input buffer first */
264         len = s->buf_end - s->buf_ptr;
265         if (len > 0) {
266             if (len > size)
267                 len = size;
268             memcpy(buf, s->buf_ptr, len);
269             s->buf_ptr += len;
270         } else {
271             len = read (s->fd, buf, size);
272             if (len < 0) {
273                 if (errno != EINTR && errno != EAGAIN)
274                     return -errno;
275                 else
276                     continue;
277             } else if (len == 0) {
278                 break;
279             }
280         }
281         size -= len;
282         buf += len;
283     }
284     return size1 - size;
285 }
286
287 /* used only when posting data */
288 static int http_write(URLContext *h, UINT8 *buf, int size)
289 {
290     HTTPContext *s = h->priv_data;
291     int ret, size1;
292
293     size1 = size;
294     while (size > 0) {
295         ret = write (s->fd, buf, size);
296         if (ret < 0 && errno != EINTR && errno != EAGAIN)
297             return -errno;
298         size -= ret;
299         buf += ret;
300     }
301     return size1 - size;
302 }
303
304 static int http_close(URLContext *h)
305 {
306     HTTPContext *s = h->priv_data;
307     close(s->fd);
308     return 0;
309 }
310
311 URLProtocol http_protocol = {
312     "http",
313     http_open,
314     http_read,
315     http_write,
316     NULL, /* seek */
317     http_close,
318 };