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