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.

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