2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
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.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
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
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
26 # include <arpa/inet.h>
28 # include "barpainet.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, const char *hoststr);
51 static int http_write(URLContext *h, uint8_t *buf, int size);
54 /* return non zero if error */
55 static int http_open(URLContext *h, const char *uri, int flags)
57 const char *path, *proxy_path;
58 char hostname[1024], hoststr[1024];
61 int port, use_proxy, err;
63 URLContext *hd = NULL;
67 s = av_malloc(sizeof(HTTPContext));
73 proxy_path = getenv("http_proxy");
74 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
75 strstart(proxy_path, "http://", NULL);
77 /* fill the dest addr */
79 /* needed in any case to build the host string */
80 url_split(NULL, 0, hostname, sizeof(hostname), &port,
81 path1, sizeof(path1), uri);
83 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
85 pstrcpy(hoststr, sizeof(hoststr), hostname);
89 url_split(NULL, 0, hostname, sizeof(hostname), &port,
101 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
102 err = url_open(&hd, buf, URL_RDWR);
107 if (http_connect(h, path, hoststr) < 0)
109 if (s->http_code == 303 && s->location[0] != '\0') {
110 /* url moved, get next */
123 static int http_getc(HTTPContext *s)
126 if (s->buf_ptr >= s->buf_end) {
127 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
130 } else if (len == 0) {
133 s->buf_ptr = s->buffer;
134 s->buf_end = s->buffer + len;
137 return *s->buf_ptr++;
140 static int process_line(HTTPContext *s, char *line, int line_count)
149 if (line_count == 0) {
150 while (!isspace(*p) && *p != '\0')
154 s->http_code = strtol(p, NULL, 10);
156 printf("http_code=%d\n", s->http_code);
159 while (*p != '\0' && *p != ':')
169 if (!strcmp(tag, "Location")) {
170 strcpy(s->location, p);
176 static int http_connect(URLContext *h, const char *path, const char *hoststr)
178 HTTPContext *s = h->priv_data;
183 /* send http header */
184 post = h->flags & URL_WRONLY;
186 snprintf(s->buffer, sizeof(s->buffer),
188 "User-Agent: FFmpeg %s\n"
192 post ? "POST" : "GET",
197 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
200 /* init input buffer */
201 s->buf_ptr = s->buffer;
202 s->buf_end = s->buffer;
204 s->location[0] = '\0';
210 /* wait for header */
218 if (q > line && q[-1] == '\r')
222 printf("header='%s'\n", line);
224 err = process_line(s, line, s->line_count);
232 if ((q - line) < sizeof(line) - 1)
239 static int http_read(URLContext *h, uint8_t *buf, int size)
241 HTTPContext *s = h->priv_data;
246 /* read bytes from input buffer first */
247 len = s->buf_end - s->buf_ptr;
251 memcpy(buf, s->buf_ptr, len);
254 len = url_read (s->hd, buf, size);
257 } else if (len == 0) {
267 /* used only when posting data */
268 static int http_write(URLContext *h, uint8_t *buf, int size)
270 HTTPContext *s = h->priv_data;
271 return url_write(s->hd, buf, size);
274 static int http_close(URLContext *h)
276 HTTPContext *s = h->priv_data;
282 URLProtocol http_protocol = {