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.

789 lines
24KB

  1. /*
  2. * HTTP protocol for ffmpeg client
  3. * Copyright (c) 2000, 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 ffmpeg 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. char *user_agent;
  44. int64_t off, filesize;
  45. char location[MAX_URL_SIZE];
  46. HTTPAuthState auth_state;
  47. HTTPAuthState proxy_auth_state;
  48. char *headers;
  49. int willclose; /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
  50. int chunked_post;
  51. int end_chunked_post; /**< A flag which indicates if the end of chunked encoding has been sent. */
  52. int end_header; /**< A flag which indicates we have finished to read POST reply. */
  53. int multiple_requests; /**< A flag which indicates if we use persistent connections. */
  54. uint8_t *post_data;
  55. int post_datalen;
  56. } HTTPContext;
  57. #define OFFSET(x) offsetof(HTTPContext, x)
  58. #define D AV_OPT_FLAG_DECODING_PARAM
  59. #define E AV_OPT_FLAG_ENCODING_PARAM
  60. #define DEC AV_OPT_FLAG_DECODING_PARAM
  61. static const AVOption options[] = {
  62. {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.dbl = 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. {"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
  65. {"multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, D|E },
  66. {"post_data", "custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D|E },
  67. {NULL}
  68. };
  69. #define HTTP_CLASS(flavor)\
  70. static const AVClass flavor ## _context_class = {\
  71. .class_name = #flavor,\
  72. .item_name = av_default_item_name,\
  73. .option = options,\
  74. .version = LIBAVUTIL_VERSION_INT,\
  75. }
  76. HTTP_CLASS(http);
  77. HTTP_CLASS(https);
  78. static int http_connect(URLContext *h, const char *path, const char *local_path,
  79. const char *hoststr, const char *auth,
  80. const char *proxyauth, int *new_location);
  81. void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
  82. {
  83. memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
  84. &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
  85. memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state,
  86. &((HTTPContext*)src->priv_data)->proxy_auth_state,
  87. sizeof(HTTPAuthState));
  88. }
  89. /* return non zero if error */
  90. static int http_open_cnx(URLContext *h)
  91. {
  92. const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
  93. char hostname[1024], hoststr[1024], proto[10];
  94. char auth[1024], proxyauth[1024] = "";
  95. char path1[1024];
  96. char buf[1024], urlbuf[1024];
  97. int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
  98. HTTPAuthType cur_auth_type, cur_proxy_auth_type;
  99. HTTPContext *s = h->priv_data;
  100. URLContext *hd = NULL;
  101. proxy_path = getenv("http_proxy");
  102. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  103. av_strstart(proxy_path, "http://", NULL);
  104. /* fill the dest addr */
  105. redo:
  106. /* needed in any case to build the host string */
  107. av_url_split(proto, sizeof(proto), auth, sizeof(auth),
  108. hostname, sizeof(hostname), &port,
  109. path1, sizeof(path1), s->location);
  110. ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  111. if (!strcmp(proto, "https")) {
  112. lower_proto = "tls";
  113. use_proxy = 0;
  114. if (port < 0)
  115. port = 443;
  116. }
  117. if (port < 0)
  118. port = 80;
  119. if (path1[0] == '\0')
  120. path = "/";
  121. else
  122. path = path1;
  123. local_path = path;
  124. if (use_proxy) {
  125. /* Reassemble the request URL without auth string - we don't
  126. * want to leak the auth to the proxy. */
  127. ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
  128. path1);
  129. path = urlbuf;
  130. av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
  131. hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
  132. }
  133. ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
  134. if (!s->hd) {
  135. err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE,
  136. &h->interrupt_callback, NULL);
  137. if (err < 0)
  138. goto fail;
  139. s->hd = hd;
  140. }
  141. cur_auth_type = s->auth_state.auth_type;
  142. cur_proxy_auth_type = s->auth_state.auth_type;
  143. if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
  144. goto fail;
  145. attempts++;
  146. if (s->http_code == 401) {
  147. if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
  148. s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
  149. ffurl_close(hd);
  150. goto redo;
  151. } else
  152. goto fail;
  153. }
  154. if (s->http_code == 407) {
  155. if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
  156. s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
  157. ffurl_close(hd);
  158. goto redo;
  159. } else
  160. goto fail;
  161. }
  162. if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
  163. && location_changed == 1) {
  164. /* url moved, get next */
  165. ffurl_close(hd);
  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 (hd)
  178. ffurl_close(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. strcpy(s->location, p);
  279. *new_location = 1;
  280. } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
  281. s->filesize = atoll(p);
  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 = atoll(p);
  288. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  289. s->filesize = atoll(slash+1);
  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[1024];
  321. int err = 0;
  322. for (;;) {
  323. if ((err = http_get_line(s, line, sizeof(line))) < 0)
  324. return err;
  325. av_dlog(NULL, "header='%s'\n", line);
  326. err = process_line(h, line, s->line_count, new_location);
  327. if (err < 0)
  328. return err;
  329. if (err == 0)
  330. break;
  331. s->line_count++;
  332. }
  333. return err;
  334. }
  335. static int http_connect(URLContext *h, const char *path, const char *local_path,
  336. const char *hoststr, const char *auth,
  337. const char *proxyauth, int *new_location)
  338. {
  339. HTTPContext *s = h->priv_data;
  340. int post, err;
  341. char headers[1024] = "";
  342. char *authstr = NULL, *proxyauthstr = NULL;
  343. int64_t off = s->off;
  344. int len = 0;
  345. const char *method;
  346. /* send http header */
  347. post = h->flags & AVIO_FLAG_WRITE;
  348. if (s->post_data) {
  349. /* force POST method and disable chunked encoding when
  350. * custom HTTP post data is set */
  351. post = 1;
  352. s->chunked_post = 0;
  353. }
  354. method = post ? "POST" : "GET";
  355. authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
  356. method);
  357. proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
  358. local_path, method);
  359. /* set default headers if needed */
  360. if (!has_header(s->headers, "\r\nUser-Agent: "))
  361. len += av_strlcatf(headers + len, sizeof(headers) - len,
  362. "User-Agent: %s\r\n",
  363. s->user_agent ? s->user_agent : 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. s->chunksize = -1;
  425. /* wait for header */
  426. err = http_read_header(h, new_location);
  427. if (err < 0)
  428. return err;
  429. return (off == s->off) ? 0 : -1;
  430. }
  431. static int http_buf_read(URLContext *h, uint8_t *buf, int size)
  432. {
  433. HTTPContext *s = h->priv_data;
  434. int len;
  435. /* read bytes from input buffer first */
  436. len = s->buf_end - s->buf_ptr;
  437. if (len > 0) {
  438. if (len > size)
  439. len = size;
  440. memcpy(buf, s->buf_ptr, len);
  441. s->buf_ptr += len;
  442. } else {
  443. if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
  444. return AVERROR_EOF;
  445. len = ffurl_read(s->hd, buf, size);
  446. }
  447. if (len > 0) {
  448. s->off += len;
  449. if (s->chunksize > 0)
  450. s->chunksize -= len;
  451. }
  452. return len;
  453. }
  454. static int http_read(URLContext *h, uint8_t *buf, int size)
  455. {
  456. HTTPContext *s = h->priv_data;
  457. int err, new_location;
  458. if (s->end_chunked_post) {
  459. if (!s->end_header) {
  460. err = http_read_header(h, &new_location);
  461. if (err < 0)
  462. return err;
  463. }
  464. return http_buf_read(h, buf, size);
  465. }
  466. if (s->chunksize >= 0) {
  467. if (!s->chunksize) {
  468. char line[32];
  469. for(;;) {
  470. do {
  471. if ((err = http_get_line(s, line, sizeof(line))) < 0)
  472. return err;
  473. } while (!*line); /* skip CR LF from last chunk */
  474. s->chunksize = strtoll(line, NULL, 16);
  475. av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
  476. if (!s->chunksize)
  477. return 0;
  478. break;
  479. }
  480. }
  481. size = FFMIN(size, s->chunksize);
  482. }
  483. return http_buf_read(h, buf, size);
  484. }
  485. /* used only when posting data */
  486. static int http_write(URLContext *h, const uint8_t *buf, int size)
  487. {
  488. char temp[11] = ""; /* 32-bit hex + CRLF + nul */
  489. int ret;
  490. char crlf[] = "\r\n";
  491. HTTPContext *s = h->priv_data;
  492. if (!s->chunked_post) {
  493. /* non-chunked data is sent without any special encoding */
  494. return ffurl_write(s->hd, buf, size);
  495. }
  496. /* silently ignore zero-size data since chunk encoding that would
  497. * signal EOF */
  498. if (size > 0) {
  499. /* upload data using chunked encoding */
  500. snprintf(temp, sizeof(temp), "%x\r\n", size);
  501. if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
  502. (ret = ffurl_write(s->hd, buf, size)) < 0 ||
  503. (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
  504. return ret;
  505. }
  506. return size;
  507. }
  508. static int http_shutdown(URLContext *h, int flags)
  509. {
  510. int ret = 0;
  511. char footer[] = "0\r\n\r\n";
  512. HTTPContext *s = h->priv_data;
  513. /* signal end of chunked encoding if used */
  514. if ((flags & AVIO_FLAG_WRITE) && s->chunked_post) {
  515. ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
  516. ret = ret > 0 ? 0 : ret;
  517. s->end_chunked_post = 1;
  518. }
  519. return ret;
  520. }
  521. static int http_close(URLContext *h)
  522. {
  523. int ret = 0;
  524. HTTPContext *s = h->priv_data;
  525. if (!s->end_chunked_post) {
  526. /* Close the write direction by sending the end of chunked encoding. */
  527. ret = http_shutdown(h, h->flags);
  528. }
  529. if (s->hd)
  530. ffurl_close(s->hd);
  531. return ret;
  532. }
  533. static int64_t http_seek(URLContext *h, int64_t off, int whence)
  534. {
  535. HTTPContext *s = h->priv_data;
  536. URLContext *old_hd = s->hd;
  537. int64_t old_off = s->off;
  538. uint8_t old_buf[BUFFER_SIZE];
  539. int old_buf_size;
  540. if (whence == AVSEEK_SIZE)
  541. return s->filesize;
  542. else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
  543. return -1;
  544. /* we save the old context in case the seek fails */
  545. old_buf_size = s->buf_end - s->buf_ptr;
  546. memcpy(old_buf, s->buf_ptr, old_buf_size);
  547. s->hd = NULL;
  548. if (whence == SEEK_CUR)
  549. off += s->off;
  550. else if (whence == SEEK_END)
  551. off += s->filesize;
  552. s->off = off;
  553. /* if it fails, continue on old connection */
  554. if (http_open_cnx(h) < 0) {
  555. memcpy(s->buffer, old_buf, old_buf_size);
  556. s->buf_ptr = s->buffer;
  557. s->buf_end = s->buffer + old_buf_size;
  558. s->hd = old_hd;
  559. s->off = old_off;
  560. return -1;
  561. }
  562. ffurl_close(old_hd);
  563. return off;
  564. }
  565. static int
  566. http_get_file_handle(URLContext *h)
  567. {
  568. HTTPContext *s = h->priv_data;
  569. return ffurl_get_file_handle(s->hd);
  570. }
  571. #if CONFIG_HTTP_PROTOCOL
  572. URLProtocol ff_http_protocol = {
  573. .name = "http",
  574. .url_open = http_open,
  575. .url_read = http_read,
  576. .url_write = http_write,
  577. .url_seek = http_seek,
  578. .url_close = http_close,
  579. .url_get_file_handle = http_get_file_handle,
  580. .url_shutdown = http_shutdown,
  581. .priv_data_size = sizeof(HTTPContext),
  582. .priv_data_class = &http_context_class,
  583. .flags = URL_PROTOCOL_FLAG_NETWORK,
  584. };
  585. #endif
  586. #if CONFIG_HTTPS_PROTOCOL
  587. URLProtocol ff_https_protocol = {
  588. .name = "https",
  589. .url_open = http_open,
  590. .url_read = http_read,
  591. .url_write = http_write,
  592. .url_seek = http_seek,
  593. .url_close = http_close,
  594. .url_get_file_handle = http_get_file_handle,
  595. .priv_data_size = sizeof(HTTPContext),
  596. .priv_data_class = &https_context_class,
  597. .flags = URL_PROTOCOL_FLAG_NETWORK,
  598. };
  599. #endif
  600. #if CONFIG_HTTPPROXY_PROTOCOL
  601. static int http_proxy_close(URLContext *h)
  602. {
  603. HTTPContext *s = h->priv_data;
  604. if (s->hd)
  605. ffurl_close(s->hd);
  606. return 0;
  607. }
  608. static int http_proxy_open(URLContext *h, const char *uri, int flags)
  609. {
  610. HTTPContext *s = h->priv_data;
  611. char hostname[1024], hoststr[1024];
  612. char auth[1024], pathbuf[1024], *path;
  613. char lower_url[100];
  614. int port, ret = 0, attempts = 0;
  615. HTTPAuthType cur_auth_type;
  616. char *authstr;
  617. int new_loc;
  618. h->is_streamed = 1;
  619. av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  620. pathbuf, sizeof(pathbuf), uri);
  621. ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  622. path = pathbuf;
  623. if (*path == '/')
  624. path++;
  625. ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
  626. NULL);
  627. redo:
  628. ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
  629. &h->interrupt_callback, NULL);
  630. if (ret < 0)
  631. return ret;
  632. authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
  633. path, "CONNECT");
  634. snprintf(s->buffer, sizeof(s->buffer),
  635. "CONNECT %s HTTP/1.1\r\n"
  636. "Host: %s\r\n"
  637. "Connection: close\r\n"
  638. "%s%s"
  639. "\r\n",
  640. path,
  641. hoststr,
  642. authstr ? "Proxy-" : "", authstr ? authstr : "");
  643. av_freep(&authstr);
  644. if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
  645. goto fail;
  646. s->buf_ptr = s->buffer;
  647. s->buf_end = s->buffer;
  648. s->line_count = 0;
  649. s->filesize = -1;
  650. cur_auth_type = s->proxy_auth_state.auth_type;
  651. /* Note: This uses buffering, potentially reading more than the
  652. * HTTP header. If tunneling a protocol where the server starts
  653. * the conversation, we might buffer part of that here, too.
  654. * Reading that requires using the proper ffurl_read() function
  655. * on this URLContext, not using the fd directly (as the tls
  656. * protocol does). This shouldn't be an issue for tls though,
  657. * since the client starts the conversation there, so there
  658. * is no extra data that we might buffer up here.
  659. */
  660. ret = http_read_header(h, &new_loc);
  661. if (ret < 0)
  662. goto fail;
  663. attempts++;
  664. if (s->http_code == 407 &&
  665. (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
  666. s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
  667. ffurl_close(s->hd);
  668. s->hd = NULL;
  669. goto redo;
  670. }
  671. if (s->http_code < 400)
  672. return 0;
  673. ret = AVERROR(EIO);
  674. fail:
  675. http_proxy_close(h);
  676. return ret;
  677. }
  678. static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
  679. {
  680. HTTPContext *s = h->priv_data;
  681. return ffurl_write(s->hd, buf, size);
  682. }
  683. URLProtocol ff_httpproxy_protocol = {
  684. .name = "httpproxy",
  685. .url_open = http_proxy_open,
  686. .url_read = http_buf_read,
  687. .url_write = http_proxy_write,
  688. .url_close = http_proxy_close,
  689. .url_get_file_handle = http_get_file_handle,
  690. .priv_data_size = sizeof(HTTPContext),
  691. .flags = URL_PROTOCOL_FLAG_NETWORK,
  692. };
  693. #endif