annotate ffmpeg/libavformat/http.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * HTTP protocol for ffmpeg client
yading@11 3 * Copyright (c) 2000, 2001 Fabrice Bellard
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 #include "libavutil/avstring.h"
yading@11 23 #include "avformat.h"
yading@11 24 #include "internal.h"
yading@11 25 #include "network.h"
yading@11 26 #include "http.h"
yading@11 27 #include "os_support.h"
yading@11 28 #include "httpauth.h"
yading@11 29 #include "url.h"
yading@11 30 #include "libavutil/opt.h"
yading@11 31
yading@11 32 /* XXX: POST protocol is not completely implemented because ffmpeg uses
yading@11 33 only a subset of it. */
yading@11 34
yading@11 35 /* The IO buffer size is unrelated to the max URL size in itself, but needs
yading@11 36 * to be large enough to fit the full request headers (including long
yading@11 37 * path names).
yading@11 38 */
yading@11 39 #define BUFFER_SIZE MAX_URL_SIZE
yading@11 40 #define MAX_REDIRECTS 8
yading@11 41
yading@11 42 typedef struct {
yading@11 43 const AVClass *class;
yading@11 44 URLContext *hd;
yading@11 45 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
yading@11 46 int line_count;
yading@11 47 int http_code;
yading@11 48 int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
yading@11 49 char *content_type;
yading@11 50 char *user_agent;
yading@11 51 int64_t off, filesize;
yading@11 52 char location[MAX_URL_SIZE];
yading@11 53 HTTPAuthState auth_state;
yading@11 54 HTTPAuthState proxy_auth_state;
yading@11 55 char *headers;
yading@11 56 int willclose; /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
yading@11 57 int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
yading@11 58 int chunked_post;
yading@11 59 int end_chunked_post; /**< A flag which indicates if the end of chunked encoding has been sent. */
yading@11 60 int end_header; /**< A flag which indicates we have finished to read POST reply. */
yading@11 61 int multiple_requests; /**< A flag which indicates if we use persistent connections. */
yading@11 62 uint8_t *post_data;
yading@11 63 int post_datalen;
yading@11 64 int is_akamai;
yading@11 65 int rw_timeout;
yading@11 66 char *mime_type;
yading@11 67 char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
yading@11 68 } HTTPContext;
yading@11 69
yading@11 70 #define OFFSET(x) offsetof(HTTPContext, x)
yading@11 71 #define D AV_OPT_FLAG_DECODING_PARAM
yading@11 72 #define E AV_OPT_FLAG_ENCODING_PARAM
yading@11 73 #define DEFAULT_USER_AGENT "Mozilla/5.0 Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
yading@11 74 static const AVOption options[] = {
yading@11 75 {"seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, D },
yading@11 76 {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
yading@11 77 {"headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
yading@11 78 {"content_type", "force a content type", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
yading@11 79 {"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = DEFAULT_USER_AGENT}, 0, 0, D },
yading@11 80 {"multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
yading@11 81 {"post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D|E },
yading@11 82 {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
yading@11 83 {"mime_type", "set MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
yading@11 84 {"cookies", "set cookies to be sent in applicable future requests, use newline delimited Set-Cookie HTTP field value syntax", OFFSET(cookies), AV_OPT_TYPE_STRING, {0}, 0, 0, 0 },
yading@11 85 {NULL}
yading@11 86 };
yading@11 87 #define HTTP_CLASS(flavor)\
yading@11 88 static const AVClass flavor ## _context_class = {\
yading@11 89 .class_name = #flavor,\
yading@11 90 .item_name = av_default_item_name,\
yading@11 91 .option = options,\
yading@11 92 .version = LIBAVUTIL_VERSION_INT,\
yading@11 93 }
yading@11 94
yading@11 95 HTTP_CLASS(http);
yading@11 96 HTTP_CLASS(https);
yading@11 97
yading@11 98 static int http_connect(URLContext *h, const char *path, const char *local_path,
yading@11 99 const char *hoststr, const char *auth,
yading@11 100 const char *proxyauth, int *new_location);
yading@11 101
yading@11 102 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
yading@11 103 {
yading@11 104 memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
yading@11 105 &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
yading@11 106 memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state,
yading@11 107 &((HTTPContext*)src->priv_data)->proxy_auth_state,
yading@11 108 sizeof(HTTPAuthState));
yading@11 109 }
yading@11 110
yading@11 111 /* return non zero if error */
yading@11 112 static int http_open_cnx(URLContext *h)
yading@11 113 {
yading@11 114 const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
yading@11 115 char hostname[1024], hoststr[1024], proto[10];
yading@11 116 char auth[1024], proxyauth[1024] = "";
yading@11 117 char path1[MAX_URL_SIZE];
yading@11 118 char buf[1024], urlbuf[MAX_URL_SIZE];
yading@11 119 int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
yading@11 120 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
yading@11 121 HTTPContext *s = h->priv_data;
yading@11 122
yading@11 123 /* fill the dest addr */
yading@11 124 redo:
yading@11 125 /* needed in any case to build the host string */
yading@11 126 av_url_split(proto, sizeof(proto), auth, sizeof(auth),
yading@11 127 hostname, sizeof(hostname), &port,
yading@11 128 path1, sizeof(path1), s->location);
yading@11 129 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
yading@11 130
yading@11 131 proxy_path = getenv("http_proxy");
yading@11 132 use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
yading@11 133 proxy_path != NULL && av_strstart(proxy_path, "http://", NULL);
yading@11 134
yading@11 135 if (!strcmp(proto, "https")) {
yading@11 136 lower_proto = "tls";
yading@11 137 use_proxy = 0;
yading@11 138 if (port < 0)
yading@11 139 port = 443;
yading@11 140 }
yading@11 141 if (port < 0)
yading@11 142 port = 80;
yading@11 143
yading@11 144 if (path1[0] == '\0')
yading@11 145 path = "/";
yading@11 146 else
yading@11 147 path = path1;
yading@11 148 local_path = path;
yading@11 149 if (use_proxy) {
yading@11 150 /* Reassemble the request URL without auth string - we don't
yading@11 151 * want to leak the auth to the proxy. */
yading@11 152 ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
yading@11 153 path1);
yading@11 154 path = urlbuf;
yading@11 155 av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
yading@11 156 hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
yading@11 157 }
yading@11 158
yading@11 159 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
yading@11 160
yading@11 161 if (!s->hd) {
yading@11 162 AVDictionary *opts = NULL;
yading@11 163 char opts_format[20];
yading@11 164 if (s->rw_timeout != -1) {
yading@11 165 snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout);
yading@11 166 av_dict_set(&opts, "timeout", opts_format, 0);
yading@11 167 } /* if option is not given, don't pass it and let tcp use its own default */
yading@11 168 err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE,
yading@11 169 &h->interrupt_callback, &opts);
yading@11 170 av_dict_free(&opts);
yading@11 171 if (err < 0)
yading@11 172 goto fail;
yading@11 173 }
yading@11 174
yading@11 175 cur_auth_type = s->auth_state.auth_type;
yading@11 176 cur_proxy_auth_type = s->auth_state.auth_type;
yading@11 177 if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
yading@11 178 goto fail;
yading@11 179 attempts++;
yading@11 180 if (s->http_code == 401) {
yading@11 181 if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
yading@11 182 s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
yading@11 183 ffurl_closep(&s->hd);
yading@11 184 goto redo;
yading@11 185 } else
yading@11 186 goto fail;
yading@11 187 }
yading@11 188 if (s->http_code == 407) {
yading@11 189 if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
yading@11 190 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
yading@11 191 ffurl_closep(&s->hd);
yading@11 192 goto redo;
yading@11 193 } else
yading@11 194 goto fail;
yading@11 195 }
yading@11 196 if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
yading@11 197 && location_changed == 1) {
yading@11 198 /* url moved, get next */
yading@11 199 ffurl_closep(&s->hd);
yading@11 200 if (redirects++ >= MAX_REDIRECTS)
yading@11 201 return AVERROR(EIO);
yading@11 202 /* Restart the authentication process with the new target, which
yading@11 203 * might use a different auth mechanism. */
yading@11 204 memset(&s->auth_state, 0, sizeof(s->auth_state));
yading@11 205 attempts = 0;
yading@11 206 location_changed = 0;
yading@11 207 goto redo;
yading@11 208 }
yading@11 209 return 0;
yading@11 210 fail:
yading@11 211 if (s->hd)
yading@11 212 ffurl_closep(&s->hd);
yading@11 213 return AVERROR(EIO);
yading@11 214 }
yading@11 215
yading@11 216 int ff_http_do_new_request(URLContext *h, const char *uri)
yading@11 217 {
yading@11 218 HTTPContext *s = h->priv_data;
yading@11 219
yading@11 220 s->off = 0;
yading@11 221 av_strlcpy(s->location, uri, sizeof(s->location));
yading@11 222
yading@11 223 return http_open_cnx(h);
yading@11 224 }
yading@11 225
yading@11 226 static int http_open(URLContext *h, const char *uri, int flags)
yading@11 227 {
yading@11 228 HTTPContext *s = h->priv_data;
yading@11 229
yading@11 230 if( s->seekable == 1 )
yading@11 231 h->is_streamed = 0;
yading@11 232 else
yading@11 233 h->is_streamed = 1;
yading@11 234
yading@11 235 s->filesize = -1;
yading@11 236 av_strlcpy(s->location, uri, sizeof(s->location));
yading@11 237
yading@11 238 if (s->headers) {
yading@11 239 int len = strlen(s->headers);
yading@11 240 if (len < 2 || strcmp("\r\n", s->headers + len - 2))
yading@11 241 av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
yading@11 242 }
yading@11 243
yading@11 244 return http_open_cnx(h);
yading@11 245 }
yading@11 246 static int http_getc(HTTPContext *s)
yading@11 247 {
yading@11 248 int len;
yading@11 249 if (s->buf_ptr >= s->buf_end) {
yading@11 250 len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
yading@11 251 if (len < 0) {
yading@11 252 return len;
yading@11 253 } else if (len == 0) {
yading@11 254 return -1;
yading@11 255 } else {
yading@11 256 s->buf_ptr = s->buffer;
yading@11 257 s->buf_end = s->buffer + len;
yading@11 258 }
yading@11 259 }
yading@11 260 return *s->buf_ptr++;
yading@11 261 }
yading@11 262
yading@11 263 static int http_get_line(HTTPContext *s, char *line, int line_size)
yading@11 264 {
yading@11 265 int ch;
yading@11 266 char *q;
yading@11 267
yading@11 268 q = line;
yading@11 269 for(;;) {
yading@11 270 ch = http_getc(s);
yading@11 271 if (ch < 0)
yading@11 272 return ch;
yading@11 273 if (ch == '\n') {
yading@11 274 /* process line */
yading@11 275 if (q > line && q[-1] == '\r')
yading@11 276 q--;
yading@11 277 *q = '\0';
yading@11 278
yading@11 279 return 0;
yading@11 280 } else {
yading@11 281 if ((q - line) < line_size - 1)
yading@11 282 *q++ = ch;
yading@11 283 }
yading@11 284 }
yading@11 285 }
yading@11 286
yading@11 287 static int process_line(URLContext *h, char *line, int line_count,
yading@11 288 int *new_location)
yading@11 289 {
yading@11 290 HTTPContext *s = h->priv_data;
yading@11 291 char *tag, *p, *end;
yading@11 292
yading@11 293 /* end of header */
yading@11 294 if (line[0] == '\0') {
yading@11 295 s->end_header = 1;
yading@11 296 return 0;
yading@11 297 }
yading@11 298
yading@11 299 p = line;
yading@11 300 if (line_count == 0) {
yading@11 301 while (!av_isspace(*p) && *p != '\0')
yading@11 302 p++;
yading@11 303 while (av_isspace(*p))
yading@11 304 p++;
yading@11 305 s->http_code = strtol(p, &end, 10);
yading@11 306
yading@11 307 av_dlog(NULL, "http_code=%d\n", s->http_code);
yading@11 308
yading@11 309 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
yading@11 310 * don't abort until all headers have been parsed. */
yading@11 311 if (s->http_code >= 400 && s->http_code < 600 && (s->http_code != 401
yading@11 312 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
yading@11 313 (s->http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
yading@11 314 end += strspn(end, SPACE_CHARS);
yading@11 315 av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
yading@11 316 s->http_code, end);
yading@11 317 return -1;
yading@11 318 }
yading@11 319 } else {
yading@11 320 while (*p != '\0' && *p != ':')
yading@11 321 p++;
yading@11 322 if (*p != ':')
yading@11 323 return 1;
yading@11 324
yading@11 325 *p = '\0';
yading@11 326 tag = line;
yading@11 327 p++;
yading@11 328 while (av_isspace(*p))
yading@11 329 p++;
yading@11 330 if (!av_strcasecmp(tag, "Location")) {
yading@11 331 av_strlcpy(s->location, p, sizeof(s->location));
yading@11 332 *new_location = 1;
yading@11 333 } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
yading@11 334 s->filesize = strtoll(p, NULL, 10);
yading@11 335 } else if (!av_strcasecmp (tag, "Content-Range")) {
yading@11 336 /* "bytes $from-$to/$document_size" */
yading@11 337 const char *slash;
yading@11 338 if (!strncmp (p, "bytes ", 6)) {
yading@11 339 p += 6;
yading@11 340 s->off = strtoll(p, NULL, 10);
yading@11 341 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
yading@11 342 s->filesize = strtoll(slash+1, NULL, 10);
yading@11 343 }
yading@11 344 if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
yading@11 345 h->is_streamed = 0; /* we _can_ in fact seek */
yading@11 346 } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5) && s->seekable == -1) {
yading@11 347 h->is_streamed = 0;
yading@11 348 } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
yading@11 349 s->filesize = -1;
yading@11 350 s->chunksize = 0;
yading@11 351 } else if (!av_strcasecmp (tag, "WWW-Authenticate")) {
yading@11 352 ff_http_auth_handle_header(&s->auth_state, tag, p);
yading@11 353 } else if (!av_strcasecmp (tag, "Authentication-Info")) {
yading@11 354 ff_http_auth_handle_header(&s->auth_state, tag, p);
yading@11 355 } else if (!av_strcasecmp (tag, "Proxy-Authenticate")) {
yading@11 356 ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
yading@11 357 } else if (!av_strcasecmp (tag, "Connection")) {
yading@11 358 if (!strcmp(p, "close"))
yading@11 359 s->willclose = 1;
yading@11 360 } else if (!av_strcasecmp (tag, "Server") && !av_strcasecmp (p, "AkamaiGHost")) {
yading@11 361 s->is_akamai = 1;
yading@11 362 } else if (!av_strcasecmp (tag, "Content-Type")) {
yading@11 363 av_free(s->mime_type); s->mime_type = av_strdup(p);
yading@11 364 } else if (!av_strcasecmp (tag, "Set-Cookie")) {
yading@11 365 if (!s->cookies) {
yading@11 366 if (!(s->cookies = av_strdup(p)))
yading@11 367 return AVERROR(ENOMEM);
yading@11 368 } else {
yading@11 369 char *tmp = s->cookies;
yading@11 370 size_t str_size = strlen(tmp) + strlen(p) + 2;
yading@11 371 if (!(s->cookies = av_malloc(str_size))) {
yading@11 372 s->cookies = tmp;
yading@11 373 return AVERROR(ENOMEM);
yading@11 374 }
yading@11 375 snprintf(s->cookies, str_size, "%s\n%s", tmp, p);
yading@11 376 av_free(tmp);
yading@11 377 }
yading@11 378 }
yading@11 379 }
yading@11 380 return 1;
yading@11 381 }
yading@11 382
yading@11 383 /**
yading@11 384 * Create a string containing cookie values for use as a HTTP cookie header
yading@11 385 * field value for a particular path and domain from the cookie values stored in
yading@11 386 * the HTTP protocol context. The cookie string is stored in *cookies.
yading@11 387 *
yading@11 388 * @return a negative value if an error condition occurred, 0 otherwise
yading@11 389 */
yading@11 390 static int get_cookies(HTTPContext *s, char **cookies, const char *path,
yading@11 391 const char *domain)
yading@11 392 {
yading@11 393 // cookie strings will look like Set-Cookie header field values. Multiple
yading@11 394 // Set-Cookie fields will result in multiple values delimited by a newline
yading@11 395 int ret = 0;
yading@11 396 char *next, *cookie, *set_cookies = av_strdup(s->cookies), *cset_cookies = set_cookies;
yading@11 397
yading@11 398 if (!set_cookies) return AVERROR(EINVAL);
yading@11 399
yading@11 400 *cookies = NULL;
yading@11 401 while ((cookie = av_strtok(set_cookies, "\n", &next))) {
yading@11 402 int domain_offset = 0;
yading@11 403 char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
yading@11 404 set_cookies = NULL;
yading@11 405
yading@11 406 while ((param = av_strtok(cookie, "; ", &next_param))) {
yading@11 407 cookie = NULL;
yading@11 408 if (!av_strncasecmp("path=", param, 5)) {
yading@11 409 av_free(cpath);
yading@11 410 cpath = av_strdup(&param[5]);
yading@11 411 } else if (!av_strncasecmp("domain=", param, 7)) {
yading@11 412 av_free(cdomain);
yading@11 413 cdomain = av_strdup(&param[7]);
yading@11 414 } else if (!av_strncasecmp("secure", param, 6) ||
yading@11 415 !av_strncasecmp("comment", param, 7) ||
yading@11 416 !av_strncasecmp("max-age", param, 7) ||
yading@11 417 !av_strncasecmp("version", param, 7)) {
yading@11 418 // ignore Comment, Max-Age, Secure and Version
yading@11 419 } else {
yading@11 420 av_free(cvalue);
yading@11 421 cvalue = av_strdup(param);
yading@11 422 }
yading@11 423 }
yading@11 424
yading@11 425 // ensure all of the necessary values are valid
yading@11 426 if (!cdomain || !cpath || !cvalue) {
yading@11 427 av_log(s, AV_LOG_WARNING,
yading@11 428 "Invalid cookie found, no value, path or domain specified\n");
yading@11 429 goto done_cookie;
yading@11 430 }
yading@11 431
yading@11 432 // check if the request path matches the cookie path
yading@11 433 if (av_strncasecmp(path, cpath, strlen(cpath)))
yading@11 434 goto done_cookie;
yading@11 435
yading@11 436 // the domain should be at least the size of our cookie domain
yading@11 437 domain_offset = strlen(domain) - strlen(cdomain);
yading@11 438 if (domain_offset < 0)
yading@11 439 goto done_cookie;
yading@11 440
yading@11 441 // match the cookie domain
yading@11 442 if (av_strcasecmp(&domain[domain_offset], cdomain))
yading@11 443 goto done_cookie;
yading@11 444
yading@11 445 // cookie parameters match, so copy the value
yading@11 446 if (!*cookies) {
yading@11 447 if (!(*cookies = av_strdup(cvalue))) {
yading@11 448 ret = AVERROR(ENOMEM);
yading@11 449 goto done_cookie;
yading@11 450 }
yading@11 451 } else {
yading@11 452 char *tmp = *cookies;
yading@11 453 size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
yading@11 454 if (!(*cookies = av_malloc(str_size))) {
yading@11 455 ret = AVERROR(ENOMEM);
yading@11 456 goto done_cookie;
yading@11 457 }
yading@11 458 snprintf(*cookies, str_size, "%s; %s", tmp, cvalue);
yading@11 459 av_free(tmp);
yading@11 460 }
yading@11 461
yading@11 462 done_cookie:
yading@11 463 av_free(cdomain);
yading@11 464 av_free(cpath);
yading@11 465 av_free(cvalue);
yading@11 466 if (ret < 0) {
yading@11 467 if (*cookies) av_freep(cookies);
yading@11 468 av_free(cset_cookies);
yading@11 469 return ret;
yading@11 470 }
yading@11 471 }
yading@11 472
yading@11 473 av_free(cset_cookies);
yading@11 474
yading@11 475 return 0;
yading@11 476 }
yading@11 477
yading@11 478 static inline int has_header(const char *str, const char *header)
yading@11 479 {
yading@11 480 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
yading@11 481 if (!str)
yading@11 482 return 0;
yading@11 483 return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
yading@11 484 }
yading@11 485
yading@11 486 static int http_read_header(URLContext *h, int *new_location)
yading@11 487 {
yading@11 488 HTTPContext *s = h->priv_data;
yading@11 489 char line[MAX_URL_SIZE];
yading@11 490 int err = 0;
yading@11 491
yading@11 492 s->chunksize = -1;
yading@11 493
yading@11 494 for (;;) {
yading@11 495 if ((err = http_get_line(s, line, sizeof(line))) < 0)
yading@11 496 return err;
yading@11 497
yading@11 498 av_dlog(NULL, "header='%s'\n", line);
yading@11 499
yading@11 500 err = process_line(h, line, s->line_count, new_location);
yading@11 501 if (err < 0)
yading@11 502 return err;
yading@11 503 if (err == 0)
yading@11 504 break;
yading@11 505 s->line_count++;
yading@11 506 }
yading@11 507
yading@11 508 return err;
yading@11 509 }
yading@11 510
yading@11 511 static int http_connect(URLContext *h, const char *path, const char *local_path,
yading@11 512 const char *hoststr, const char *auth,
yading@11 513 const char *proxyauth, int *new_location)
yading@11 514 {
yading@11 515 HTTPContext *s = h->priv_data;
yading@11 516 int post, err;
yading@11 517 char headers[4096] = "";
yading@11 518 char *authstr = NULL, *proxyauthstr = NULL;
yading@11 519 int64_t off = s->off;
yading@11 520 int len = 0;
yading@11 521 const char *method;
yading@11 522
yading@11 523
yading@11 524 /* send http header */
yading@11 525 post = h->flags & AVIO_FLAG_WRITE;
yading@11 526
yading@11 527 if (s->post_data) {
yading@11 528 /* force POST method and disable chunked encoding when
yading@11 529 * custom HTTP post data is set */
yading@11 530 post = 1;
yading@11 531 s->chunked_post = 0;
yading@11 532 }
yading@11 533
yading@11 534 method = post ? "POST" : "GET";
yading@11 535 authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
yading@11 536 method);
yading@11 537 proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
yading@11 538 local_path, method);
yading@11 539
yading@11 540 /* set default headers if needed */
yading@11 541 if (!has_header(s->headers, "\r\nUser-Agent: "))
yading@11 542 len += av_strlcatf(headers + len, sizeof(headers) - len,
yading@11 543 "User-Agent: %s\r\n", s->user_agent);
yading@11 544 if (!has_header(s->headers, "\r\nAccept: "))
yading@11 545 len += av_strlcpy(headers + len, "Accept: */*\r\n",
yading@11 546 sizeof(headers) - len);
yading@11 547 // Note: we send this on purpose even when s->off is 0 when we're probing,
yading@11 548 // since it allows us to detect more reliably if a (non-conforming)
yading@11 549 // server supports seeking by analysing the reply headers.
yading@11 550 if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->seekable == -1))
yading@11 551 len += av_strlcatf(headers + len, sizeof(headers) - len,
yading@11 552 "Range: bytes=%"PRId64"-\r\n", s->off);
yading@11 553
yading@11 554 if (!has_header(s->headers, "\r\nConnection: ")) {
yading@11 555 if (s->multiple_requests) {
yading@11 556 len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
yading@11 557 sizeof(headers) - len);
yading@11 558 } else {
yading@11 559 len += av_strlcpy(headers + len, "Connection: close\r\n",
yading@11 560 sizeof(headers) - len);
yading@11 561 }
yading@11 562 }
yading@11 563
yading@11 564 if (!has_header(s->headers, "\r\nHost: "))
yading@11 565 len += av_strlcatf(headers + len, sizeof(headers) - len,
yading@11 566 "Host: %s\r\n", hoststr);
yading@11 567 if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
yading@11 568 len += av_strlcatf(headers + len, sizeof(headers) - len,
yading@11 569 "Content-Length: %d\r\n", s->post_datalen);
yading@11 570 if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
yading@11 571 len += av_strlcatf(headers + len, sizeof(headers) - len,
yading@11 572 "Content-Type: %s\r\n", s->content_type);
yading@11 573 if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
yading@11 574 char *cookies = NULL;
yading@11 575 if (!get_cookies(s, &cookies, path, hoststr)) {
yading@11 576 len += av_strlcatf(headers + len, sizeof(headers) - len,
yading@11 577 "Cookie: %s\r\n", cookies);
yading@11 578 av_free(cookies);
yading@11 579 }
yading@11 580 }
yading@11 581
yading@11 582 /* now add in custom headers */
yading@11 583 if (s->headers)
yading@11 584 av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
yading@11 585
yading@11 586 snprintf(s->buffer, sizeof(s->buffer),
yading@11 587 "%s %s HTTP/1.1\r\n"
yading@11 588 "%s"
yading@11 589 "%s"
yading@11 590 "%s"
yading@11 591 "%s%s"
yading@11 592 "\r\n",
yading@11 593 method,
yading@11 594 path,
yading@11 595 post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
yading@11 596 headers,
yading@11 597 authstr ? authstr : "",
yading@11 598 proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
yading@11 599
yading@11 600 av_freep(&authstr);
yading@11 601 av_freep(&proxyauthstr);
yading@11 602 if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
yading@11 603 return err;
yading@11 604
yading@11 605 if (s->post_data)
yading@11 606 if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
yading@11 607 return err;
yading@11 608
yading@11 609 /* init input buffer */
yading@11 610 s->buf_ptr = s->buffer;
yading@11 611 s->buf_end = s->buffer;
yading@11 612 s->line_count = 0;
yading@11 613 s->off = 0;
yading@11 614 s->filesize = -1;
yading@11 615 s->willclose = 0;
yading@11 616 s->end_chunked_post = 0;
yading@11 617 s->end_header = 0;
yading@11 618 if (post && !s->post_data) {
yading@11 619 /* Pretend that it did work. We didn't read any header yet, since
yading@11 620 * we've still to send the POST data, but the code calling this
yading@11 621 * function will check http_code after we return. */
yading@11 622 s->http_code = 200;
yading@11 623 return 0;
yading@11 624 }
yading@11 625
yading@11 626 /* wait for header */
yading@11 627 err = http_read_header(h, new_location);
yading@11 628 if (err < 0)
yading@11 629 return err;
yading@11 630
yading@11 631 return (off == s->off) ? 0 : -1;
yading@11 632 }
yading@11 633
yading@11 634
yading@11 635 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
yading@11 636 {
yading@11 637 HTTPContext *s = h->priv_data;
yading@11 638 int len;
yading@11 639 /* read bytes from input buffer first */
yading@11 640 len = s->buf_end - s->buf_ptr;
yading@11 641 if (len > 0) {
yading@11 642 if (len > size)
yading@11 643 len = size;
yading@11 644 memcpy(buf, s->buf_ptr, len);
yading@11 645 s->buf_ptr += len;
yading@11 646 } else {
yading@11 647 if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
yading@11 648 return AVERROR_EOF;
yading@11 649 len = ffurl_read(s->hd, buf, size);
yading@11 650 }
yading@11 651 if (len > 0) {
yading@11 652 s->off += len;
yading@11 653 if (s->chunksize > 0)
yading@11 654 s->chunksize -= len;
yading@11 655 }
yading@11 656 return len;
yading@11 657 }
yading@11 658
yading@11 659 static int http_read(URLContext *h, uint8_t *buf, int size)
yading@11 660 {
yading@11 661 HTTPContext *s = h->priv_data;
yading@11 662 int err, new_location;
yading@11 663
yading@11 664 if (!s->hd)
yading@11 665 return AVERROR_EOF;
yading@11 666
yading@11 667 if (s->end_chunked_post && !s->end_header) {
yading@11 668 err = http_read_header(h, &new_location);
yading@11 669 if (err < 0)
yading@11 670 return err;
yading@11 671 }
yading@11 672
yading@11 673 if (s->chunksize >= 0) {
yading@11 674 if (!s->chunksize) {
yading@11 675 char line[32];
yading@11 676
yading@11 677 for(;;) {
yading@11 678 do {
yading@11 679 if ((err = http_get_line(s, line, sizeof(line))) < 0)
yading@11 680 return err;
yading@11 681 } while (!*line); /* skip CR LF from last chunk */
yading@11 682
yading@11 683 s->chunksize = strtoll(line, NULL, 16);
yading@11 684
yading@11 685 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
yading@11 686
yading@11 687 if (!s->chunksize)
yading@11 688 return 0;
yading@11 689 break;
yading@11 690 }
yading@11 691 }
yading@11 692 size = FFMIN(size, s->chunksize);
yading@11 693 }
yading@11 694 return http_buf_read(h, buf, size);
yading@11 695 }
yading@11 696
yading@11 697 /* used only when posting data */
yading@11 698 static int http_write(URLContext *h, const uint8_t *buf, int size)
yading@11 699 {
yading@11 700 char temp[11] = ""; /* 32-bit hex + CRLF + nul */
yading@11 701 int ret;
yading@11 702 char crlf[] = "\r\n";
yading@11 703 HTTPContext *s = h->priv_data;
yading@11 704
yading@11 705 if (!s->chunked_post) {
yading@11 706 /* non-chunked data is sent without any special encoding */
yading@11 707 return ffurl_write(s->hd, buf, size);
yading@11 708 }
yading@11 709
yading@11 710 /* silently ignore zero-size data since chunk encoding that would
yading@11 711 * signal EOF */
yading@11 712 if (size > 0) {
yading@11 713 /* upload data using chunked encoding */
yading@11 714 snprintf(temp, sizeof(temp), "%x\r\n", size);
yading@11 715
yading@11 716 if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
yading@11 717 (ret = ffurl_write(s->hd, buf, size)) < 0 ||
yading@11 718 (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
yading@11 719 return ret;
yading@11 720 }
yading@11 721 return size;
yading@11 722 }
yading@11 723
yading@11 724 static int http_shutdown(URLContext *h, int flags)
yading@11 725 {
yading@11 726 int ret = 0;
yading@11 727 char footer[] = "0\r\n\r\n";
yading@11 728 HTTPContext *s = h->priv_data;
yading@11 729
yading@11 730 /* signal end of chunked encoding if used */
yading@11 731 if ((flags & AVIO_FLAG_WRITE) && s->chunked_post) {
yading@11 732 ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
yading@11 733 ret = ret > 0 ? 0 : ret;
yading@11 734 s->end_chunked_post = 1;
yading@11 735 }
yading@11 736
yading@11 737 return ret;
yading@11 738 }
yading@11 739
yading@11 740 static int http_close(URLContext *h)
yading@11 741 {
yading@11 742 int ret = 0;
yading@11 743 HTTPContext *s = h->priv_data;
yading@11 744
yading@11 745 if (!s->end_chunked_post) {
yading@11 746 /* Close the write direction by sending the end of chunked encoding. */
yading@11 747 ret = http_shutdown(h, h->flags);
yading@11 748 }
yading@11 749
yading@11 750 if (s->hd)
yading@11 751 ffurl_closep(&s->hd);
yading@11 752 return ret;
yading@11 753 }
yading@11 754
yading@11 755 static int64_t http_seek(URLContext *h, int64_t off, int whence)
yading@11 756 {
yading@11 757 HTTPContext *s = h->priv_data;
yading@11 758 URLContext *old_hd = s->hd;
yading@11 759 int64_t old_off = s->off;
yading@11 760 uint8_t old_buf[BUFFER_SIZE];
yading@11 761 int old_buf_size;
yading@11 762
yading@11 763 if (whence == AVSEEK_SIZE)
yading@11 764 return s->filesize;
yading@11 765 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
yading@11 766 return -1;
yading@11 767
yading@11 768 /* we save the old context in case the seek fails */
yading@11 769 old_buf_size = s->buf_end - s->buf_ptr;
yading@11 770 memcpy(old_buf, s->buf_ptr, old_buf_size);
yading@11 771 s->hd = NULL;
yading@11 772 if (whence == SEEK_CUR)
yading@11 773 off += s->off;
yading@11 774 else if (whence == SEEK_END)
yading@11 775 off += s->filesize;
yading@11 776 s->off = off;
yading@11 777
yading@11 778 /* if it fails, continue on old connection */
yading@11 779 if (http_open_cnx(h) < 0) {
yading@11 780 memcpy(s->buffer, old_buf, old_buf_size);
yading@11 781 s->buf_ptr = s->buffer;
yading@11 782 s->buf_end = s->buffer + old_buf_size;
yading@11 783 s->hd = old_hd;
yading@11 784 s->off = old_off;
yading@11 785 return -1;
yading@11 786 }
yading@11 787 ffurl_close(old_hd);
yading@11 788 return off;
yading@11 789 }
yading@11 790
yading@11 791 static int
yading@11 792 http_get_file_handle(URLContext *h)
yading@11 793 {
yading@11 794 HTTPContext *s = h->priv_data;
yading@11 795 return ffurl_get_file_handle(s->hd);
yading@11 796 }
yading@11 797
yading@11 798 #if CONFIG_HTTP_PROTOCOL
yading@11 799 URLProtocol ff_http_protocol = {
yading@11 800 .name = "http",
yading@11 801 .url_open = http_open,
yading@11 802 .url_read = http_read,
yading@11 803 .url_write = http_write,
yading@11 804 .url_seek = http_seek,
yading@11 805 .url_close = http_close,
yading@11 806 .url_get_file_handle = http_get_file_handle,
yading@11 807 .url_shutdown = http_shutdown,
yading@11 808 .priv_data_size = sizeof(HTTPContext),
yading@11 809 .priv_data_class = &http_context_class,
yading@11 810 .flags = URL_PROTOCOL_FLAG_NETWORK,
yading@11 811 };
yading@11 812 #endif
yading@11 813 #if CONFIG_HTTPS_PROTOCOL
yading@11 814 URLProtocol ff_https_protocol = {
yading@11 815 .name = "https",
yading@11 816 .url_open = http_open,
yading@11 817 .url_read = http_read,
yading@11 818 .url_write = http_write,
yading@11 819 .url_seek = http_seek,
yading@11 820 .url_close = http_close,
yading@11 821 .url_get_file_handle = http_get_file_handle,
yading@11 822 .url_shutdown = http_shutdown,
yading@11 823 .priv_data_size = sizeof(HTTPContext),
yading@11 824 .priv_data_class = &https_context_class,
yading@11 825 .flags = URL_PROTOCOL_FLAG_NETWORK,
yading@11 826 };
yading@11 827 #endif
yading@11 828
yading@11 829 #if CONFIG_HTTPPROXY_PROTOCOL
yading@11 830 static int http_proxy_close(URLContext *h)
yading@11 831 {
yading@11 832 HTTPContext *s = h->priv_data;
yading@11 833 if (s->hd)
yading@11 834 ffurl_closep(&s->hd);
yading@11 835 return 0;
yading@11 836 }
yading@11 837
yading@11 838 static int http_proxy_open(URLContext *h, const char *uri, int flags)
yading@11 839 {
yading@11 840 HTTPContext *s = h->priv_data;
yading@11 841 char hostname[1024], hoststr[1024];
yading@11 842 char auth[1024], pathbuf[1024], *path;
yading@11 843 char lower_url[100];
yading@11 844 int port, ret = 0, attempts = 0;
yading@11 845 HTTPAuthType cur_auth_type;
yading@11 846 char *authstr;
yading@11 847 int new_loc;
yading@11 848 AVDictionary *opts = NULL;
yading@11 849 char opts_format[20];
yading@11 850
yading@11 851 if( s->seekable == 1 )
yading@11 852 h->is_streamed = 0;
yading@11 853 else
yading@11 854 h->is_streamed = 1;
yading@11 855
yading@11 856 av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
yading@11 857 pathbuf, sizeof(pathbuf), uri);
yading@11 858 ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
yading@11 859 path = pathbuf;
yading@11 860 if (*path == '/')
yading@11 861 path++;
yading@11 862
yading@11 863 ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
yading@11 864 NULL);
yading@11 865 redo:
yading@11 866 if (s->rw_timeout != -1) {
yading@11 867 snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout);
yading@11 868 av_dict_set(&opts, "timeout", opts_format, 0);
yading@11 869 } /* if option is not given, don't pass it and let tcp use its own default */
yading@11 870 ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
yading@11 871 &h->interrupt_callback, &opts);
yading@11 872 av_dict_free(&opts);
yading@11 873 if (ret < 0)
yading@11 874 return ret;
yading@11 875
yading@11 876 authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
yading@11 877 path, "CONNECT");
yading@11 878 snprintf(s->buffer, sizeof(s->buffer),
yading@11 879 "CONNECT %s HTTP/1.1\r\n"
yading@11 880 "Host: %s\r\n"
yading@11 881 "Connection: close\r\n"
yading@11 882 "%s%s"
yading@11 883 "\r\n",
yading@11 884 path,
yading@11 885 hoststr,
yading@11 886 authstr ? "Proxy-" : "", authstr ? authstr : "");
yading@11 887 av_freep(&authstr);
yading@11 888
yading@11 889 if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
yading@11 890 goto fail;
yading@11 891
yading@11 892 s->buf_ptr = s->buffer;
yading@11 893 s->buf_end = s->buffer;
yading@11 894 s->line_count = 0;
yading@11 895 s->filesize = -1;
yading@11 896 cur_auth_type = s->proxy_auth_state.auth_type;
yading@11 897
yading@11 898 /* Note: This uses buffering, potentially reading more than the
yading@11 899 * HTTP header. If tunneling a protocol where the server starts
yading@11 900 * the conversation, we might buffer part of that here, too.
yading@11 901 * Reading that requires using the proper ffurl_read() function
yading@11 902 * on this URLContext, not using the fd directly (as the tls
yading@11 903 * protocol does). This shouldn't be an issue for tls though,
yading@11 904 * since the client starts the conversation there, so there
yading@11 905 * is no extra data that we might buffer up here.
yading@11 906 */
yading@11 907 ret = http_read_header(h, &new_loc);
yading@11 908 if (ret < 0)
yading@11 909 goto fail;
yading@11 910
yading@11 911 attempts++;
yading@11 912 if (s->http_code == 407 &&
yading@11 913 (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
yading@11 914 s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
yading@11 915 ffurl_closep(&s->hd);
yading@11 916 goto redo;
yading@11 917 }
yading@11 918
yading@11 919 if (s->http_code < 400)
yading@11 920 return 0;
yading@11 921 ret = AVERROR(EIO);
yading@11 922
yading@11 923 fail:
yading@11 924 http_proxy_close(h);
yading@11 925 return ret;
yading@11 926 }
yading@11 927
yading@11 928 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
yading@11 929 {
yading@11 930 HTTPContext *s = h->priv_data;
yading@11 931 return ffurl_write(s->hd, buf, size);
yading@11 932 }
yading@11 933
yading@11 934 URLProtocol ff_httpproxy_protocol = {
yading@11 935 .name = "httpproxy",
yading@11 936 .url_open = http_proxy_open,
yading@11 937 .url_read = http_buf_read,
yading@11 938 .url_write = http_proxy_write,
yading@11 939 .url_close = http_proxy_close,
yading@11 940 .url_get_file_handle = http_get_file_handle,
yading@11 941 .priv_data_size = sizeof(HTTPContext),
yading@11 942 .flags = URL_PROTOCOL_FLAG_NETWORK,
yading@11 943 };
yading@11 944 #endif