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.

696 lines
21KB

  1. /*
  2. * HTTP protocol for avconv client
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "avformat.h"
  23. #include <unistd.h>
  24. #include "internal.h"
  25. #include "network.h"
  26. #include "http.h"
  27. #include "os_support.h"
  28. #include "httpauth.h"
  29. #include "url.h"
  30. #include "libavutil/opt.h"
  31. /* XXX: POST protocol is not completely implemented because avconv uses
  32. only a subset of it. */
  33. /* used for protocol handling */
  34. #define BUFFER_SIZE 1024
  35. #define MAX_REDIRECTS 8
  36. typedef struct {
  37. const AVClass *class;
  38. URLContext *hd;
  39. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  40. int line_count;
  41. int http_code;
  42. int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
  43. int64_t off, filesize;
  44. char location[MAX_URL_SIZE];
  45. HTTPAuthState auth_state;
  46. HTTPAuthState proxy_auth_state;
  47. char *headers;
  48. int willclose; /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
  49. int chunked_post;
  50. } HTTPContext;
  51. #define OFFSET(x) offsetof(HTTPContext, x)
  52. #define D AV_OPT_FLAG_DECODING_PARAM
  53. #define E AV_OPT_FLAG_ENCODING_PARAM
  54. static const AVOption options[] = {
  55. {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1, E },
  56. {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
  57. {NULL}
  58. };
  59. #define HTTP_CLASS(flavor)\
  60. static const AVClass flavor ## _context_class = {\
  61. .class_name = #flavor,\
  62. .item_name = av_default_item_name,\
  63. .option = options,\
  64. .version = LIBAVUTIL_VERSION_INT,\
  65. };
  66. HTTP_CLASS(http);
  67. HTTP_CLASS(https);
  68. static int http_connect(URLContext *h, const char *path, const char *local_path,
  69. const char *hoststr, const char *auth,
  70. const char *proxyauth, int *new_location);
  71. void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
  72. {
  73. memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
  74. &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
  75. memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state,
  76. &((HTTPContext*)src->priv_data)->proxy_auth_state,
  77. sizeof(HTTPAuthState));
  78. }
  79. /* return non zero if error */
  80. static int http_open_cnx(URLContext *h)
  81. {
  82. const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
  83. char hostname[1024], hoststr[1024], proto[10];
  84. char auth[1024], proxyauth[1024];
  85. char path1[1024];
  86. char buf[1024], urlbuf[1024];
  87. int port, use_proxy, err, location_changed = 0, redirects = 0;
  88. HTTPAuthType cur_auth_type, cur_proxy_auth_type;
  89. HTTPContext *s = h->priv_data;
  90. URLContext *hd = NULL;
  91. proxy_path = getenv("http_proxy");
  92. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  93. av_strstart(proxy_path, "http://", NULL);
  94. /* fill the dest addr */
  95. redo:
  96. /* needed in any case to build the host string */
  97. av_url_split(proto, sizeof(proto), auth, sizeof(auth),
  98. hostname, sizeof(hostname), &port,
  99. path1, sizeof(path1), s->location);
  100. ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  101. if (path1[0] == '\0')
  102. path = "/";
  103. else
  104. path = path1;
  105. local_path = path;
  106. if (use_proxy) {
  107. /* Reassemble the request URL without auth string - we don't
  108. * want to leak the auth to the proxy. */
  109. ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
  110. path1);
  111. path = urlbuf;
  112. av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
  113. hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
  114. }
  115. if (!strcmp(proto, "https")) {
  116. lower_proto = "tls";
  117. if (port < 0)
  118. port = 443;
  119. }
  120. if (port < 0)
  121. port = 80;
  122. ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
  123. err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE,
  124. &h->interrupt_callback, NULL);
  125. if (err < 0)
  126. goto fail;
  127. s->hd = hd;
  128. cur_auth_type = s->auth_state.auth_type;
  129. cur_proxy_auth_type = s->auth_state.auth_type;
  130. if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
  131. goto fail;
  132. if (s->http_code == 401) {
  133. if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
  134. ffurl_close(hd);
  135. goto redo;
  136. } else
  137. goto fail;
  138. }
  139. if (s->http_code == 407) {
  140. if (cur_proxy_auth_type == HTTP_AUTH_NONE &&
  141. s->proxy_auth_state.auth_type != HTTP_AUTH_NONE) {
  142. ffurl_close(hd);
  143. goto redo;
  144. } else
  145. goto fail;
  146. }
  147. if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
  148. && location_changed == 1) {
  149. /* url moved, get next */
  150. ffurl_close(hd);
  151. if (redirects++ >= MAX_REDIRECTS)
  152. return AVERROR(EIO);
  153. location_changed = 0;
  154. goto redo;
  155. }
  156. return 0;
  157. fail:
  158. if (hd)
  159. ffurl_close(hd);
  160. s->hd = NULL;
  161. return AVERROR(EIO);
  162. }
  163. static int http_open(URLContext *h, const char *uri, int flags)
  164. {
  165. HTTPContext *s = h->priv_data;
  166. h->is_streamed = 1;
  167. s->filesize = -1;
  168. av_strlcpy(s->location, uri, sizeof(s->location));
  169. if (s->headers) {
  170. int len = strlen(s->headers);
  171. if (len < 2 || strcmp("\r\n", s->headers + len - 2))
  172. av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
  173. }
  174. return http_open_cnx(h);
  175. }
  176. static int http_getc(HTTPContext *s)
  177. {
  178. int len;
  179. if (s->buf_ptr >= s->buf_end) {
  180. len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
  181. if (len < 0) {
  182. return AVERROR(EIO);
  183. } else if (len == 0) {
  184. return -1;
  185. } else {
  186. s->buf_ptr = s->buffer;
  187. s->buf_end = s->buffer + len;
  188. }
  189. }
  190. return *s->buf_ptr++;
  191. }
  192. static int http_get_line(HTTPContext *s, char *line, int line_size)
  193. {
  194. int ch;
  195. char *q;
  196. q = line;
  197. for(;;) {
  198. ch = http_getc(s);
  199. if (ch < 0)
  200. return AVERROR(EIO);
  201. if (ch == '\n') {
  202. /* process line */
  203. if (q > line && q[-1] == '\r')
  204. q--;
  205. *q = '\0';
  206. return 0;
  207. } else {
  208. if ((q - line) < line_size - 1)
  209. *q++ = ch;
  210. }
  211. }
  212. }
  213. static int process_line(URLContext *h, char *line, int line_count,
  214. int *new_location)
  215. {
  216. HTTPContext *s = h->priv_data;
  217. char *tag, *p, *end;
  218. /* end of header */
  219. if (line[0] == '\0')
  220. return 0;
  221. p = line;
  222. if (line_count == 0) {
  223. while (!isspace(*p) && *p != '\0')
  224. p++;
  225. while (isspace(*p))
  226. p++;
  227. s->http_code = strtol(p, &end, 10);
  228. av_dlog(NULL, "http_code=%d\n", s->http_code);
  229. /* error codes are 4xx and 5xx, but regard 401 as a success, so we
  230. * don't abort until all headers have been parsed. */
  231. if (s->http_code >= 400 && s->http_code < 600 && (s->http_code != 401
  232. || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
  233. (s->http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
  234. end += strspn(end, SPACE_CHARS);
  235. av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
  236. s->http_code, end);
  237. return -1;
  238. }
  239. } else {
  240. while (*p != '\0' && *p != ':')
  241. p++;
  242. if (*p != ':')
  243. return 1;
  244. *p = '\0';
  245. tag = line;
  246. p++;
  247. while (isspace(*p))
  248. p++;
  249. if (!av_strcasecmp(tag, "Location")) {
  250. strcpy(s->location, p);
  251. *new_location = 1;
  252. } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
  253. s->filesize = atoll(p);
  254. } else if (!av_strcasecmp (tag, "Content-Range")) {
  255. /* "bytes $from-$to/$document_size" */
  256. const char *slash;
  257. if (!strncmp (p, "bytes ", 6)) {
  258. p += 6;
  259. s->off = atoll(p);
  260. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  261. s->filesize = atoll(slash+1);
  262. }
  263. h->is_streamed = 0; /* we _can_ in fact seek */
  264. } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5)) {
  265. h->is_streamed = 0;
  266. } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
  267. s->filesize = -1;
  268. s->chunksize = 0;
  269. } else if (!av_strcasecmp (tag, "WWW-Authenticate")) {
  270. ff_http_auth_handle_header(&s->auth_state, tag, p);
  271. } else if (!av_strcasecmp (tag, "Authentication-Info")) {
  272. ff_http_auth_handle_header(&s->auth_state, tag, p);
  273. } else if (!av_strcasecmp (tag, "Proxy-Authenticate")) {
  274. ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
  275. } else if (!av_strcasecmp (tag, "Connection")) {
  276. if (!strcmp(p, "close"))
  277. s->willclose = 1;
  278. }
  279. }
  280. return 1;
  281. }
  282. static inline int has_header(const char *str, const char *header)
  283. {
  284. /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
  285. if (!str)
  286. return 0;
  287. return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
  288. }
  289. static int http_connect(URLContext *h, const char *path, const char *local_path,
  290. const char *hoststr, const char *auth,
  291. const char *proxyauth, int *new_location)
  292. {
  293. HTTPContext *s = h->priv_data;
  294. int post, err;
  295. char line[1024];
  296. char headers[1024] = "";
  297. char *authstr = NULL, *proxyauthstr = NULL;
  298. int64_t off = s->off;
  299. int len = 0;
  300. const char *method;
  301. /* send http header */
  302. post = h->flags & AVIO_FLAG_WRITE;
  303. method = post ? "POST" : "GET";
  304. authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
  305. method);
  306. proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
  307. local_path, method);
  308. /* set default headers if needed */
  309. if (!has_header(s->headers, "\r\nUser-Agent: "))
  310. len += av_strlcatf(headers + len, sizeof(headers) - len,
  311. "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
  312. if (!has_header(s->headers, "\r\nAccept: "))
  313. len += av_strlcpy(headers + len, "Accept: */*\r\n",
  314. sizeof(headers) - len);
  315. if (!has_header(s->headers, "\r\nRange: ") && !post)
  316. len += av_strlcatf(headers + len, sizeof(headers) - len,
  317. "Range: bytes=%"PRId64"-\r\n", s->off);
  318. if (!has_header(s->headers, "\r\nConnection: "))
  319. len += av_strlcpy(headers + len, "Connection: close\r\n",
  320. sizeof(headers)-len);
  321. if (!has_header(s->headers, "\r\nHost: "))
  322. len += av_strlcatf(headers + len, sizeof(headers) - len,
  323. "Host: %s\r\n", hoststr);
  324. /* now add in custom headers */
  325. if (s->headers)
  326. av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
  327. snprintf(s->buffer, sizeof(s->buffer),
  328. "%s %s HTTP/1.1\r\n"
  329. "%s"
  330. "%s"
  331. "%s"
  332. "%s%s"
  333. "\r\n",
  334. method,
  335. path,
  336. post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
  337. headers,
  338. authstr ? authstr : "",
  339. proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
  340. av_freep(&authstr);
  341. av_freep(&proxyauthstr);
  342. if (ffurl_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
  343. return AVERROR(EIO);
  344. /* init input buffer */
  345. s->buf_ptr = s->buffer;
  346. s->buf_end = s->buffer;
  347. s->line_count = 0;
  348. s->off = 0;
  349. s->filesize = -1;
  350. s->willclose = 0;
  351. if (post) {
  352. /* Pretend that it did work. We didn't read any header yet, since
  353. * we've still to send the POST data, but the code calling this
  354. * function will check http_code after we return. */
  355. s->http_code = 200;
  356. return 0;
  357. }
  358. s->chunksize = -1;
  359. /* wait for header */
  360. for(;;) {
  361. if (http_get_line(s, line, sizeof(line)) < 0)
  362. return AVERROR(EIO);
  363. av_dlog(NULL, "header='%s'\n", line);
  364. err = process_line(h, line, s->line_count, new_location);
  365. if (err < 0)
  366. return err;
  367. if (err == 0)
  368. break;
  369. s->line_count++;
  370. }
  371. return (off == s->off) ? 0 : -1;
  372. }
  373. static int http_buf_read(URLContext *h, uint8_t *buf, int size)
  374. {
  375. HTTPContext *s = h->priv_data;
  376. int len;
  377. /* read bytes from input buffer first */
  378. len = s->buf_end - s->buf_ptr;
  379. if (len > 0) {
  380. if (len > size)
  381. len = size;
  382. memcpy(buf, s->buf_ptr, len);
  383. s->buf_ptr += len;
  384. } else {
  385. if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
  386. return AVERROR_EOF;
  387. len = ffurl_read(s->hd, buf, size);
  388. }
  389. if (len > 0) {
  390. s->off += len;
  391. if (s->chunksize > 0)
  392. s->chunksize -= len;
  393. }
  394. return len;
  395. }
  396. static int http_read(URLContext *h, uint8_t *buf, int size)
  397. {
  398. HTTPContext *s = h->priv_data;
  399. if (s->chunksize >= 0) {
  400. if (!s->chunksize) {
  401. char line[32];
  402. for(;;) {
  403. do {
  404. if (http_get_line(s, line, sizeof(line)) < 0)
  405. return AVERROR(EIO);
  406. } while (!*line); /* skip CR LF from last chunk */
  407. s->chunksize = strtoll(line, NULL, 16);
  408. av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
  409. if (!s->chunksize)
  410. return 0;
  411. break;
  412. }
  413. }
  414. size = FFMIN(size, s->chunksize);
  415. }
  416. return http_buf_read(h, buf, size);
  417. }
  418. /* used only when posting data */
  419. static int http_write(URLContext *h, const uint8_t *buf, int size)
  420. {
  421. char temp[11] = ""; /* 32-bit hex + CRLF + nul */
  422. int ret;
  423. char crlf[] = "\r\n";
  424. HTTPContext *s = h->priv_data;
  425. if (!s->chunked_post) {
  426. /* non-chunked data is sent without any special encoding */
  427. return ffurl_write(s->hd, buf, size);
  428. }
  429. /* silently ignore zero-size data since chunk encoding that would
  430. * signal EOF */
  431. if (size > 0) {
  432. /* upload data using chunked encoding */
  433. snprintf(temp, sizeof(temp), "%x\r\n", size);
  434. if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
  435. (ret = ffurl_write(s->hd, buf, size)) < 0 ||
  436. (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
  437. return ret;
  438. }
  439. return size;
  440. }
  441. static int http_close(URLContext *h)
  442. {
  443. int ret = 0;
  444. char footer[] = "0\r\n\r\n";
  445. HTTPContext *s = h->priv_data;
  446. /* signal end of chunked encoding if used */
  447. if ((h->flags & AVIO_FLAG_WRITE) && s->chunked_post) {
  448. ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
  449. ret = ret > 0 ? 0 : ret;
  450. }
  451. if (s->hd)
  452. ffurl_close(s->hd);
  453. return ret;
  454. }
  455. static int64_t http_seek(URLContext *h, int64_t off, int whence)
  456. {
  457. HTTPContext *s = h->priv_data;
  458. URLContext *old_hd = s->hd;
  459. int64_t old_off = s->off;
  460. uint8_t old_buf[BUFFER_SIZE];
  461. int old_buf_size;
  462. if (whence == AVSEEK_SIZE)
  463. return s->filesize;
  464. else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
  465. return -1;
  466. /* we save the old context in case the seek fails */
  467. old_buf_size = s->buf_end - s->buf_ptr;
  468. memcpy(old_buf, s->buf_ptr, old_buf_size);
  469. s->hd = NULL;
  470. if (whence == SEEK_CUR)
  471. off += s->off;
  472. else if (whence == SEEK_END)
  473. off += s->filesize;
  474. s->off = off;
  475. /* if it fails, continue on old connection */
  476. if (http_open_cnx(h) < 0) {
  477. memcpy(s->buffer, old_buf, old_buf_size);
  478. s->buf_ptr = s->buffer;
  479. s->buf_end = s->buffer + old_buf_size;
  480. s->hd = old_hd;
  481. s->off = old_off;
  482. return -1;
  483. }
  484. ffurl_close(old_hd);
  485. return off;
  486. }
  487. static int
  488. http_get_file_handle(URLContext *h)
  489. {
  490. HTTPContext *s = h->priv_data;
  491. return ffurl_get_file_handle(s->hd);
  492. }
  493. #if CONFIG_HTTP_PROTOCOL
  494. URLProtocol ff_http_protocol = {
  495. .name = "http",
  496. .url_open = http_open,
  497. .url_read = http_read,
  498. .url_write = http_write,
  499. .url_seek = http_seek,
  500. .url_close = http_close,
  501. .url_get_file_handle = http_get_file_handle,
  502. .priv_data_size = sizeof(HTTPContext),
  503. .priv_data_class = &http_context_class,
  504. };
  505. #endif
  506. #if CONFIG_HTTPS_PROTOCOL
  507. URLProtocol ff_https_protocol = {
  508. .name = "https",
  509. .url_open = http_open,
  510. .url_read = http_read,
  511. .url_write = http_write,
  512. .url_seek = http_seek,
  513. .url_close = http_close,
  514. .url_get_file_handle = http_get_file_handle,
  515. .priv_data_size = sizeof(HTTPContext),
  516. .priv_data_class = &https_context_class,
  517. };
  518. #endif
  519. #if CONFIG_HTTPPROXY_PROTOCOL
  520. static int http_proxy_close(URLContext *h)
  521. {
  522. HTTPContext *s = h->priv_data;
  523. if (s->hd)
  524. ffurl_close(s->hd);
  525. return 0;
  526. }
  527. static int http_proxy_open(URLContext *h, const char *uri, int flags)
  528. {
  529. HTTPContext *s = h->priv_data;
  530. char hostname[1024], hoststr[1024];
  531. char auth[1024], pathbuf[1024], *path;
  532. char line[1024], lower_url[100];
  533. int port, ret = 0;
  534. HTTPAuthType cur_auth_type;
  535. char *authstr;
  536. h->is_streamed = 1;
  537. av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  538. pathbuf, sizeof(pathbuf), uri);
  539. ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  540. path = pathbuf;
  541. if (*path == '/')
  542. path++;
  543. ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
  544. NULL);
  545. redo:
  546. ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
  547. &h->interrupt_callback, NULL);
  548. if (ret < 0)
  549. return ret;
  550. authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
  551. path, "CONNECT");
  552. snprintf(s->buffer, sizeof(s->buffer),
  553. "CONNECT %s HTTP/1.1\r\n"
  554. "Host: %s\r\n"
  555. "Connection: close\r\n"
  556. "%s%s"
  557. "\r\n",
  558. path,
  559. hoststr,
  560. authstr ? "Proxy-" : "", authstr ? authstr : "");
  561. av_freep(&authstr);
  562. if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
  563. goto fail;
  564. s->buf_ptr = s->buffer;
  565. s->buf_end = s->buffer;
  566. s->line_count = 0;
  567. s->filesize = -1;
  568. cur_auth_type = s->proxy_auth_state.auth_type;
  569. for (;;) {
  570. int new_loc;
  571. // Note: This uses buffering, potentially reading more than the
  572. // HTTP header. If tunneling a protocol where the server starts
  573. // the conversation, we might buffer part of that here, too.
  574. // Reading that requires using the proper ffurl_read() function
  575. // on this URLContext, not using the fd directly (as the tls
  576. // protocol does). This shouldn't be an issue for tls though,
  577. // since the client starts the conversation there, so there
  578. // is no extra data that we might buffer up here.
  579. if (http_get_line(s, line, sizeof(line)) < 0) {
  580. ret = AVERROR(EIO);
  581. goto fail;
  582. }
  583. av_dlog(h, "header='%s'\n", line);
  584. ret = process_line(h, line, s->line_count, &new_loc);
  585. if (ret < 0)
  586. goto fail;
  587. if (ret == 0)
  588. break;
  589. s->line_count++;
  590. }
  591. if (s->http_code == 407 && cur_auth_type == HTTP_AUTH_NONE &&
  592. s->proxy_auth_state.auth_type != HTTP_AUTH_NONE) {
  593. ffurl_close(s->hd);
  594. s->hd = NULL;
  595. goto redo;
  596. }
  597. if (s->http_code < 400)
  598. return 0;
  599. ret = AVERROR(EIO);
  600. fail:
  601. http_proxy_close(h);
  602. return ret;
  603. }
  604. static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
  605. {
  606. HTTPContext *s = h->priv_data;
  607. return ffurl_write(s->hd, buf, size);
  608. }
  609. URLProtocol ff_httpproxy_protocol = {
  610. .name = "httpproxy",
  611. .url_open = http_proxy_open,
  612. .url_read = http_buf_read,
  613. .url_write = http_proxy_write,
  614. .url_close = http_proxy_close,
  615. .url_get_file_handle = http_get_file_handle,
  616. .priv_data_size = sizeof(HTTPContext),
  617. };
  618. #endif