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.

791 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_closep(&hd);
  150. s->hd = NULL;
  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(&hd);
  159. s->hd = NULL;
  160. goto redo;
  161. } else
  162. goto fail;
  163. }
  164. if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
  165. && location_changed == 1) {
  166. /* url moved, get next */
  167. ffurl_closep(&hd);
  168. s->hd = NULL;
  169. if (redirects++ >= MAX_REDIRECTS)
  170. return AVERROR(EIO);
  171. /* Restart the authentication process with the new target, which
  172. * might use a different auth mechanism. */
  173. memset(&s->auth_state, 0, sizeof(s->auth_state));
  174. attempts = 0;
  175. location_changed = 0;
  176. goto redo;
  177. }
  178. return 0;
  179. fail:
  180. if (hd)
  181. ffurl_closep(&hd);
  182. s->hd = NULL;
  183. return AVERROR(EIO);
  184. }
  185. int ff_http_do_new_request(URLContext *h, const char *uri)
  186. {
  187. HTTPContext *s = h->priv_data;
  188. s->off = 0;
  189. av_strlcpy(s->location, uri, sizeof(s->location));
  190. return http_open_cnx(h);
  191. }
  192. static int http_open(URLContext *h, const char *uri, int flags)
  193. {
  194. HTTPContext *s = h->priv_data;
  195. h->is_streamed = 1;
  196. s->filesize = -1;
  197. av_strlcpy(s->location, uri, sizeof(s->location));
  198. if (s->headers) {
  199. int len = strlen(s->headers);
  200. if (len < 2 || strcmp("\r\n", s->headers + len - 2))
  201. av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
  202. }
  203. return http_open_cnx(h);
  204. }
  205. static int http_getc(HTTPContext *s)
  206. {
  207. int len;
  208. if (s->buf_ptr >= s->buf_end) {
  209. len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
  210. if (len < 0) {
  211. return len;
  212. } else if (len == 0) {
  213. return -1;
  214. } else {
  215. s->buf_ptr = s->buffer;
  216. s->buf_end = s->buffer + len;
  217. }
  218. }
  219. return *s->buf_ptr++;
  220. }
  221. static int http_get_line(HTTPContext *s, char *line, int line_size)
  222. {
  223. int ch;
  224. char *q;
  225. q = line;
  226. for(;;) {
  227. ch = http_getc(s);
  228. if (ch < 0)
  229. return ch;
  230. if (ch == '\n') {
  231. /* process line */
  232. if (q > line && q[-1] == '\r')
  233. q--;
  234. *q = '\0';
  235. return 0;
  236. } else {
  237. if ((q - line) < line_size - 1)
  238. *q++ = ch;
  239. }
  240. }
  241. }
  242. static int process_line(URLContext *h, char *line, int line_count,
  243. int *new_location)
  244. {
  245. HTTPContext *s = h->priv_data;
  246. char *tag, *p, *end;
  247. /* end of header */
  248. if (line[0] == '\0') {
  249. s->end_header = 1;
  250. return 0;
  251. }
  252. p = line;
  253. if (line_count == 0) {
  254. while (!isspace(*p) && *p != '\0')
  255. p++;
  256. while (isspace(*p))
  257. p++;
  258. s->http_code = strtol(p, &end, 10);
  259. av_dlog(NULL, "http_code=%d\n", s->http_code);
  260. /* error codes are 4xx and 5xx, but regard 401 as a success, so we
  261. * don't abort until all headers have been parsed. */
  262. if (s->http_code >= 400 && s->http_code < 600 && (s->http_code != 401
  263. || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
  264. (s->http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
  265. end += strspn(end, SPACE_CHARS);
  266. av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
  267. s->http_code, end);
  268. return -1;
  269. }
  270. } else {
  271. while (*p != '\0' && *p != ':')
  272. p++;
  273. if (*p != ':')
  274. return 1;
  275. *p = '\0';
  276. tag = line;
  277. p++;
  278. while (isspace(*p))
  279. p++;
  280. if (!av_strcasecmp(tag, "Location")) {
  281. strcpy(s->location, p);
  282. *new_location = 1;
  283. } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
  284. s->filesize = atoll(p);
  285. } else if (!av_strcasecmp (tag, "Content-Range")) {
  286. /* "bytes $from-$to/$document_size" */
  287. const char *slash;
  288. if (!strncmp (p, "bytes ", 6)) {
  289. p += 6;
  290. s->off = atoll(p);
  291. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  292. s->filesize = atoll(slash+1);
  293. }
  294. h->is_streamed = 0; /* we _can_ in fact seek */
  295. } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5)) {
  296. h->is_streamed = 0;
  297. } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
  298. s->filesize = -1;
  299. s->chunksize = 0;
  300. } else if (!av_strcasecmp (tag, "WWW-Authenticate")) {
  301. ff_http_auth_handle_header(&s->auth_state, tag, p);
  302. } else if (!av_strcasecmp (tag, "Authentication-Info")) {
  303. ff_http_auth_handle_header(&s->auth_state, tag, p);
  304. } else if (!av_strcasecmp (tag, "Proxy-Authenticate")) {
  305. ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
  306. } else if (!av_strcasecmp (tag, "Connection")) {
  307. if (!strcmp(p, "close"))
  308. s->willclose = 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[1024];
  324. int err = 0;
  325. for (;;) {
  326. if ((err = http_get_line(s, line, sizeof(line))) < 0)
  327. return err;
  328. av_dlog(NULL, "header='%s'\n", line);
  329. err = process_line(h, line, s->line_count, new_location);
  330. if (err < 0)
  331. return err;
  332. if (err == 0)
  333. break;
  334. s->line_count++;
  335. }
  336. return err;
  337. }
  338. static int http_connect(URLContext *h, const char *path, const char *local_path,
  339. const char *hoststr, const char *auth,
  340. const char *proxyauth, int *new_location)
  341. {
  342. HTTPContext *s = h->priv_data;
  343. int post, err;
  344. char headers[1024] = "";
  345. char *authstr = NULL, *proxyauthstr = NULL;
  346. int64_t off = s->off;
  347. int len = 0;
  348. const char *method;
  349. /* send http header */
  350. post = h->flags & AVIO_FLAG_WRITE;
  351. if (s->post_data) {
  352. /* force POST method and disable chunked encoding when
  353. * custom HTTP post data is set */
  354. post = 1;
  355. s->chunked_post = 0;
  356. }
  357. method = post ? "POST" : "GET";
  358. authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
  359. method);
  360. proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
  361. local_path, method);
  362. /* set default headers if needed */
  363. if (!has_header(s->headers, "\r\nUser-Agent: "))
  364. len += av_strlcatf(headers + len, sizeof(headers) - len,
  365. "User-Agent: %s\r\n",
  366. s->user_agent ? s->user_agent : LIBAVFORMAT_IDENT);
  367. if (!has_header(s->headers, "\r\nAccept: "))
  368. len += av_strlcpy(headers + len, "Accept: */*\r\n",
  369. sizeof(headers) - len);
  370. if (!has_header(s->headers, "\r\nRange: ") && !post)
  371. len += av_strlcatf(headers + len, sizeof(headers) - len,
  372. "Range: bytes=%"PRId64"-\r\n", s->off);
  373. if (!has_header(s->headers, "\r\nConnection: ")) {
  374. if (s->multiple_requests) {
  375. len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
  376. sizeof(headers) - len);
  377. } else {
  378. len += av_strlcpy(headers + len, "Connection: close\r\n",
  379. sizeof(headers) - len);
  380. }
  381. }
  382. if (!has_header(s->headers, "\r\nHost: "))
  383. len += av_strlcatf(headers + len, sizeof(headers) - len,
  384. "Host: %s\r\n", hoststr);
  385. if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
  386. len += av_strlcatf(headers + len, sizeof(headers) - len,
  387. "Content-Length: %d\r\n", s->post_datalen);
  388. /* now add in custom headers */
  389. if (s->headers)
  390. av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
  391. snprintf(s->buffer, sizeof(s->buffer),
  392. "%s %s HTTP/1.1\r\n"
  393. "%s"
  394. "%s"
  395. "%s"
  396. "%s%s"
  397. "\r\n",
  398. method,
  399. path,
  400. post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
  401. headers,
  402. authstr ? authstr : "",
  403. proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
  404. av_freep(&authstr);
  405. av_freep(&proxyauthstr);
  406. if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
  407. return err;
  408. if (s->post_data)
  409. if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
  410. return err;
  411. /* init input buffer */
  412. s->buf_ptr = s->buffer;
  413. s->buf_end = s->buffer;
  414. s->line_count = 0;
  415. s->off = 0;
  416. s->filesize = -1;
  417. s->willclose = 0;
  418. s->end_chunked_post = 0;
  419. s->end_header = 0;
  420. if (post && !s->post_data) {
  421. /* Pretend that it did work. We didn't read any header yet, since
  422. * we've still to send the POST data, but the code calling this
  423. * function will check http_code after we return. */
  424. s->http_code = 200;
  425. return 0;
  426. }
  427. s->chunksize = -1;
  428. /* wait for header */
  429. err = http_read_header(h, new_location);
  430. if (err < 0)
  431. return err;
  432. return (off == s->off) ? 0 : -1;
  433. }
  434. static int http_buf_read(URLContext *h, uint8_t *buf, int size)
  435. {
  436. HTTPContext *s = h->priv_data;
  437. int len;
  438. /* read bytes from input buffer first */
  439. len = s->buf_end - s->buf_ptr;
  440. if (len > 0) {
  441. if (len > size)
  442. len = size;
  443. memcpy(buf, s->buf_ptr, len);
  444. s->buf_ptr += len;
  445. } else {
  446. if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
  447. return AVERROR_EOF;
  448. len = ffurl_read(s->hd, buf, size);
  449. }
  450. if (len > 0) {
  451. s->off += len;
  452. if (s->chunksize > 0)
  453. s->chunksize -= len;
  454. }
  455. return len;
  456. }
  457. static int http_read(URLContext *h, uint8_t *buf, int size)
  458. {
  459. HTTPContext *s = h->priv_data;
  460. int err, new_location;
  461. if (s->end_chunked_post) {
  462. if (!s->end_header) {
  463. err = http_read_header(h, &new_location);
  464. if (err < 0)
  465. return err;
  466. }
  467. return http_buf_read(h, buf, size);
  468. }
  469. if (s->chunksize >= 0) {
  470. if (!s->chunksize) {
  471. char line[32];
  472. for(;;) {
  473. do {
  474. if ((err = http_get_line(s, line, sizeof(line))) < 0)
  475. return err;
  476. } while (!*line); /* skip CR LF from last chunk */
  477. s->chunksize = strtoll(line, NULL, 16);
  478. av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
  479. if (!s->chunksize)
  480. return 0;
  481. break;
  482. }
  483. }
  484. size = FFMIN(size, s->chunksize);
  485. }
  486. return http_buf_read(h, buf, size);
  487. }
  488. /* used only when posting data */
  489. static int http_write(URLContext *h, const uint8_t *buf, int size)
  490. {
  491. char temp[11] = ""; /* 32-bit hex + CRLF + nul */
  492. int ret;
  493. char crlf[] = "\r\n";
  494. HTTPContext *s = h->priv_data;
  495. if (!s->chunked_post) {
  496. /* non-chunked data is sent without any special encoding */
  497. return ffurl_write(s->hd, buf, size);
  498. }
  499. /* silently ignore zero-size data since chunk encoding that would
  500. * signal EOF */
  501. if (size > 0) {
  502. /* upload data using chunked encoding */
  503. snprintf(temp, sizeof(temp), "%x\r\n", size);
  504. if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
  505. (ret = ffurl_write(s->hd, buf, size)) < 0 ||
  506. (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
  507. return ret;
  508. }
  509. return size;
  510. }
  511. static int http_shutdown(URLContext *h, int flags)
  512. {
  513. int ret = 0;
  514. char footer[] = "0\r\n\r\n";
  515. HTTPContext *s = h->priv_data;
  516. /* signal end of chunked encoding if used */
  517. if ((flags & AVIO_FLAG_WRITE) && s->chunked_post) {
  518. ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
  519. ret = ret > 0 ? 0 : ret;
  520. s->end_chunked_post = 1;
  521. }
  522. return ret;
  523. }
  524. static int http_close(URLContext *h)
  525. {
  526. int ret = 0;
  527. HTTPContext *s = h->priv_data;
  528. if (!s->end_chunked_post) {
  529. /* Close the write direction by sending the end of chunked encoding. */
  530. ret = http_shutdown(h, h->flags);
  531. }
  532. if (s->hd)
  533. ffurl_closep(&s->hd);
  534. return ret;
  535. }
  536. static int64_t http_seek(URLContext *h, int64_t off, int whence)
  537. {
  538. HTTPContext *s = h->priv_data;
  539. URLContext *old_hd = s->hd;
  540. int64_t old_off = s->off;
  541. uint8_t old_buf[BUFFER_SIZE];
  542. int old_buf_size;
  543. if (whence == AVSEEK_SIZE)
  544. return s->filesize;
  545. else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
  546. return -1;
  547. /* we save the old context in case the seek fails */
  548. old_buf_size = s->buf_end - s->buf_ptr;
  549. memcpy(old_buf, s->buf_ptr, old_buf_size);
  550. s->hd = NULL;
  551. if (whence == SEEK_CUR)
  552. off += s->off;
  553. else if (whence == SEEK_END)
  554. off += s->filesize;
  555. s->off = off;
  556. /* if it fails, continue on old connection */
  557. if (http_open_cnx(h) < 0) {
  558. memcpy(s->buffer, old_buf, old_buf_size);
  559. s->buf_ptr = s->buffer;
  560. s->buf_end = s->buffer + old_buf_size;
  561. s->hd = old_hd;
  562. s->off = old_off;
  563. return -1;
  564. }
  565. ffurl_close(old_hd);
  566. return off;
  567. }
  568. static int
  569. http_get_file_handle(URLContext *h)
  570. {
  571. HTTPContext *s = h->priv_data;
  572. return ffurl_get_file_handle(s->hd);
  573. }
  574. #if CONFIG_HTTP_PROTOCOL
  575. URLProtocol ff_http_protocol = {
  576. .name = "http",
  577. .url_open = http_open,
  578. .url_read = http_read,
  579. .url_write = http_write,
  580. .url_seek = http_seek,
  581. .url_close = http_close,
  582. .url_get_file_handle = http_get_file_handle,
  583. .url_shutdown = http_shutdown,
  584. .priv_data_size = sizeof(HTTPContext),
  585. .priv_data_class = &http_context_class,
  586. .flags = URL_PROTOCOL_FLAG_NETWORK,
  587. };
  588. #endif
  589. #if CONFIG_HTTPS_PROTOCOL
  590. URLProtocol ff_https_protocol = {
  591. .name = "https",
  592. .url_open = http_open,
  593. .url_read = http_read,
  594. .url_write = http_write,
  595. .url_seek = http_seek,
  596. .url_close = http_close,
  597. .url_get_file_handle = http_get_file_handle,
  598. .priv_data_size = sizeof(HTTPContext),
  599. .priv_data_class = &https_context_class,
  600. .flags = URL_PROTOCOL_FLAG_NETWORK,
  601. };
  602. #endif
  603. #if CONFIG_HTTPPROXY_PROTOCOL
  604. static int http_proxy_close(URLContext *h)
  605. {
  606. HTTPContext *s = h->priv_data;
  607. if (s->hd)
  608. ffurl_closep(&s->hd);
  609. return 0;
  610. }
  611. static int http_proxy_open(URLContext *h, const char *uri, int flags)
  612. {
  613. HTTPContext *s = h->priv_data;
  614. char hostname[1024], hoststr[1024];
  615. char auth[1024], pathbuf[1024], *path;
  616. char lower_url[100];
  617. int port, ret = 0, attempts = 0;
  618. HTTPAuthType cur_auth_type;
  619. char *authstr;
  620. int new_loc;
  621. h->is_streamed = 1;
  622. av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  623. pathbuf, sizeof(pathbuf), uri);
  624. ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  625. path = pathbuf;
  626. if (*path == '/')
  627. path++;
  628. ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
  629. NULL);
  630. redo:
  631. ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
  632. &h->interrupt_callback, NULL);
  633. if (ret < 0)
  634. return ret;
  635. authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
  636. path, "CONNECT");
  637. snprintf(s->buffer, sizeof(s->buffer),
  638. "CONNECT %s HTTP/1.1\r\n"
  639. "Host: %s\r\n"
  640. "Connection: close\r\n"
  641. "%s%s"
  642. "\r\n",
  643. path,
  644. hoststr,
  645. authstr ? "Proxy-" : "", authstr ? authstr : "");
  646. av_freep(&authstr);
  647. if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
  648. goto fail;
  649. s->buf_ptr = s->buffer;
  650. s->buf_end = s->buffer;
  651. s->line_count = 0;
  652. s->filesize = -1;
  653. cur_auth_type = s->proxy_auth_state.auth_type;
  654. /* Note: This uses buffering, potentially reading more than the
  655. * HTTP header. If tunneling a protocol where the server starts
  656. * the conversation, we might buffer part of that here, too.
  657. * Reading that requires using the proper ffurl_read() function
  658. * on this URLContext, not using the fd directly (as the tls
  659. * protocol does). This shouldn't be an issue for tls though,
  660. * since the client starts the conversation there, so there
  661. * is no extra data that we might buffer up here.
  662. */
  663. ret = http_read_header(h, &new_loc);
  664. if (ret < 0)
  665. goto fail;
  666. attempts++;
  667. if (s->http_code == 407 &&
  668. (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
  669. s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
  670. ffurl_closep(&s->hd);
  671. goto redo;
  672. }
  673. if (s->http_code < 400)
  674. return 0;
  675. ret = AVERROR(EIO);
  676. fail:
  677. http_proxy_close(h);
  678. return ret;
  679. }
  680. static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
  681. {
  682. HTTPContext *s = h->priv_data;
  683. return ffurl_write(s->hd, buf, size);
  684. }
  685. URLProtocol ff_httpproxy_protocol = {
  686. .name = "httpproxy",
  687. .url_open = http_proxy_open,
  688. .url_read = http_buf_read,
  689. .url_write = http_proxy_write,
  690. .url_close = http_proxy_close,
  691. .url_get_file_handle = http_get_file_handle,
  692. .priv_data_size = sizeof(HTTPContext),
  693. .flags = URL_PROTOCOL_FLAG_NETWORK,
  694. };
  695. #endif