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.

794 lines
25KB

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