2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Gerard Lantau.
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.
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.
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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
33 /* XXX: POST protocol is not completly implemented because ffmpeg use
34 only a subset of it */
38 /* used for protocol handling */
39 #define BUFFER_SIZE 1024
44 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
47 char location[URL_SIZE];
50 static int http_connect(URLContext *h, const char *path);
51 static int http_write(URLContext *h, UINT8 *buf, int size);
53 /* return non zero if error */
54 static int http_open(URLContext *h, const char *uri, int flags)
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;
65 s = malloc(sizeof(HTTPContext));
71 proxy_path = getenv("http_proxy");
72 use_proxy = (proxy_path != NULL) && !getenv("no_proxy");
74 /* fill the dest addr */
81 if (!strstart(p, "http://", &p))
84 while (*p != ':' && *p != '/' && *p != '\0') {
85 if ((q - hostname) < sizeof(hostname) - 1)
93 port = strtoul(p, (char **)&p, 10);
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);
112 memcpy (&dest_addr.sin_addr, hp->h_addr, sizeof(dest_addr.sin_addr));
115 fd = socket(PF_INET, SOCK_STREAM, 0);
119 if (connect(fd, (struct sockaddr *)&dest_addr,
120 sizeof(dest_addr)) < 0)
124 if (http_connect(h, path) < 0)
126 if (s->http_code == 303 && s->location[0] != '\0') {
127 /* url moved, get next */
140 static int http_getc(HTTPContext *s)
143 if (s->buf_ptr >= s->buf_end) {
145 len = read(s->fd, s->buffer, BUFFER_SIZE);
147 if (errno == EAGAIN || errno == EINTR)
150 } else if (len == 0) {
153 s->buf_ptr = s->buffer;
154 s->buf_end = s->buffer + len;
157 return *s->buf_ptr++;
160 static int process_line(HTTPContext *s, char *line, int line_count)
169 if (line_count == 0) {
170 while (!isspace(*p) && *p != '\0')
174 s->http_code = strtol(p, NULL, 10);
176 printf("http_code=%d\n", s->http_code);
179 while (*p != '\0' && *p != ':')
189 if (!strcmp(tag, "Location")) {
190 strcpy(s->location, p);
196 static int http_connect(URLContext *h, const char *path)
198 HTTPContext *s = h->priv_data;
203 /* send http header */
204 post = h->flags & URL_WRONLY;
206 snprintf(s->buffer, sizeof(s->buffer),
208 "User-Agent: FFmpeg %s\n"
211 post ? "POST" : "GET",
216 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
219 /* init input buffer */
220 s->buf_ptr = s->buffer;
221 s->buf_end = s->buffer;
223 s->location[0] = '\0';
227 /* wait for header */
235 if (q > line && q[-1] == '\r')
239 printf("header='%s'\n", line);
241 err = process_line(s, line, s->line_count);
249 if ((q - line) < sizeof(line) - 1)
256 static int http_read(URLContext *h, UINT8 *buf, int size)
258 HTTPContext *s = h->priv_data;
263 /* read bytes from input buffer first */
264 len = s->buf_end - s->buf_ptr;
268 memcpy(buf, s->buf_ptr, len);
271 len = read (s->fd, buf, size);
273 if (errno != EINTR && errno != EAGAIN)
277 } else if (len == 0) {
287 /* used only when posting data */
288 static int http_write(URLContext *h, UINT8 *buf, int size)
290 HTTPContext *s = h->priv_data;
295 ret = write (s->fd, buf, size);
296 if (ret < 0 && errno != EINTR && errno != EAGAIN)
304 static int http_close(URLContext *h)
306 HTTPContext *s = h->priv_data;
311 URLProtocol http_protocol = {