cf-h1-proxy.c 32 KB

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