2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
27 # include <arpa/inet.h>
29 # include "barpainet.h"
35 /* XXX: POST protocol is not completly implemented because ffmpeg use
36 only a subset of it */
40 /* used for protocol handling */
41 #define BUFFER_SIZE 1024
46 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
49 char location[URL_SIZE];
52 static int http_connect(URLContext *h, const char *path, const char *hoststr,
54 static int http_write(URLContext *h, uint8_t *buf, int size);
57 /* return non zero if error */
58 static int http_open(URLContext *h, const char *uri, int flags)
60 const char *path, *proxy_path;
61 char hostname[1024], hoststr[1024];
65 int port, use_proxy, err;
67 URLContext *hd = NULL;
71 s = av_malloc(sizeof(HTTPContext));
77 proxy_path = getenv("http_proxy");
78 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
79 strstart(proxy_path, "http://", NULL);
81 /* fill the dest addr */
83 /* needed in any case to build the host string */
84 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
85 path1, sizeof(path1), uri);
87 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
89 pstrcpy(hoststr, sizeof(hoststr), hostname);
93 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
105 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
106 err = url_open(&hd, buf, URL_RDWR);
111 if (http_connect(h, path, hoststr, auth) < 0)
113 if (s->http_code == 303 && s->location[0] != '\0') {
114 /* url moved, get next */
127 static int http_getc(HTTPContext *s)
130 if (s->buf_ptr >= s->buf_end) {
131 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
134 } else if (len == 0) {
137 s->buf_ptr = s->buffer;
138 s->buf_end = s->buffer + len;
141 return *s->buf_ptr++;
144 static int process_line(HTTPContext *s, char *line, int line_count)
153 if (line_count == 0) {
154 while (!isspace(*p) && *p != '\0')
158 s->http_code = strtol(p, NULL, 10);
160 printf("http_code=%d\n", s->http_code);
163 while (*p != '\0' && *p != ':')
173 if (!strcmp(tag, "Location")) {
174 strcpy(s->location, p);
180 static int http_connect(URLContext *h, const char *path, const char *hoststr,
183 HTTPContext *s = h->priv_data;
189 /* send http header */
190 post = h->flags & URL_WRONLY;
192 auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
193 snprintf(s->buffer, sizeof(s->buffer),
198 "Authorization: Basic %s\r\n"
200 post ? "POST" : "GET",
207 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
210 /* init input buffer */
211 s->buf_ptr = s->buffer;
212 s->buf_end = s->buffer;
214 s->location[0] = '\0';
220 /* wait for header */
228 if (q > line && q[-1] == '\r')
232 printf("header='%s'\n", line);
234 err = process_line(s, line, s->line_count);
242 if ((q - line) < sizeof(line) - 1)
249 static int http_read(URLContext *h, uint8_t *buf, int size)
251 HTTPContext *s = h->priv_data;
254 /* read bytes from input buffer first */
255 len = s->buf_end - s->buf_ptr;
259 memcpy(buf, s->buf_ptr, len);
262 len = url_read(s->hd, buf, size);
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 = {