simplify b64_encode()
[libav.git] / libavformat / http.c
CommitLineData
de6d9b64
FB
1/*
2 * HTTP protocol for ffmpeg client
19720f15 3 * Copyright (c) 2000, 2001 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
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.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
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
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 18 */
8be1c656 19#include "avformat.h"
de6d9b64 20#include <unistd.h>
de6d9b64 21#include <sys/types.h>
b8a78f41 22#include <sys/socket.h>
de6d9b64 23#include <netinet/in.h>
9ddd71fc
FR
24#ifndef __BEOS__
25# include <arpa/inet.h>
26#else
27# include "barpainet.h"
28#endif
de6d9b64
FB
29#include <netdb.h>
30
de6d9b64
FB
31
32/* XXX: POST protocol is not completly implemented because ffmpeg use
33 only a subset of it */
34
35//#define DEBUG
36
37/* used for protocol handling */
38#define BUFFER_SIZE 1024
39#define URL_SIZE 4096
40
41typedef struct {
0e2a2197 42 URLContext *hd;
de6d9b64
FB
43 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
44 int line_count;
45 int http_code;
46 char location[URL_SIZE];
47} HTTPContext;
48
6ba5cbc6
PD
49static int http_connect(URLContext *h, const char *path, const char *hoststr,
50 const char *auth);
0c1a9eda 51static int http_write(URLContext *h, uint8_t *buf, int size);
88730be6 52static char *b64_encode(const unsigned char *src );
de6d9b64 53
0e2a2197 54
de6d9b64
FB
55/* return non zero if error */
56static int http_open(URLContext *h, const char *uri, int flags)
57{
0e2a2197
FB
58 const char *path, *proxy_path;
59 char hostname[1024], hoststr[1024];
6ba5cbc6 60 char auth[1024];
0e2a2197
FB
61 char path1[1024];
62 char buf[1024];
63 int port, use_proxy, err;
de6d9b64 64 HTTPContext *s;
0e2a2197 65 URLContext *hd = NULL;
de6d9b64
FB
66
67 h->is_streamed = 1;
68
1ea4f593 69 s = av_malloc(sizeof(HTTPContext));
de6d9b64
FB
70 if (!s) {
71 return -ENOMEM;
72 }
73 h->priv_data = s;
74
75 proxy_path = getenv("http_proxy");
115329f1 76 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
0e2a2197 77 strstart(proxy_path, "http://", NULL);
de6d9b64
FB
78
79 /* fill the dest addr */
80 redo:
0e2a2197 81 /* needed in any case to build the host string */
115329f1 82 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
0e2a2197
FB
83 path1, sizeof(path1), uri);
84 if (port > 0) {
85 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
de6d9b64 86 } else {
0e2a2197 87 pstrcpy(hoststr, sizeof(hoststr), hostname);
de6d9b64 88 }
0e2a2197 89
de6d9b64 90 if (use_proxy) {
115329f1 91 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
0e2a2197 92 NULL, 0, proxy_path);
de6d9b64
FB
93 path = uri;
94 } else {
0e2a2197 95 if (path1[0] == '\0')
de6d9b64
FB
96 path = "/";
97 else
0e2a2197 98 path = path1;
de6d9b64 99 }
0e2a2197
FB
100 if (port < 0)
101 port = 80;
de6d9b64 102
0e2a2197
FB
103 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
104 err = url_open(&hd, buf, URL_RDWR);
105 if (err < 0)
de6d9b64
FB
106 goto fail;
107
0e2a2197 108 s->hd = hd;
6ba5cbc6 109 if (http_connect(h, path, hoststr, auth) < 0)
de6d9b64
FB
110 goto fail;
111 if (s->http_code == 303 && s->location[0] != '\0') {
112 /* url moved, get next */
113 uri = s->location;
0e2a2197 114 url_close(hd);
de6d9b64
FB
115 goto redo;
116 }
de6d9b64
FB
117 return 0;
118 fail:
0e2a2197
FB
119 if (hd)
120 url_close(hd);
1ea4f593 121 av_free(s);
0bd586c5 122 return AVERROR_IO;
de6d9b64
FB
123}
124
125static int http_getc(HTTPContext *s)
126{
127 int len;
128 if (s->buf_ptr >= s->buf_end) {
0e2a2197 129 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
de6d9b64 130 if (len < 0) {
0bd586c5 131 return AVERROR_IO;
de6d9b64
FB
132 } else if (len == 0) {
133 return -1;
134 } else {
135 s->buf_ptr = s->buffer;
136 s->buf_end = s->buffer + len;
137 }
138 }
139 return *s->buf_ptr++;
140}
141
142static int process_line(HTTPContext *s, char *line, int line_count)
143{
144 char *tag, *p;
115329f1 145
de6d9b64
FB
146 /* end of header */
147 if (line[0] == '\0')
148 return 0;
149
150 p = line;
151 if (line_count == 0) {
152 while (!isspace(*p) && *p != '\0')
153 p++;
154 while (isspace(*p))
155 p++;
156 s->http_code = strtol(p, NULL, 10);
157#ifdef DEBUG
158 printf("http_code=%d\n", s->http_code);
159#endif
160 } else {
161 while (*p != '\0' && *p != ':')
162 p++;
115329f1 163 if (*p != ':')
de6d9b64 164 return 1;
115329f1 165
de6d9b64
FB
166 *p = '\0';
167 tag = line;
168 p++;
169 while (isspace(*p))
170 p++;
171 if (!strcmp(tag, "Location")) {
172 strcpy(s->location, p);
173 }
174 }
175 return 1;
176}
177
6ba5cbc6
PD
178static int http_connect(URLContext *h, const char *path, const char *hoststr,
179 const char *auth)
de6d9b64
FB
180{
181 HTTPContext *s = h->priv_data;
182 int post, err, ch;
183 char line[1024], *q;
184
185
186 /* send http header */
187 post = h->flags & URL_WRONLY;
188
189 snprintf(s->buffer, sizeof(s->buffer),
9eef2b77
MN
190 "%s %s HTTP/1.0\r\n"
191 "User-Agent: %s\r\n"
192 "Accept: */*\r\n"
193 "Host: %s\r\n"
6ba5cbc6 194 "Authorization: Basic %s\r\n"
9eef2b77 195 "\r\n",
de6d9b64
FB
196 post ? "POST" : "GET",
197 path,
4250dafc 198 LIBAVFORMAT_IDENT,
6ba5cbc6
PD
199 hoststr,
200 b64_encode(auth));
115329f1 201
de6d9b64 202 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
0bd586c5 203 return AVERROR_IO;
115329f1 204
de6d9b64
FB
205 /* init input buffer */
206 s->buf_ptr = s->buffer;
207 s->buf_end = s->buffer;
208 s->line_count = 0;
209 s->location[0] = '\0';
ddceb31d
PG
210 if (post) {
211 sleep(1);
de6d9b64 212 return 0;
ddceb31d 213 }
115329f1 214
de6d9b64
FB
215 /* wait for header */
216 q = line;
217 for(;;) {
218 ch = http_getc(s);
219 if (ch < 0)
0bd586c5 220 return AVERROR_IO;
de6d9b64
FB
221 if (ch == '\n') {
222 /* process line */
223 if (q > line && q[-1] == '\r')
224 q--;
225 *q = '\0';
226#ifdef DEBUG
227 printf("header='%s'\n", line);
228#endif
229 err = process_line(s, line, s->line_count);
230 if (err < 0)
231 return err;
232 if (err == 0)
233 return 0;
234 s->line_count++;
235 q = line;
236 } else {
237 if ((q - line) < sizeof(line) - 1)
238 *q++ = ch;
239 }
240 }
241}
242
243
0c1a9eda 244static int http_read(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
245{
246 HTTPContext *s = h->priv_data;
9eef2b77 247 int len;
de6d9b64 248
9eef2b77
MN
249 /* read bytes from input buffer first */
250 len = s->buf_end - s->buf_ptr;
251 if (len > 0) {
252 if (len > size)
253 len = size;
254 memcpy(buf, s->buf_ptr, len);
255 s->buf_ptr += len;
256 } else {
257 len = url_read(s->hd, buf, size);
de6d9b64 258 }
9eef2b77 259 return len;
de6d9b64
FB
260}
261
262/* used only when posting data */
0c1a9eda 263static int http_write(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
264{
265 HTTPContext *s = h->priv_data;
0e2a2197 266 return url_write(s->hd, buf, size);
de6d9b64
FB
267}
268
269static int http_close(URLContext *h)
270{
271 HTTPContext *s = h->priv_data;
0e2a2197
FB
272 url_close(s->hd);
273 av_free(s);
de6d9b64
FB
274 return 0;
275}
276
277URLProtocol http_protocol = {
278 "http",
279 http_open,
280 http_read,
281 http_write,
282 NULL, /* seek */
283 http_close,
284};
0e2a2197 285
6ba5cbc6
PD
286/*****************************************************************************
287 * b64_encode: stolen from VLC's http.c
7028259a 288 * simplified by michael
6ba5cbc6 289 *****************************************************************************/
115329f1 290
88730be6 291static char *b64_encode( const unsigned char *src )
6ba5cbc6
PD
292{
293 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
568e18b1
MN
294 unsigned int len= strlen(src);
295 char *ret, *dst;
6ba5cbc6
PD
296 unsigned i_bits = 0;
297 unsigned i_shift = 0;
568e18b1
MN
298
299 if(len < UINT_MAX/4){
300 ret=dst= av_malloc( len * 4 / 3 + 12 );
301 }else
302 return NULL;
303
7028259a
MN
304 while(*src){
305 i_bits = (i_bits << 8) + *src++;
306 i_shift += 8;
115329f1 307
7028259a
MN
308 do{
309 *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
6ba5cbc6 310 i_shift -= 6;
7028259a 311 }while( i_shift > 6 || (*src == 0 && i_shift>0));
6ba5cbc6 312 }
7028259a
MN
313 *dst++ = '=';
314 *dst = '\0';
115329f1 315
6ba5cbc6
PD
316 return ret;
317}
318