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