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.

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