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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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,
51 static int http_write(URLContext *h, uint8_t *buf, int size);
52 static char *b64_encode(const unsigned char *src );
55 /* return non zero if error */
56 static int http_open(URLContext *h, const char *uri, int flags)
58 const char *path, *proxy_path;
59 char hostname[1024], hoststr[1024];
63 int port, use_proxy, err;
65 URLContext *hd = NULL;
69 s = av_malloc(sizeof(HTTPContext));
75 proxy_path = getenv("http_proxy");
76 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
77 strstart(proxy_path, "http://", NULL);
79 /* fill the dest addr */
81 /* needed in any case to build the host string */
82 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
83 path1, sizeof(path1), uri);
85 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
87 pstrcpy(hoststr, sizeof(hoststr), hostname);
91 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
103 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
104 err = url_open(&hd, buf, URL_RDWR);
109 if (http_connect(h, path, hoststr, auth) < 0)
111 if (s->http_code == 303 && s->location[0] != '\0') {
112 /* url moved, get next */
125 static int http_getc(HTTPContext *s)
128 if (s->buf_ptr >= s->buf_end) {
129 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
132 } else if (len == 0) {
135 s->buf_ptr = s->buffer;
136 s->buf_end = s->buffer + len;
139 return *s->buf_ptr++;
142 static int process_line(HTTPContext *s, char *line, int line_count)
151 if (line_count == 0) {
152 while (!isspace(*p) && *p != '\0')
156 s->http_code = strtol(p, NULL, 10);
158 printf("http_code=%d\n", s->http_code);
161 while (*p != '\0' && *p != ':')
171 if (!strcmp(tag, "Location")) {
172 strcpy(s->location, p);
178 static int http_connect(URLContext *h, const char *path, const char *hoststr,
181 HTTPContext *s = h->priv_data;
187 /* send http header */
188 post = h->flags & URL_WRONLY;
190 auth_b64 = b64_encode(auth);
191 snprintf(s->buffer, sizeof(s->buffer),
196 "Authorization: Basic %s\r\n"
198 post ? "POST" : "GET",
205 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
208 /* init input buffer */
209 s->buf_ptr = s->buffer;
210 s->buf_end = s->buffer;
212 s->location[0] = '\0';
218 /* wait for header */
226 if (q > line && q[-1] == '\r')
230 printf("header='%s'\n", line);
232 err = process_line(s, line, s->line_count);
240 if ((q - line) < sizeof(line) - 1)
247 static int http_read(URLContext *h, uint8_t *buf, int size)
249 HTTPContext *s = h->priv_data;
252 /* read bytes from input buffer first */
253 len = s->buf_end - s->buf_ptr;
257 memcpy(buf, s->buf_ptr, len);
260 len = url_read(s->hd, buf, size);
265 /* used only when posting data */
266 static int http_write(URLContext *h, uint8_t *buf, int size)
268 HTTPContext *s = h->priv_data;
269 return url_write(s->hd, buf, size);
272 static int http_close(URLContext *h)
274 HTTPContext *s = h->priv_data;
280 URLProtocol http_protocol = {
289 /*****************************************************************************
290 * b64_encode: stolen from VLC's http.c
291 * simplified by michael
292 *****************************************************************************/
294 static char *b64_encode( const unsigned char *src )
296 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
297 unsigned int len= strlen(src);
300 unsigned i_shift = 0;
302 if(len < UINT_MAX/4){
303 ret=dst= av_malloc( len * 4 / 3 + 12 );
308 i_bits = (i_bits << 8) + *src++;
312 *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
314 }while( i_shift > 6 || (*src == 0 && i_shift>0));