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.

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