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.

1817 lines
60KB

  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/avassert.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/time.h"
  29. #include "libavutil/parseutils.h"
  30. #include "avformat.h"
  31. #include "http.h"
  32. #include "httpauth.h"
  33. #include "internal.h"
  34. #include "network.h"
  35. #include "os_support.h"
  36. #include "url.h"
  37. /* XXX: POST protocol is not completely implemented because ffmpeg uses
  38. * only a subset of it. */
  39. /* The IO buffer size is unrelated to the max URL size in itself, but needs
  40. * to be large enough to fit the full request headers (including long
  41. * path names). */
  42. #define BUFFER_SIZE MAX_URL_SIZE
  43. #define MAX_REDIRECTS 8
  44. #define HTTP_SINGLE 1
  45. #define HTTP_MUTLI 2
  46. #define MAX_EXPIRY 19
  47. #define WHITESPACES " \n\t\r"
  48. typedef enum {
  49. LOWER_PROTO,
  50. READ_HEADERS,
  51. WRITE_REPLY_HEADERS,
  52. FINISH
  53. }HandshakeState;
  54. typedef struct HTTPContext {
  55. const AVClass *class;
  56. URLContext *hd;
  57. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  58. int line_count;
  59. int http_code;
  60. /* Used if "Transfer-Encoding: chunked" otherwise -1. */
  61. uint64_t chunksize;
  62. int chunkend;
  63. uint64_t off, end_off, filesize;
  64. char *location;
  65. HTTPAuthState auth_state;
  66. HTTPAuthState proxy_auth_state;
  67. char *http_proxy;
  68. char *headers;
  69. char *mime_type;
  70. char *user_agent;
  71. #if FF_API_HTTP_USER_AGENT
  72. char *user_agent_deprecated;
  73. #endif
  74. char *content_type;
  75. /* Set if the server correctly handles Connection: close and will close
  76. * the connection after feeding us the content. */
  77. int willclose;
  78. int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
  79. int chunked_post;
  80. /* A flag which indicates if the end of chunked encoding has been sent. */
  81. int end_chunked_post;
  82. /* A flag which indicates we have finished to read POST reply. */
  83. int end_header;
  84. /* A flag which indicates if we use persistent connections. */
  85. int multiple_requests;
  86. uint8_t *post_data;
  87. int post_datalen;
  88. int is_akamai;
  89. int is_mediagateway;
  90. char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
  91. /* A dictionary containing cookies keyed by cookie name */
  92. AVDictionary *cookie_dict;
  93. int icy;
  94. /* how much data was read since the last ICY metadata packet */
  95. uint64_t icy_data_read;
  96. /* after how many bytes of read data a new metadata packet will be found */
  97. uint64_t icy_metaint;
  98. char *icy_metadata_headers;
  99. char *icy_metadata_packet;
  100. AVDictionary *metadata;
  101. #if CONFIG_ZLIB
  102. int compressed;
  103. z_stream inflate_stream;
  104. uint8_t *inflate_buffer;
  105. #endif /* CONFIG_ZLIB */
  106. AVDictionary *chained_options;
  107. int send_expect_100;
  108. char *method;
  109. int reconnect;
  110. int reconnect_at_eof;
  111. int reconnect_streamed;
  112. int reconnect_delay;
  113. int reconnect_delay_max;
  114. int listen;
  115. char *resource;
  116. int reply_code;
  117. int is_multi_client;
  118. HandshakeState handshake_step;
  119. int is_connected_server;
  120. } HTTPContext;
  121. #define OFFSET(x) offsetof(HTTPContext, x)
  122. #define D AV_OPT_FLAG_DECODING_PARAM
  123. #define E AV_OPT_FLAG_ENCODING_PARAM
  124. #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
  125. static const AVOption options[] = {
  126. { "seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D },
  127. { "chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
  128. { "http_proxy", "set HTTP proxy to tunnel through", OFFSET(http_proxy), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
  129. { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
  130. { "content_type", "set a specific content type for the POST messages", OFFSET(content_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
  131. { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
  132. #if FF_API_HTTP_USER_AGENT
  133. { "user-agent", "override User-Agent header", OFFSET(user_agent_deprecated), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
  134. #endif
  135. { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D | E },
  136. { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
  137. { "mime_type", "export the MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
  138. { "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, { .str = NULL }, 0, 0, D },
  139. { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, D },
  140. { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
  141. { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
  142. { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
  143. { "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"},
  144. { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, "auth_type"},
  145. { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, "auth_type"},
  146. { "send_expect_100", "Force sending an Expect: 100-continue header for POST", OFFSET(send_expect_100), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
  147. { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
  148. { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
  149. { "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 },
  150. { "method", "Override the HTTP method or set the expected HTTP method from a client", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
  151. { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
  152. { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
  153. { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
  154. { "reconnect_delay_max", "max reconnect delay in seconds after which to give up", OFFSET(reconnect_delay_max), AV_OPT_TYPE_INT, { .i64 = 120 }, 0, UINT_MAX/1000/1000, D },
  155. { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
  156. { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
  157. { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
  158. { NULL }
  159. };
  160. static int http_connect(URLContext *h, const char *path, const char *local_path,
  161. const char *hoststr, const char *auth,
  162. const char *proxyauth, int *new_location);
  163. static int http_read_header(URLContext *h, int *new_location);
  164. void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
  165. {
  166. memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
  167. &((HTTPContext *)src->priv_data)->auth_state,
  168. sizeof(HTTPAuthState));
  169. memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
  170. &((HTTPContext *)src->priv_data)->proxy_auth_state,
  171. sizeof(HTTPAuthState));
  172. }
  173. static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
  174. {
  175. const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
  176. char hostname[1024], hoststr[1024], proto[10];
  177. char auth[1024], proxyauth[1024] = "";
  178. char path1[MAX_URL_SIZE];
  179. char buf[1024], urlbuf[MAX_URL_SIZE];
  180. int port, use_proxy, err, location_changed = 0;
  181. HTTPContext *s = h->priv_data;
  182. av_url_split(proto, sizeof(proto), auth, sizeof(auth),
  183. hostname, sizeof(hostname), &port,
  184. path1, sizeof(path1), s->location);
  185. ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  186. proxy_path = s->http_proxy ? s->http_proxy : getenv("http_proxy");
  187. use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
  188. proxy_path && av_strstart(proxy_path, "http://", NULL);
  189. if (!strcmp(proto, "https")) {
  190. lower_proto = "tls";
  191. use_proxy = 0;
  192. if (port < 0)
  193. port = 443;
  194. }
  195. if (port < 0)
  196. port = 80;
  197. if (path1[0] == '\0')
  198. path = "/";
  199. else
  200. path = path1;
  201. local_path = path;
  202. if (use_proxy) {
  203. /* Reassemble the request URL without auth string - we don't
  204. * want to leak the auth to the proxy. */
  205. ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
  206. path1);
  207. path = urlbuf;
  208. av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
  209. hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
  210. }
  211. ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
  212. if (!s->hd) {
  213. err = ffurl_open_whitelist(&s->hd, buf, AVIO_FLAG_READ_WRITE,
  214. &h->interrupt_callback, options,
  215. h->protocol_whitelist, h->protocol_blacklist, h);
  216. if (err < 0)
  217. return err;
  218. }
  219. err = http_connect(h, path, local_path, hoststr,
  220. auth, proxyauth, &location_changed);
  221. if (err < 0)
  222. return err;
  223. return location_changed;
  224. }
  225. /* return non zero if error */
  226. static int http_open_cnx(URLContext *h, AVDictionary **options)
  227. {
  228. HTTPAuthType cur_auth_type, cur_proxy_auth_type;
  229. HTTPContext *s = h->priv_data;
  230. int location_changed, attempts = 0, redirects = 0;
  231. redo:
  232. av_dict_copy(options, s->chained_options, 0);
  233. cur_auth_type = s->auth_state.auth_type;
  234. cur_proxy_auth_type = s->auth_state.auth_type;
  235. location_changed = http_open_cnx_internal(h, options);
  236. if (location_changed < 0)
  237. goto fail;
  238. attempts++;
  239. if (s->http_code == 401) {
  240. if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
  241. s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
  242. ffurl_closep(&s->hd);
  243. goto redo;
  244. } else
  245. goto fail;
  246. }
  247. if (s->http_code == 407) {
  248. if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
  249. s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
  250. ffurl_closep(&s->hd);
  251. goto redo;
  252. } else
  253. goto fail;
  254. }
  255. if ((s->http_code == 301 || s->http_code == 302 ||
  256. s->http_code == 303 || s->http_code == 307) &&
  257. location_changed == 1) {
  258. /* url moved, get next */
  259. ffurl_closep(&s->hd);
  260. if (redirects++ >= MAX_REDIRECTS)
  261. return AVERROR(EIO);
  262. /* Restart the authentication process with the new target, which
  263. * might use a different auth mechanism. */
  264. memset(&s->auth_state, 0, sizeof(s->auth_state));
  265. attempts = 0;
  266. location_changed = 0;
  267. goto redo;
  268. }
  269. return 0;
  270. fail:
  271. if (s->hd)
  272. ffurl_closep(&s->hd);
  273. if (location_changed < 0)
  274. return location_changed;
  275. return ff_http_averror(s->http_code, AVERROR(EIO));
  276. }
  277. int ff_http_do_new_request(URLContext *h, const char *uri)
  278. {
  279. HTTPContext *s = h->priv_data;
  280. AVDictionary *options = NULL;
  281. int ret;
  282. s->chunkend = 0;
  283. s->off = 0;
  284. s->icy_data_read = 0;
  285. av_free(s->location);
  286. s->location = av_strdup(uri);
  287. if (!s->location)
  288. return AVERROR(ENOMEM);
  289. ret = http_open_cnx(h, &options);
  290. av_dict_free(&options);
  291. return ret;
  292. }
  293. int ff_http_averror(int status_code, int default_averror)
  294. {
  295. switch (status_code) {
  296. case 400: return AVERROR_HTTP_BAD_REQUEST;
  297. case 401: return AVERROR_HTTP_UNAUTHORIZED;
  298. case 403: return AVERROR_HTTP_FORBIDDEN;
  299. case 404: return AVERROR_HTTP_NOT_FOUND;
  300. default: break;
  301. }
  302. if (status_code >= 400 && status_code <= 499)
  303. return AVERROR_HTTP_OTHER_4XX;
  304. else if (status_code >= 500)
  305. return AVERROR_HTTP_SERVER_ERROR;
  306. else
  307. return default_averror;
  308. }
  309. static int http_write_reply(URLContext* h, int status_code)
  310. {
  311. int ret, body = 0, reply_code, message_len;
  312. const char *reply_text, *content_type;
  313. HTTPContext *s = h->priv_data;
  314. char message[BUFFER_SIZE];
  315. content_type = "text/plain";
  316. if (status_code < 0)
  317. body = 1;
  318. switch (status_code) {
  319. case AVERROR_HTTP_BAD_REQUEST:
  320. case 400:
  321. reply_code = 400;
  322. reply_text = "Bad Request";
  323. break;
  324. case AVERROR_HTTP_FORBIDDEN:
  325. case 403:
  326. reply_code = 403;
  327. reply_text = "Forbidden";
  328. break;
  329. case AVERROR_HTTP_NOT_FOUND:
  330. case 404:
  331. reply_code = 404;
  332. reply_text = "Not Found";
  333. break;
  334. case 200:
  335. reply_code = 200;
  336. reply_text = "OK";
  337. content_type = s->content_type ? s->content_type : "application/octet-stream";
  338. break;
  339. case AVERROR_HTTP_SERVER_ERROR:
  340. case 500:
  341. reply_code = 500;
  342. reply_text = "Internal server error";
  343. break;
  344. default:
  345. return AVERROR(EINVAL);
  346. }
  347. if (body) {
  348. s->chunked_post = 0;
  349. message_len = snprintf(message, sizeof(message),
  350. "HTTP/1.1 %03d %s\r\n"
  351. "Content-Type: %s\r\n"
  352. "Content-Length: %"SIZE_SPECIFIER"\r\n"
  353. "%s"
  354. "\r\n"
  355. "%03d %s\r\n",
  356. reply_code,
  357. reply_text,
  358. content_type,
  359. strlen(reply_text) + 6, // 3 digit status code + space + \r\n
  360. s->headers ? s->headers : "",
  361. reply_code,
  362. reply_text);
  363. } else {
  364. s->chunked_post = 1;
  365. message_len = snprintf(message, sizeof(message),
  366. "HTTP/1.1 %03d %s\r\n"
  367. "Content-Type: %s\r\n"
  368. "Transfer-Encoding: chunked\r\n"
  369. "%s"
  370. "\r\n",
  371. reply_code,
  372. reply_text,
  373. content_type,
  374. s->headers ? s->headers : "");
  375. }
  376. av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
  377. if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
  378. return ret;
  379. return 0;
  380. }
  381. static void handle_http_errors(URLContext *h, int error)
  382. {
  383. av_assert0(error < 0);
  384. http_write_reply(h, error);
  385. }
  386. static int http_handshake(URLContext *c)
  387. {
  388. int ret, err, new_location;
  389. HTTPContext *ch = c->priv_data;
  390. URLContext *cl = ch->hd;
  391. switch (ch->handshake_step) {
  392. case LOWER_PROTO:
  393. av_log(c, AV_LOG_TRACE, "Lower protocol\n");
  394. if ((ret = ffurl_handshake(cl)) > 0)
  395. return 2 + ret;
  396. if (ret < 0)
  397. return ret;
  398. ch->handshake_step = READ_HEADERS;
  399. ch->is_connected_server = 1;
  400. return 2;
  401. case READ_HEADERS:
  402. av_log(c, AV_LOG_TRACE, "Read headers\n");
  403. if ((err = http_read_header(c, &new_location)) < 0) {
  404. handle_http_errors(c, err);
  405. return err;
  406. }
  407. ch->handshake_step = WRITE_REPLY_HEADERS;
  408. return 1;
  409. case WRITE_REPLY_HEADERS:
  410. av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
  411. if ((err = http_write_reply(c, ch->reply_code)) < 0)
  412. return err;
  413. ch->handshake_step = FINISH;
  414. return 1;
  415. case FINISH:
  416. return 0;
  417. }
  418. // this should never be reached.
  419. return AVERROR(EINVAL);
  420. }
  421. static int http_listen(URLContext *h, const char *uri, int flags,
  422. AVDictionary **options) {
  423. HTTPContext *s = h->priv_data;
  424. int ret;
  425. char hostname[1024], proto[10];
  426. char lower_url[100];
  427. const char *lower_proto = "tcp";
  428. int port;
  429. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
  430. NULL, 0, uri);
  431. if (!strcmp(proto, "https"))
  432. lower_proto = "tls";
  433. ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
  434. NULL);
  435. if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
  436. goto fail;
  437. if ((ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
  438. &h->interrupt_callback, options,
  439. h->protocol_whitelist, h->protocol_blacklist, h
  440. )) < 0)
  441. goto fail;
  442. s->handshake_step = LOWER_PROTO;
  443. if (s->listen == HTTP_SINGLE) { /* single client */
  444. s->reply_code = 200;
  445. while ((ret = http_handshake(h)) > 0);
  446. }
  447. fail:
  448. av_dict_free(&s->chained_options);
  449. return ret;
  450. }
  451. static int http_open(URLContext *h, const char *uri, int flags,
  452. AVDictionary **options)
  453. {
  454. HTTPContext *s = h->priv_data;
  455. int ret;
  456. if( s->seekable == 1 )
  457. h->is_streamed = 0;
  458. else
  459. h->is_streamed = 1;
  460. s->filesize = UINT64_MAX;
  461. s->location = av_strdup(uri);
  462. if (!s->location)
  463. return AVERROR(ENOMEM);
  464. if (options)
  465. av_dict_copy(&s->chained_options, *options, 0);
  466. if (s->headers) {
  467. int len = strlen(s->headers);
  468. if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
  469. av_log(h, AV_LOG_WARNING,
  470. "No trailing CRLF found in HTTP header.\n");
  471. ret = av_reallocp(&s->headers, len + 3);
  472. if (ret < 0)
  473. return ret;
  474. s->headers[len] = '\r';
  475. s->headers[len + 1] = '\n';
  476. s->headers[len + 2] = '\0';
  477. }
  478. }
  479. if (s->listen) {
  480. return http_listen(h, uri, flags, options);
  481. }
  482. ret = http_open_cnx(h, options);
  483. if (ret < 0)
  484. av_dict_free(&s->chained_options);
  485. return ret;
  486. }
  487. static int http_accept(URLContext *s, URLContext **c)
  488. {
  489. int ret;
  490. HTTPContext *sc = s->priv_data;
  491. HTTPContext *cc;
  492. URLContext *sl = sc->hd;
  493. URLContext *cl = NULL;
  494. av_assert0(sc->listen);
  495. if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
  496. goto fail;
  497. cc = (*c)->priv_data;
  498. if ((ret = ffurl_accept(sl, &cl)) < 0)
  499. goto fail;
  500. cc->hd = cl;
  501. cc->is_multi_client = 1;
  502. fail:
  503. return ret;
  504. }
  505. static int http_getc(HTTPContext *s)
  506. {
  507. int len;
  508. if (s->buf_ptr >= s->buf_end) {
  509. len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
  510. if (len < 0) {
  511. return len;
  512. } else if (len == 0) {
  513. return AVERROR_EOF;
  514. } else {
  515. s->buf_ptr = s->buffer;
  516. s->buf_end = s->buffer + len;
  517. }
  518. }
  519. return *s->buf_ptr++;
  520. }
  521. static int http_get_line(HTTPContext *s, char *line, int line_size)
  522. {
  523. int ch;
  524. char *q;
  525. q = line;
  526. for (;;) {
  527. ch = http_getc(s);
  528. if (ch < 0)
  529. return ch;
  530. if (ch == '\n') {
  531. /* process line */
  532. if (q > line && q[-1] == '\r')
  533. q--;
  534. *q = '\0';
  535. return 0;
  536. } else {
  537. if ((q - line) < line_size - 1)
  538. *q++ = ch;
  539. }
  540. }
  541. }
  542. static int check_http_code(URLContext *h, int http_code, const char *end)
  543. {
  544. HTTPContext *s = h->priv_data;
  545. /* error codes are 4xx and 5xx, but regard 401 as a success, so we
  546. * don't abort until all headers have been parsed. */
  547. if (http_code >= 400 && http_code < 600 &&
  548. (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
  549. (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
  550. end += strspn(end, SPACE_CHARS);
  551. av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
  552. return ff_http_averror(http_code, AVERROR(EIO));
  553. }
  554. return 0;
  555. }
  556. static int parse_location(HTTPContext *s, const char *p)
  557. {
  558. char redirected_location[MAX_URL_SIZE], *new_loc;
  559. ff_make_absolute_url(redirected_location, sizeof(redirected_location),
  560. s->location, p);
  561. new_loc = av_strdup(redirected_location);
  562. if (!new_loc)
  563. return AVERROR(ENOMEM);
  564. av_free(s->location);
  565. s->location = new_loc;
  566. return 0;
  567. }
  568. /* "bytes $from-$to/$document_size" */
  569. static void parse_content_range(URLContext *h, const char *p)
  570. {
  571. HTTPContext *s = h->priv_data;
  572. const char *slash;
  573. if (!strncmp(p, "bytes ", 6)) {
  574. p += 6;
  575. s->off = strtoull(p, NULL, 10);
  576. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  577. s->filesize = strtoull(slash + 1, NULL, 10);
  578. }
  579. if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
  580. h->is_streamed = 0; /* we _can_ in fact seek */
  581. }
  582. static int parse_content_encoding(URLContext *h, const char *p)
  583. {
  584. if (!av_strncasecmp(p, "gzip", 4) ||
  585. !av_strncasecmp(p, "deflate", 7)) {
  586. #if CONFIG_ZLIB
  587. HTTPContext *s = h->priv_data;
  588. s->compressed = 1;
  589. inflateEnd(&s->inflate_stream);
  590. if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
  591. av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
  592. s->inflate_stream.msg);
  593. return AVERROR(ENOSYS);
  594. }
  595. if (zlibCompileFlags() & (1 << 17)) {
  596. av_log(h, AV_LOG_WARNING,
  597. "Your zlib was compiled without gzip support.\n");
  598. return AVERROR(ENOSYS);
  599. }
  600. #else
  601. av_log(h, AV_LOG_WARNING,
  602. "Compressed (%s) content, need zlib with gzip support\n", p);
  603. return AVERROR(ENOSYS);
  604. #endif /* CONFIG_ZLIB */
  605. } else if (!av_strncasecmp(p, "identity", 8)) {
  606. // The normal, no-encoding case (although servers shouldn't include
  607. // the header at all if this is the case).
  608. } else {
  609. av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
  610. }
  611. return 0;
  612. }
  613. // Concat all Icy- header lines
  614. static int parse_icy(HTTPContext *s, const char *tag, const char *p)
  615. {
  616. int len = 4 + strlen(p) + strlen(tag);
  617. int is_first = !s->icy_metadata_headers;
  618. int ret;
  619. av_dict_set(&s->metadata, tag, p, 0);
  620. if (s->icy_metadata_headers)
  621. len += strlen(s->icy_metadata_headers);
  622. if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
  623. return ret;
  624. if (is_first)
  625. *s->icy_metadata_headers = '\0';
  626. av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
  627. return 0;
  628. }
  629. static int parse_set_cookie_expiry_time(const char *exp_str, struct tm *buf)
  630. {
  631. char exp_buf[MAX_EXPIRY];
  632. int i, j, exp_buf_len = MAX_EXPIRY-1;
  633. char *expiry;
  634. // strip off any punctuation or whitespace
  635. for (i = 0, j = 0; exp_str[i] != '\0' && j < exp_buf_len; i++) {
  636. if ((exp_str[i] >= '0' && exp_str[i] <= '9') ||
  637. (exp_str[i] >= 'A' && exp_str[i] <= 'Z') ||
  638. (exp_str[i] >= 'a' && exp_str[i] <= 'z')) {
  639. exp_buf[j] = exp_str[i];
  640. j++;
  641. }
  642. }
  643. exp_buf[j] = '\0';
  644. expiry = exp_buf;
  645. // move the string beyond the day of week
  646. while ((*expiry < '0' || *expiry > '9') && *expiry != '\0')
  647. expiry++;
  648. return av_small_strptime(expiry, "%d%b%Y%H%M%S", buf) ? 0 : AVERROR(EINVAL);
  649. }
  650. static int parse_set_cookie(const char *set_cookie, AVDictionary **dict)
  651. {
  652. char *param, *next_param, *cstr, *back;
  653. if (!(cstr = av_strdup(set_cookie)))
  654. return AVERROR(EINVAL);
  655. // strip any trailing whitespace
  656. back = &cstr[strlen(cstr)-1];
  657. while (strchr(WHITESPACES, *back)) {
  658. *back='\0';
  659. back--;
  660. }
  661. next_param = cstr;
  662. while ((param = av_strtok(next_param, ";", &next_param))) {
  663. char *name, *value;
  664. param += strspn(param, WHITESPACES);
  665. if ((name = av_strtok(param, "=", &value))) {
  666. if (av_dict_set(dict, name, value, 0) < 0) {
  667. av_free(cstr);
  668. return -1;
  669. }
  670. }
  671. }
  672. av_free(cstr);
  673. return 0;
  674. }
  675. static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)
  676. {
  677. AVDictionary *new_params = NULL;
  678. AVDictionaryEntry *e, *cookie_entry;
  679. char *eql, *name;
  680. // ensure the cookie is parsable
  681. if (parse_set_cookie(p, &new_params))
  682. return -1;
  683. // if there is no cookie value there is nothing to parse
  684. cookie_entry = av_dict_get(new_params, "", NULL, AV_DICT_IGNORE_SUFFIX);
  685. if (!cookie_entry || !cookie_entry->value) {
  686. av_dict_free(&new_params);
  687. return -1;
  688. }
  689. // ensure the cookie is not expired or older than an existing value
  690. if ((e = av_dict_get(new_params, "expires", NULL, 0)) && e->value) {
  691. struct tm new_tm = {0};
  692. if (!parse_set_cookie_expiry_time(e->value, &new_tm)) {
  693. AVDictionaryEntry *e2;
  694. // if the cookie has already expired ignore it
  695. if (av_timegm(&new_tm) < av_gettime() / 1000000) {
  696. av_dict_free(&new_params);
  697. return -1;
  698. }
  699. // only replace an older cookie with the same name
  700. e2 = av_dict_get(*cookies, cookie_entry->key, NULL, 0);
  701. if (e2 && e2->value) {
  702. AVDictionary *old_params = NULL;
  703. if (!parse_set_cookie(p, &old_params)) {
  704. e2 = av_dict_get(old_params, "expires", NULL, 0);
  705. if (e2 && e2->value) {
  706. struct tm old_tm = {0};
  707. if (!parse_set_cookie_expiry_time(e->value, &old_tm)) {
  708. if (av_timegm(&new_tm) < av_timegm(&old_tm)) {
  709. av_dict_free(&new_params);
  710. av_dict_free(&old_params);
  711. return -1;
  712. }
  713. }
  714. }
  715. }
  716. av_dict_free(&old_params);
  717. }
  718. }
  719. }
  720. // duplicate the cookie name (dict will dupe the value)
  721. if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);
  722. if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);
  723. // add the cookie to the dictionary
  724. av_dict_set(cookies, name, eql, AV_DICT_DONT_STRDUP_KEY);
  725. return 0;
  726. }
  727. static int cookie_string(AVDictionary *dict, char **cookies)
  728. {
  729. AVDictionaryEntry *e = NULL;
  730. int len = 1;
  731. // determine how much memory is needed for the cookies string
  732. while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
  733. len += strlen(e->key) + strlen(e->value) + 1;
  734. // reallocate the cookies
  735. e = NULL;
  736. if (*cookies) av_free(*cookies);
  737. *cookies = av_malloc(len);
  738. if (!*cookies) return AVERROR(ENOMEM);
  739. *cookies[0] = '\0';
  740. // write out the cookies
  741. while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
  742. av_strlcatf(*cookies, len, "%s%s\n", e->key, e->value);
  743. return 0;
  744. }
  745. static int process_line(URLContext *h, char *line, int line_count,
  746. int *new_location)
  747. {
  748. HTTPContext *s = h->priv_data;
  749. const char *auto_method = h->flags & AVIO_FLAG_READ ? "POST" : "GET";
  750. char *tag, *p, *end, *method, *resource, *version;
  751. int ret;
  752. /* end of header */
  753. if (line[0] == '\0') {
  754. s->end_header = 1;
  755. return 0;
  756. }
  757. p = line;
  758. if (line_count == 0) {
  759. if (s->is_connected_server) {
  760. // HTTP method
  761. method = p;
  762. while (*p && !av_isspace(*p))
  763. p++;
  764. *(p++) = '\0';
  765. av_log(h, AV_LOG_TRACE, "Received method: %s\n", method);
  766. if (s->method) {
  767. if (av_strcasecmp(s->method, method)) {
  768. av_log(h, AV_LOG_ERROR, "Received and expected HTTP method do not match. (%s expected, %s received)\n",
  769. s->method, method);
  770. return ff_http_averror(400, AVERROR(EIO));
  771. }
  772. } else {
  773. // use autodetected HTTP method to expect
  774. av_log(h, AV_LOG_TRACE, "Autodetected %s HTTP method\n", auto_method);
  775. if (av_strcasecmp(auto_method, method)) {
  776. av_log(h, AV_LOG_ERROR, "Received and autodetected HTTP method did not match "
  777. "(%s autodetected %s received)\n", auto_method, method);
  778. return ff_http_averror(400, AVERROR(EIO));
  779. }
  780. if (!(s->method = av_strdup(method)))
  781. return AVERROR(ENOMEM);
  782. }
  783. // HTTP resource
  784. while (av_isspace(*p))
  785. p++;
  786. resource = p;
  787. while (!av_isspace(*p))
  788. p++;
  789. *(p++) = '\0';
  790. av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
  791. if (!(s->resource = av_strdup(resource)))
  792. return AVERROR(ENOMEM);
  793. // HTTP version
  794. while (av_isspace(*p))
  795. p++;
  796. version = p;
  797. while (*p && !av_isspace(*p))
  798. p++;
  799. *p = '\0';
  800. if (av_strncasecmp(version, "HTTP/", 5)) {
  801. av_log(h, AV_LOG_ERROR, "Malformed HTTP version string.\n");
  802. return ff_http_averror(400, AVERROR(EIO));
  803. }
  804. av_log(h, AV_LOG_TRACE, "HTTP version string: %s\n", version);
  805. } else {
  806. while (!av_isspace(*p) && *p != '\0')
  807. p++;
  808. while (av_isspace(*p))
  809. p++;
  810. s->http_code = strtol(p, &end, 10);
  811. av_log(h, AV_LOG_TRACE, "http_code=%d\n", s->http_code);
  812. if ((ret = check_http_code(h, s->http_code, end)) < 0)
  813. return ret;
  814. }
  815. } else {
  816. while (*p != '\0' && *p != ':')
  817. p++;
  818. if (*p != ':')
  819. return 1;
  820. *p = '\0';
  821. tag = line;
  822. p++;
  823. while (av_isspace(*p))
  824. p++;
  825. if (!av_strcasecmp(tag, "Location")) {
  826. if ((ret = parse_location(s, p)) < 0)
  827. return ret;
  828. *new_location = 1;
  829. } else if (!av_strcasecmp(tag, "Content-Length") &&
  830. s->filesize == UINT64_MAX) {
  831. s->filesize = strtoull(p, NULL, 10);
  832. } else if (!av_strcasecmp(tag, "Content-Range")) {
  833. parse_content_range(h, p);
  834. } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
  835. !strncmp(p, "bytes", 5) &&
  836. s->seekable == -1) {
  837. h->is_streamed = 0;
  838. } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
  839. !av_strncasecmp(p, "chunked", 7)) {
  840. s->filesize = UINT64_MAX;
  841. s->chunksize = 0;
  842. } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
  843. ff_http_auth_handle_header(&s->auth_state, tag, p);
  844. } else if (!av_strcasecmp(tag, "Authentication-Info")) {
  845. ff_http_auth_handle_header(&s->auth_state, tag, p);
  846. } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
  847. ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
  848. } else if (!av_strcasecmp(tag, "Connection")) {
  849. if (!strcmp(p, "close"))
  850. s->willclose = 1;
  851. } else if (!av_strcasecmp(tag, "Server")) {
  852. if (!av_strcasecmp(p, "AkamaiGHost")) {
  853. s->is_akamai = 1;
  854. } else if (!av_strncasecmp(p, "MediaGateway", 12)) {
  855. s->is_mediagateway = 1;
  856. }
  857. } else if (!av_strcasecmp(tag, "Content-Type")) {
  858. av_free(s->mime_type);
  859. s->mime_type = av_strdup(p);
  860. } else if (!av_strcasecmp(tag, "Set-Cookie")) {
  861. if (parse_cookie(s, p, &s->cookie_dict))
  862. av_log(h, AV_LOG_WARNING, "Unable to parse '%s'\n", p);
  863. } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
  864. s->icy_metaint = strtoull(p, NULL, 10);
  865. } else if (!av_strncasecmp(tag, "Icy-", 4)) {
  866. if ((ret = parse_icy(s, tag, p)) < 0)
  867. return ret;
  868. } else if (!av_strcasecmp(tag, "Content-Encoding")) {
  869. if ((ret = parse_content_encoding(h, p)) < 0)
  870. return ret;
  871. }
  872. }
  873. return 1;
  874. }
  875. /**
  876. * Create a string containing cookie values for use as a HTTP cookie header
  877. * field value for a particular path and domain from the cookie values stored in
  878. * the HTTP protocol context. The cookie string is stored in *cookies.
  879. *
  880. * @return a negative value if an error condition occurred, 0 otherwise
  881. */
  882. static int get_cookies(HTTPContext *s, char **cookies, const char *path,
  883. const char *domain)
  884. {
  885. // cookie strings will look like Set-Cookie header field values. Multiple
  886. // Set-Cookie fields will result in multiple values delimited by a newline
  887. int ret = 0;
  888. char *cookie, *set_cookies = av_strdup(s->cookies), *next = set_cookies;
  889. if (!set_cookies) return AVERROR(EINVAL);
  890. // destroy any cookies in the dictionary.
  891. av_dict_free(&s->cookie_dict);
  892. *cookies = NULL;
  893. while ((cookie = av_strtok(next, "\n", &next))) {
  894. AVDictionary *cookie_params = NULL;
  895. AVDictionaryEntry *cookie_entry, *e;
  896. // store the cookie in a dict in case it is updated in the response
  897. if (parse_cookie(s, cookie, &s->cookie_dict))
  898. av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);
  899. // continue on to the next cookie if this one cannot be parsed
  900. if (parse_set_cookie(cookie, &cookie_params))
  901. continue;
  902. // if the cookie has no value, skip it
  903. cookie_entry = av_dict_get(cookie_params, "", NULL, AV_DICT_IGNORE_SUFFIX);
  904. if (!cookie_entry || !cookie_entry->value) {
  905. av_dict_free(&cookie_params);
  906. continue;
  907. }
  908. // if the cookie has expired, don't add it
  909. if ((e = av_dict_get(cookie_params, "expires", NULL, 0)) && e->value) {
  910. struct tm tm_buf = {0};
  911. if (!parse_set_cookie_expiry_time(e->value, &tm_buf)) {
  912. if (av_timegm(&tm_buf) < av_gettime() / 1000000) {
  913. av_dict_free(&cookie_params);
  914. continue;
  915. }
  916. }
  917. }
  918. // if no domain in the cookie assume it appied to this request
  919. if ((e = av_dict_get(cookie_params, "domain", NULL, 0)) && e->value) {
  920. // find the offset comparison is on the min domain (b.com, not a.b.com)
  921. int domain_offset = strlen(domain) - strlen(e->value);
  922. if (domain_offset < 0) {
  923. av_dict_free(&cookie_params);
  924. continue;
  925. }
  926. // match the cookie domain
  927. if (av_strcasecmp(&domain[domain_offset], e->value)) {
  928. av_dict_free(&cookie_params);
  929. continue;
  930. }
  931. }
  932. // ensure this cookie matches the path
  933. e = av_dict_get(cookie_params, "path", NULL, 0);
  934. if (!e || av_strncasecmp(path, e->value, strlen(e->value))) {
  935. av_dict_free(&cookie_params);
  936. continue;
  937. }
  938. // cookie parameters match, so copy the value
  939. if (!*cookies) {
  940. if (!(*cookies = av_asprintf("%s=%s", cookie_entry->key, cookie_entry->value))) {
  941. ret = AVERROR(ENOMEM);
  942. break;
  943. }
  944. } else {
  945. char *tmp = *cookies;
  946. size_t str_size = strlen(cookie_entry->key) + strlen(cookie_entry->value) + strlen(*cookies) + 4;
  947. if (!(*cookies = av_malloc(str_size))) {
  948. ret = AVERROR(ENOMEM);
  949. av_free(tmp);
  950. break;
  951. }
  952. snprintf(*cookies, str_size, "%s; %s=%s", tmp, cookie_entry->key, cookie_entry->value);
  953. av_free(tmp);
  954. }
  955. }
  956. av_free(set_cookies);
  957. return ret;
  958. }
  959. static inline int has_header(const char *str, const char *header)
  960. {
  961. /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
  962. if (!str)
  963. return 0;
  964. return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
  965. }
  966. static int http_read_header(URLContext *h, int *new_location)
  967. {
  968. HTTPContext *s = h->priv_data;
  969. char line[MAX_URL_SIZE];
  970. int err = 0;
  971. s->chunksize = UINT64_MAX;
  972. for (;;) {
  973. if ((err = http_get_line(s, line, sizeof(line))) < 0)
  974. return err;
  975. av_log(h, AV_LOG_TRACE, "header='%s'\n", line);
  976. err = process_line(h, line, s->line_count, new_location);
  977. if (err < 0)
  978. return err;
  979. if (err == 0)
  980. break;
  981. s->line_count++;
  982. }
  983. if (s->seekable == -1 && s->is_mediagateway && s->filesize == 2000000000)
  984. h->is_streamed = 1; /* we can in fact _not_ seek */
  985. // add any new cookies into the existing cookie string
  986. cookie_string(s->cookie_dict, &s->cookies);
  987. av_dict_free(&s->cookie_dict);
  988. return err;
  989. }
  990. static int http_connect(URLContext *h, const char *path, const char *local_path,
  991. const char *hoststr, const char *auth,
  992. const char *proxyauth, int *new_location)
  993. {
  994. HTTPContext *s = h->priv_data;
  995. int post, err;
  996. char headers[HTTP_HEADERS_SIZE] = "";
  997. char *authstr = NULL, *proxyauthstr = NULL;
  998. uint64_t off = s->off;
  999. int len = 0;
  1000. const char *method;
  1001. int send_expect_100 = 0;
  1002. int ret;
  1003. /* send http header */
  1004. post = h->flags & AVIO_FLAG_WRITE;
  1005. if (s->post_data) {
  1006. /* force POST method and disable chunked encoding when
  1007. * custom HTTP post data is set */
  1008. post = 1;
  1009. s->chunked_post = 0;
  1010. }
  1011. if (s->method)
  1012. method = s->method;
  1013. else
  1014. method = post ? "POST" : "GET";
  1015. authstr = ff_http_auth_create_response(&s->auth_state, auth,
  1016. local_path, method);
  1017. proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
  1018. local_path, method);
  1019. if (post && !s->post_data) {
  1020. send_expect_100 = s->send_expect_100;
  1021. /* The user has supplied authentication but we don't know the auth type,
  1022. * send Expect: 100-continue to get the 401 response including the
  1023. * WWW-Authenticate header, or an 100 continue if no auth actually
  1024. * is needed. */
  1025. if (auth && *auth &&
  1026. s->auth_state.auth_type == HTTP_AUTH_NONE &&
  1027. s->http_code != 401)
  1028. send_expect_100 = 1;
  1029. }
  1030. #if FF_API_HTTP_USER_AGENT
  1031. if (strcmp(s->user_agent_deprecated, DEFAULT_USER_AGENT)) {
  1032. av_log(s, AV_LOG_WARNING, "the user-agent option is deprecated, please use user_agent option\n");
  1033. s->user_agent = av_strdup(s->user_agent_deprecated);
  1034. }
  1035. #endif
  1036. /* set default headers if needed */
  1037. if (!has_header(s->headers, "\r\nUser-Agent: "))
  1038. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1039. "User-Agent: %s\r\n", s->user_agent);
  1040. if (!has_header(s->headers, "\r\nAccept: "))
  1041. len += av_strlcpy(headers + len, "Accept: */*\r\n",
  1042. sizeof(headers) - len);
  1043. // Note: we send this on purpose even when s->off is 0 when we're probing,
  1044. // since it allows us to detect more reliably if a (non-conforming)
  1045. // server supports seeking by analysing the reply headers.
  1046. if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable == -1)) {
  1047. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1048. "Range: bytes=%"PRIu64"-", s->off);
  1049. if (s->end_off)
  1050. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1051. "%"PRId64, s->end_off - 1);
  1052. len += av_strlcpy(headers + len, "\r\n",
  1053. sizeof(headers) - len);
  1054. }
  1055. if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
  1056. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1057. "Expect: 100-continue\r\n");
  1058. if (!has_header(s->headers, "\r\nConnection: ")) {
  1059. if (s->multiple_requests)
  1060. len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
  1061. sizeof(headers) - len);
  1062. else
  1063. len += av_strlcpy(headers + len, "Connection: close\r\n",
  1064. sizeof(headers) - len);
  1065. }
  1066. if (!has_header(s->headers, "\r\nHost: "))
  1067. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1068. "Host: %s\r\n", hoststr);
  1069. if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
  1070. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1071. "Content-Length: %d\r\n", s->post_datalen);
  1072. if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
  1073. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1074. "Content-Type: %s\r\n", s->content_type);
  1075. if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
  1076. char *cookies = NULL;
  1077. if (!get_cookies(s, &cookies, path, hoststr) && cookies) {
  1078. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1079. "Cookie: %s\r\n", cookies);
  1080. av_free(cookies);
  1081. }
  1082. }
  1083. if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
  1084. len += av_strlcatf(headers + len, sizeof(headers) - len,
  1085. "Icy-MetaData: %d\r\n", 1);
  1086. /* now add in custom headers */
  1087. if (s->headers)
  1088. av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
  1089. ret = snprintf(s->buffer, sizeof(s->buffer),
  1090. "%s %s HTTP/1.1\r\n"
  1091. "%s"
  1092. "%s"
  1093. "%s"
  1094. "%s%s"
  1095. "\r\n",
  1096. method,
  1097. path,
  1098. post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
  1099. headers,
  1100. authstr ? authstr : "",
  1101. proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
  1102. av_log(h, AV_LOG_DEBUG, "request: %s\n", s->buffer);
  1103. if (strlen(headers) + 1 == sizeof(headers) ||
  1104. ret >= sizeof(s->buffer)) {
  1105. av_log(h, AV_LOG_ERROR, "overlong headers\n");
  1106. err = AVERROR(EINVAL);
  1107. goto done;
  1108. }
  1109. if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
  1110. goto done;
  1111. if (s->post_data)
  1112. if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
  1113. goto done;
  1114. /* init input buffer */
  1115. s->buf_ptr = s->buffer;
  1116. s->buf_end = s->buffer;
  1117. s->line_count = 0;
  1118. s->off = 0;
  1119. s->icy_data_read = 0;
  1120. s->filesize = UINT64_MAX;
  1121. s->willclose = 0;
  1122. s->end_chunked_post = 0;
  1123. s->end_header = 0;
  1124. #if CONFIG_ZLIB
  1125. s->compressed = 0;
  1126. #endif
  1127. if (post && !s->post_data && !send_expect_100) {
  1128. /* Pretend that it did work. We didn't read any header yet, since
  1129. * we've still to send the POST data, but the code calling this
  1130. * function will check http_code after we return. */
  1131. s->http_code = 200;
  1132. err = 0;
  1133. goto done;
  1134. }
  1135. /* wait for header */
  1136. err = http_read_header(h, new_location);
  1137. if (err < 0)
  1138. goto done;
  1139. if (*new_location)
  1140. s->off = off;
  1141. err = (off == s->off) ? 0 : -1;
  1142. done:
  1143. av_freep(&authstr);
  1144. av_freep(&proxyauthstr);
  1145. return err;
  1146. }
  1147. static int http_buf_read(URLContext *h, uint8_t *buf, int size)
  1148. {
  1149. HTTPContext *s = h->priv_data;
  1150. int len;
  1151. if (s->chunksize != UINT64_MAX) {
  1152. if (s->chunkend) {
  1153. return AVERROR_EOF;
  1154. }
  1155. if (!s->chunksize) {
  1156. char line[32];
  1157. int err;
  1158. do {
  1159. if ((err = http_get_line(s, line, sizeof(line))) < 0)
  1160. return err;
  1161. } while (!*line); /* skip CR LF from last chunk */
  1162. s->chunksize = strtoull(line, NULL, 16);
  1163. av_log(h, AV_LOG_TRACE,
  1164. "Chunked encoding data size: %"PRIu64"\n",
  1165. s->chunksize);
  1166. if (!s->chunksize && s->multiple_requests) {
  1167. http_get_line(s, line, sizeof(line)); // read empty chunk
  1168. s->chunkend = 1;
  1169. return 0;
  1170. }
  1171. else if (!s->chunksize) {
  1172. av_log(h, AV_LOG_DEBUG, "Last chunk received, closing conn\n");
  1173. ffurl_closep(&s->hd);
  1174. return 0;
  1175. }
  1176. else if (s->chunksize == UINT64_MAX) {
  1177. av_log(h, AV_LOG_ERROR, "Invalid chunk size %"PRIu64"\n",
  1178. s->chunksize);
  1179. return AVERROR(EINVAL);
  1180. }
  1181. }
  1182. size = FFMIN(size, s->chunksize);
  1183. }
  1184. /* read bytes from input buffer first */
  1185. len = s->buf_end - s->buf_ptr;
  1186. if (len > 0) {
  1187. if (len > size)
  1188. len = size;
  1189. memcpy(buf, s->buf_ptr, len);
  1190. s->buf_ptr += len;
  1191. } else {
  1192. uint64_t target_end = s->end_off ? s->end_off : s->filesize;
  1193. if ((!s->willclose || s->chunksize == UINT64_MAX) && s->off >= target_end)
  1194. return AVERROR_EOF;
  1195. len = ffurl_read(s->hd, buf, size);
  1196. if (!len && (!s->willclose || s->chunksize == UINT64_MAX) && s->off < target_end) {
  1197. av_log(h, AV_LOG_ERROR,
  1198. "Stream ends prematurely at %"PRIu64", should be %"PRIu64"\n",
  1199. s->off, target_end
  1200. );
  1201. return AVERROR(EIO);
  1202. }
  1203. }
  1204. if (len > 0) {
  1205. s->off += len;
  1206. if (s->chunksize > 0 && s->chunksize != UINT64_MAX) {
  1207. av_assert0(s->chunksize >= len);
  1208. s->chunksize -= len;
  1209. }
  1210. }
  1211. return len;
  1212. }
  1213. #if CONFIG_ZLIB
  1214. #define DECOMPRESS_BUF_SIZE (256 * 1024)
  1215. static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
  1216. {
  1217. HTTPContext *s = h->priv_data;
  1218. int ret;
  1219. if (!s->inflate_buffer) {
  1220. s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
  1221. if (!s->inflate_buffer)
  1222. return AVERROR(ENOMEM);
  1223. }
  1224. if (s->inflate_stream.avail_in == 0) {
  1225. int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
  1226. if (read <= 0)
  1227. return read;
  1228. s->inflate_stream.next_in = s->inflate_buffer;
  1229. s->inflate_stream.avail_in = read;
  1230. }
  1231. s->inflate_stream.avail_out = size;
  1232. s->inflate_stream.next_out = buf;
  1233. ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
  1234. if (ret != Z_OK && ret != Z_STREAM_END)
  1235. av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
  1236. ret, s->inflate_stream.msg);
  1237. return size - s->inflate_stream.avail_out;
  1238. }
  1239. #endif /* CONFIG_ZLIB */
  1240. static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect);
  1241. static int http_read_stream(URLContext *h, uint8_t *buf, int size)
  1242. {
  1243. HTTPContext *s = h->priv_data;
  1244. int err, new_location, read_ret;
  1245. int64_t seek_ret;
  1246. if (!s->hd)
  1247. return AVERROR_EOF;
  1248. if (s->end_chunked_post && !s->end_header) {
  1249. err = http_read_header(h, &new_location);
  1250. if (err < 0)
  1251. return err;
  1252. }
  1253. #if CONFIG_ZLIB
  1254. if (s->compressed)
  1255. return http_buf_read_compressed(h, buf, size);
  1256. #endif /* CONFIG_ZLIB */
  1257. read_ret = http_buf_read(h, buf, size);
  1258. if ( (read_ret < 0 && s->reconnect && (!h->is_streamed || s->reconnect_streamed) && s->filesize > 0 && s->off < s->filesize)
  1259. || (read_ret == 0 && s->reconnect_at_eof && (!h->is_streamed || s->reconnect_streamed))) {
  1260. uint64_t target = h->is_streamed ? 0 : s->off;
  1261. if (s->reconnect_delay > s->reconnect_delay_max)
  1262. return AVERROR(EIO);
  1263. av_log(h, AV_LOG_INFO, "Will reconnect at %"PRIu64" error=%s.\n", s->off, av_err2str(read_ret));
  1264. av_usleep(1000U*1000*s->reconnect_delay);
  1265. s->reconnect_delay = 1 + 2*s->reconnect_delay;
  1266. seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
  1267. if (seek_ret != target) {
  1268. av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRIu64".\n", target);
  1269. return read_ret;
  1270. }
  1271. read_ret = http_buf_read(h, buf, size);
  1272. } else
  1273. s->reconnect_delay = 0;
  1274. return read_ret;
  1275. }
  1276. // Like http_read_stream(), but no short reads.
  1277. // Assumes partial reads are an error.
  1278. static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
  1279. {
  1280. int pos = 0;
  1281. while (pos < size) {
  1282. int len = http_read_stream(h, buf + pos, size - pos);
  1283. if (len < 0)
  1284. return len;
  1285. pos += len;
  1286. }
  1287. return pos;
  1288. }
  1289. static void update_metadata(HTTPContext *s, char *data)
  1290. {
  1291. char *key;
  1292. char *val;
  1293. char *end;
  1294. char *next = data;
  1295. while (*next) {
  1296. key = next;
  1297. val = strstr(key, "='");
  1298. if (!val)
  1299. break;
  1300. end = strstr(val, "';");
  1301. if (!end)
  1302. break;
  1303. *val = '\0';
  1304. *end = '\0';
  1305. val += 2;
  1306. av_dict_set(&s->metadata, key, val, 0);
  1307. next = end + 2;
  1308. }
  1309. }
  1310. static int store_icy(URLContext *h, int size)
  1311. {
  1312. HTTPContext *s = h->priv_data;
  1313. /* until next metadata packet */
  1314. uint64_t remaining;
  1315. if (s->icy_metaint < s->icy_data_read)
  1316. return AVERROR_INVALIDDATA;
  1317. remaining = s->icy_metaint - s->icy_data_read;
  1318. if (!remaining) {
  1319. /* The metadata packet is variable sized. It has a 1 byte header
  1320. * which sets the length of the packet (divided by 16). If it's 0,
  1321. * the metadata doesn't change. After the packet, icy_metaint bytes
  1322. * of normal data follows. */
  1323. uint8_t ch;
  1324. int len = http_read_stream_all(h, &ch, 1);
  1325. if (len < 0)
  1326. return len;
  1327. if (ch > 0) {
  1328. char data[255 * 16 + 1];
  1329. int ret;
  1330. len = ch * 16;
  1331. ret = http_read_stream_all(h, data, len);
  1332. if (ret < 0)
  1333. return ret;
  1334. data[len + 1] = 0;
  1335. if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
  1336. return ret;
  1337. update_metadata(s, data);
  1338. }
  1339. s->icy_data_read = 0;
  1340. remaining = s->icy_metaint;
  1341. }
  1342. return FFMIN(size, remaining);
  1343. }
  1344. static int http_read(URLContext *h, uint8_t *buf, int size)
  1345. {
  1346. HTTPContext *s = h->priv_data;
  1347. if (s->icy_metaint > 0) {
  1348. size = store_icy(h, size);
  1349. if (size < 0)
  1350. return size;
  1351. }
  1352. size = http_read_stream(h, buf, size);
  1353. if (size > 0)
  1354. s->icy_data_read += size;
  1355. return size;
  1356. }
  1357. /* used only when posting data */
  1358. static int http_write(URLContext *h, const uint8_t *buf, int size)
  1359. {
  1360. char temp[11] = ""; /* 32-bit hex + CRLF + nul */
  1361. int ret;
  1362. char crlf[] = "\r\n";
  1363. HTTPContext *s = h->priv_data;
  1364. if (!s->chunked_post) {
  1365. /* non-chunked data is sent without any special encoding */
  1366. return ffurl_write(s->hd, buf, size);
  1367. }
  1368. /* silently ignore zero-size data since chunk encoding that would
  1369. * signal EOF */
  1370. if (size > 0) {
  1371. /* upload data using chunked encoding */
  1372. snprintf(temp, sizeof(temp), "%x\r\n", size);
  1373. if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
  1374. (ret = ffurl_write(s->hd, buf, size)) < 0 ||
  1375. (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
  1376. return ret;
  1377. }
  1378. return size;
  1379. }
  1380. static int http_shutdown(URLContext *h, int flags)
  1381. {
  1382. int ret = 0;
  1383. char footer[] = "0\r\n\r\n";
  1384. HTTPContext *s = h->priv_data;
  1385. /* signal end of chunked encoding if used */
  1386. if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
  1387. ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
  1388. ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
  1389. ret = ret > 0 ? 0 : ret;
  1390. s->end_chunked_post = 1;
  1391. }
  1392. return ret;
  1393. }
  1394. static int http_close(URLContext *h)
  1395. {
  1396. int ret = 0;
  1397. HTTPContext *s = h->priv_data;
  1398. #if CONFIG_ZLIB
  1399. inflateEnd(&s->inflate_stream);
  1400. av_freep(&s->inflate_buffer);
  1401. #endif /* CONFIG_ZLIB */
  1402. if (!s->end_chunked_post)
  1403. /* Close the write direction by sending the end of chunked encoding. */
  1404. ret = http_shutdown(h, h->flags);
  1405. if (s->hd)
  1406. ffurl_closep(&s->hd);
  1407. av_dict_free(&s->chained_options);
  1408. return ret;
  1409. }
  1410. static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
  1411. {
  1412. HTTPContext *s = h->priv_data;
  1413. URLContext *old_hd = s->hd;
  1414. uint64_t old_off = s->off;
  1415. uint8_t old_buf[BUFFER_SIZE];
  1416. int old_buf_size, ret;
  1417. AVDictionary *options = NULL;
  1418. if (whence == AVSEEK_SIZE)
  1419. return s->filesize;
  1420. else if (!force_reconnect &&
  1421. ((whence == SEEK_CUR && off == 0) ||
  1422. (whence == SEEK_SET && off == s->off)))
  1423. return s->off;
  1424. else if ((s->filesize == UINT64_MAX && whence == SEEK_END))
  1425. return AVERROR(ENOSYS);
  1426. if (whence == SEEK_CUR)
  1427. off += s->off;
  1428. else if (whence == SEEK_END)
  1429. off += s->filesize;
  1430. else if (whence != SEEK_SET)
  1431. return AVERROR(EINVAL);
  1432. if (off < 0)
  1433. return AVERROR(EINVAL);
  1434. s->off = off;
  1435. if (s->off && h->is_streamed)
  1436. return AVERROR(ENOSYS);
  1437. /* we save the old context in case the seek fails */
  1438. old_buf_size = s->buf_end - s->buf_ptr;
  1439. memcpy(old_buf, s->buf_ptr, old_buf_size);
  1440. s->hd = NULL;
  1441. /* if it fails, continue on old connection */
  1442. if ((ret = http_open_cnx(h, &options)) < 0) {
  1443. av_dict_free(&options);
  1444. memcpy(s->buffer, old_buf, old_buf_size);
  1445. s->buf_ptr = s->buffer;
  1446. s->buf_end = s->buffer + old_buf_size;
  1447. s->hd = old_hd;
  1448. s->off = old_off;
  1449. return ret;
  1450. }
  1451. av_dict_free(&options);
  1452. ffurl_close(old_hd);
  1453. return off;
  1454. }
  1455. static int64_t http_seek(URLContext *h, int64_t off, int whence)
  1456. {
  1457. return http_seek_internal(h, off, whence, 0);
  1458. }
  1459. static int http_get_file_handle(URLContext *h)
  1460. {
  1461. HTTPContext *s = h->priv_data;
  1462. return ffurl_get_file_handle(s->hd);
  1463. }
  1464. static int http_get_short_seek(URLContext *h)
  1465. {
  1466. HTTPContext *s = h->priv_data;
  1467. return ffurl_get_short_seek(s->hd);
  1468. }
  1469. #define HTTP_CLASS(flavor) \
  1470. static const AVClass flavor ## _context_class = { \
  1471. .class_name = # flavor, \
  1472. .item_name = av_default_item_name, \
  1473. .option = options, \
  1474. .version = LIBAVUTIL_VERSION_INT, \
  1475. }
  1476. #if CONFIG_HTTP_PROTOCOL
  1477. HTTP_CLASS(http);
  1478. const URLProtocol ff_http_protocol = {
  1479. .name = "http",
  1480. .url_open2 = http_open,
  1481. .url_accept = http_accept,
  1482. .url_handshake = http_handshake,
  1483. .url_read = http_read,
  1484. .url_write = http_write,
  1485. .url_seek = http_seek,
  1486. .url_close = http_close,
  1487. .url_get_file_handle = http_get_file_handle,
  1488. .url_get_short_seek = http_get_short_seek,
  1489. .url_shutdown = http_shutdown,
  1490. .priv_data_size = sizeof(HTTPContext),
  1491. .priv_data_class = &http_context_class,
  1492. .flags = URL_PROTOCOL_FLAG_NETWORK,
  1493. .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy"
  1494. };
  1495. #endif /* CONFIG_HTTP_PROTOCOL */
  1496. #if CONFIG_HTTPS_PROTOCOL
  1497. HTTP_CLASS(https);
  1498. const URLProtocol ff_https_protocol = {
  1499. .name = "https",
  1500. .url_open2 = http_open,
  1501. .url_read = http_read,
  1502. .url_write = http_write,
  1503. .url_seek = http_seek,
  1504. .url_close = http_close,
  1505. .url_get_file_handle = http_get_file_handle,
  1506. .url_get_short_seek = http_get_short_seek,
  1507. .url_shutdown = http_shutdown,
  1508. .priv_data_size = sizeof(HTTPContext),
  1509. .priv_data_class = &https_context_class,
  1510. .flags = URL_PROTOCOL_FLAG_NETWORK,
  1511. .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy"
  1512. };
  1513. #endif /* CONFIG_HTTPS_PROTOCOL */
  1514. #if CONFIG_HTTPPROXY_PROTOCOL
  1515. static int http_proxy_close(URLContext *h)
  1516. {
  1517. HTTPContext *s = h->priv_data;
  1518. if (s->hd)
  1519. ffurl_closep(&s->hd);
  1520. return 0;
  1521. }
  1522. static int http_proxy_open(URLContext *h, const char *uri, int flags)
  1523. {
  1524. HTTPContext *s = h->priv_data;
  1525. char hostname[1024], hoststr[1024];
  1526. char auth[1024], pathbuf[1024], *path;
  1527. char lower_url[100];
  1528. int port, ret = 0, attempts = 0;
  1529. HTTPAuthType cur_auth_type;
  1530. char *authstr;
  1531. int new_loc;
  1532. if( s->seekable == 1 )
  1533. h->is_streamed = 0;
  1534. else
  1535. h->is_streamed = 1;
  1536. av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  1537. pathbuf, sizeof(pathbuf), uri);
  1538. ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  1539. path = pathbuf;
  1540. if (*path == '/')
  1541. path++;
  1542. ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
  1543. NULL);
  1544. redo:
  1545. ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
  1546. &h->interrupt_callback, NULL,
  1547. h->protocol_whitelist, h->protocol_blacklist, h);
  1548. if (ret < 0)
  1549. return ret;
  1550. authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
  1551. path, "CONNECT");
  1552. snprintf(s->buffer, sizeof(s->buffer),
  1553. "CONNECT %s HTTP/1.1\r\n"
  1554. "Host: %s\r\n"
  1555. "Connection: close\r\n"
  1556. "%s%s"
  1557. "\r\n",
  1558. path,
  1559. hoststr,
  1560. authstr ? "Proxy-" : "", authstr ? authstr : "");
  1561. av_freep(&authstr);
  1562. if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
  1563. goto fail;
  1564. s->buf_ptr = s->buffer;
  1565. s->buf_end = s->buffer;
  1566. s->line_count = 0;
  1567. s->filesize = UINT64_MAX;
  1568. cur_auth_type = s->proxy_auth_state.auth_type;
  1569. /* Note: This uses buffering, potentially reading more than the
  1570. * HTTP header. If tunneling a protocol where the server starts
  1571. * the conversation, we might buffer part of that here, too.
  1572. * Reading that requires using the proper ffurl_read() function
  1573. * on this URLContext, not using the fd directly (as the tls
  1574. * protocol does). This shouldn't be an issue for tls though,
  1575. * since the client starts the conversation there, so there
  1576. * is no extra data that we might buffer up here.
  1577. */
  1578. ret = http_read_header(h, &new_loc);
  1579. if (ret < 0)
  1580. goto fail;
  1581. attempts++;
  1582. if (s->http_code == 407 &&
  1583. (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
  1584. s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
  1585. ffurl_closep(&s->hd);
  1586. goto redo;
  1587. }
  1588. if (s->http_code < 400)
  1589. return 0;
  1590. ret = ff_http_averror(s->http_code, AVERROR(EIO));
  1591. fail:
  1592. http_proxy_close(h);
  1593. return ret;
  1594. }
  1595. static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
  1596. {
  1597. HTTPContext *s = h->priv_data;
  1598. return ffurl_write(s->hd, buf, size);
  1599. }
  1600. const URLProtocol ff_httpproxy_protocol = {
  1601. .name = "httpproxy",
  1602. .url_open = http_proxy_open,
  1603. .url_read = http_buf_read,
  1604. .url_write = http_proxy_write,
  1605. .url_close = http_proxy_close,
  1606. .url_get_file_handle = http_get_file_handle,
  1607. .priv_data_size = sizeof(HTTPContext),
  1608. .flags = URL_PROTOCOL_FLAG_NETWORK,
  1609. };
  1610. #endif /* CONFIG_HTTPPROXY_PROTOCOL */