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;
186 /* send http header */
187 post = h->flags & URL_WRONLY;
189 snprintf(s->buffer, sizeof(s->buffer),
194 "Authorization: Basic %s\r\n"
196 post ? "POST" : "GET",
202 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
205 /* init input buffer */
206 s->buf_ptr = s->buffer;
207 s->buf_end = s->buffer;
209 s->location[0] = '\0';
215 /* wait for header */
223 if (q > line && q[-1] == '\r')
227 printf("header='%s'\n", line);
229 err = process_line(s, line, s->line_count);
237 if ((q - line) < sizeof(line) - 1)
244 static int http_read(URLContext *h, uint8_t *buf, int size)
246 HTTPContext *s = h->priv_data;
249 /* read bytes from input buffer first */
250 len = s->buf_end - s->buf_ptr;
254 memcpy(buf, s->buf_ptr, len);
257 len = url_read(s->hd, buf, size);
262 /* used only when posting data */
263 static int http_write(URLContext *h, uint8_t *buf, int size)
265 HTTPContext *s = h->priv_data;
266 return url_write(s->hd, buf, size);
269 static int http_close(URLContext *h)
271 HTTPContext *s = h->priv_data;
277 URLProtocol http_protocol = {
286 /*****************************************************************************
287 * b64_encode: stolen from VLC's http.c
288 *****************************************************************************/
290 static char *b64_encode( const unsigned char *src )
292 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
293 unsigned int len= strlen(src);
296 unsigned i_shift = 0;
298 if(len < UINT_MAX/4){
299 ret=dst= av_malloc( len * 4 / 3 + 12 );
307 i_bits = ( i_bits << 8 )|( *src++ );
310 else if( i_shift > 0 )
312 i_bits <<= 6 - i_shift;
321 while( i_shift >= 6 )
324 *dst++ = b64[(i_bits >> i_shift)&0x3f];