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 "internal.h"
00025 #include "network.h"
00026 #include "http.h"
00027 #include "os_support.h"
00028 #include "httpauth.h"
00029 #include "url.h"
00030 #include "libavutil/opt.h"
00031
00032
00033
00034
00035
00036
00037
00038
00039 #define BUFFER_SIZE MAX_URL_SIZE
00040 #define MAX_REDIRECTS 8
00041
00042 typedef struct {
00043 const AVClass *class;
00044 URLContext *hd;
00045 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
00046 int line_count;
00047 int http_code;
00048 int64_t chunksize;
00049 char *content_type;
00050 char *user_agent;
00051 int64_t off, filesize;
00052 char location[MAX_URL_SIZE];
00053 HTTPAuthState auth_state;
00054 HTTPAuthState proxy_auth_state;
00055 char *headers;
00056 int willclose;
00057 int seekable;
00058 int chunked_post;
00059 int end_chunked_post;
00060 int end_header;
00061 int multiple_requests;
00062 uint8_t *post_data;
00063 int post_datalen;
00064 int is_akamai;
00065 int rw_timeout;
00066 char *mime_type;
00067 } HTTPContext;
00068
00069 #define OFFSET(x) offsetof(HTTPContext, x)
00070 #define D AV_OPT_FLAG_DECODING_PARAM
00071 #define E AV_OPT_FLAG_ENCODING_PARAM
00072 #define DEC AV_OPT_FLAG_DECODING_PARAM
00073 static const AVOption options[] = {
00074 {"seekable", "Control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, D },
00075 {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
00076 {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
00077 {"content_type", "force a content type", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
00078 {"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
00079 {"multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
00080 {"post_data", "custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D|E },
00081 {"timeout", "timeout of socket i/o operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
00082 {"mime_type", "", OFFSET(mime_type), AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
00083 {NULL}
00084 };
00085 #define HTTP_CLASS(flavor)\
00086 static const AVClass flavor ## _context_class = {\
00087 .class_name = #flavor,\
00088 .item_name = av_default_item_name,\
00089 .option = options,\
00090 .version = LIBAVUTIL_VERSION_INT,\
00091 }
00092
00093 HTTP_CLASS(http);
00094 HTTP_CLASS(https);
00095
00096 static int http_connect(URLContext *h, const char *path, const char *local_path,
00097 const char *hoststr, const char *auth,
00098 const char *proxyauth, int *new_location);
00099
00100 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
00101 {
00102 memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
00103 &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
00104 memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state,
00105 &((HTTPContext*)src->priv_data)->proxy_auth_state,
00106 sizeof(HTTPAuthState));
00107 }
00108
00109
00110 static int http_open_cnx(URLContext *h)
00111 {
00112 const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
00113 char hostname[1024], hoststr[1024], proto[10];
00114 char auth[1024], proxyauth[1024] = "";
00115 char path1[MAX_URL_SIZE];
00116 char buf[1024], urlbuf[MAX_URL_SIZE];
00117 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
00118 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
00119 HTTPContext *s = h->priv_data;
00120
00121 proxy_path = getenv("http_proxy");
00122 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
00123 av_strstart(proxy_path, "http://", NULL);
00124
00125
00126 redo:
00127
00128 av_url_split(proto, sizeof(proto), auth, sizeof(auth),
00129 hostname, sizeof(hostname), &port,
00130 path1, sizeof(path1), s->location);
00131 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
00132
00133 if (!strcmp(proto, "https")) {
00134 lower_proto = "tls";
00135 use_proxy = 0;
00136 if (port < 0)
00137 port = 443;
00138 }
00139 if (port < 0)
00140 port = 80;
00141
00142 if (path1[0] == '\0')
00143 path = "/";
00144 else
00145 path = path1;
00146 local_path = path;
00147 if (use_proxy) {
00148
00149
00150 ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
00151 path1);
00152 path = urlbuf;
00153 av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
00154 hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
00155 }
00156
00157 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
00158
00159 if (!s->hd) {
00160 AVDictionary *opts = NULL;
00161 char opts_format[20];
00162 if (s->rw_timeout != -1) {
00163 snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout);
00164 av_dict_set(&opts, "timeout", opts_format, 0);
00165 }
00166 err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE,
00167 &h->interrupt_callback, &opts);
00168 av_dict_free(&opts);
00169 if (err < 0)
00170 goto fail;
00171 }
00172
00173 cur_auth_type = s->auth_state.auth_type;
00174 cur_proxy_auth_type = s->auth_state.auth_type;
00175 if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
00176 goto fail;
00177 attempts++;
00178 if (s->http_code == 401) {
00179 if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
00180 s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
00181 ffurl_closep(&s->hd);
00182 goto redo;
00183 } else
00184 goto fail;
00185 }
00186 if (s->http_code == 407) {
00187 if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
00188 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
00189 ffurl_closep(&s->hd);
00190 goto redo;
00191 } else
00192 goto fail;
00193 }
00194 if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
00195 && location_changed == 1) {
00196
00197 ffurl_closep(&s->hd);
00198 if (redirects++ >= MAX_REDIRECTS)
00199 return AVERROR(EIO);
00200
00201
00202 memset(&s->auth_state, 0, sizeof(s->auth_state));
00203 attempts = 0;
00204 location_changed = 0;
00205 goto redo;
00206 }
00207 return 0;
00208 fail:
00209 if (s->hd)
00210 ffurl_closep(&s->hd);
00211 return AVERROR(EIO);
00212 }
00213
00214 int ff_http_do_new_request(URLContext *h, const char *uri)
00215 {
00216 HTTPContext *s = h->priv_data;
00217
00218 s->off = 0;
00219 av_strlcpy(s->location, uri, sizeof(s->location));
00220
00221 return http_open_cnx(h);
00222 }
00223
00224 static int http_open(URLContext *h, const char *uri, int flags)
00225 {
00226 HTTPContext *s = h->priv_data;
00227
00228 if( s->seekable == 1 )
00229 h->is_streamed = 0;
00230 else
00231 h->is_streamed = 1;
00232
00233 s->filesize = -1;
00234 av_strlcpy(s->location, uri, sizeof(s->location));
00235
00236 if (s->headers) {
00237 int len = strlen(s->headers);
00238 if (len < 2 || strcmp("\r\n", s->headers + len - 2))
00239 av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
00240 }
00241
00242 return http_open_cnx(h);
00243 }
00244 static int http_getc(HTTPContext *s)
00245 {
00246 int len;
00247 if (s->buf_ptr >= s->buf_end) {
00248 len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
00249 if (len < 0) {
00250 return len;
00251 } else if (len == 0) {
00252 return -1;
00253 } else {
00254 s->buf_ptr = s->buffer;
00255 s->buf_end = s->buffer + len;
00256 }
00257 }
00258 return *s->buf_ptr++;
00259 }
00260
00261 static int http_get_line(HTTPContext *s, char *line, int line_size)
00262 {
00263 int ch;
00264 char *q;
00265
00266 q = line;
00267 for(;;) {
00268 ch = http_getc(s);
00269 if (ch < 0)
00270 return ch;
00271 if (ch == '\n') {
00272
00273 if (q > line && q[-1] == '\r')
00274 q--;
00275 *q = '\0';
00276
00277 return 0;
00278 } else {
00279 if ((q - line) < line_size - 1)
00280 *q++ = ch;
00281 }
00282 }
00283 }
00284
00285 static int process_line(URLContext *h, char *line, int line_count,
00286 int *new_location)
00287 {
00288 HTTPContext *s = h->priv_data;
00289 char *tag, *p, *end;
00290
00291
00292 if (line[0] == '\0') {
00293 s->end_header = 1;
00294 return 0;
00295 }
00296
00297 p = line;
00298 if (line_count == 0) {
00299 while (!isspace(*p) && *p != '\0')
00300 p++;
00301 while (isspace(*p))
00302 p++;
00303 s->http_code = strtol(p, &end, 10);
00304
00305 av_dlog(NULL, "http_code=%d\n", s->http_code);
00306
00307
00308
00309 if (s->http_code >= 400 && s->http_code < 600 && (s->http_code != 401
00310 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
00311 (s->http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
00312 end += strspn(end, SPACE_CHARS);
00313 av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
00314 s->http_code, end);
00315 return -1;
00316 }
00317 } else {
00318 while (*p != '\0' && *p != ':')
00319 p++;
00320 if (*p != ':')
00321 return 1;
00322
00323 *p = '\0';
00324 tag = line;
00325 p++;
00326 while (isspace(*p))
00327 p++;
00328 if (!av_strcasecmp(tag, "Location")) {
00329 av_strlcpy(s->location, p, sizeof(s->location));
00330 *new_location = 1;
00331 } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
00332 s->filesize = strtoll(p, NULL, 10);
00333 } else if (!av_strcasecmp (tag, "Content-Range")) {
00334
00335 const char *slash;
00336 if (!strncmp (p, "bytes ", 6)) {
00337 p += 6;
00338 s->off = strtoll(p, NULL, 10);
00339 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
00340 s->filesize = strtoll(slash+1, NULL, 10);
00341 }
00342 if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
00343 h->is_streamed = 0;
00344 } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5) && s->seekable == -1) {
00345 h->is_streamed = 0;
00346 } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
00347 s->filesize = -1;
00348 s->chunksize = 0;
00349 } else if (!av_strcasecmp (tag, "WWW-Authenticate")) {
00350 ff_http_auth_handle_header(&s->auth_state, tag, p);
00351 } else if (!av_strcasecmp (tag, "Authentication-Info")) {
00352 ff_http_auth_handle_header(&s->auth_state, tag, p);
00353 } else if (!av_strcasecmp (tag, "Proxy-Authenticate")) {
00354 ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
00355 } else if (!av_strcasecmp (tag, "Connection")) {
00356 if (!strcmp(p, "close"))
00357 s->willclose = 1;
00358 } else if (!av_strcasecmp (tag, "Server") && !av_strcasecmp (p, "AkamaiGHost")) {
00359 s->is_akamai = 1;
00360 } else if (!av_strcasecmp (tag, "Content-Type")) {
00361 av_free(s->mime_type); s->mime_type = av_strdup(p);
00362 }
00363 }
00364 return 1;
00365 }
00366
00367 static inline int has_header(const char *str, const char *header)
00368 {
00369
00370 if (!str)
00371 return 0;
00372 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
00373 }
00374
00375 static int http_read_header(URLContext *h, int *new_location)
00376 {
00377 HTTPContext *s = h->priv_data;
00378 char line[MAX_URL_SIZE];
00379 int err = 0;
00380
00381 s->chunksize = -1;
00382
00383 for (;;) {
00384 if ((err = http_get_line(s, line, sizeof(line))) < 0)
00385 return err;
00386
00387 av_dlog(NULL, "header='%s'\n", line);
00388
00389 err = process_line(h, line, s->line_count, new_location);
00390 if (err < 0)
00391 return err;
00392 if (err == 0)
00393 break;
00394 s->line_count++;
00395 }
00396
00397 return err;
00398 }
00399
00400 static int http_connect(URLContext *h, const char *path, const char *local_path,
00401 const char *hoststr, const char *auth,
00402 const char *proxyauth, int *new_location)
00403 {
00404 HTTPContext *s = h->priv_data;
00405 int post, err;
00406 char headers[4096] = "";
00407 char *authstr = NULL, *proxyauthstr = NULL;
00408 int64_t off = s->off;
00409 int len = 0;
00410 const char *method;
00411
00412
00413
00414 post = h->flags & AVIO_FLAG_WRITE;
00415
00416 if (s->post_data) {
00417
00418
00419 post = 1;
00420 s->chunked_post = 0;
00421 }
00422
00423 method = post ? "POST" : "GET";
00424 authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
00425 method);
00426 proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
00427 local_path, method);
00428
00429
00430 if (!has_header(s->headers, "\r\nUser-Agent: "))
00431 len += av_strlcatf(headers + len, sizeof(headers) - len,
00432 "User-Agent: %s\r\n",
00433 s->user_agent ? s->user_agent : LIBAVFORMAT_IDENT);
00434 if (!has_header(s->headers, "\r\nAccept: "))
00435 len += av_strlcpy(headers + len, "Accept: */*\r\n",
00436 sizeof(headers) - len);
00437
00438
00439
00440 if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->seekable == -1))
00441 len += av_strlcatf(headers + len, sizeof(headers) - len,
00442 "Range: bytes=%"PRId64"-\r\n", s->off);
00443
00444 if (!has_header(s->headers, "\r\nConnection: ")) {
00445 if (s->multiple_requests) {
00446 len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
00447 sizeof(headers) - len);
00448 } else {
00449 len += av_strlcpy(headers + len, "Connection: close\r\n",
00450 sizeof(headers) - len);
00451 }
00452 }
00453
00454 if (!has_header(s->headers, "\r\nHost: "))
00455 len += av_strlcatf(headers + len, sizeof(headers) - len,
00456 "Host: %s\r\n", hoststr);
00457 if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
00458 len += av_strlcatf(headers + len, sizeof(headers) - len,
00459 "Content-Length: %d\r\n", s->post_datalen);
00460 if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
00461 len += av_strlcatf(headers + len, sizeof(headers) - len,
00462 "Content-Type: %s\r\n", s->content_type);
00463
00464
00465 if (s->headers)
00466 av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
00467
00468 snprintf(s->buffer, sizeof(s->buffer),
00469 "%s %s HTTP/1.1\r\n"
00470 "%s"
00471 "%s"
00472 "%s"
00473 "%s%s"
00474 "\r\n",
00475 method,
00476 path,
00477 post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
00478 headers,
00479 authstr ? authstr : "",
00480 proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
00481
00482 av_freep(&authstr);
00483 av_freep(&proxyauthstr);
00484 if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
00485 return err;
00486
00487 if (s->post_data)
00488 if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
00489 return err;
00490
00491
00492 s->buf_ptr = s->buffer;
00493 s->buf_end = s->buffer;
00494 s->line_count = 0;
00495 s->off = 0;
00496 s->filesize = -1;
00497 s->willclose = 0;
00498 s->end_chunked_post = 0;
00499 s->end_header = 0;
00500 if (post && !s->post_data) {
00501
00502
00503
00504 s->http_code = 200;
00505 return 0;
00506 }
00507
00508
00509 err = http_read_header(h, new_location);
00510 if (err < 0)
00511 return err;
00512
00513 return (off == s->off) ? 0 : -1;
00514 }
00515
00516
00517 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
00518 {
00519 HTTPContext *s = h->priv_data;
00520 int len;
00521
00522 len = s->buf_end - s->buf_ptr;
00523 if (len > 0) {
00524 if (len > size)
00525 len = size;
00526 memcpy(buf, s->buf_ptr, len);
00527 s->buf_ptr += len;
00528 } else {
00529 if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
00530 return AVERROR_EOF;
00531 len = ffurl_read(s->hd, buf, size);
00532 }
00533 if (len > 0) {
00534 s->off += len;
00535 if (s->chunksize > 0)
00536 s->chunksize -= len;
00537 }
00538 return len;
00539 }
00540
00541 static int http_read(URLContext *h, uint8_t *buf, int size)
00542 {
00543 HTTPContext *s = h->priv_data;
00544 int err, new_location;
00545
00546 if (!s->hd)
00547 return AVERROR_EOF;
00548
00549 if (s->end_chunked_post && !s->end_header) {
00550 err = http_read_header(h, &new_location);
00551 if (err < 0)
00552 return err;
00553 }
00554
00555 if (s->chunksize >= 0) {
00556 if (!s->chunksize) {
00557 char line[32];
00558
00559 for(;;) {
00560 do {
00561 if ((err = http_get_line(s, line, sizeof(line))) < 0)
00562 return err;
00563 } while (!*line);
00564
00565 s->chunksize = strtoll(line, NULL, 16);
00566
00567 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
00568
00569 if (!s->chunksize)
00570 return 0;
00571 break;
00572 }
00573 }
00574 size = FFMIN(size, s->chunksize);
00575 }
00576 return http_buf_read(h, buf, size);
00577 }
00578
00579
00580 static int http_write(URLContext *h, const uint8_t *buf, int size)
00581 {
00582 char temp[11] = "";
00583 int ret;
00584 char crlf[] = "\r\n";
00585 HTTPContext *s = h->priv_data;
00586
00587 if (!s->chunked_post) {
00588
00589 return ffurl_write(s->hd, buf, size);
00590 }
00591
00592
00593
00594 if (size > 0) {
00595
00596 snprintf(temp, sizeof(temp), "%x\r\n", size);
00597
00598 if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
00599 (ret = ffurl_write(s->hd, buf, size)) < 0 ||
00600 (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
00601 return ret;
00602 }
00603 return size;
00604 }
00605
00606 static int http_shutdown(URLContext *h, int flags)
00607 {
00608 int ret = 0;
00609 char footer[] = "0\r\n\r\n";
00610 HTTPContext *s = h->priv_data;
00611
00612
00613 if ((flags & AVIO_FLAG_WRITE) && s->chunked_post) {
00614 ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
00615 ret = ret > 0 ? 0 : ret;
00616 s->end_chunked_post = 1;
00617 }
00618
00619 return ret;
00620 }
00621
00622 static int http_close(URLContext *h)
00623 {
00624 int ret = 0;
00625 HTTPContext *s = h->priv_data;
00626
00627 if (!s->end_chunked_post) {
00628
00629 ret = http_shutdown(h, h->flags);
00630 }
00631
00632 if (s->hd)
00633 ffurl_closep(&s->hd);
00634 return ret;
00635 }
00636
00637 static int64_t http_seek(URLContext *h, int64_t off, int whence)
00638 {
00639 HTTPContext *s = h->priv_data;
00640 URLContext *old_hd = s->hd;
00641 int64_t old_off = s->off;
00642 uint8_t old_buf[BUFFER_SIZE];
00643 int old_buf_size;
00644
00645 if (whence == AVSEEK_SIZE)
00646 return s->filesize;
00647 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
00648 return -1;
00649
00650
00651 old_buf_size = s->buf_end - s->buf_ptr;
00652 memcpy(old_buf, s->buf_ptr, old_buf_size);
00653 s->hd = NULL;
00654 if (whence == SEEK_CUR)
00655 off += s->off;
00656 else if (whence == SEEK_END)
00657 off += s->filesize;
00658 s->off = off;
00659
00660
00661 if (http_open_cnx(h) < 0) {
00662 memcpy(s->buffer, old_buf, old_buf_size);
00663 s->buf_ptr = s->buffer;
00664 s->buf_end = s->buffer + old_buf_size;
00665 s->hd = old_hd;
00666 s->off = old_off;
00667 return -1;
00668 }
00669 ffurl_close(old_hd);
00670 return off;
00671 }
00672
00673 static int
00674 http_get_file_handle(URLContext *h)
00675 {
00676 HTTPContext *s = h->priv_data;
00677 return ffurl_get_file_handle(s->hd);
00678 }
00679
00680 #if CONFIG_HTTP_PROTOCOL
00681 URLProtocol ff_http_protocol = {
00682 .name = "http",
00683 .url_open = http_open,
00684 .url_read = http_read,
00685 .url_write = http_write,
00686 .url_seek = http_seek,
00687 .url_close = http_close,
00688 .url_get_file_handle = http_get_file_handle,
00689 .url_shutdown = http_shutdown,
00690 .priv_data_size = sizeof(HTTPContext),
00691 .priv_data_class = &http_context_class,
00692 .flags = URL_PROTOCOL_FLAG_NETWORK,
00693 };
00694 #endif
00695 #if CONFIG_HTTPS_PROTOCOL
00696 URLProtocol ff_https_protocol = {
00697 .name = "https",
00698 .url_open = http_open,
00699 .url_read = http_read,
00700 .url_write = http_write,
00701 .url_seek = http_seek,
00702 .url_close = http_close,
00703 .url_get_file_handle = http_get_file_handle,
00704 .url_shutdown = http_shutdown,
00705 .priv_data_size = sizeof(HTTPContext),
00706 .priv_data_class = &https_context_class,
00707 .flags = URL_PROTOCOL_FLAG_NETWORK,
00708 };
00709 #endif
00710
00711 #if CONFIG_HTTPPROXY_PROTOCOL
00712 static int http_proxy_close(URLContext *h)
00713 {
00714 HTTPContext *s = h->priv_data;
00715 if (s->hd)
00716 ffurl_closep(&s->hd);
00717 return 0;
00718 }
00719
00720 static int http_proxy_open(URLContext *h, const char *uri, int flags)
00721 {
00722 HTTPContext *s = h->priv_data;
00723 char hostname[1024], hoststr[1024];
00724 char auth[1024], pathbuf[1024], *path;
00725 char lower_url[100];
00726 int port, ret = 0, attempts = 0;
00727 HTTPAuthType cur_auth_type;
00728 char *authstr;
00729 int new_loc;
00730 AVDictionary *opts = NULL;
00731 char opts_format[20];
00732
00733 if( s->seekable == 1 )
00734 h->is_streamed = 0;
00735 else
00736 h->is_streamed = 1;
00737
00738 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
00739 pathbuf, sizeof(pathbuf), uri);
00740 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
00741 path = pathbuf;
00742 if (*path == '/')
00743 path++;
00744
00745 ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
00746 NULL);
00747 redo:
00748 if (s->rw_timeout != -1) {
00749 snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout);
00750 av_dict_set(&opts, "timeout", opts_format, 0);
00751 }
00752 ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
00753 &h->interrupt_callback, &opts);
00754 av_dict_free(&opts);
00755 if (ret < 0)
00756 return ret;
00757
00758 authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
00759 path, "CONNECT");
00760 snprintf(s->buffer, sizeof(s->buffer),
00761 "CONNECT %s HTTP/1.1\r\n"
00762 "Host: %s\r\n"
00763 "Connection: close\r\n"
00764 "%s%s"
00765 "\r\n",
00766 path,
00767 hoststr,
00768 authstr ? "Proxy-" : "", authstr ? authstr : "");
00769 av_freep(&authstr);
00770
00771 if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
00772 goto fail;
00773
00774 s->buf_ptr = s->buffer;
00775 s->buf_end = s->buffer;
00776 s->line_count = 0;
00777 s->filesize = -1;
00778 cur_auth_type = s->proxy_auth_state.auth_type;
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789 ret = http_read_header(h, &new_loc);
00790 if (ret < 0)
00791 goto fail;
00792
00793 attempts++;
00794 if (s->http_code == 407 &&
00795 (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
00796 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
00797 ffurl_closep(&s->hd);
00798 goto redo;
00799 }
00800
00801 if (s->http_code < 400)
00802 return 0;
00803 ret = AVERROR(EIO);
00804
00805 fail:
00806 http_proxy_close(h);
00807 return ret;
00808 }
00809
00810 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
00811 {
00812 HTTPContext *s = h->priv_data;
00813 return ffurl_write(s->hd, buf, size);
00814 }
00815
00816 URLProtocol ff_httpproxy_protocol = {
00817 .name = "httpproxy",
00818 .url_open = http_proxy_open,
00819 .url_read = http_buf_read,
00820 .url_write = http_proxy_write,
00821 .url_close = http_proxy_close,
00822 .url_get_file_handle = http_get_file_handle,
00823 .priv_data_size = sizeof(HTTPContext),
00824 .flags = URL_PROTOCOL_FLAG_NETWORK,
00825 };
00826 #endif