http.c 22 KB

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