cf-h1-proxy.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
  26. #include <curl/curl.h>
  27. #ifdef USE_HYPER
  28. #include <hyper.h>
  29. #endif
  30. #include "urldata.h"
  31. #include "dynbuf.h"
  32. #include "sendf.h"
  33. #include "http.h"
  34. #include "http1.h"
  35. #include "http_proxy.h"
  36. #include "url.h"
  37. #include "select.h"
  38. #include "progress.h"
  39. #include "cfilters.h"
  40. #include "cf-h1-proxy.h"
  41. #include "connect.h"
  42. #include "curl_trc.h"
  43. #include "curlx.h"
  44. #include "vtls/vtls.h"
  45. #include "transfer.h"
  46. #include "multiif.h"
  47. /* The last 3 #include files should be in this order */
  48. #include "curl_printf.h"
  49. #include "curl_memory.h"
  50. #include "memdebug.h"
  51. typedef enum {
  52. H1_TUNNEL_INIT, /* init/default/no tunnel state */
  53. H1_TUNNEL_CONNECT, /* CONNECT request is being send */
  54. H1_TUNNEL_RECEIVE, /* CONNECT answer is being received */
  55. H1_TUNNEL_RESPONSE, /* CONNECT response received completely */
  56. H1_TUNNEL_ESTABLISHED,
  57. H1_TUNNEL_FAILED
  58. } h1_tunnel_state;
  59. /* struct for HTTP CONNECT tunneling */
  60. struct h1_tunnel_state {
  61. struct HTTP CONNECT;
  62. struct dynbuf rcvbuf;
  63. struct dynbuf request_data;
  64. size_t nsent;
  65. size_t headerlines;
  66. struct Curl_chunker ch;
  67. enum keeponval {
  68. KEEPON_DONE,
  69. KEEPON_CONNECT,
  70. KEEPON_IGNORE
  71. } keepon;
  72. curl_off_t cl; /* size of content to read and ignore */
  73. h1_tunnel_state tunnel_state;
  74. BIT(chunked_encoding);
  75. BIT(close_connection);
  76. };
  77. static bool tunnel_is_established(struct h1_tunnel_state *ts)
  78. {
  79. return ts && (ts->tunnel_state == H1_TUNNEL_ESTABLISHED);
  80. }
  81. static bool tunnel_is_failed(struct h1_tunnel_state *ts)
  82. {
  83. return ts && (ts->tunnel_state == H1_TUNNEL_FAILED);
  84. }
  85. static CURLcode tunnel_reinit(struct Curl_cfilter *cf,
  86. struct Curl_easy *data,
  87. struct h1_tunnel_state *ts)
  88. {
  89. (void)data;
  90. (void)cf;
  91. DEBUGASSERT(ts);
  92. Curl_dyn_reset(&ts->rcvbuf);
  93. Curl_dyn_reset(&ts->request_data);
  94. ts->tunnel_state = H1_TUNNEL_INIT;
  95. ts->keepon = KEEPON_CONNECT;
  96. ts->cl = 0;
  97. ts->close_connection = FALSE;
  98. return CURLE_OK;
  99. }
  100. static CURLcode tunnel_init(struct Curl_cfilter *cf,
  101. struct Curl_easy *data,
  102. struct h1_tunnel_state **pts)
  103. {
  104. struct h1_tunnel_state *ts;
  105. if(cf->conn->handler->flags & PROTOPT_NOTCPPROXY) {
  106. failf(data, "%s cannot be done over CONNECT", cf->conn->handler->scheme);
  107. return CURLE_UNSUPPORTED_PROTOCOL;
  108. }
  109. ts = calloc(1, sizeof(*ts));
  110. if(!ts)
  111. return CURLE_OUT_OF_MEMORY;
  112. infof(data, "allocate connect buffer");
  113. Curl_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
  114. Curl_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
  115. Curl_httpchunk_init(data, &ts->ch, TRUE);
  116. *pts = ts;
  117. connkeep(cf->conn, "HTTP proxy CONNECT");
  118. return tunnel_reinit(cf, data, ts);
  119. }
  120. static void h1_tunnel_go_state(struct Curl_cfilter *cf,
  121. struct h1_tunnel_state *ts,
  122. h1_tunnel_state new_state,
  123. struct Curl_easy *data)
  124. {
  125. if(ts->tunnel_state == new_state)
  126. return;
  127. /* entering this one */
  128. switch(new_state) {
  129. case H1_TUNNEL_INIT:
  130. CURL_TRC_CF(data, cf, "new tunnel state 'init'");
  131. tunnel_reinit(cf, data, ts);
  132. break;
  133. case H1_TUNNEL_CONNECT:
  134. CURL_TRC_CF(data, cf, "new tunnel state 'connect'");
  135. ts->tunnel_state = H1_TUNNEL_CONNECT;
  136. ts->keepon = KEEPON_CONNECT;
  137. Curl_dyn_reset(&ts->rcvbuf);
  138. break;
  139. case H1_TUNNEL_RECEIVE:
  140. CURL_TRC_CF(data, cf, "new tunnel state 'receive'");
  141. ts->tunnel_state = H1_TUNNEL_RECEIVE;
  142. break;
  143. case H1_TUNNEL_RESPONSE:
  144. CURL_TRC_CF(data, cf, "new tunnel state 'response'");
  145. ts->tunnel_state = H1_TUNNEL_RESPONSE;
  146. break;
  147. case H1_TUNNEL_ESTABLISHED:
  148. CURL_TRC_CF(data, cf, "new tunnel state 'established'");
  149. infof(data, "CONNECT phase completed");
  150. data->state.authproxy.done = TRUE;
  151. data->state.authproxy.multipass = FALSE;
  152. FALLTHROUGH();
  153. case H1_TUNNEL_FAILED:
  154. if(new_state == H1_TUNNEL_FAILED)
  155. CURL_TRC_CF(data, cf, "new tunnel state 'failed'");
  156. ts->tunnel_state = new_state;
  157. Curl_dyn_reset(&ts->rcvbuf);
  158. Curl_dyn_reset(&ts->request_data);
  159. /* restore the protocol pointer */
  160. data->info.httpcode = 0; /* clear it as it might've been used for the
  161. proxy */
  162. /* If a proxy-authorization header was used for the proxy, then we should
  163. make sure that it isn't accidentally used for the document request
  164. after we've connected. So let's free and clear it here. */
  165. Curl_safefree(data->state.aptr.proxyuserpwd);
  166. #ifdef USE_HYPER
  167. data->state.hconnect = FALSE;
  168. #endif
  169. break;
  170. }
  171. }
  172. static void tunnel_free(struct Curl_cfilter *cf,
  173. struct Curl_easy *data)
  174. {
  175. if(cf) {
  176. struct h1_tunnel_state *ts = cf->ctx;
  177. if(ts) {
  178. h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
  179. Curl_dyn_free(&ts->rcvbuf);
  180. Curl_dyn_free(&ts->request_data);
  181. Curl_httpchunk_free(data, &ts->ch);
  182. free(ts);
  183. cf->ctx = NULL;
  184. }
  185. }
  186. }
  187. static bool tunnel_want_send(struct h1_tunnel_state *ts)
  188. {
  189. return (ts->tunnel_state == H1_TUNNEL_CONNECT);
  190. }
  191. #ifndef USE_HYPER
  192. static CURLcode start_CONNECT(struct Curl_cfilter *cf,
  193. struct Curl_easy *data,
  194. struct h1_tunnel_state *ts)
  195. {
  196. struct httpreq *req = NULL;
  197. int http_minor;
  198. CURLcode result;
  199. /* This only happens if we've looped here due to authentication
  200. reasons, and we don't really use the newly cloned URL here
  201. then. Just free() it. */
  202. Curl_safefree(data->req.newurl);
  203. result = Curl_http_proxy_create_CONNECT(&req, cf, data, 1);
  204. if(result)
  205. goto out;
  206. infof(data, "Establish HTTP proxy tunnel to %s", req->authority);
  207. Curl_dyn_reset(&ts->request_data);
  208. ts->nsent = 0;
  209. ts->headerlines = 0;
  210. http_minor = (cf->conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ? 0 : 1;
  211. result = Curl_h1_req_write_head(req, http_minor, &ts->request_data);
  212. if(!result)
  213. result = Curl_creader_set_null(data);
  214. out:
  215. if(result)
  216. failf(data, "Failed sending CONNECT to proxy");
  217. if(req)
  218. Curl_http_req_free(req);
  219. return result;
  220. }
  221. static CURLcode send_CONNECT(struct Curl_cfilter *cf,
  222. struct Curl_easy *data,
  223. struct h1_tunnel_state *ts,
  224. bool *done)
  225. {
  226. char *buf = Curl_dyn_ptr(&ts->request_data);
  227. size_t request_len = Curl_dyn_len(&ts->request_data);
  228. size_t blen = request_len;
  229. CURLcode result = CURLE_OK;
  230. ssize_t nwritten;
  231. if(blen <= ts->nsent)
  232. goto out; /* we are done */
  233. blen -= ts->nsent;
  234. buf += ts->nsent;
  235. nwritten = cf->next->cft->do_send(cf->next, data, buf, blen, &result);
  236. if(nwritten < 0) {
  237. if(result == CURLE_AGAIN) {
  238. result = CURLE_OK;
  239. }
  240. goto out;
  241. }
  242. DEBUGASSERT(blen >= (size_t)nwritten);
  243. ts->nsent += (size_t)nwritten;
  244. Curl_debug(data, CURLINFO_HEADER_OUT, buf, (size_t)nwritten);
  245. out:
  246. if(result)
  247. failf(data, "Failed sending CONNECT to proxy");
  248. *done = (!result && (ts->nsent >= request_len));
  249. return result;
  250. }
  251. static CURLcode on_resp_header(struct Curl_cfilter *cf,
  252. struct Curl_easy *data,
  253. struct h1_tunnel_state *ts,
  254. const char *header)
  255. {
  256. CURLcode result = CURLE_OK;
  257. struct SingleRequest *k = &data->req;
  258. (void)cf;
  259. if((checkprefix("WWW-Authenticate:", header) &&
  260. (401 == k->httpcode)) ||
  261. (checkprefix("Proxy-authenticate:", header) &&
  262. (407 == k->httpcode))) {
  263. bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
  264. char *auth = Curl_copy_header_value(header);
  265. if(!auth)
  266. return CURLE_OUT_OF_MEMORY;
  267. CURL_TRC_CF(data, cf, "CONNECT: fwd auth header '%s'", header);
  268. result = Curl_http_input_auth(data, proxy, auth);
  269. free(auth);
  270. if(result)
  271. return result;
  272. }
  273. else if(checkprefix("Content-Length:", header)) {
  274. if(k->httpcode/100 == 2) {
  275. /* A client MUST ignore any Content-Length or Transfer-Encoding
  276. header fields received in a successful response to CONNECT.
  277. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  278. infof(data, "Ignoring Content-Length in CONNECT %03d response",
  279. k->httpcode);
  280. }
  281. else {
  282. (void)curlx_strtoofft(header + strlen("Content-Length:"),
  283. NULL, 10, &ts->cl);
  284. }
  285. }
  286. else if(Curl_compareheader(header,
  287. STRCONST("Connection:"), STRCONST("close")))
  288. ts->close_connection = TRUE;
  289. else if(checkprefix("Transfer-Encoding:", header)) {
  290. if(k->httpcode/100 == 2) {
  291. /* A client MUST ignore any Content-Length or Transfer-Encoding
  292. header fields received in a successful response to CONNECT.
  293. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  294. infof(data, "Ignoring Transfer-Encoding in "
  295. "CONNECT %03d response", k->httpcode);
  296. }
  297. else if(Curl_compareheader(header,
  298. STRCONST("Transfer-Encoding:"),
  299. STRCONST("chunked"))) {
  300. infof(data, "CONNECT responded chunked");
  301. ts->chunked_encoding = TRUE;
  302. /* reset our chunky engine */
  303. Curl_httpchunk_reset(data, &ts->ch, TRUE);
  304. }
  305. }
  306. else if(Curl_compareheader(header,
  307. STRCONST("Proxy-Connection:"),
  308. STRCONST("close")))
  309. ts->close_connection = TRUE;
  310. else if(!strncmp(header, "HTTP/1.", 7) &&
  311. ((header[7] == '0') || (header[7] == '1')) &&
  312. (header[8] == ' ') &&
  313. ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
  314. !ISDIGIT(header[12])) {
  315. /* store the HTTP code from the proxy */
  316. data->info.httpproxycode = k->httpcode = (header[9] - '0') * 100 +
  317. (header[10] - '0') * 10 + (header[11] - '0');
  318. }
  319. return result;
  320. }
  321. static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
  322. struct Curl_easy *data,
  323. struct h1_tunnel_state *ts,
  324. bool *done)
  325. {
  326. CURLcode result = CURLE_OK;
  327. struct SingleRequest *k = &data->req;
  328. char *linep;
  329. size_t line_len;
  330. int error, writetype;
  331. #define SELECT_OK 0
  332. #define SELECT_ERROR 1
  333. error = SELECT_OK;
  334. *done = FALSE;
  335. if(!Curl_conn_data_pending(data, cf->sockindex))
  336. return CURLE_OK;
  337. while(ts->keepon) {
  338. ssize_t nread;
  339. char byte;
  340. /* Read one byte at a time to avoid a race condition. Wait at most one
  341. second before looping to ensure continuous pgrsUpdates. */
  342. result = Curl_conn_recv(data, cf->sockindex, &byte, 1, &nread);
  343. if(result == CURLE_AGAIN)
  344. /* socket buffer drained, return */
  345. return CURLE_OK;
  346. if(Curl_pgrsUpdate(data))
  347. return CURLE_ABORTED_BY_CALLBACK;
  348. if(result) {
  349. ts->keepon = KEEPON_DONE;
  350. break;
  351. }
  352. if(nread <= 0) {
  353. if(data->set.proxyauth && data->state.authproxy.avail &&
  354. data->state.aptr.proxyuserpwd) {
  355. /* proxy auth was requested and there was proxy auth available,
  356. then deem this as "mere" proxy disconnect */
  357. ts->close_connection = TRUE;
  358. infof(data, "Proxy CONNECT connection closed");
  359. }
  360. else {
  361. error = SELECT_ERROR;
  362. failf(data, "Proxy CONNECT aborted");
  363. }
  364. ts->keepon = KEEPON_DONE;
  365. break;
  366. }
  367. if(ts->keepon == KEEPON_IGNORE) {
  368. /* This means we are currently ignoring a response-body */
  369. if(ts->cl) {
  370. /* A Content-Length based body: simply count down the counter
  371. and make sure to break out of the loop when we're done! */
  372. ts->cl--;
  373. if(ts->cl <= 0) {
  374. ts->keepon = KEEPON_DONE;
  375. break;
  376. }
  377. }
  378. else if(ts->chunked_encoding) {
  379. /* chunked-encoded body, so we need to do the chunked dance
  380. properly to know when the end of the body is reached */
  381. size_t consumed = 0;
  382. /* now parse the chunked piece of data so that we can
  383. properly tell when the stream ends */
  384. result = Curl_httpchunk_read(data, &ts->ch, &byte, 1, &consumed);
  385. if(result)
  386. return result;
  387. if(Curl_httpchunk_is_done(data, &ts->ch)) {
  388. /* we're done reading chunks! */
  389. infof(data, "chunk reading DONE");
  390. ts->keepon = KEEPON_DONE;
  391. }
  392. }
  393. continue;
  394. }
  395. if(Curl_dyn_addn(&ts->rcvbuf, &byte, 1)) {
  396. failf(data, "CONNECT response too large");
  397. return CURLE_RECV_ERROR;
  398. }
  399. /* if this is not the end of a header line then continue */
  400. if(byte != 0x0a)
  401. continue;
  402. ts->headerlines++;
  403. linep = Curl_dyn_ptr(&ts->rcvbuf);
  404. line_len = Curl_dyn_len(&ts->rcvbuf); /* amount of bytes in this line */
  405. /* output debug if that is requested */
  406. Curl_debug(data, CURLINFO_HEADER_IN, linep, line_len);
  407. /* send the header to the callback */
  408. writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
  409. (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
  410. result = Curl_client_write(data, writetype, linep, line_len);
  411. if(result)
  412. return result;
  413. result = Curl_bump_headersize(data, line_len, TRUE);
  414. if(result)
  415. return result;
  416. /* Newlines are CRLF, so the CR is ignored as the line isn't
  417. really terminated until the LF comes. Treat a following CR
  418. as end-of-headers as well.*/
  419. if(('\r' == linep[0]) ||
  420. ('\n' == linep[0])) {
  421. /* end of response-headers from the proxy */
  422. if((407 == k->httpcode) && !data->state.authproblem) {
  423. /* If we get a 407 response code with content length
  424. when we have no auth problem, we must ignore the
  425. whole response-body */
  426. ts->keepon = KEEPON_IGNORE;
  427. if(ts->cl) {
  428. infof(data, "Ignore %" CURL_FORMAT_CURL_OFF_T
  429. " bytes of response-body", ts->cl);
  430. }
  431. else if(ts->chunked_encoding) {
  432. infof(data, "Ignore chunked response-body");
  433. }
  434. else {
  435. /* without content-length or chunked encoding, we
  436. can't keep the connection alive since the close is
  437. the end signal so we bail out at once instead */
  438. CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
  439. ts->keepon = KEEPON_DONE;
  440. }
  441. }
  442. else {
  443. ts->keepon = KEEPON_DONE;
  444. }
  445. DEBUGASSERT(ts->keepon == KEEPON_IGNORE
  446. || ts->keepon == KEEPON_DONE);
  447. continue;
  448. }
  449. result = on_resp_header(cf, data, ts, linep);
  450. if(result)
  451. return result;
  452. Curl_dyn_reset(&ts->rcvbuf);
  453. } /* while there's buffer left and loop is requested */
  454. if(error)
  455. result = CURLE_RECV_ERROR;
  456. *done = (ts->keepon == KEEPON_DONE);
  457. if(!result && *done && data->info.httpproxycode/100 != 2) {
  458. /* Deal with the possibly already received authenticate
  459. headers. 'newurl' is set to a new URL if we must loop. */
  460. result = Curl_http_auth_act(data);
  461. }
  462. return result;
  463. }
  464. #else /* USE_HYPER */
  465. static CURLcode CONNECT_host(struct Curl_cfilter *cf,
  466. struct Curl_easy *data,
  467. char **pauthority,
  468. char **phost_header)
  469. {
  470. const char *hostname;
  471. int port;
  472. bool ipv6_ip;
  473. CURLcode result;
  474. char *authority; /* for CONNECT, the destination host + port */
  475. char *host_header = NULL; /* Host: authority */
  476. result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
  477. if(result)
  478. return result;
  479. authority = aprintf("%s%s%s:%d", ipv6_ip?"[":"", hostname, ipv6_ip?"]":"",
  480. port);
  481. if(!authority)
  482. return CURLE_OUT_OF_MEMORY;
  483. /* If user is not overriding the Host header later */
  484. if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
  485. host_header = aprintf("Host: %s\r\n", authority);
  486. if(!host_header) {
  487. free(authority);
  488. return CURLE_OUT_OF_MEMORY;
  489. }
  490. }
  491. *pauthority = authority;
  492. *phost_header = host_header;
  493. return CURLE_OK;
  494. }
  495. /* The Hyper version of CONNECT */
  496. static CURLcode start_CONNECT(struct Curl_cfilter *cf,
  497. struct Curl_easy *data,
  498. struct h1_tunnel_state *ts)
  499. {
  500. struct connectdata *conn = cf->conn;
  501. struct hyptransfer *h = &data->hyp;
  502. curl_socket_t tunnelsocket = Curl_conn_cf_get_socket(cf, data);
  503. hyper_io *io = NULL;
  504. hyper_request *req = NULL;
  505. hyper_headers *headers = NULL;
  506. hyper_clientconn_options *options = NULL;
  507. hyper_task *handshake = NULL;
  508. hyper_task *task = NULL; /* for the handshake */
  509. hyper_clientconn *client = NULL;
  510. hyper_task *sendtask = NULL; /* for the send */
  511. char *authority = NULL; /* for CONNECT */
  512. char *host_header = NULL; /* Host: */
  513. CURLcode result = CURLE_OUT_OF_MEMORY;
  514. (void)ts;
  515. io = hyper_io_new();
  516. if(!io) {
  517. failf(data, "Couldn't create hyper IO");
  518. result = CURLE_OUT_OF_MEMORY;
  519. goto error;
  520. }
  521. /* tell Hyper how to read/write network data */
  522. h->io_ctx.data = data;
  523. h->io_ctx.sockindex = cf->sockindex;
  524. hyper_io_set_userdata(io, &h->io_ctx);
  525. hyper_io_set_read(io, Curl_hyper_recv);
  526. hyper_io_set_write(io, Curl_hyper_send);
  527. conn->sockfd = tunnelsocket;
  528. data->state.hconnect = TRUE;
  529. /* create an executor to poll futures */
  530. if(!h->exec) {
  531. h->exec = hyper_executor_new();
  532. if(!h->exec) {
  533. failf(data, "Couldn't create hyper executor");
  534. result = CURLE_OUT_OF_MEMORY;
  535. goto error;
  536. }
  537. }
  538. options = hyper_clientconn_options_new();
  539. if(!options) {
  540. failf(data, "Couldn't create hyper client options");
  541. result = CURLE_OUT_OF_MEMORY;
  542. goto error;
  543. }
  544. hyper_clientconn_options_set_preserve_header_case(options, 1);
  545. hyper_clientconn_options_set_preserve_header_order(options, 1);
  546. hyper_clientconn_options_exec(options, h->exec);
  547. /* "Both the `io` and the `options` are consumed in this function
  548. call" */
  549. handshake = hyper_clientconn_handshake(io, options);
  550. if(!handshake) {
  551. failf(data, "Couldn't create hyper client handshake");
  552. result = CURLE_OUT_OF_MEMORY;
  553. goto error;
  554. }
  555. io = NULL;
  556. options = NULL;
  557. if(HYPERE_OK != hyper_executor_push(h->exec, handshake)) {
  558. failf(data, "Couldn't hyper_executor_push the handshake");
  559. result = CURLE_OUT_OF_MEMORY;
  560. goto error;
  561. }
  562. handshake = NULL; /* ownership passed on */
  563. task = hyper_executor_poll(h->exec);
  564. if(!task) {
  565. failf(data, "Couldn't hyper_executor_poll the handshake");
  566. result = CURLE_OUT_OF_MEMORY;
  567. goto error;
  568. }
  569. client = hyper_task_value(task);
  570. hyper_task_free(task);
  571. req = hyper_request_new();
  572. if(!req) {
  573. failf(data, "Couldn't hyper_request_new");
  574. result = CURLE_OUT_OF_MEMORY;
  575. goto error;
  576. }
  577. if(hyper_request_set_method(req, (uint8_t *)"CONNECT",
  578. strlen("CONNECT"))) {
  579. failf(data, "error setting method");
  580. result = CURLE_OUT_OF_MEMORY;
  581. goto error;
  582. }
  583. /* This only happens if we've looped here due to authentication
  584. reasons, and we don't really use the newly cloned URL here
  585. then. Just free() it. */
  586. Curl_safefree(data->req.newurl);
  587. result = CONNECT_host(cf, data, &authority, &host_header);
  588. if(result)
  589. goto error;
  590. infof(data, "Establish HTTP proxy tunnel to %s", authority);
  591. if(hyper_request_set_uri(req, (uint8_t *)authority,
  592. strlen(authority))) {
  593. failf(data, "error setting path");
  594. result = CURLE_OUT_OF_MEMORY;
  595. goto error;
  596. }
  597. if(data->set.verbose) {
  598. char *se = aprintf("CONNECT %s HTTP/1.1\r\n", authority);
  599. if(!se) {
  600. result = CURLE_OUT_OF_MEMORY;
  601. goto error;
  602. }
  603. Curl_debug(data, CURLINFO_HEADER_OUT, se, strlen(se));
  604. free(se);
  605. }
  606. /* Setup the proxy-authorization header, if any */
  607. result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
  608. authority, TRUE);
  609. if(result)
  610. goto error;
  611. Curl_safefree(authority);
  612. /* default is 1.1 */
  613. if((conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) &&
  614. (HYPERE_OK != hyper_request_set_version(req,
  615. HYPER_HTTP_VERSION_1_0))) {
  616. failf(data, "error setting HTTP version");
  617. result = CURLE_OUT_OF_MEMORY;
  618. goto error;
  619. }
  620. headers = hyper_request_headers(req);
  621. if(!headers) {
  622. failf(data, "hyper_request_headers");
  623. result = CURLE_OUT_OF_MEMORY;
  624. goto error;
  625. }
  626. if(host_header) {
  627. result = Curl_hyper_header(data, headers, host_header);
  628. if(result)
  629. goto error;
  630. Curl_safefree(host_header);
  631. }
  632. if(data->state.aptr.proxyuserpwd) {
  633. result = Curl_hyper_header(data, headers,
  634. data->state.aptr.proxyuserpwd);
  635. if(result)
  636. goto error;
  637. }
  638. if(!Curl_checkProxyheaders(data, conn, STRCONST("User-Agent")) &&
  639. data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) {
  640. struct dynbuf ua;
  641. Curl_dyn_init(&ua, DYN_HTTP_REQUEST);
  642. result = Curl_dyn_addf(&ua, "User-Agent: %s\r\n",
  643. data->set.str[STRING_USERAGENT]);
  644. if(result)
  645. goto error;
  646. result = Curl_hyper_header(data, headers, Curl_dyn_ptr(&ua));
  647. if(result)
  648. goto error;
  649. Curl_dyn_free(&ua);
  650. }
  651. if(!Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection"))) {
  652. result = Curl_hyper_header(data, headers,
  653. "Proxy-Connection: Keep-Alive");
  654. if(result)
  655. goto error;
  656. }
  657. result = Curl_add_custom_headers(data, TRUE, headers);
  658. if(result)
  659. goto error;
  660. result = Curl_creader_set_null(data);
  661. if(result)
  662. goto error;
  663. sendtask = hyper_clientconn_send(client, req);
  664. if(!sendtask) {
  665. failf(data, "hyper_clientconn_send");
  666. result = CURLE_OUT_OF_MEMORY;
  667. goto error;
  668. }
  669. req = NULL;
  670. if(HYPERE_OK != hyper_executor_push(h->exec, sendtask)) {
  671. failf(data, "Couldn't hyper_executor_push the send");
  672. result = CURLE_OUT_OF_MEMORY;
  673. goto error;
  674. }
  675. sendtask = NULL; /* ownership passed on */
  676. hyper_clientconn_free(client);
  677. client = NULL;
  678. error:
  679. free(host_header);
  680. free(authority);
  681. if(io)
  682. hyper_io_free(io);
  683. if(options)
  684. hyper_clientconn_options_free(options);
  685. if(handshake)
  686. hyper_task_free(handshake);
  687. if(client)
  688. hyper_clientconn_free(client);
  689. if(req)
  690. hyper_request_free(req);
  691. return result;
  692. }
  693. static CURLcode send_CONNECT(struct Curl_cfilter *cf,
  694. struct Curl_easy *data,
  695. struct h1_tunnel_state *ts,
  696. bool *done)
  697. {
  698. struct hyptransfer *h = &data->hyp;
  699. struct connectdata *conn = cf->conn;
  700. hyper_task *task = NULL;
  701. hyper_error *hypererr = NULL;
  702. CURLcode result = CURLE_OK;
  703. (void)ts;
  704. (void)conn;
  705. do {
  706. task = hyper_executor_poll(h->exec);
  707. if(task) {
  708. bool error = hyper_task_type(task) == HYPER_TASK_ERROR;
  709. if(error)
  710. hypererr = hyper_task_value(task);
  711. hyper_task_free(task);
  712. if(error) {
  713. /* this could probably use a better error code? */
  714. result = CURLE_OUT_OF_MEMORY;
  715. goto error;
  716. }
  717. }
  718. } while(task);
  719. error:
  720. *done = (result == CURLE_OK);
  721. if(hypererr) {
  722. uint8_t errbuf[256];
  723. size_t errlen = hyper_error_print(hypererr, errbuf, sizeof(errbuf));
  724. failf(data, "Hyper: %.*s", (int)errlen, errbuf);
  725. hyper_error_free(hypererr);
  726. }
  727. return result;
  728. }
  729. static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
  730. struct Curl_easy *data,
  731. struct h1_tunnel_state *ts,
  732. bool *done)
  733. {
  734. struct hyptransfer *h = &data->hyp;
  735. CURLcode result;
  736. int didwhat;
  737. (void)ts;
  738. result = Curl_hyper_stream(data, cf->conn, &didwhat,
  739. CURL_CSELECT_IN | CURL_CSELECT_OUT);
  740. *done = data->req.done;
  741. if(result || !*done)
  742. return result;
  743. if(h->exec) {
  744. hyper_executor_free(h->exec);
  745. h->exec = NULL;
  746. }
  747. if(h->read_waker) {
  748. hyper_waker_free(h->read_waker);
  749. h->read_waker = NULL;
  750. }
  751. if(h->write_waker) {
  752. hyper_waker_free(h->write_waker);
  753. h->write_waker = NULL;
  754. }
  755. return result;
  756. }
  757. #endif /* USE_HYPER */
  758. static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
  759. struct Curl_easy *data,
  760. struct h1_tunnel_state *ts)
  761. {
  762. struct connectdata *conn = cf->conn;
  763. CURLcode result;
  764. bool done;
  765. if(tunnel_is_established(ts))
  766. return CURLE_OK;
  767. if(tunnel_is_failed(ts))
  768. return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
  769. do {
  770. timediff_t check;
  771. check = Curl_timeleft(data, NULL, TRUE);
  772. if(check <= 0) {
  773. failf(data, "Proxy CONNECT aborted due to timeout");
  774. result = CURLE_OPERATION_TIMEDOUT;
  775. goto out;
  776. }
  777. switch(ts->tunnel_state) {
  778. case H1_TUNNEL_INIT:
  779. /* Prepare the CONNECT request and make a first attempt to send. */
  780. CURL_TRC_CF(data, cf, "CONNECT start");
  781. result = start_CONNECT(cf, data, ts);
  782. if(result)
  783. goto out;
  784. h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
  785. FALLTHROUGH();
  786. case H1_TUNNEL_CONNECT:
  787. /* see that the request is completely sent */
  788. CURL_TRC_CF(data, cf, "CONNECT send");
  789. result = send_CONNECT(cf, data, ts, &done);
  790. if(result || !done)
  791. goto out;
  792. h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
  793. FALLTHROUGH();
  794. case H1_TUNNEL_RECEIVE:
  795. /* read what is there */
  796. CURL_TRC_CF(data, cf, "CONNECT receive");
  797. result = recv_CONNECT_resp(cf, data, ts, &done);
  798. if(Curl_pgrsUpdate(data)) {
  799. result = CURLE_ABORTED_BY_CALLBACK;
  800. goto out;
  801. }
  802. /* error or not complete yet. return for more multi-multi */
  803. if(result || !done)
  804. goto out;
  805. /* got it */
  806. h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
  807. FALLTHROUGH();
  808. case H1_TUNNEL_RESPONSE:
  809. CURL_TRC_CF(data, cf, "CONNECT response");
  810. if(data->req.newurl) {
  811. /* not the "final" response, we need to do a follow up request.
  812. * If the other side indicated a connection close, or if someone
  813. * else told us to close this connection, do so now.
  814. */
  815. Curl_req_soft_reset(&data->req, data);
  816. if(ts->close_connection || conn->bits.close) {
  817. /* Close this filter and the sub-chain, re-connect the
  818. * sub-chain and continue. Closing this filter will
  819. * reset our tunnel state. To avoid recursion, we return
  820. * and expect to be called again.
  821. */
  822. CURL_TRC_CF(data, cf, "CONNECT need to close+open");
  823. infof(data, "Connect me again please");
  824. Curl_conn_cf_close(cf, data);
  825. connkeep(conn, "HTTP proxy CONNECT");
  826. result = Curl_conn_cf_connect(cf->next, data, FALSE, &done);
  827. goto out;
  828. }
  829. else {
  830. /* staying on this connection, reset state */
  831. h1_tunnel_go_state(cf, ts, H1_TUNNEL_INIT, data);
  832. }
  833. }
  834. break;
  835. default:
  836. break;
  837. }
  838. } while(data->req.newurl);
  839. DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
  840. if(data->info.httpproxycode/100 != 2) {
  841. /* a non-2xx response and we have no next url to try. */
  842. Curl_safefree(data->req.newurl);
  843. /* failure, close this connection to avoid reuse */
  844. streamclose(conn, "proxy CONNECT failure");
  845. h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
  846. failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
  847. return CURLE_RECV_ERROR;
  848. }
  849. /* 2xx response, SUCCESS! */
  850. h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
  851. infof(data, "CONNECT tunnel established, response %d",
  852. data->info.httpproxycode);
  853. result = CURLE_OK;
  854. out:
  855. if(result)
  856. h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
  857. return result;
  858. }
  859. static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
  860. struct Curl_easy *data,
  861. bool blocking, bool *done)
  862. {
  863. CURLcode result;
  864. struct h1_tunnel_state *ts = cf->ctx;
  865. if(cf->connected) {
  866. *done = TRUE;
  867. return CURLE_OK;
  868. }
  869. CURL_TRC_CF(data, cf, "connect");
  870. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  871. if(result || !*done)
  872. return result;
  873. *done = FALSE;
  874. if(!ts) {
  875. result = tunnel_init(cf, data, &ts);
  876. if(result)
  877. return result;
  878. cf->ctx = ts;
  879. }
  880. /* TODO: can we do blocking? */
  881. /* We want "seamless" operations through HTTP proxy tunnel */
  882. result = H1_CONNECT(cf, data, ts);
  883. if(result)
  884. goto out;
  885. Curl_safefree(data->state.aptr.proxyuserpwd);
  886. out:
  887. *done = (result == CURLE_OK) && tunnel_is_established(cf->ctx);
  888. if(*done) {
  889. cf->connected = TRUE;
  890. /* The real request will follow the CONNECT, reset request partially */
  891. Curl_req_soft_reset(&data->req, data);
  892. Curl_client_reset(data);
  893. Curl_pgrsSetUploadCounter(data, 0);
  894. Curl_pgrsSetDownloadCounter(data, 0);
  895. tunnel_free(cf, data);
  896. }
  897. return result;
  898. }
  899. static void cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
  900. struct Curl_easy *data,
  901. struct easy_pollset *ps)
  902. {
  903. struct h1_tunnel_state *ts = cf->ctx;
  904. if(!cf->connected) {
  905. /* If we are not connected, but the filter "below" is
  906. * and not waiting on something, we are tunneling. */
  907. curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
  908. if(ts) {
  909. /* when we've sent a CONNECT to a proxy, we should rather either
  910. wait for the socket to become readable to be able to get the
  911. response headers or if we're still sending the request, wait
  912. for write. */
  913. if(tunnel_want_send(ts))
  914. Curl_pollset_set_out_only(data, ps, sock);
  915. else
  916. Curl_pollset_set_in_only(data, ps, sock);
  917. }
  918. else
  919. Curl_pollset_set_out_only(data, ps, sock);
  920. }
  921. }
  922. static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
  923. struct Curl_easy *data)
  924. {
  925. CURL_TRC_CF(data, cf, "destroy");
  926. tunnel_free(cf, data);
  927. }
  928. static void cf_h1_proxy_close(struct Curl_cfilter *cf,
  929. struct Curl_easy *data)
  930. {
  931. CURL_TRC_CF(data, cf, "close");
  932. if(cf) {
  933. cf->connected = FALSE;
  934. if(cf->ctx) {
  935. h1_tunnel_go_state(cf, cf->ctx, H1_TUNNEL_INIT, data);
  936. }
  937. if(cf->next)
  938. cf->next->cft->do_close(cf->next, data);
  939. }
  940. }
  941. struct Curl_cftype Curl_cft_h1_proxy = {
  942. "H1-PROXY",
  943. CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
  944. 0,
  945. cf_h1_proxy_destroy,
  946. cf_h1_proxy_connect,
  947. cf_h1_proxy_close,
  948. Curl_cf_http_proxy_get_host,
  949. cf_h1_proxy_adjust_pollset,
  950. Curl_cf_def_data_pending,
  951. Curl_cf_def_send,
  952. Curl_cf_def_recv,
  953. Curl_cf_def_cntrl,
  954. Curl_cf_def_conn_is_alive,
  955. Curl_cf_def_conn_keep_alive,
  956. Curl_cf_def_query,
  957. };
  958. CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
  959. struct Curl_easy *data)
  960. {
  961. struct Curl_cfilter *cf;
  962. CURLcode result;
  963. (void)data;
  964. result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, NULL);
  965. if(!result)
  966. Curl_conn_cf_insert_after(cf_at, cf);
  967. return result;
  968. }
  969. #endif /* !CURL_DISABLE_PROXY && ! CURL_DISABLE_HTTP */