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
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
25 # include <arpa/inet.h>
27 # include "barpainet.h"
32 /* XXX: POST protocol is not completly implemented because ffmpeg use
33 only a subset of it */
37 /* used for protocol handling */
38 #define BUFFER_SIZE 1024
43 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
46 char location[URL_SIZE];
49 static int http_connect(URLContext *h, const char *path, const char *hoststr);
50 static int http_write(URLContext *h, uint8_t *buf, int size);
53 /* return non zero if error */
54 static int http_open(URLContext *h, const char *uri, int flags)
56 const char *path, *proxy_path;
57 char hostname[1024], hoststr[1024];
60 int port, use_proxy, err;
62 URLContext *hd = NULL;
66 s = av_malloc(sizeof(HTTPContext));
72 proxy_path = getenv("http_proxy");
73 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
74 strstart(proxy_path, "http://", NULL);
76 /* fill the dest addr */
78 /* needed in any case to build the host string */
79 url_split(NULL, 0, hostname, sizeof(hostname), &port,
80 path1, sizeof(path1), uri);
82 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
84 pstrcpy(hoststr, sizeof(hoststr), hostname);
88 url_split(NULL, 0, hostname, sizeof(hostname), &port,
100 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
101 err = url_open(&hd, buf, URL_RDWR);
106 if (http_connect(h, path, hoststr) < 0)
108 if (s->http_code == 303 && s->location[0] != '\0') {
109 /* url moved, get next */
122 static int http_getc(HTTPContext *s)
125 if (s->buf_ptr >= s->buf_end) {
126 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
129 } else if (len == 0) {
132 s->buf_ptr = s->buffer;
133 s->buf_end = s->buffer + len;
136 return *s->buf_ptr++;
139 static int process_line(HTTPContext *s, char *line, int line_count)
148 if (line_count == 0) {
149 while (!isspace(*p) && *p != '\0')
153 s->http_code = strtol(p, NULL, 10);
155 printf("http_code=%d\n", s->http_code);
158 while (*p != '\0' && *p != ':')
168 if (!strcmp(tag, "Location")) {
169 strcpy(s->location, p);
175 static int http_connect(URLContext *h, const char *path, const char *hoststr)
177 HTTPContext *s = h->priv_data;
182 /* send http header */
183 post = h->flags & URL_WRONLY;
185 snprintf(s->buffer, sizeof(s->buffer),
191 post ? "POST" : "GET",
196 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
199 /* init input buffer */
200 s->buf_ptr = s->buffer;
201 s->buf_end = s->buffer;
203 s->location[0] = '\0';
209 /* wait for header */
217 if (q > line && q[-1] == '\r')
221 printf("header='%s'\n", line);
223 err = process_line(s, line, s->line_count);
231 if ((q - line) < sizeof(line) - 1)
238 static int http_read(URLContext *h, uint8_t *buf, int size)
240 HTTPContext *s = h->priv_data;
245 /* read bytes from input buffer first */
246 len = s->buf_end - s->buf_ptr;
250 memcpy(buf, s->buf_ptr, len);
253 len = url_read (s->hd, buf, size);
256 } else if (len == 0) {
266 /* used only when posting data */
267 static int http_write(URLContext *h, uint8_t *buf, int size)
269 HTTPContext *s = h->priv_data;
270 return url_write(s->hd, buf, size);
273 static int http_close(URLContext *h)
275 HTTPContext *s = h->priv_data;
281 URLProtocol http_protocol = {