Mercurial > libavformat.hg
annotate http.c @ 1787:eb16c64144ee libavformat
This fixes error handling for BeOS, removing the need for some ifdefs.
AVERROR_ defines are moved to avcodec.h as they are needed in there as well. Feel free to move that to avutil/common.h.
Bumped up avcodec/format version numbers as though it's binary compatible we will want to rebuild apps as error values changed.
Please from now on use return AVERROR(EFOO) instead of the ugly return -EFOO in your code.
This also removes the need for berrno.h.
| author | mmu_man |
|---|---|
| date | Tue, 13 Feb 2007 18:26:14 +0000 |
| parents | 1f7a6dc01100 |
| children | ec025d5fbbe2 |
| 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> | |
| 1754 | 23 #include "network.h" |
| 0 | 24 |
| 1432 | 25 #include "base64.h" |
| 0 | 26 |
| 27 /* XXX: POST protocol is not completly implemented because ffmpeg use | |
| 28 only a subset of it */ | |
| 29 | |
| 30 //#define DEBUG | |
| 31 | |
| 32 /* used for protocol handling */ | |
| 33 #define BUFFER_SIZE 1024 | |
| 34 #define URL_SIZE 4096 | |
| 1647 | 35 #define MAX_REDIRECTS 8 |
| 0 | 36 |
| 37 typedef struct { | |
| 38 URLContext *hd; | |
| 39 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end; | |
| 40 int line_count; | |
| 41 int http_code; | |
| 1647 | 42 offset_t off, filesize; |
| 0 | 43 char location[URL_SIZE]; |
| 44 } HTTPContext; | |
| 45 | |
|
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
46 static int http_connect(URLContext *h, const char *path, const char *hoststr, |
| 1647 | 47 const char *auth, int *new_location); |
| 65 | 48 static int http_write(URLContext *h, uint8_t *buf, int size); |
| 0 | 49 |
| 50 | |
| 51 /* return non zero if error */ | |
| 1647 | 52 static int http_open_cnx(URLContext *h) |
| 0 | 53 { |
| 54 const char *path, *proxy_path; | |
| 55 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
|
56 char auth[1024]; |
| 0 | 57 char path1[1024]; |
| 58 char buf[1024]; | |
| 1647 | 59 int port, use_proxy, err, location_changed = 0, redirects = 0; |
| 60 HTTPContext *s = h->priv_data; | |
| 0 | 61 URLContext *hd = NULL; |
| 62 | |
| 63 proxy_path = getenv("http_proxy"); | |
| 885 | 64 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") && |
| 0 | 65 strstart(proxy_path, "http://", NULL); |
| 66 | |
| 67 /* fill the dest addr */ | |
| 68 redo: | |
| 69 /* needed in any case to build the host string */ | |
| 885 | 70 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
| 1647 | 71 path1, sizeof(path1), s->location); |
| 0 | 72 if (port > 0) { |
| 73 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port); | |
| 74 } else { | |
| 75 pstrcpy(hoststr, sizeof(hoststr), hostname); | |
| 76 } | |
| 77 | |
| 78 if (use_proxy) { | |
| 885 | 79 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, |
| 0 | 80 NULL, 0, proxy_path); |
| 1647 | 81 path = s->location; |
| 0 | 82 } else { |
| 83 if (path1[0] == '\0') | |
| 84 path = "/"; | |
| 85 else | |
| 86 path = path1; | |
| 87 } | |
| 88 if (port < 0) | |
| 89 port = 80; | |
| 90 | |
| 91 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port); | |
| 92 err = url_open(&hd, buf, URL_RDWR); | |
| 93 if (err < 0) | |
| 94 goto fail; | |
| 95 | |
| 96 s->hd = hd; | |
| 1647 | 97 if (http_connect(h, path, hoststr, auth, &location_changed) < 0) |
| 0 | 98 goto fail; |
| 1647 | 99 if (s->http_code == 303 && location_changed == 1) { |
| 0 | 100 /* url moved, get next */ |
| 101 url_close(hd); | |
| 1647 | 102 if (redirects++ >= MAX_REDIRECTS) |
| 103 return AVERROR_IO; | |
| 104 location_changed = 0; | |
| 0 | 105 goto redo; |
| 106 } | |
| 107 return 0; | |
| 108 fail: | |
| 109 if (hd) | |
| 110 url_close(hd); | |
| 482 | 111 return AVERROR_IO; |
| 0 | 112 } |
| 113 | |
| 1647 | 114 static int http_open(URLContext *h, const char *uri, int flags) |
| 115 { | |
| 116 HTTPContext *s; | |
| 117 int ret; | |
| 118 | |
| 119 h->is_streamed = 1; | |
| 120 | |
| 121 s = av_malloc(sizeof(HTTPContext)); | |
| 122 if (!s) { | |
|
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1754
diff
changeset
|
123 return AVERROR(ENOMEM); |
| 1647 | 124 } |
| 125 h->priv_data = s; | |
| 126 s->filesize = -1; | |
| 127 s->off = 0; | |
| 128 pstrcpy (s->location, URL_SIZE, uri); | |
| 129 | |
| 130 ret = http_open_cnx(h); | |
| 131 if (ret != 0) | |
| 132 av_free (s); | |
| 133 return ret; | |
| 134 } | |
| 0 | 135 static int http_getc(HTTPContext *s) |
| 136 { | |
| 137 int len; | |
| 138 if (s->buf_ptr >= s->buf_end) { | |
| 139 len = url_read(s->hd, s->buffer, BUFFER_SIZE); | |
| 140 if (len < 0) { | |
| 482 | 141 return AVERROR_IO; |
| 0 | 142 } else if (len == 0) { |
| 143 return -1; | |
| 144 } else { | |
| 145 s->buf_ptr = s->buffer; | |
| 146 s->buf_end = s->buffer + len; | |
| 147 } | |
| 148 } | |
| 149 return *s->buf_ptr++; | |
| 150 } | |
| 151 | |
| 1647 | 152 static int process_line(URLContext *h, char *line, int line_count, |
| 153 int *new_location) | |
| 0 | 154 { |
| 1647 | 155 HTTPContext *s = h->priv_data; |
| 0 | 156 char *tag, *p; |
| 885 | 157 |
| 0 | 158 /* end of header */ |
| 159 if (line[0] == '\0') | |
| 160 return 0; | |
| 161 | |
| 162 p = line; | |
| 163 if (line_count == 0) { | |
| 164 while (!isspace(*p) && *p != '\0') | |
| 165 p++; | |
| 166 while (isspace(*p)) | |
| 167 p++; | |
| 168 s->http_code = strtol(p, NULL, 10); | |
| 169 #ifdef DEBUG | |
| 170 printf("http_code=%d\n", s->http_code); | |
| 171 #endif | |
| 172 } else { | |
| 173 while (*p != '\0' && *p != ':') | |
| 174 p++; | |
| 885 | 175 if (*p != ':') |
| 0 | 176 return 1; |
| 885 | 177 |
| 0 | 178 *p = '\0'; |
| 179 tag = line; | |
| 180 p++; | |
| 181 while (isspace(*p)) | |
| 182 p++; | |
| 183 if (!strcmp(tag, "Location")) { | |
| 184 strcpy(s->location, p); | |
| 1647 | 185 *new_location = 1; |
| 186 } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) { | |
| 187 s->filesize = atoll(p); | |
| 188 } else if (!strcmp (tag, "Content-Range")) { | |
| 189 /* "bytes $from-$to/$document_size" */ | |
| 190 const char *slash; | |
| 191 if (!strncmp (p, "bytes ", 6)) { | |
| 192 p += 6; | |
| 193 s->off = atoll(p); | |
| 194 if ((slash = strchr(p, '/')) && strlen(slash) > 0) | |
| 195 s->filesize = atoll(slash+1); | |
| 196 } | |
| 197 h->is_streamed = 0; /* we _can_ in fact seek */ | |
| 0 | 198 } |
| 199 } | |
| 200 return 1; | |
| 201 } | |
| 202 | |
|
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
203 static int http_connect(URLContext *h, const char *path, const char *hoststr, |
| 1647 | 204 const char *auth, int *new_location) |
| 0 | 205 { |
| 206 HTTPContext *s = h->priv_data; | |
| 207 int post, err, ch; | |
| 208 char line[1024], *q; | |
|
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
209 char *auth_b64; |
| 1647 | 210 offset_t off = s->off; |
| 0 | 211 |
| 212 | |
| 213 /* send http header */ | |
| 214 post = h->flags & URL_WRONLY; | |
| 215 | |
| 1432 | 216 auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth)); |
| 0 | 217 snprintf(s->buffer, sizeof(s->buffer), |
| 1647 | 218 "%s %s HTTP/1.1\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
|
219 "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
|
220 "Accept: */*\r\n" |
| 1737 | 221 "Range: bytes=%"PRId64"-\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
|
222 "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
|
223 "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
|
224 "\r\n", |
| 0 | 225 post ? "POST" : "GET", |
| 226 path, | |
| 327 | 227 LIBAVFORMAT_IDENT, |
| 1647 | 228 s->off, |
|
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
229 hoststr, |
|
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
230 auth_b64); |
| 885 | 231 |
|
1181
c2f51d81c72e
Fix memleak, patch by I. Po % yyymmmm # gmail O com %
gpoirier
parents:
1180
diff
changeset
|
232 av_freep(&auth_b64); |
| 0 | 233 if (http_write(h, s->buffer, strlen(s->buffer)) < 0) |
| 482 | 234 return AVERROR_IO; |
| 885 | 235 |
| 0 | 236 /* init input buffer */ |
| 237 s->buf_ptr = s->buffer; | |
| 238 s->buf_end = s->buffer; | |
| 239 s->line_count = 0; | |
| 1647 | 240 s->off = 0; |
| 0 | 241 if (post) { |
| 242 sleep(1); | |
| 243 return 0; | |
| 244 } | |
| 885 | 245 |
| 0 | 246 /* wait for header */ |
| 247 q = line; | |
| 248 for(;;) { | |
| 249 ch = http_getc(s); | |
| 250 if (ch < 0) | |
| 482 | 251 return AVERROR_IO; |
| 0 | 252 if (ch == '\n') { |
| 253 /* process line */ | |
| 254 if (q > line && q[-1] == '\r') | |
| 255 q--; | |
| 256 *q = '\0'; | |
| 257 #ifdef DEBUG | |
| 258 printf("header='%s'\n", line); | |
| 259 #endif | |
| 1647 | 260 err = process_line(h, line, s->line_count, new_location); |
| 0 | 261 if (err < 0) |
| 262 return err; | |
| 263 if (err == 0) | |
| 1647 | 264 break; |
| 0 | 265 s->line_count++; |
| 266 q = line; | |
| 267 } else { | |
| 268 if ((q - line) < sizeof(line) - 1) | |
| 269 *q++ = ch; | |
| 270 } | |
| 271 } | |
| 1647 | 272 |
| 273 return (off == s->off) ? 0 : -1; | |
| 0 | 274 } |
| 275 | |
| 276 | |
| 65 | 277 static int http_read(URLContext *h, uint8_t *buf, int size) |
| 0 | 278 { |
| 279 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
|
280 int len; |
| 0 | 281 |
|
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
|
282 /* 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
|
283 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
|
284 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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 } 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
|
290 len = url_read(s->hd, buf, size); |
| 0 | 291 } |
| 1647 | 292 if (len > 0) |
| 293 s->off += len; | |
|
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
|
294 return len; |
| 0 | 295 } |
| 296 | |
| 297 /* used only when posting data */ | |
| 65 | 298 static int http_write(URLContext *h, uint8_t *buf, int size) |
| 0 | 299 { |
| 300 HTTPContext *s = h->priv_data; | |
| 301 return url_write(s->hd, buf, size); | |
| 302 } | |
| 303 | |
| 304 static int http_close(URLContext *h) | |
| 305 { | |
| 306 HTTPContext *s = h->priv_data; | |
| 307 url_close(s->hd); | |
| 308 av_free(s); | |
| 309 return 0; | |
| 310 } | |
| 311 | |
| 1647 | 312 static offset_t http_seek(URLContext *h, offset_t off, int whence) |
| 313 { | |
| 314 HTTPContext *s = h->priv_data; | |
| 315 URLContext *old_hd = s->hd; | |
| 316 offset_t old_off = s->off; | |
| 317 | |
| 318 if (whence == AVSEEK_SIZE) | |
| 319 return s->filesize; | |
| 320 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed) | |
| 321 return -1; | |
| 322 | |
| 323 /* we save the old context in case the seek fails */ | |
| 324 s->hd = NULL; | |
| 325 if (whence == SEEK_CUR) | |
| 326 off += s->off; | |
| 327 else if (whence == SEEK_END) | |
| 328 off += s->filesize; | |
| 329 s->off = off; | |
| 330 | |
| 331 /* if it fails, continue on old connection */ | |
| 332 if (http_open_cnx(h) < 0) { | |
| 333 s->hd = old_hd; | |
| 334 s->off = old_off; | |
| 335 return -1; | |
| 336 } | |
| 337 url_close(old_hd); | |
| 338 return off; | |
| 339 } | |
| 340 | |
| 0 | 341 URLProtocol http_protocol = { |
| 342 "http", | |
| 343 http_open, | |
| 344 http_read, | |
| 345 http_write, | |
| 1647 | 346 http_seek, |
| 0 | 347 http_close, |
| 348 }; |
