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.

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