00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avstring.h"
00023 #include "avformat.h"
00024 #include <unistd.h>
00025 #include <strings.h>
00026 #include "internal.h"
00027 #include "network.h"
00028 #include "http.h"
00029 #include "os_support.h"
00030 #include "httpauth.h"
00031 #include "libavutil/opt.h"
00032
00033
00034
00035
00036
00037 #define BUFFER_SIZE 1024
00038 #define MAX_REDIRECTS 8
00039
00040 typedef struct {
00041 const AVClass *class;
00042 URLContext *hd;
00043 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
00044 int line_count;
00045 int http_code;
00046 int64_t chunksize;
00047 int64_t off, filesize;
00048 char location[MAX_URL_SIZE];
00049 HTTPAuthState auth_state;
00050 unsigned char headers[BUFFER_SIZE];
00051 int willclose;
00052 } HTTPContext;
00053
00054 #define OFFSET(x) offsetof(HTTPContext, x)
00055 static const AVOption options[] = {
00056 {"chunksize", "use chunked transfer-encoding for posts, -1 disables it, 0 enables it", OFFSET(chunksize), FF_OPT_TYPE_INT64, 0, -1, 0 },
00057 {NULL}
00058 };
00059 static const AVClass httpcontext_class = {
00060 "HTTP", av_default_item_name, options, LIBAVUTIL_VERSION_INT
00061 };
00062
00063 static int http_connect(URLContext *h, const char *path, const char *hoststr,
00064 const char *auth, int *new_location);
00065
00066 void ff_http_set_headers(URLContext *h, const char *headers)
00067 {
00068 HTTPContext *s = h->priv_data;
00069 int len = strlen(headers);
00070
00071 if (len && strcmp("\r\n", headers + len - 2))
00072 av_log(NULL, AV_LOG_ERROR, "No trailing CRLF found in HTTP header.\n");
00073
00074 av_strlcpy(s->headers, headers, sizeof(s->headers));
00075 }
00076
00077 void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked)
00078 {
00079 ((HTTPContext*)h->priv_data)->chunksize = is_chunked ? 0 : -1;
00080 }
00081
00082 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
00083 {
00084 memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
00085 &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
00086 }
00087
00088
00089 static int http_open_cnx(URLContext *h)
00090 {
00091 const char *path, *proxy_path;
00092 char hostname[1024], hoststr[1024];
00093 char auth[1024];
00094 char path1[1024];
00095 char buf[1024];
00096 int port, use_proxy, err, location_changed = 0, redirects = 0;
00097 HTTPAuthType cur_auth_type;
00098 HTTPContext *s = h->priv_data;
00099 URLContext *hd = NULL;
00100
00101 proxy_path = getenv("http_proxy");
00102 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
00103 av_strstart(proxy_path, "http://", NULL);
00104
00105
00106 redo:
00107
00108 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
00109 path1, sizeof(path1), s->location);
00110 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
00111
00112 if (use_proxy) {
00113 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
00114 NULL, 0, proxy_path);
00115 path = s->location;
00116 } else {
00117 if (path1[0] == '\0')
00118 path = "/";
00119 else
00120 path = path1;
00121 }
00122 if (port < 0)
00123 port = 80;
00124
00125 ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
00126 err = url_open(&hd, buf, URL_RDWR);
00127 if (err < 0)
00128 goto fail;
00129
00130 s->hd = hd;
00131 cur_auth_type = s->auth_state.auth_type;
00132 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
00133 goto fail;
00134 if (s->http_code == 401) {
00135 if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
00136 url_close(hd);
00137 goto redo;
00138 } else
00139 goto fail;
00140 }
00141 if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
00142 && location_changed == 1) {
00143
00144 url_close(hd);
00145 if (redirects++ >= MAX_REDIRECTS)
00146 return AVERROR(EIO);
00147 location_changed = 0;
00148 goto redo;
00149 }
00150 return 0;
00151 fail:
00152 if (hd)
00153 url_close(hd);
00154 s->hd = NULL;
00155 return AVERROR(EIO);
00156 }
00157
00158 static int http_open(URLContext *h, const char *uri, int flags)
00159 {
00160 HTTPContext *s = h->priv_data;
00161
00162 h->is_streamed = 1;
00163
00164 s->filesize = -1;
00165 av_strlcpy(s->location, uri, sizeof(s->location));
00166
00167 return http_open_cnx(h);
00168 }
00169 static int http_getc(HTTPContext *s)
00170 {
00171 int len;
00172 if (s->buf_ptr >= s->buf_end) {
00173 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
00174 if (len < 0) {
00175 return AVERROR(EIO);
00176 } else if (len == 0) {
00177 return -1;
00178 } else {
00179 s->buf_ptr = s->buffer;
00180 s->buf_end = s->buffer + len;
00181 }
00182 }
00183 return *s->buf_ptr++;
00184 }
00185
00186 static int http_get_line(HTTPContext *s, char *line, int line_size)
00187 {
00188 int ch;
00189 char *q;
00190
00191 q = line;
00192 for(;;) {
00193 ch = http_getc(s);
00194 if (ch < 0)
00195 return AVERROR(EIO);
00196 if (ch == '\n') {
00197
00198 if (q > line && q[-1] == '\r')
00199 q--;
00200 *q = '\0';
00201
00202 return 0;
00203 } else {
00204 if ((q - line) < line_size - 1)
00205 *q++ = ch;
00206 }
00207 }
00208 }
00209
00210 static int process_line(URLContext *h, char *line, int line_count,
00211 int *new_location)
00212 {
00213 HTTPContext *s = h->priv_data;
00214 char *tag, *p, *end;
00215
00216
00217 if (line[0] == '\0')
00218 return 0;
00219
00220 p = line;
00221 if (line_count == 0) {
00222 while (!isspace(*p) && *p != '\0')
00223 p++;
00224 while (isspace(*p))
00225 p++;
00226 s->http_code = strtol(p, &end, 10);
00227
00228 av_dlog(NULL, "http_code=%d\n", s->http_code);
00229
00230
00231
00232 if (s->http_code >= 400 && s->http_code < 600 && s->http_code != 401) {
00233 end += strspn(end, SPACE_CHARS);
00234 av_log(NULL, AV_LOG_WARNING, "HTTP error %d %s\n",
00235 s->http_code, end);
00236 return -1;
00237 }
00238 } else {
00239 while (*p != '\0' && *p != ':')
00240 p++;
00241 if (*p != ':')
00242 return 1;
00243
00244 *p = '\0';
00245 tag = line;
00246 p++;
00247 while (isspace(*p))
00248 p++;
00249 if (!strcmp(tag, "Location")) {
00250 strcpy(s->location, p);
00251 *new_location = 1;
00252 } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
00253 s->filesize = atoll(p);
00254 } else if (!strcmp (tag, "Content-Range")) {
00255
00256 const char *slash;
00257 if (!strncmp (p, "bytes ", 6)) {
00258 p += 6;
00259 s->off = atoll(p);
00260 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
00261 s->filesize = atoll(slash+1);
00262 }
00263 h->is_streamed = 0;
00264 } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
00265 s->filesize = -1;
00266 s->chunksize = 0;
00267 } else if (!strcmp (tag, "WWW-Authenticate")) {
00268 ff_http_auth_handle_header(&s->auth_state, tag, p);
00269 } else if (!strcmp (tag, "Authentication-Info")) {
00270 ff_http_auth_handle_header(&s->auth_state, tag, p);
00271 } else if (!strcmp (tag, "Connection")) {
00272 if (!strcmp(p, "close"))
00273 s->willclose = 1;
00274 }
00275 }
00276 return 1;
00277 }
00278
00279 static inline int has_header(const char *str, const char *header)
00280 {
00281
00282 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
00283 }
00284
00285 static int http_connect(URLContext *h, const char *path, const char *hoststr,
00286 const char *auth, int *new_location)
00287 {
00288 HTTPContext *s = h->priv_data;
00289 int post, err;
00290 char line[1024];
00291 char headers[1024] = "";
00292 char *authstr = NULL;
00293 int64_t off = s->off;
00294 int len = 0;
00295
00296
00297
00298 post = h->flags & URL_WRONLY;
00299 authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
00300 post ? "POST" : "GET");
00301
00302
00303 if (!has_header(s->headers, "\r\nUser-Agent: "))
00304 len += av_strlcatf(headers + len, sizeof(headers) - len,
00305 "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
00306 if (!has_header(s->headers, "\r\nAccept: "))
00307 len += av_strlcpy(headers + len, "Accept: */*\r\n",
00308 sizeof(headers) - len);
00309 if (!has_header(s->headers, "\r\nRange: "))
00310 len += av_strlcatf(headers + len, sizeof(headers) - len,
00311 "Range: bytes=%"PRId64"-\r\n", s->off);
00312 if (!has_header(s->headers, "\r\nConnection: "))
00313 len += av_strlcpy(headers + len, "Connection: close\r\n",
00314 sizeof(headers)-len);
00315 if (!has_header(s->headers, "\r\nHost: "))
00316 len += av_strlcatf(headers + len, sizeof(headers) - len,
00317 "Host: %s\r\n", hoststr);
00318
00319
00320 av_strlcpy(headers+len, s->headers, sizeof(headers)-len);
00321
00322 snprintf(s->buffer, sizeof(s->buffer),
00323 "%s %s HTTP/1.1\r\n"
00324 "%s"
00325 "%s"
00326 "%s"
00327 "\r\n",
00328 post ? "POST" : "GET",
00329 path,
00330 post && s->chunksize >= 0 ? "Transfer-Encoding: chunked\r\n" : "",
00331 headers,
00332 authstr ? authstr : "");
00333
00334 av_freep(&authstr);
00335 if (url_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
00336 return AVERROR(EIO);
00337
00338
00339 s->buf_ptr = s->buffer;
00340 s->buf_end = s->buffer;
00341 s->line_count = 0;
00342 s->off = 0;
00343 s->filesize = -1;
00344 s->willclose = 0;
00345 if (post) {
00346
00347
00348
00349 s->http_code = 200;
00350 return 0;
00351 }
00352 s->chunksize = -1;
00353
00354
00355 for(;;) {
00356 if (http_get_line(s, line, sizeof(line)) < 0)
00357 return AVERROR(EIO);
00358
00359 av_dlog(NULL, "header='%s'\n", line);
00360
00361 err = process_line(h, line, s->line_count, new_location);
00362 if (err < 0)
00363 return err;
00364 if (err == 0)
00365 break;
00366 s->line_count++;
00367 }
00368
00369 return (off == s->off) ? 0 : -1;
00370 }
00371
00372
00373 static int http_read(URLContext *h, uint8_t *buf, int size)
00374 {
00375 HTTPContext *s = h->priv_data;
00376 int len;
00377
00378 if (s->chunksize >= 0) {
00379 if (!s->chunksize) {
00380 char line[32];
00381
00382 for(;;) {
00383 do {
00384 if (http_get_line(s, line, sizeof(line)) < 0)
00385 return AVERROR(EIO);
00386 } while (!*line);
00387
00388 s->chunksize = strtoll(line, NULL, 16);
00389
00390 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
00391
00392 if (!s->chunksize)
00393 return 0;
00394 break;
00395 }
00396 }
00397 size = FFMIN(size, s->chunksize);
00398 }
00399
00400 len = s->buf_end - s->buf_ptr;
00401 if (len > 0) {
00402 if (len > size)
00403 len = size;
00404 memcpy(buf, s->buf_ptr, len);
00405 s->buf_ptr += len;
00406 } else {
00407 if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
00408 return AVERROR_EOF;
00409 len = url_read(s->hd, buf, size);
00410 }
00411 if (len > 0) {
00412 s->off += len;
00413 if (s->chunksize > 0)
00414 s->chunksize -= len;
00415 }
00416 return len;
00417 }
00418
00419
00420 static int http_write(URLContext *h, const uint8_t *buf, int size)
00421 {
00422 char temp[11] = "";
00423 int ret;
00424 char crlf[] = "\r\n";
00425 HTTPContext *s = h->priv_data;
00426
00427 if (s->chunksize == -1) {
00428
00429 return url_write(s->hd, buf, size);
00430 }
00431
00432
00433
00434 if (size > 0) {
00435
00436 snprintf(temp, sizeof(temp), "%x\r\n", size);
00437
00438 if ((ret = url_write(s->hd, temp, strlen(temp))) < 0 ||
00439 (ret = url_write(s->hd, buf, size)) < 0 ||
00440 (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
00441 return ret;
00442 }
00443 return size;
00444 }
00445
00446 static int http_close(URLContext *h)
00447 {
00448 int ret = 0;
00449 char footer[] = "0\r\n\r\n";
00450 HTTPContext *s = h->priv_data;
00451
00452
00453 if ((h->flags & URL_WRONLY) && s->chunksize != -1) {
00454 ret = url_write(s->hd, footer, sizeof(footer) - 1);
00455 ret = ret > 0 ? 0 : ret;
00456 }
00457
00458 if (s->hd)
00459 url_close(s->hd);
00460 return ret;
00461 }
00462
00463 static int64_t http_seek(URLContext *h, int64_t off, int whence)
00464 {
00465 HTTPContext *s = h->priv_data;
00466 URLContext *old_hd = s->hd;
00467 int64_t old_off = s->off;
00468 uint8_t old_buf[BUFFER_SIZE];
00469 int old_buf_size;
00470
00471 if (whence == AVSEEK_SIZE)
00472 return s->filesize;
00473 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
00474 return -1;
00475
00476
00477 old_buf_size = s->buf_end - s->buf_ptr;
00478 memcpy(old_buf, s->buf_ptr, old_buf_size);
00479 s->hd = NULL;
00480 if (whence == SEEK_CUR)
00481 off += s->off;
00482 else if (whence == SEEK_END)
00483 off += s->filesize;
00484 s->off = off;
00485
00486
00487 if (http_open_cnx(h) < 0) {
00488 memcpy(s->buffer, old_buf, old_buf_size);
00489 s->buf_ptr = s->buffer;
00490 s->buf_end = s->buffer + old_buf_size;
00491 s->hd = old_hd;
00492 s->off = old_off;
00493 return -1;
00494 }
00495 url_close(old_hd);
00496 return off;
00497 }
00498
00499 static int
00500 http_get_file_handle(URLContext *h)
00501 {
00502 HTTPContext *s = h->priv_data;
00503 return url_get_file_handle(s->hd);
00504 }
00505
00506 URLProtocol ff_http_protocol = {
00507 "http",
00508 http_open,
00509 http_read,
00510 http_write,
00511 http_seek,
00512 http_close,
00513 .url_get_file_handle = http_get_file_handle,
00514 .priv_data_size = sizeof(HTTPContext),
00515 .priv_data_class = &httpcontext_class,
00516 };