You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1253 lines
41KB

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