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.

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