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.

788 lines
24KB

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