yading@11: /* yading@11: * HTTP protocol for ffmpeg client yading@11: * Copyright (c) 2000, 2001 Fabrice Bellard yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "libavutil/avstring.h" yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "network.h" yading@11: #include "http.h" yading@11: #include "os_support.h" yading@11: #include "httpauth.h" yading@11: #include "url.h" yading@11: #include "libavutil/opt.h" yading@11: yading@11: /* XXX: POST protocol is not completely implemented because ffmpeg uses yading@11: only a subset of it. */ yading@11: yading@11: /* The IO buffer size is unrelated to the max URL size in itself, but needs yading@11: * to be large enough to fit the full request headers (including long yading@11: * path names). yading@11: */ yading@11: #define BUFFER_SIZE MAX_URL_SIZE yading@11: #define MAX_REDIRECTS 8 yading@11: yading@11: typedef struct { yading@11: const AVClass *class; yading@11: URLContext *hd; yading@11: unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end; yading@11: int line_count; yading@11: int http_code; yading@11: int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */ yading@11: char *content_type; yading@11: char *user_agent; yading@11: int64_t off, filesize; yading@11: char location[MAX_URL_SIZE]; yading@11: HTTPAuthState auth_state; yading@11: HTTPAuthState proxy_auth_state; yading@11: char *headers; yading@11: int willclose; /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */ yading@11: int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */ yading@11: int chunked_post; yading@11: int end_chunked_post; /**< A flag which indicates if the end of chunked encoding has been sent. */ yading@11: int end_header; /**< A flag which indicates we have finished to read POST reply. */ yading@11: int multiple_requests; /**< A flag which indicates if we use persistent connections. */ yading@11: uint8_t *post_data; yading@11: int post_datalen; yading@11: int is_akamai; yading@11: int rw_timeout; yading@11: char *mime_type; yading@11: char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name) yading@11: } HTTPContext; yading@11: yading@11: #define OFFSET(x) offsetof(HTTPContext, x) yading@11: #define D AV_OPT_FLAG_DECODING_PARAM yading@11: #define E AV_OPT_FLAG_ENCODING_PARAM yading@11: #define DEFAULT_USER_AGENT "Mozilla/5.0 Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION) yading@11: static const AVOption options[] = { yading@11: {"seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, D }, yading@11: {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E }, yading@11: {"headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E }, yading@11: {"content_type", "force a content type", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E }, yading@11: {"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = DEFAULT_USER_AGENT}, 0, 0, D }, yading@11: {"multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E }, yading@11: {"post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D|E }, yading@11: {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E }, yading@11: {"mime_type", "set MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, {0}, 0, 0, 0 }, yading@11: {"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: {NULL} yading@11: }; yading@11: #define HTTP_CLASS(flavor)\ yading@11: static const AVClass flavor ## _context_class = {\ yading@11: .class_name = #flavor,\ yading@11: .item_name = av_default_item_name,\ yading@11: .option = options,\ yading@11: .version = LIBAVUTIL_VERSION_INT,\ yading@11: } yading@11: yading@11: HTTP_CLASS(http); yading@11: HTTP_CLASS(https); yading@11: yading@11: static int http_connect(URLContext *h, const char *path, const char *local_path, yading@11: const char *hoststr, const char *auth, yading@11: const char *proxyauth, int *new_location); yading@11: yading@11: void ff_http_init_auth_state(URLContext *dest, const URLContext *src) yading@11: { yading@11: memcpy(&((HTTPContext*)dest->priv_data)->auth_state, yading@11: &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState)); yading@11: memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state, yading@11: &((HTTPContext*)src->priv_data)->proxy_auth_state, yading@11: sizeof(HTTPAuthState)); yading@11: } yading@11: yading@11: /* return non zero if error */ yading@11: static int http_open_cnx(URLContext *h) yading@11: { yading@11: const char *path, *proxy_path, *lower_proto = "tcp", *local_path; yading@11: char hostname[1024], hoststr[1024], proto[10]; yading@11: char auth[1024], proxyauth[1024] = ""; yading@11: char path1[MAX_URL_SIZE]; yading@11: char buf[1024], urlbuf[MAX_URL_SIZE]; yading@11: int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0; yading@11: HTTPAuthType cur_auth_type, cur_proxy_auth_type; yading@11: HTTPContext *s = h->priv_data; yading@11: yading@11: /* fill the dest addr */ yading@11: redo: yading@11: /* needed in any case to build the host string */ yading@11: av_url_split(proto, sizeof(proto), auth, sizeof(auth), yading@11: hostname, sizeof(hostname), &port, yading@11: path1, sizeof(path1), s->location); yading@11: ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL); yading@11: yading@11: proxy_path = getenv("http_proxy"); yading@11: use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) && yading@11: proxy_path != NULL && av_strstart(proxy_path, "http://", NULL); yading@11: yading@11: if (!strcmp(proto, "https")) { yading@11: lower_proto = "tls"; yading@11: use_proxy = 0; yading@11: if (port < 0) yading@11: port = 443; yading@11: } yading@11: if (port < 0) yading@11: port = 80; yading@11: yading@11: if (path1[0] == '\0') yading@11: path = "/"; yading@11: else yading@11: path = path1; yading@11: local_path = path; yading@11: if (use_proxy) { yading@11: /* Reassemble the request URL without auth string - we don't yading@11: * want to leak the auth to the proxy. */ yading@11: ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s", yading@11: path1); yading@11: path = urlbuf; yading@11: av_url_split(NULL, 0, proxyauth, sizeof(proxyauth), yading@11: hostname, sizeof(hostname), &port, NULL, 0, proxy_path); yading@11: } yading@11: yading@11: ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL); yading@11: yading@11: if (!s->hd) { yading@11: AVDictionary *opts = NULL; yading@11: char opts_format[20]; yading@11: if (s->rw_timeout != -1) { yading@11: snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout); yading@11: av_dict_set(&opts, "timeout", opts_format, 0); yading@11: } /* if option is not given, don't pass it and let tcp use its own default */ yading@11: err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE, yading@11: &h->interrupt_callback, &opts); yading@11: av_dict_free(&opts); yading@11: if (err < 0) yading@11: goto fail; yading@11: } yading@11: yading@11: cur_auth_type = s->auth_state.auth_type; yading@11: cur_proxy_auth_type = s->auth_state.auth_type; yading@11: if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0) yading@11: goto fail; yading@11: attempts++; yading@11: if (s->http_code == 401) { yading@11: if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) && yading@11: s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) { yading@11: ffurl_closep(&s->hd); yading@11: goto redo; yading@11: } else yading@11: goto fail; yading@11: } yading@11: if (s->http_code == 407) { yading@11: if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) && yading@11: s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) { yading@11: ffurl_closep(&s->hd); yading@11: goto redo; yading@11: } else yading@11: goto fail; yading@11: } yading@11: if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307) yading@11: && location_changed == 1) { yading@11: /* url moved, get next */ yading@11: ffurl_closep(&s->hd); yading@11: if (redirects++ >= MAX_REDIRECTS) yading@11: return AVERROR(EIO); yading@11: /* Restart the authentication process with the new target, which yading@11: * might use a different auth mechanism. */ yading@11: memset(&s->auth_state, 0, sizeof(s->auth_state)); yading@11: attempts = 0; yading@11: location_changed = 0; yading@11: goto redo; yading@11: } yading@11: return 0; yading@11: fail: yading@11: if (s->hd) yading@11: ffurl_closep(&s->hd); yading@11: return AVERROR(EIO); yading@11: } yading@11: yading@11: int ff_http_do_new_request(URLContext *h, const char *uri) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: yading@11: s->off = 0; yading@11: av_strlcpy(s->location, uri, sizeof(s->location)); yading@11: yading@11: return http_open_cnx(h); yading@11: } yading@11: yading@11: static int http_open(URLContext *h, const char *uri, int flags) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: yading@11: if( s->seekable == 1 ) yading@11: h->is_streamed = 0; yading@11: else yading@11: h->is_streamed = 1; yading@11: yading@11: s->filesize = -1; yading@11: av_strlcpy(s->location, uri, sizeof(s->location)); yading@11: yading@11: if (s->headers) { yading@11: int len = strlen(s->headers); yading@11: if (len < 2 || strcmp("\r\n", s->headers + len - 2)) yading@11: av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n"); yading@11: } yading@11: yading@11: return http_open_cnx(h); yading@11: } yading@11: static int http_getc(HTTPContext *s) yading@11: { yading@11: int len; yading@11: if (s->buf_ptr >= s->buf_end) { yading@11: len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE); yading@11: if (len < 0) { yading@11: return len; yading@11: } else if (len == 0) { yading@11: return -1; yading@11: } else { yading@11: s->buf_ptr = s->buffer; yading@11: s->buf_end = s->buffer + len; yading@11: } yading@11: } yading@11: return *s->buf_ptr++; yading@11: } yading@11: yading@11: static int http_get_line(HTTPContext *s, char *line, int line_size) yading@11: { yading@11: int ch; yading@11: char *q; yading@11: yading@11: q = line; yading@11: for(;;) { yading@11: ch = http_getc(s); yading@11: if (ch < 0) yading@11: return ch; yading@11: if (ch == '\n') { yading@11: /* process line */ yading@11: if (q > line && q[-1] == '\r') yading@11: q--; yading@11: *q = '\0'; yading@11: yading@11: return 0; yading@11: } else { yading@11: if ((q - line) < line_size - 1) yading@11: *q++ = ch; yading@11: } yading@11: } yading@11: } yading@11: yading@11: static int process_line(URLContext *h, char *line, int line_count, yading@11: int *new_location) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: char *tag, *p, *end; yading@11: yading@11: /* end of header */ yading@11: if (line[0] == '\0') { yading@11: s->end_header = 1; yading@11: return 0; yading@11: } yading@11: yading@11: p = line; yading@11: if (line_count == 0) { yading@11: while (!av_isspace(*p) && *p != '\0') yading@11: p++; yading@11: while (av_isspace(*p)) yading@11: p++; yading@11: s->http_code = strtol(p, &end, 10); yading@11: yading@11: av_dlog(NULL, "http_code=%d\n", s->http_code); yading@11: yading@11: /* error codes are 4xx and 5xx, but regard 401 as a success, so we yading@11: * don't abort until all headers have been parsed. */ yading@11: if (s->http_code >= 400 && s->http_code < 600 && (s->http_code != 401 yading@11: || s->auth_state.auth_type != HTTP_AUTH_NONE) && yading@11: (s->http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) { yading@11: end += strspn(end, SPACE_CHARS); yading@11: av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", yading@11: s->http_code, end); yading@11: return -1; yading@11: } yading@11: } else { yading@11: while (*p != '\0' && *p != ':') yading@11: p++; yading@11: if (*p != ':') yading@11: return 1; yading@11: yading@11: *p = '\0'; yading@11: tag = line; yading@11: p++; yading@11: while (av_isspace(*p)) yading@11: p++; yading@11: if (!av_strcasecmp(tag, "Location")) { yading@11: av_strlcpy(s->location, p, sizeof(s->location)); yading@11: *new_location = 1; yading@11: } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) { yading@11: s->filesize = strtoll(p, NULL, 10); yading@11: } else if (!av_strcasecmp (tag, "Content-Range")) { yading@11: /* "bytes $from-$to/$document_size" */ yading@11: const char *slash; yading@11: if (!strncmp (p, "bytes ", 6)) { yading@11: p += 6; yading@11: s->off = strtoll(p, NULL, 10); yading@11: if ((slash = strchr(p, '/')) && strlen(slash) > 0) yading@11: s->filesize = strtoll(slash+1, NULL, 10); yading@11: } yading@11: if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647)) yading@11: h->is_streamed = 0; /* we _can_ in fact seek */ yading@11: } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5) && s->seekable == -1) { yading@11: h->is_streamed = 0; yading@11: } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) { yading@11: s->filesize = -1; yading@11: s->chunksize = 0; yading@11: } else if (!av_strcasecmp (tag, "WWW-Authenticate")) { yading@11: ff_http_auth_handle_header(&s->auth_state, tag, p); yading@11: } else if (!av_strcasecmp (tag, "Authentication-Info")) { yading@11: ff_http_auth_handle_header(&s->auth_state, tag, p); yading@11: } else if (!av_strcasecmp (tag, "Proxy-Authenticate")) { yading@11: ff_http_auth_handle_header(&s->proxy_auth_state, tag, p); yading@11: } else if (!av_strcasecmp (tag, "Connection")) { yading@11: if (!strcmp(p, "close")) yading@11: s->willclose = 1; yading@11: } else if (!av_strcasecmp (tag, "Server") && !av_strcasecmp (p, "AkamaiGHost")) { yading@11: s->is_akamai = 1; yading@11: } else if (!av_strcasecmp (tag, "Content-Type")) { yading@11: av_free(s->mime_type); s->mime_type = av_strdup(p); yading@11: } else if (!av_strcasecmp (tag, "Set-Cookie")) { yading@11: if (!s->cookies) { yading@11: if (!(s->cookies = av_strdup(p))) yading@11: return AVERROR(ENOMEM); yading@11: } else { yading@11: char *tmp = s->cookies; yading@11: size_t str_size = strlen(tmp) + strlen(p) + 2; yading@11: if (!(s->cookies = av_malloc(str_size))) { yading@11: s->cookies = tmp; yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: snprintf(s->cookies, str_size, "%s\n%s", tmp, p); yading@11: av_free(tmp); yading@11: } yading@11: } yading@11: } yading@11: return 1; yading@11: } yading@11: yading@11: /** yading@11: * Create a string containing cookie values for use as a HTTP cookie header yading@11: * field value for a particular path and domain from the cookie values stored in yading@11: * the HTTP protocol context. The cookie string is stored in *cookies. yading@11: * yading@11: * @return a negative value if an error condition occurred, 0 otherwise yading@11: */ yading@11: static int get_cookies(HTTPContext *s, char **cookies, const char *path, yading@11: const char *domain) yading@11: { yading@11: // cookie strings will look like Set-Cookie header field values. Multiple yading@11: // Set-Cookie fields will result in multiple values delimited by a newline yading@11: int ret = 0; yading@11: char *next, *cookie, *set_cookies = av_strdup(s->cookies), *cset_cookies = set_cookies; yading@11: yading@11: if (!set_cookies) return AVERROR(EINVAL); yading@11: yading@11: *cookies = NULL; yading@11: while ((cookie = av_strtok(set_cookies, "\n", &next))) { yading@11: int domain_offset = 0; yading@11: char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL; yading@11: set_cookies = NULL; yading@11: yading@11: while ((param = av_strtok(cookie, "; ", &next_param))) { yading@11: cookie = NULL; yading@11: if (!av_strncasecmp("path=", param, 5)) { yading@11: av_free(cpath); yading@11: cpath = av_strdup(¶m[5]); yading@11: } else if (!av_strncasecmp("domain=", param, 7)) { yading@11: av_free(cdomain); yading@11: cdomain = av_strdup(¶m[7]); yading@11: } else if (!av_strncasecmp("secure", param, 6) || yading@11: !av_strncasecmp("comment", param, 7) || yading@11: !av_strncasecmp("max-age", param, 7) || yading@11: !av_strncasecmp("version", param, 7)) { yading@11: // ignore Comment, Max-Age, Secure and Version yading@11: } else { yading@11: av_free(cvalue); yading@11: cvalue = av_strdup(param); yading@11: } yading@11: } yading@11: yading@11: // ensure all of the necessary values are valid yading@11: if (!cdomain || !cpath || !cvalue) { yading@11: av_log(s, AV_LOG_WARNING, yading@11: "Invalid cookie found, no value, path or domain specified\n"); yading@11: goto done_cookie; yading@11: } yading@11: yading@11: // check if the request path matches the cookie path yading@11: if (av_strncasecmp(path, cpath, strlen(cpath))) yading@11: goto done_cookie; yading@11: yading@11: // the domain should be at least the size of our cookie domain yading@11: domain_offset = strlen(domain) - strlen(cdomain); yading@11: if (domain_offset < 0) yading@11: goto done_cookie; yading@11: yading@11: // match the cookie domain yading@11: if (av_strcasecmp(&domain[domain_offset], cdomain)) yading@11: goto done_cookie; yading@11: yading@11: // cookie parameters match, so copy the value yading@11: if (!*cookies) { yading@11: if (!(*cookies = av_strdup(cvalue))) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto done_cookie; yading@11: } yading@11: } else { yading@11: char *tmp = *cookies; yading@11: size_t str_size = strlen(cvalue) + strlen(*cookies) + 3; yading@11: if (!(*cookies = av_malloc(str_size))) { yading@11: ret = AVERROR(ENOMEM); yading@11: goto done_cookie; yading@11: } yading@11: snprintf(*cookies, str_size, "%s; %s", tmp, cvalue); yading@11: av_free(tmp); yading@11: } yading@11: yading@11: done_cookie: yading@11: av_free(cdomain); yading@11: av_free(cpath); yading@11: av_free(cvalue); yading@11: if (ret < 0) { yading@11: if (*cookies) av_freep(cookies); yading@11: av_free(cset_cookies); yading@11: return ret; yading@11: } yading@11: } yading@11: yading@11: av_free(cset_cookies); yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static inline int has_header(const char *str, const char *header) yading@11: { yading@11: /* header + 2 to skip over CRLF prefix. (make sure you have one!) */ yading@11: if (!str) yading@11: return 0; yading@11: return av_stristart(str, header + 2, NULL) || av_stristr(str, header); yading@11: } yading@11: yading@11: static int http_read_header(URLContext *h, int *new_location) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: char line[MAX_URL_SIZE]; yading@11: int err = 0; yading@11: yading@11: s->chunksize = -1; yading@11: yading@11: for (;;) { yading@11: if ((err = http_get_line(s, line, sizeof(line))) < 0) yading@11: return err; yading@11: yading@11: av_dlog(NULL, "header='%s'\n", line); yading@11: yading@11: err = process_line(h, line, s->line_count, new_location); yading@11: if (err < 0) yading@11: return err; yading@11: if (err == 0) yading@11: break; yading@11: s->line_count++; yading@11: } yading@11: yading@11: return err; yading@11: } yading@11: yading@11: static int http_connect(URLContext *h, const char *path, const char *local_path, yading@11: const char *hoststr, const char *auth, yading@11: const char *proxyauth, int *new_location) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: int post, err; yading@11: char headers[4096] = ""; yading@11: char *authstr = NULL, *proxyauthstr = NULL; yading@11: int64_t off = s->off; yading@11: int len = 0; yading@11: const char *method; yading@11: yading@11: yading@11: /* send http header */ yading@11: post = h->flags & AVIO_FLAG_WRITE; yading@11: yading@11: if (s->post_data) { yading@11: /* force POST method and disable chunked encoding when yading@11: * custom HTTP post data is set */ yading@11: post = 1; yading@11: s->chunked_post = 0; yading@11: } yading@11: yading@11: method = post ? "POST" : "GET"; yading@11: authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path, yading@11: method); yading@11: proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth, yading@11: local_path, method); yading@11: yading@11: /* set default headers if needed */ yading@11: if (!has_header(s->headers, "\r\nUser-Agent: ")) yading@11: len += av_strlcatf(headers + len, sizeof(headers) - len, yading@11: "User-Agent: %s\r\n", s->user_agent); yading@11: if (!has_header(s->headers, "\r\nAccept: ")) yading@11: len += av_strlcpy(headers + len, "Accept: */*\r\n", yading@11: sizeof(headers) - len); yading@11: // Note: we send this on purpose even when s->off is 0 when we're probing, yading@11: // since it allows us to detect more reliably if a (non-conforming) yading@11: // server supports seeking by analysing the reply headers. yading@11: if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->seekable == -1)) yading@11: len += av_strlcatf(headers + len, sizeof(headers) - len, yading@11: "Range: bytes=%"PRId64"-\r\n", s->off); yading@11: yading@11: if (!has_header(s->headers, "\r\nConnection: ")) { yading@11: if (s->multiple_requests) { yading@11: len += av_strlcpy(headers + len, "Connection: keep-alive\r\n", yading@11: sizeof(headers) - len); yading@11: } else { yading@11: len += av_strlcpy(headers + len, "Connection: close\r\n", yading@11: sizeof(headers) - len); yading@11: } yading@11: } yading@11: yading@11: if (!has_header(s->headers, "\r\nHost: ")) yading@11: len += av_strlcatf(headers + len, sizeof(headers) - len, yading@11: "Host: %s\r\n", hoststr); yading@11: if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data) yading@11: len += av_strlcatf(headers + len, sizeof(headers) - len, yading@11: "Content-Length: %d\r\n", s->post_datalen); yading@11: if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type) yading@11: len += av_strlcatf(headers + len, sizeof(headers) - len, yading@11: "Content-Type: %s\r\n", s->content_type); yading@11: if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) { yading@11: char *cookies = NULL; yading@11: if (!get_cookies(s, &cookies, path, hoststr)) { yading@11: len += av_strlcatf(headers + len, sizeof(headers) - len, yading@11: "Cookie: %s\r\n", cookies); yading@11: av_free(cookies); yading@11: } yading@11: } yading@11: yading@11: /* now add in custom headers */ yading@11: if (s->headers) yading@11: av_strlcpy(headers + len, s->headers, sizeof(headers) - len); yading@11: yading@11: snprintf(s->buffer, sizeof(s->buffer), yading@11: "%s %s HTTP/1.1\r\n" yading@11: "%s" yading@11: "%s" yading@11: "%s" yading@11: "%s%s" yading@11: "\r\n", yading@11: method, yading@11: path, yading@11: post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "", yading@11: headers, yading@11: authstr ? authstr : "", yading@11: proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : ""); yading@11: yading@11: av_freep(&authstr); yading@11: av_freep(&proxyauthstr); yading@11: if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0) yading@11: return err; yading@11: yading@11: if (s->post_data) yading@11: if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0) yading@11: return err; yading@11: yading@11: /* init input buffer */ yading@11: s->buf_ptr = s->buffer; yading@11: s->buf_end = s->buffer; yading@11: s->line_count = 0; yading@11: s->off = 0; yading@11: s->filesize = -1; yading@11: s->willclose = 0; yading@11: s->end_chunked_post = 0; yading@11: s->end_header = 0; yading@11: if (post && !s->post_data) { yading@11: /* Pretend that it did work. We didn't read any header yet, since yading@11: * we've still to send the POST data, but the code calling this yading@11: * function will check http_code after we return. */ yading@11: s->http_code = 200; yading@11: return 0; yading@11: } yading@11: yading@11: /* wait for header */ yading@11: err = http_read_header(h, new_location); yading@11: if (err < 0) yading@11: return err; yading@11: yading@11: return (off == s->off) ? 0 : -1; yading@11: } yading@11: yading@11: yading@11: static int http_buf_read(URLContext *h, uint8_t *buf, int size) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: int len; yading@11: /* read bytes from input buffer first */ yading@11: len = s->buf_end - s->buf_ptr; yading@11: if (len > 0) { yading@11: if (len > size) yading@11: len = size; yading@11: memcpy(buf, s->buf_ptr, len); yading@11: s->buf_ptr += len; yading@11: } else { yading@11: if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize) yading@11: return AVERROR_EOF; yading@11: len = ffurl_read(s->hd, buf, size); yading@11: } yading@11: if (len > 0) { yading@11: s->off += len; yading@11: if (s->chunksize > 0) yading@11: s->chunksize -= len; yading@11: } yading@11: return len; yading@11: } yading@11: yading@11: static int http_read(URLContext *h, uint8_t *buf, int size) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: int err, new_location; yading@11: yading@11: if (!s->hd) yading@11: return AVERROR_EOF; yading@11: yading@11: if (s->end_chunked_post && !s->end_header) { yading@11: err = http_read_header(h, &new_location); yading@11: if (err < 0) yading@11: return err; yading@11: } yading@11: yading@11: if (s->chunksize >= 0) { yading@11: if (!s->chunksize) { yading@11: char line[32]; yading@11: yading@11: for(;;) { yading@11: do { yading@11: if ((err = http_get_line(s, line, sizeof(line))) < 0) yading@11: return err; yading@11: } while (!*line); /* skip CR LF from last chunk */ yading@11: yading@11: s->chunksize = strtoll(line, NULL, 16); yading@11: yading@11: av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize); yading@11: yading@11: if (!s->chunksize) yading@11: return 0; yading@11: break; yading@11: } yading@11: } yading@11: size = FFMIN(size, s->chunksize); yading@11: } yading@11: return http_buf_read(h, buf, size); yading@11: } yading@11: yading@11: /* used only when posting data */ yading@11: static int http_write(URLContext *h, const uint8_t *buf, int size) yading@11: { yading@11: char temp[11] = ""; /* 32-bit hex + CRLF + nul */ yading@11: int ret; yading@11: char crlf[] = "\r\n"; yading@11: HTTPContext *s = h->priv_data; yading@11: yading@11: if (!s->chunked_post) { yading@11: /* non-chunked data is sent without any special encoding */ yading@11: return ffurl_write(s->hd, buf, size); yading@11: } yading@11: yading@11: /* silently ignore zero-size data since chunk encoding that would yading@11: * signal EOF */ yading@11: if (size > 0) { yading@11: /* upload data using chunked encoding */ yading@11: snprintf(temp, sizeof(temp), "%x\r\n", size); yading@11: yading@11: if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 || yading@11: (ret = ffurl_write(s->hd, buf, size)) < 0 || yading@11: (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0) yading@11: return ret; yading@11: } yading@11: return size; yading@11: } yading@11: yading@11: static int http_shutdown(URLContext *h, int flags) yading@11: { yading@11: int ret = 0; yading@11: char footer[] = "0\r\n\r\n"; yading@11: HTTPContext *s = h->priv_data; yading@11: yading@11: /* signal end of chunked encoding if used */ yading@11: if ((flags & AVIO_FLAG_WRITE) && s->chunked_post) { yading@11: ret = ffurl_write(s->hd, footer, sizeof(footer) - 1); yading@11: ret = ret > 0 ? 0 : ret; yading@11: s->end_chunked_post = 1; yading@11: } yading@11: yading@11: return ret; yading@11: } yading@11: yading@11: static int http_close(URLContext *h) yading@11: { yading@11: int ret = 0; yading@11: HTTPContext *s = h->priv_data; yading@11: yading@11: if (!s->end_chunked_post) { yading@11: /* Close the write direction by sending the end of chunked encoding. */ yading@11: ret = http_shutdown(h, h->flags); yading@11: } yading@11: yading@11: if (s->hd) yading@11: ffurl_closep(&s->hd); yading@11: return ret; yading@11: } yading@11: yading@11: static int64_t http_seek(URLContext *h, int64_t off, int whence) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: URLContext *old_hd = s->hd; yading@11: int64_t old_off = s->off; yading@11: uint8_t old_buf[BUFFER_SIZE]; yading@11: int old_buf_size; yading@11: yading@11: if (whence == AVSEEK_SIZE) yading@11: return s->filesize; yading@11: else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed) yading@11: return -1; yading@11: yading@11: /* we save the old context in case the seek fails */ yading@11: old_buf_size = s->buf_end - s->buf_ptr; yading@11: memcpy(old_buf, s->buf_ptr, old_buf_size); yading@11: s->hd = NULL; yading@11: if (whence == SEEK_CUR) yading@11: off += s->off; yading@11: else if (whence == SEEK_END) yading@11: off += s->filesize; yading@11: s->off = off; yading@11: yading@11: /* if it fails, continue on old connection */ yading@11: if (http_open_cnx(h) < 0) { yading@11: memcpy(s->buffer, old_buf, old_buf_size); yading@11: s->buf_ptr = s->buffer; yading@11: s->buf_end = s->buffer + old_buf_size; yading@11: s->hd = old_hd; yading@11: s->off = old_off; yading@11: return -1; yading@11: } yading@11: ffurl_close(old_hd); yading@11: return off; yading@11: } yading@11: yading@11: static int yading@11: http_get_file_handle(URLContext *h) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: return ffurl_get_file_handle(s->hd); yading@11: } yading@11: yading@11: #if CONFIG_HTTP_PROTOCOL yading@11: URLProtocol ff_http_protocol = { yading@11: .name = "http", yading@11: .url_open = http_open, yading@11: .url_read = http_read, yading@11: .url_write = http_write, yading@11: .url_seek = http_seek, yading@11: .url_close = http_close, yading@11: .url_get_file_handle = http_get_file_handle, yading@11: .url_shutdown = http_shutdown, yading@11: .priv_data_size = sizeof(HTTPContext), yading@11: .priv_data_class = &http_context_class, yading@11: .flags = URL_PROTOCOL_FLAG_NETWORK, yading@11: }; yading@11: #endif yading@11: #if CONFIG_HTTPS_PROTOCOL yading@11: URLProtocol ff_https_protocol = { yading@11: .name = "https", yading@11: .url_open = http_open, yading@11: .url_read = http_read, yading@11: .url_write = http_write, yading@11: .url_seek = http_seek, yading@11: .url_close = http_close, yading@11: .url_get_file_handle = http_get_file_handle, yading@11: .url_shutdown = http_shutdown, yading@11: .priv_data_size = sizeof(HTTPContext), yading@11: .priv_data_class = &https_context_class, yading@11: .flags = URL_PROTOCOL_FLAG_NETWORK, yading@11: }; yading@11: #endif yading@11: yading@11: #if CONFIG_HTTPPROXY_PROTOCOL yading@11: static int http_proxy_close(URLContext *h) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: if (s->hd) yading@11: ffurl_closep(&s->hd); yading@11: return 0; yading@11: } yading@11: yading@11: static int http_proxy_open(URLContext *h, const char *uri, int flags) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: char hostname[1024], hoststr[1024]; yading@11: char auth[1024], pathbuf[1024], *path; yading@11: char lower_url[100]; yading@11: int port, ret = 0, attempts = 0; yading@11: HTTPAuthType cur_auth_type; yading@11: char *authstr; yading@11: int new_loc; yading@11: AVDictionary *opts = NULL; yading@11: char opts_format[20]; yading@11: yading@11: if( s->seekable == 1 ) yading@11: h->is_streamed = 0; yading@11: else yading@11: h->is_streamed = 1; yading@11: yading@11: av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port, yading@11: pathbuf, sizeof(pathbuf), uri); yading@11: ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL); yading@11: path = pathbuf; yading@11: if (*path == '/') yading@11: path++; yading@11: yading@11: ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port, yading@11: NULL); yading@11: redo: yading@11: if (s->rw_timeout != -1) { yading@11: snprintf(opts_format, sizeof(opts_format), "%d", s->rw_timeout); yading@11: av_dict_set(&opts, "timeout", opts_format, 0); yading@11: } /* if option is not given, don't pass it and let tcp use its own default */ yading@11: ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE, yading@11: &h->interrupt_callback, &opts); yading@11: av_dict_free(&opts); yading@11: if (ret < 0) yading@11: return ret; yading@11: yading@11: authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth, yading@11: path, "CONNECT"); yading@11: snprintf(s->buffer, sizeof(s->buffer), yading@11: "CONNECT %s HTTP/1.1\r\n" yading@11: "Host: %s\r\n" yading@11: "Connection: close\r\n" yading@11: "%s%s" yading@11: "\r\n", yading@11: path, yading@11: hoststr, yading@11: authstr ? "Proxy-" : "", authstr ? authstr : ""); yading@11: av_freep(&authstr); yading@11: yading@11: if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0) yading@11: goto fail; yading@11: yading@11: s->buf_ptr = s->buffer; yading@11: s->buf_end = s->buffer; yading@11: s->line_count = 0; yading@11: s->filesize = -1; yading@11: cur_auth_type = s->proxy_auth_state.auth_type; yading@11: yading@11: /* Note: This uses buffering, potentially reading more than the yading@11: * HTTP header. If tunneling a protocol where the server starts yading@11: * the conversation, we might buffer part of that here, too. yading@11: * Reading that requires using the proper ffurl_read() function yading@11: * on this URLContext, not using the fd directly (as the tls yading@11: * protocol does). This shouldn't be an issue for tls though, yading@11: * since the client starts the conversation there, so there yading@11: * is no extra data that we might buffer up here. yading@11: */ yading@11: ret = http_read_header(h, &new_loc); yading@11: if (ret < 0) yading@11: goto fail; yading@11: yading@11: attempts++; yading@11: if (s->http_code == 407 && yading@11: (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) && yading@11: s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) { yading@11: ffurl_closep(&s->hd); yading@11: goto redo; yading@11: } yading@11: yading@11: if (s->http_code < 400) yading@11: return 0; yading@11: ret = AVERROR(EIO); yading@11: yading@11: fail: yading@11: http_proxy_close(h); yading@11: return ret; yading@11: } yading@11: yading@11: static int http_proxy_write(URLContext *h, const uint8_t *buf, int size) yading@11: { yading@11: HTTPContext *s = h->priv_data; yading@11: return ffurl_write(s->hd, buf, size); yading@11: } yading@11: yading@11: URLProtocol ff_httpproxy_protocol = { yading@11: .name = "httpproxy", yading@11: .url_open = http_proxy_open, yading@11: .url_read = http_buf_read, yading@11: .url_write = http_proxy_write, yading@11: .url_close = http_proxy_close, yading@11: .url_get_file_handle = http_get_file_handle, yading@11: .priv_data_size = sizeof(HTTPContext), yading@11: .flags = URL_PROTOCOL_FLAG_NETWORK, yading@11: }; yading@11: #endif