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.

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