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.

742 lines
23KB

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