Mercurial > libavformat.hg
annotate http.c @ 1449:64a44155a525 libavformat
now we set codec_tag, still have to figure out how to handle flipping
author | reynaldo |
---|---|
date | Fri, 03 Nov 2006 20:13:42 +0000 |
parents | 4f4bebc36aad |
children | 0a990429e524 |
rev | line source |
---|---|
0 | 1 /* |
2 * HTTP protocol for ffmpeg client | |
3 * Copyright (c) 2000, 2001 Fabrice Bellard. | |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1181
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1181
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1181
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1181
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1181
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 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. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1181
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
21 #include "avformat.h" | |
22 #include <unistd.h> | |
23 #include <sys/types.h> | |
24 #include <sys/socket.h> | |
25 #include <netinet/in.h> | |
26 #ifndef __BEOS__ | |
27 # include <arpa/inet.h> | |
28 #else | |
29 # include "barpainet.h" | |
30 #endif | |
31 #include <netdb.h> | |
32 | |
1432 | 33 #include "base64.h" |
0 | 34 |
35 /* XXX: POST protocol is not completly implemented because ffmpeg use | |
36 only a subset of it */ | |
37 | |
38 //#define DEBUG | |
39 | |
40 /* used for protocol handling */ | |
41 #define BUFFER_SIZE 1024 | |
42 #define URL_SIZE 4096 | |
43 | |
44 typedef struct { | |
45 URLContext *hd; | |
46 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end; | |
47 int line_count; | |
48 int http_code; | |
49 char location[URL_SIZE]; | |
50 } HTTPContext; | |
51 | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
52 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
|
53 const char *auth); |
65 | 54 static int http_write(URLContext *h, uint8_t *buf, int size); |
0 | 55 |
56 | |
57 /* return non zero if error */ | |
58 static int http_open(URLContext *h, const char *uri, int flags) | |
59 { | |
60 const char *path, *proxy_path; | |
61 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
|
62 char auth[1024]; |
0 | 63 char path1[1024]; |
64 char buf[1024]; | |
65 int port, use_proxy, err; | |
66 HTTPContext *s; | |
67 URLContext *hd = NULL; | |
68 | |
69 h->is_streamed = 1; | |
70 | |
71 s = av_malloc(sizeof(HTTPContext)); | |
72 if (!s) { | |
73 return -ENOMEM; | |
74 } | |
75 h->priv_data = s; | |
76 | |
77 proxy_path = getenv("http_proxy"); | |
885 | 78 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && |
0 | 79 strstart(proxy_path, "http://", NULL); |
80 | |
81 /* fill the dest addr */ | |
82 redo: | |
83 /* needed in any case to build the host string */ | |
885 | 84 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
0 | 85 path1, sizeof(path1), uri); |
86 if (port > 0) { | |
87 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port); | |
88 } else { | |
89 pstrcpy(hoststr, sizeof(hoststr), hostname); | |
90 } | |
91 | |
92 if (use_proxy) { | |
885 | 93 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
0 | 94 NULL, 0, proxy_path); |
95 path = uri; | |
96 } else { | |
97 if (path1[0] == '\0') | |
98 path = "/"; | |
99 else | |
100 path = path1; | |
101 } | |
102 if (port < 0) | |
103 port = 80; | |
104 | |
105 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); | |
106 err = url_open(&hd, buf, URL_RDWR); | |
107 if (err < 0) | |
108 goto fail; | |
109 | |
110 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
|
111 if (http_connect(h, path, hoststr, auth) < 0) |
0 | 112 goto fail; |
113 if (s->http_code == 303 && s->location[0] != '\0') { | |
114 /* url moved, get next */ | |
115 uri = s->location; | |
116 url_close(hd); | |
117 goto redo; | |
118 } | |
119 return 0; | |
120 fail: | |
121 if (hd) | |
122 url_close(hd); | |
123 av_free(s); | |
482 | 124 return AVERROR_IO; |
0 | 125 } |
126 | |
127 static int http_getc(HTTPContext *s) | |
128 { | |
129 int len; | |
130 if (s->buf_ptr >= s->buf_end) { | |
131 len = url_read(s->hd, s->buffer, BUFFER_SIZE); | |
132 if (len < 0) { | |
482 | 133 return AVERROR_IO; |
0 | 134 } else if (len == 0) { |
135 return -1; | |
136 } else { | |
137 s->buf_ptr = s->buffer; | |
138 s->buf_end = s->buffer + len; | |
139 } | |
140 } | |
141 return *s->buf_ptr++; | |
142 } | |
143 | |
144 static int process_line(HTTPContext *s, char *line, int line_count) | |
145 { | |
146 char *tag, *p; | |
885 | 147 |
0 | 148 /* end of header */ |
149 if (line[0] == '\0') | |
150 return 0; | |
151 | |
152 p = line; | |
153 if (line_count == 0) { | |
154 while (!isspace(*p) && *p != '\0') | |
155 p++; | |
156 while (isspace(*p)) | |
157 p++; | |
158 s->http_code = strtol(p, NULL, 10); | |
159 #ifdef DEBUG | |
160 printf("http_code=%d\n", s->http_code); | |
161 #endif | |
162 } else { | |
163 while (*p != '\0' && *p != ':') | |
164 p++; | |
885 | 165 if (*p != ':') |
0 | 166 return 1; |
885 | 167 |
0 | 168 *p = '\0'; |
169 tag = line; | |
170 p++; | |
171 while (isspace(*p)) | |
172 p++; | |
173 if (!strcmp(tag, "Location")) { | |
174 strcpy(s->location, p); | |
175 } | |
176 } | |
177 return 1; | |
178 } | |
179 | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
180 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
|
181 const char *auth) |
0 | 182 { |
183 HTTPContext *s = h->priv_data; | |
184 int post, err, ch; | |
185 char line[1024], *q; | |
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
186 char *auth_b64; |
0 | 187 |
188 | |
189 /* send http header */ | |
190 post = h->flags & URL_WRONLY; | |
191 | |
1432 | 192 auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth)); |
0 | 193 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
|
194 "%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
|
195 "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
|
196 "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
|
197 "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
|
198 "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
|
199 "\r\n", |
0 | 200 post ? "POST" : "GET", |
201 path, | |
327 | 202 LIBAVFORMAT_IDENT, |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
203 hoststr, |
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
204 auth_b64); |
885 | 205 |
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
206 av_freep(&auth_b64); |
0 | 207 if (http_write(h, s->buffer, strlen(s->buffer)) < 0) |
482 | 208 return AVERROR_IO; |
885 | 209 |
0 | 210 /* init input buffer */ |
211 s->buf_ptr = s->buffer; | |
212 s->buf_end = s->buffer; | |
213 s->line_count = 0; | |
214 s->location[0] = '\0'; | |
215 if (post) { | |
216 sleep(1); | |
217 return 0; | |
218 } | |
885 | 219 |
0 | 220 /* wait for header */ |
221 q = line; | |
222 for(;;) { | |
223 ch = http_getc(s); | |
224 if (ch < 0) | |
482 | 225 return AVERROR_IO; |
0 | 226 if (ch == '\n') { |
227 /* process line */ | |
228 if (q > line && q[-1] == '\r') | |
229 q--; | |
230 *q = '\0'; | |
231 #ifdef DEBUG | |
232 printf("header='%s'\n", line); | |
233 #endif | |
234 err = process_line(s, line, s->line_count); | |
235 if (err < 0) | |
236 return err; | |
237 if (err == 0) | |
238 return 0; | |
239 s->line_count++; | |
240 q = line; | |
241 } else { | |
242 if ((q - line) < sizeof(line) - 1) | |
243 *q++ = ch; | |
244 } | |
245 } | |
246 } | |
247 | |
248 | |
65 | 249 static int http_read(URLContext *h, uint8_t *buf, int size) |
0 | 250 { |
251 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
|
252 int len; |
0 | 253 |
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
|
254 /* 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
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 } 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
|
262 len = url_read(s->hd, buf, size); |
0 | 263 } |
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
|
264 return len; |
0 | 265 } |
266 | |
267 /* used only when posting data */ | |
65 | 268 static int http_write(URLContext *h, uint8_t *buf, int size) |
0 | 269 { |
270 HTTPContext *s = h->priv_data; | |
271 return url_write(s->hd, buf, size); | |
272 } | |
273 | |
274 static int http_close(URLContext *h) | |
275 { | |
276 HTTPContext *s = h->priv_data; | |
277 url_close(s->hd); | |
278 av_free(s); | |
279 return 0; | |
280 } | |
281 | |
282 URLProtocol http_protocol = { | |
283 "http", | |
284 http_open, | |
285 http_read, | |
286 http_write, | |
287 NULL, /* seek */ | |
288 http_close, | |
289 }; |