Mercurial > libavformat.hg
annotate http.c @ 1340:79f90a950fe1 libavformat
add string param to PRINT_KEY for debug
author | bcoudurier |
---|---|
date | Fri, 29 Sep 2006 11:25:40 +0000 |
parents | c2f51d81c72e |
children | 0899bfe4105c |
rev | line source |
---|---|
0 | 1 /* |
2 * HTTP protocol for ffmpeg client | |
3 * Copyright (c) 2000, 2001 Fabrice Bellard. | |
4 * | |
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. | |
9 * | |
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. | |
14 * | |
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 | |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 */ |
19 #include "avformat.h" | |
20 #include <unistd.h> | |
21 #include <sys/types.h> | |
22 #include <sys/socket.h> | |
23 #include <netinet/in.h> | |
24 #ifndef __BEOS__ | |
25 # include <arpa/inet.h> | |
26 #else | |
27 # include "barpainet.h" | |
28 #endif | |
29 #include <netdb.h> | |
30 | |
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 | |
41 typedef struct { | |
42 URLContext *hd; | |
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 | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
49 static int http_connect(URLContext *h, const char *path, const char *hoststr, |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
50 const char *auth); |
65 | 51 static int http_write(URLContext *h, uint8_t *buf, int size); |
683
095009fc2f35
kill warnings patch by (Mns Rullgrd <mru inprovide com>)
michael
parents:
639
diff
changeset
|
52 static char *b64_encode(const unsigned char *src ); |
0 | 53 |
54 | |
55 /* return non zero if error */ | |
56 static int http_open(URLContext *h, const char *uri, int flags) | |
57 { | |
58 const char *path, *proxy_path; | |
59 char hostname[1024], hoststr[1024]; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
60 char auth[1024]; |
0 | 61 char path1[1024]; |
62 char buf[1024]; | |
63 int port, use_proxy, err; | |
64 HTTPContext *s; | |
65 URLContext *hd = NULL; | |
66 | |
67 h->is_streamed = 1; | |
68 | |
69 s = av_malloc(sizeof(HTTPContext)); | |
70 if (!s) { | |
71 return -ENOMEM; | |
72 } | |
73 h->priv_data = s; | |
74 | |
75 proxy_path = getenv("http_proxy"); | |
885 | 76 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && |
0 | 77 strstart(proxy_path, "http://", NULL); |
78 | |
79 /* fill the dest addr */ | |
80 redo: | |
81 /* needed in any case to build the host string */ | |
885 | 82 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
0 | 83 path1, sizeof(path1), uri); |
84 if (port > 0) { | |
85 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port); | |
86 } else { | |
87 pstrcpy(hoststr, sizeof(hoststr), hostname); | |
88 } | |
89 | |
90 if (use_proxy) { | |
885 | 91 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
0 | 92 NULL, 0, proxy_path); |
93 path = uri; | |
94 } else { | |
95 if (path1[0] == '\0') | |
96 path = "/"; | |
97 else | |
98 path = path1; | |
99 } | |
100 if (port < 0) | |
101 port = 80; | |
102 | |
103 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); | |
104 err = url_open(&hd, buf, URL_RDWR); | |
105 if (err < 0) | |
106 goto fail; | |
107 | |
108 s->hd = hd; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
109 if (http_connect(h, path, hoststr, auth) < 0) |
0 | 110 goto fail; |
111 if (s->http_code == 303 && s->location[0] != '\0') { | |
112 /* url moved, get next */ | |
113 uri = s->location; | |
114 url_close(hd); | |
115 goto redo; | |
116 } | |
117 return 0; | |
118 fail: | |
119 if (hd) | |
120 url_close(hd); | |
121 av_free(s); | |
482 | 122 return AVERROR_IO; |
0 | 123 } |
124 | |
125 static int http_getc(HTTPContext *s) | |
126 { | |
127 int len; | |
128 if (s->buf_ptr >= s->buf_end) { | |
129 len = url_read(s->hd, s->buffer, BUFFER_SIZE); | |
130 if (len < 0) { | |
482 | 131 return AVERROR_IO; |
0 | 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 | |
142 static int process_line(HTTPContext *s, char *line, int line_count) | |
143 { | |
144 char *tag, *p; | |
885 | 145 |
0 | 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++; | |
885 | 163 if (*p != ':') |
0 | 164 return 1; |
885 | 165 |
0 | 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 | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
178 static int http_connect(URLContext *h, const char *path, const char *hoststr, |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
179 const char *auth) |
0 | 180 { |
181 HTTPContext *s = h->priv_data; | |
182 int post, err, ch; | |
183 char line[1024], *q; | |
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
184 char *auth_b64; |
0 | 185 |
186 | |
187 /* send http header */ | |
188 post = h->flags & URL_WRONLY; | |
189 | |
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
190 auth_b64 = b64_encode(auth); |
0 | 191 snprintf(s->buffer, sizeof(s->buffer), |
385
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
192 "%s %s HTTP/1.0\r\n" |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
193 "User-Agent: %s\r\n" |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
194 "Accept: */*\r\n" |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
195 "Host: %s\r\n" |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
196 "Authorization: Basic %s\r\n" |
385
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
197 "\r\n", |
0 | 198 post ? "POST" : "GET", |
199 path, | |
327 | 200 LIBAVFORMAT_IDENT, |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
201 hoststr, |
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
202 auth_b64); |
885 | 203 |
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
204 av_freep(&auth_b64); |
0 | 205 if (http_write(h, s->buffer, strlen(s->buffer)) < 0) |
482 | 206 return AVERROR_IO; |
885 | 207 |
0 | 208 /* init input buffer */ |
209 s->buf_ptr = s->buffer; | |
210 s->buf_end = s->buffer; | |
211 s->line_count = 0; | |
212 s->location[0] = '\0'; | |
213 if (post) { | |
214 sleep(1); | |
215 return 0; | |
216 } | |
885 | 217 |
0 | 218 /* wait for header */ |
219 q = line; | |
220 for(;;) { | |
221 ch = http_getc(s); | |
222 if (ch < 0) | |
482 | 223 return AVERROR_IO; |
0 | 224 if (ch == '\n') { |
225 /* process line */ | |
226 if (q > line && q[-1] == '\r') | |
227 q--; | |
228 *q = '\0'; | |
229 #ifdef DEBUG | |
230 printf("header='%s'\n", line); | |
231 #endif | |
232 err = process_line(s, line, s->line_count); | |
233 if (err < 0) | |
234 return err; | |
235 if (err == 0) | |
236 return 0; | |
237 s->line_count++; | |
238 q = line; | |
239 } else { | |
240 if ((q - line) < sizeof(line) - 1) | |
241 *q++ = ch; | |
242 } | |
243 } | |
244 } | |
245 | |
246 | |
65 | 247 static int http_read(URLContext *h, uint8_t *buf, int size) |
0 | 248 { |
249 HTTPContext *s = h->priv_data; | |
385
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
250 int len; |
0 | 251 |
385
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
252 /* read bytes from input buffer first */ |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
253 len = s->buf_end - s->buf_ptr; |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
254 if (len > 0) { |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
255 if (len > size) |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
256 len = size; |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
257 memcpy(buf, s->buf_ptr, len); |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
258 s->buf_ptr += len; |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
259 } else { |
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
260 len = url_read(s->hd, buf, size); |
0 | 261 } |
385
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
327
diff
changeset
|
262 return len; |
0 | 263 } |
264 | |
265 /* used only when posting data */ | |
65 | 266 static int http_write(URLContext *h, uint8_t *buf, int size) |
0 | 267 { |
268 HTTPContext *s = h->priv_data; | |
269 return url_write(s->hd, buf, size); | |
270 } | |
271 | |
272 static int http_close(URLContext *h) | |
273 { | |
274 HTTPContext *s = h->priv_data; | |
275 url_close(s->hd); | |
276 av_free(s); | |
277 return 0; | |
278 } | |
279 | |
280 URLProtocol http_protocol = { | |
281 "http", | |
282 http_open, | |
283 http_read, | |
284 http_write, | |
285 NULL, /* seek */ | |
286 http_close, | |
287 }; | |
288 | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
289 /***************************************************************************** |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
290 * b64_encode: stolen from VLC's http.c |
1180 | 291 * simplified by michael |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
292 *****************************************************************************/ |
885 | 293 |
683
095009fc2f35
kill warnings patch by (Mns Rullgrd <mru inprovide com>)
michael
parents:
639
diff
changeset
|
294 static char *b64_encode( const unsigned char *src ) |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
295 { |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
296 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
639 | 297 unsigned int len= strlen(src); |
298 char *ret, *dst; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
299 unsigned i_bits = 0; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
300 unsigned i_shift = 0; |
639 | 301 |
302 if(len < UINT_MAX/4){ | |
303 ret=dst= av_malloc( len * 4 / 3 + 12 ); | |
304 }else | |
305 return NULL; | |
306 | |
1180 | 307 while(*src){ |
308 i_bits = (i_bits << 8) + *src++; | |
309 i_shift += 8; | |
885 | 310 |
1180 | 311 do{ |
312 *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f]; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
313 i_shift -= 6; |
1180 | 314 }while( i_shift > 6 || (*src == 0 && i_shift>0)); |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
315 } |
1180 | 316 *dst++ = '='; |
317 *dst = '\0'; | |
885 | 318 |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
319 return ret; |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
320 } |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
321 |