Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * HTTP protocol for ffmpeg client | |
19720f15 | 3 | * Copyright (c) 2000, 2001 Fabrice Bellard. |
de6d9b64 | 4 | * |
b78e7197 DB |
5 | * This file is part of FFmpeg. |
6 | * | |
7 | * FFmpeg is free software; you can redistribute it and/or | |
19720f15 FB |
8 | * modify it under the terms of the GNU Lesser General Public |
9 | * License as published by the Free Software Foundation; either | |
b78e7197 | 10 | * version 2.1 of the License, or (at your option) any later version. |
de6d9b64 | 11 | * |
b78e7197 | 12 | * FFmpeg is distributed in the hope that it will be useful, |
de6d9b64 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 FB |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Lesser General Public License for more details. | |
de6d9b64 | 16 | * |
19720f15 | 17 | * You should have received a copy of the GNU Lesser General Public |
b78e7197 | 18 | * License along with FFmpeg; if not, write to the Free Software |
5509bffa | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 | 20 | */ |
8be1c656 | 21 | #include "avformat.h" |
de6d9b64 | 22 | #include <unistd.h> |
de6d9b64 | 23 | #include <sys/types.h> |
b8a78f41 | 24 | #include <sys/socket.h> |
de6d9b64 | 25 | #include <netinet/in.h> |
9ddd71fc FR |
26 | #ifndef __BEOS__ |
27 | # include <arpa/inet.h> | |
28 | #else | |
29 | # include "barpainet.h" | |
30 | #endif | |
de6d9b64 FB |
31 | #include <netdb.h> |
32 | ||
a7c6aa73 | 33 | #include "base64.h" |
de6d9b64 FB |
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 { | |
0e2a2197 | 45 | URLContext *hd; |
de6d9b64 FB |
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 | ||
6ba5cbc6 PD |
52 | static int http_connect(URLContext *h, const char *path, const char *hoststr, |
53 | const char *auth); | |
0c1a9eda | 54 | static int http_write(URLContext *h, uint8_t *buf, int size); |
de6d9b64 | 55 | |
0e2a2197 | 56 | |
de6d9b64 FB |
57 | /* return non zero if error */ |
58 | static int http_open(URLContext *h, const char *uri, int flags) | |
59 | { | |
0e2a2197 FB |
60 | const char *path, *proxy_path; |
61 | char hostname[1024], hoststr[1024]; | |
6ba5cbc6 | 62 | char auth[1024]; |
0e2a2197 FB |
63 | char path1[1024]; |
64 | char buf[1024]; | |
65 | int port, use_proxy, err; | |
de6d9b64 | 66 | HTTPContext *s; |
0e2a2197 | 67 | URLContext *hd = NULL; |
de6d9b64 FB |
68 | |
69 | h->is_streamed = 1; | |
70 | ||
1ea4f593 | 71 | s = av_malloc(sizeof(HTTPContext)); |
de6d9b64 FB |
72 | if (!s) { |
73 | return -ENOMEM; | |
74 | } | |
75 | h->priv_data = s; | |
76 | ||
77 | proxy_path = getenv("http_proxy"); | |
115329f1 | 78 | use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && |
0e2a2197 | 79 | strstart(proxy_path, "http://", NULL); |
de6d9b64 FB |
80 | |
81 | /* fill the dest addr */ | |
82 | redo: | |
0e2a2197 | 83 | /* needed in any case to build the host string */ |
115329f1 | 84 | url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
0e2a2197 FB |
85 | path1, sizeof(path1), uri); |
86 | if (port > 0) { | |
87 | snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port); | |
de6d9b64 | 88 | } else { |
0e2a2197 | 89 | pstrcpy(hoststr, sizeof(hoststr), hostname); |
de6d9b64 | 90 | } |
0e2a2197 | 91 | |
de6d9b64 | 92 | if (use_proxy) { |
115329f1 | 93 | url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
0e2a2197 | 94 | NULL, 0, proxy_path); |
de6d9b64 FB |
95 | path = uri; |
96 | } else { | |
0e2a2197 | 97 | if (path1[0] == '\0') |
de6d9b64 FB |
98 | path = "/"; |
99 | else | |
0e2a2197 | 100 | path = path1; |
de6d9b64 | 101 | } |
0e2a2197 FB |
102 | if (port < 0) |
103 | port = 80; | |
de6d9b64 | 104 | |
0e2a2197 FB |
105 | snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); |
106 | err = url_open(&hd, buf, URL_RDWR); | |
107 | if (err < 0) | |
de6d9b64 FB |
108 | goto fail; |
109 | ||
0e2a2197 | 110 | s->hd = hd; |
6ba5cbc6 | 111 | if (http_connect(h, path, hoststr, auth) < 0) |
de6d9b64 FB |
112 | goto fail; |
113 | if (s->http_code == 303 && s->location[0] != '\0') { | |
114 | /* url moved, get next */ | |
115 | uri = s->location; | |
0e2a2197 | 116 | url_close(hd); |
de6d9b64 FB |
117 | goto redo; |
118 | } | |
de6d9b64 FB |
119 | return 0; |
120 | fail: | |
0e2a2197 FB |
121 | if (hd) |
122 | url_close(hd); | |
1ea4f593 | 123 | av_free(s); |
0bd586c5 | 124 | return AVERROR_IO; |
de6d9b64 FB |
125 | } |
126 | ||
127 | static int http_getc(HTTPContext *s) | |
128 | { | |
129 | int len; | |
130 | if (s->buf_ptr >= s->buf_end) { | |
0e2a2197 | 131 | len = url_read(s->hd, s->buffer, BUFFER_SIZE); |
de6d9b64 | 132 | if (len < 0) { |
0bd586c5 | 133 | return AVERROR_IO; |
de6d9b64 FB |
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; | |
115329f1 | 147 | |
de6d9b64 FB |
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++; | |
115329f1 | 165 | if (*p != ':') |
de6d9b64 | 166 | return 1; |
115329f1 | 167 | |
de6d9b64 FB |
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 | ||
6ba5cbc6 PD |
180 | static int http_connect(URLContext *h, const char *path, const char *hoststr, |
181 | const char *auth) | |
de6d9b64 FB |
182 | { |
183 | HTTPContext *s = h->priv_data; | |
184 | int post, err, ch; | |
185 | char line[1024], *q; | |
cfc4bd46 | 186 | char *auth_b64; |
de6d9b64 FB |
187 | |
188 | ||
189 | /* send http header */ | |
190 | post = h->flags & URL_WRONLY; | |
191 | ||
a7c6aa73 | 192 | auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth)); |
de6d9b64 | 193 | snprintf(s->buffer, sizeof(s->buffer), |
9eef2b77 MN |
194 | "%s %s HTTP/1.0\r\n" |
195 | "User-Agent: %s\r\n" | |
196 | "Accept: */*\r\n" | |
197 | "Host: %s\r\n" | |
6ba5cbc6 | 198 | "Authorization: Basic %s\r\n" |
9eef2b77 | 199 | "\r\n", |
de6d9b64 FB |
200 | post ? "POST" : "GET", |
201 | path, | |
4250dafc | 202 | LIBAVFORMAT_IDENT, |
6ba5cbc6 | 203 | hoststr, |
cfc4bd46 | 204 | auth_b64); |
115329f1 | 205 | |
cfc4bd46 | 206 | av_freep(&auth_b64); |
de6d9b64 | 207 | if (http_write(h, s->buffer, strlen(s->buffer)) < 0) |
0bd586c5 | 208 | return AVERROR_IO; |
115329f1 | 209 | |
de6d9b64 FB |
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'; | |
ddceb31d PG |
215 | if (post) { |
216 | sleep(1); | |
de6d9b64 | 217 | return 0; |
ddceb31d | 218 | } |
115329f1 | 219 | |
de6d9b64 FB |
220 | /* wait for header */ |
221 | q = line; | |
222 | for(;;) { | |
223 | ch = http_getc(s); | |
224 | if (ch < 0) | |
0bd586c5 | 225 | return AVERROR_IO; |
de6d9b64 FB |
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 | ||
0c1a9eda | 249 | static int http_read(URLContext *h, uint8_t *buf, int size) |
de6d9b64 FB |
250 | { |
251 | HTTPContext *s = h->priv_data; | |
9eef2b77 | 252 | int len; |
de6d9b64 | 253 | |
9eef2b77 MN |
254 | /* read bytes from input buffer first */ |
255 | len = s->buf_end - s->buf_ptr; | |
256 | if (len > 0) { | |
257 | if (len > size) | |
258 | len = size; | |
259 | memcpy(buf, s->buf_ptr, len); | |
260 | s->buf_ptr += len; | |
261 | } else { | |
262 | len = url_read(s->hd, buf, size); | |
de6d9b64 | 263 | } |
9eef2b77 | 264 | return len; |
de6d9b64 FB |
265 | } |
266 | ||
267 | /* used only when posting data */ | |
0c1a9eda | 268 | static int http_write(URLContext *h, uint8_t *buf, int size) |
de6d9b64 FB |
269 | { |
270 | HTTPContext *s = h->priv_data; | |
0e2a2197 | 271 | return url_write(s->hd, buf, size); |
de6d9b64 FB |
272 | } |
273 | ||
274 | static int http_close(URLContext *h) | |
275 | { | |
276 | HTTPContext *s = h->priv_data; | |
0e2a2197 FB |
277 | url_close(s->hd); |
278 | av_free(s); | |
de6d9b64 FB |
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 | }; |