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.

1265 lines
42KB

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