cf-h1-proxy.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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, writetype;
  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 nread;
  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, &nread);
  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(nread <= 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. size_t consumed = 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, &consumed, &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. /* send the header to the callback */
  411. writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
  412. (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
  413. result = Curl_client_write(data, writetype, linep, perline);
  414. if(result)
  415. return result;
  416. result = Curl_bump_headersize(data, perline, TRUE);
  417. if(result)
  418. return result;
  419. /* Newlines are CRLF, so the CR is ignored as the line isn't
  420. really terminated until the LF comes. Treat a following CR
  421. as end-of-headers as well.*/
  422. if(('\r' == linep[0]) ||
  423. ('\n' == linep[0])) {
  424. /* end of response-headers from the proxy */
  425. if((407 == k->httpcode) && !data->state.authproblem) {
  426. /* If we get a 407 response code with content length
  427. when we have no auth problem, we must ignore the
  428. whole response-body */
  429. ts->keepon = KEEPON_IGNORE;
  430. if(ts->cl) {
  431. infof(data, "Ignore %" CURL_FORMAT_CURL_OFF_T
  432. " bytes of response-body", ts->cl);
  433. }
  434. else if(ts->chunked_encoding) {
  435. CHUNKcode r;
  436. CURLcode extra;
  437. size_t consumed = 0;
  438. infof(data, "Ignore chunked response-body");
  439. /* We set ignorebody true here since the chunked decoder
  440. function will acknowledge that. Pay attention so that this is
  441. cleared again when this function returns! */
  442. k->ignorebody = TRUE;
  443. if(linep[1] == '\n')
  444. /* this can only be a LF if the letter at index 0 was a CR */
  445. linep++;
  446. /* now parse the chunked piece of data so that we can properly
  447. tell when the stream ends */
  448. r = Curl_httpchunk_read(data, linep + 1, 1, &consumed, &extra);
  449. if(r == CHUNKE_STOP) {
  450. /* we're done reading chunks! */
  451. infof(data, "chunk reading DONE");
  452. ts->keepon = KEEPON_DONE;
  453. }
  454. }
  455. else {
  456. /* without content-length or chunked encoding, we
  457. can't keep the connection alive since the close is
  458. the end signal so we bail out at once instead */
  459. CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
  460. ts->keepon = KEEPON_DONE;
  461. }
  462. }
  463. else {
  464. ts->keepon = KEEPON_DONE;
  465. }
  466. DEBUGASSERT(ts->keepon == KEEPON_IGNORE
  467. || ts->keepon == KEEPON_DONE);
  468. continue;
  469. }
  470. result = on_resp_header(cf, data, ts, linep);
  471. if(result)
  472. return result;
  473. Curl_dyn_reset(&ts->rcvbuf);
  474. } /* while there's buffer left and loop is requested */
  475. if(error)
  476. result = CURLE_RECV_ERROR;
  477. *done = (ts->keepon == KEEPON_DONE);
  478. if(!result && *done && data->info.httpproxycode/100 != 2) {
  479. /* Deal with the possibly already received authenticate
  480. headers. 'newurl' is set to a new URL if we must loop. */
  481. result = Curl_http_auth_act(data);
  482. }
  483. return result;
  484. }
  485. #else /* USE_HYPER */
  486. static CURLcode CONNECT_host(struct Curl_cfilter *cf,
  487. struct Curl_easy *data,
  488. char **pauthority,
  489. char **phost_header)
  490. {
  491. const char *hostname;
  492. int port;
  493. bool ipv6_ip;
  494. CURLcode result;
  495. char *authority; /* for CONNECT, the destination host + port */
  496. char *host_header = NULL; /* Host: authority */
  497. result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
  498. if(result)
  499. return result;
  500. authority = aprintf("%s%s%s:%d", ipv6_ip?"[":"", hostname, ipv6_ip?"]":"",
  501. port);
  502. if(!authority)
  503. return CURLE_OUT_OF_MEMORY;
  504. /* If user is not overriding the Host header later */
  505. if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
  506. host_header = aprintf("Host: %s\r\n", authority);
  507. if(!host_header) {
  508. free(authority);
  509. return CURLE_OUT_OF_MEMORY;
  510. }
  511. }
  512. *pauthority = authority;
  513. *phost_header = host_header;
  514. return CURLE_OK;
  515. }
  516. /* The Hyper version of CONNECT */
  517. static CURLcode start_CONNECT(struct Curl_cfilter *cf,
  518. struct Curl_easy *data,
  519. struct h1_tunnel_state *ts)
  520. {
  521. struct connectdata *conn = cf->conn;
  522. struct hyptransfer *h = &data->hyp;
  523. curl_socket_t tunnelsocket = Curl_conn_cf_get_socket(cf, data);
  524. hyper_io *io = NULL;
  525. hyper_request *req = NULL;
  526. hyper_headers *headers = NULL;
  527. hyper_clientconn_options *options = NULL;
  528. hyper_task *handshake = NULL;
  529. hyper_task *task = NULL; /* for the handshake */
  530. hyper_clientconn *client = NULL;
  531. hyper_task *sendtask = NULL; /* for the send */
  532. char *authority = NULL; /* for CONNECT */
  533. char *host_header = NULL; /* Host: */
  534. CURLcode result = CURLE_OUT_OF_MEMORY;
  535. (void)ts;
  536. io = hyper_io_new();
  537. if(!io) {
  538. failf(data, "Couldn't create hyper IO");
  539. result = CURLE_OUT_OF_MEMORY;
  540. goto error;
  541. }
  542. /* tell Hyper how to read/write network data */
  543. hyper_io_set_userdata(io, data);
  544. hyper_io_set_read(io, Curl_hyper_recv);
  545. hyper_io_set_write(io, Curl_hyper_send);
  546. conn->sockfd = tunnelsocket;
  547. data->state.hconnect = TRUE;
  548. /* create an executor to poll futures */
  549. if(!h->exec) {
  550. h->exec = hyper_executor_new();
  551. if(!h->exec) {
  552. failf(data, "Couldn't create hyper executor");
  553. result = CURLE_OUT_OF_MEMORY;
  554. goto error;
  555. }
  556. }
  557. options = hyper_clientconn_options_new();
  558. if(!options) {
  559. failf(data, "Couldn't create hyper client options");
  560. result = CURLE_OUT_OF_MEMORY;
  561. goto error;
  562. }
  563. hyper_clientconn_options_set_preserve_header_case(options, 1);
  564. hyper_clientconn_options_set_preserve_header_order(options, 1);
  565. hyper_clientconn_options_exec(options, h->exec);
  566. /* "Both the `io` and the `options` are consumed in this function
  567. call" */
  568. handshake = hyper_clientconn_handshake(io, options);
  569. if(!handshake) {
  570. failf(data, "Couldn't create hyper client handshake");
  571. result = CURLE_OUT_OF_MEMORY;
  572. goto error;
  573. }
  574. io = NULL;
  575. options = NULL;
  576. if(HYPERE_OK != hyper_executor_push(h->exec, handshake)) {
  577. failf(data, "Couldn't hyper_executor_push the handshake");
  578. result = CURLE_OUT_OF_MEMORY;
  579. goto error;
  580. }
  581. handshake = NULL; /* ownership passed on */
  582. task = hyper_executor_poll(h->exec);
  583. if(!task) {
  584. failf(data, "Couldn't hyper_executor_poll the handshake");
  585. result = CURLE_OUT_OF_MEMORY;
  586. goto error;
  587. }
  588. client = hyper_task_value(task);
  589. hyper_task_free(task);
  590. req = hyper_request_new();
  591. if(!req) {
  592. failf(data, "Couldn't hyper_request_new");
  593. result = CURLE_OUT_OF_MEMORY;
  594. goto error;
  595. }
  596. if(hyper_request_set_method(req, (uint8_t *)"CONNECT",
  597. strlen("CONNECT"))) {
  598. failf(data, "error setting method");
  599. result = CURLE_OUT_OF_MEMORY;
  600. goto error;
  601. }
  602. /* This only happens if we've looped here due to authentication
  603. reasons, and we don't really use the newly cloned URL here
  604. then. Just free() it. */
  605. Curl_safefree(data->req.newurl);
  606. result = CONNECT_host(cf, data, &authority, &host_header);
  607. if(result)
  608. goto error;
  609. infof(data, "Establish HTTP proxy tunnel to %s", authority);
  610. if(hyper_request_set_uri(req, (uint8_t *)authority,
  611. strlen(authority))) {
  612. failf(data, "error setting path");
  613. result = CURLE_OUT_OF_MEMORY;
  614. goto error;
  615. }
  616. if(data->set.verbose) {
  617. char *se = aprintf("CONNECT %s HTTP/1.1\r\n", authority);
  618. if(!se) {
  619. result = CURLE_OUT_OF_MEMORY;
  620. goto error;
  621. }
  622. Curl_debug(data, CURLINFO_HEADER_OUT, se, strlen(se));
  623. free(se);
  624. }
  625. /* Setup the proxy-authorization header, if any */
  626. result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
  627. authority, TRUE);
  628. if(result)
  629. goto error;
  630. Curl_safefree(authority);
  631. /* default is 1.1 */
  632. if((conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) &&
  633. (HYPERE_OK != hyper_request_set_version(req,
  634. HYPER_HTTP_VERSION_1_0))) {
  635. failf(data, "error setting HTTP version");
  636. result = CURLE_OUT_OF_MEMORY;
  637. goto error;
  638. }
  639. headers = hyper_request_headers(req);
  640. if(!headers) {
  641. failf(data, "hyper_request_headers");
  642. result = CURLE_OUT_OF_MEMORY;
  643. goto error;
  644. }
  645. if(host_header) {
  646. result = Curl_hyper_header(data, headers, host_header);
  647. if(result)
  648. goto error;
  649. Curl_safefree(host_header);
  650. }
  651. if(data->state.aptr.proxyuserpwd) {
  652. result = Curl_hyper_header(data, headers,
  653. data->state.aptr.proxyuserpwd);
  654. if(result)
  655. goto error;
  656. }
  657. if(!Curl_checkProxyheaders(data, conn, STRCONST("User-Agent")) &&
  658. data->set.str[STRING_USERAGENT]) {
  659. struct dynbuf ua;
  660. Curl_dyn_init(&ua, DYN_HTTP_REQUEST);
  661. result = Curl_dyn_addf(&ua, "User-Agent: %s\r\n",
  662. data->set.str[STRING_USERAGENT]);
  663. if(result)
  664. goto error;
  665. result = Curl_hyper_header(data, headers, Curl_dyn_ptr(&ua));
  666. if(result)
  667. goto error;
  668. Curl_dyn_free(&ua);
  669. }
  670. if(!Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection"))) {
  671. result = Curl_hyper_header(data, headers,
  672. "Proxy-Connection: Keep-Alive");
  673. if(result)
  674. goto error;
  675. }
  676. result = Curl_add_custom_headers(data, TRUE, headers);
  677. if(result)
  678. goto error;
  679. sendtask = hyper_clientconn_send(client, req);
  680. if(!sendtask) {
  681. failf(data, "hyper_clientconn_send");
  682. result = CURLE_OUT_OF_MEMORY;
  683. goto error;
  684. }
  685. req = NULL;
  686. if(HYPERE_OK != hyper_executor_push(h->exec, sendtask)) {
  687. failf(data, "Couldn't hyper_executor_push the send");
  688. result = CURLE_OUT_OF_MEMORY;
  689. goto error;
  690. }
  691. sendtask = NULL; /* ownership passed on */
  692. hyper_clientconn_free(client);
  693. client = NULL;
  694. error:
  695. free(host_header);
  696. free(authority);
  697. if(io)
  698. hyper_io_free(io);
  699. if(options)
  700. hyper_clientconn_options_free(options);
  701. if(handshake)
  702. hyper_task_free(handshake);
  703. if(client)
  704. hyper_clientconn_free(client);
  705. if(req)
  706. hyper_request_free(req);
  707. return result;
  708. }
  709. static CURLcode send_CONNECT(struct Curl_cfilter *cf,
  710. struct Curl_easy *data,
  711. struct h1_tunnel_state *ts,
  712. bool *done)
  713. {
  714. struct hyptransfer *h = &data->hyp;
  715. struct connectdata *conn = cf->conn;
  716. hyper_task *task = NULL;
  717. hyper_error *hypererr = NULL;
  718. CURLcode result = CURLE_OK;
  719. (void)ts;
  720. (void)conn;
  721. do {
  722. task = hyper_executor_poll(h->exec);
  723. if(task) {
  724. bool error = hyper_task_type(task) == HYPER_TASK_ERROR;
  725. if(error)
  726. hypererr = hyper_task_value(task);
  727. hyper_task_free(task);
  728. if(error) {
  729. /* this could probably use a better error code? */
  730. result = CURLE_OUT_OF_MEMORY;
  731. goto error;
  732. }
  733. }
  734. } while(task);
  735. error:
  736. *done = (result == CURLE_OK);
  737. if(hypererr) {
  738. uint8_t errbuf[256];
  739. size_t errlen = hyper_error_print(hypererr, errbuf, sizeof(errbuf));
  740. failf(data, "Hyper: %.*s", (int)errlen, errbuf);
  741. hyper_error_free(hypererr);
  742. }
  743. return result;
  744. }
  745. static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
  746. struct Curl_easy *data,
  747. struct h1_tunnel_state *ts,
  748. bool *done)
  749. {
  750. struct hyptransfer *h = &data->hyp;
  751. CURLcode result;
  752. int didwhat;
  753. (void)ts;
  754. *done = FALSE;
  755. result = Curl_hyper_stream(data, cf->conn, &didwhat, done,
  756. CURL_CSELECT_IN | CURL_CSELECT_OUT);
  757. if(result || !*done)
  758. return result;
  759. if(h->exec) {
  760. hyper_executor_free(h->exec);
  761. h->exec = NULL;
  762. }
  763. if(h->read_waker) {
  764. hyper_waker_free(h->read_waker);
  765. h->read_waker = NULL;
  766. }
  767. if(h->write_waker) {
  768. hyper_waker_free(h->write_waker);
  769. h->write_waker = NULL;
  770. }
  771. return result;
  772. }
  773. #endif /* USE_HYPER */
  774. static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
  775. struct Curl_easy *data,
  776. struct h1_tunnel_state *ts)
  777. {
  778. struct connectdata *conn = cf->conn;
  779. CURLcode result;
  780. bool done;
  781. if(tunnel_is_established(ts))
  782. return CURLE_OK;
  783. if(tunnel_is_failed(ts))
  784. return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
  785. do {
  786. timediff_t check;
  787. check = Curl_timeleft(data, NULL, TRUE);
  788. if(check <= 0) {
  789. failf(data, "Proxy CONNECT aborted due to timeout");
  790. result = CURLE_OPERATION_TIMEDOUT;
  791. goto out;
  792. }
  793. switch(ts->tunnel_state) {
  794. case H1_TUNNEL_INIT:
  795. /* Prepare the CONNECT request and make a first attempt to send. */
  796. CURL_TRC_CF(data, cf, "CONNECT start");
  797. result = start_CONNECT(cf, data, ts);
  798. if(result)
  799. goto out;
  800. h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
  801. FALLTHROUGH();
  802. case H1_TUNNEL_CONNECT:
  803. /* see that the request is completely sent */
  804. CURL_TRC_CF(data, cf, "CONNECT send");
  805. result = send_CONNECT(cf, data, ts, &done);
  806. if(result || !done)
  807. goto out;
  808. h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
  809. FALLTHROUGH();
  810. case H1_TUNNEL_RECEIVE:
  811. /* read what is there */
  812. CURL_TRC_CF(data, cf, "CONNECT receive");
  813. result = recv_CONNECT_resp(cf, data, ts, &done);
  814. if(Curl_pgrsUpdate(data)) {
  815. result = CURLE_ABORTED_BY_CALLBACK;
  816. goto out;
  817. }
  818. /* error or not complete yet. return for more multi-multi */
  819. if(result || !done)
  820. goto out;
  821. /* got it */
  822. h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
  823. FALLTHROUGH();
  824. case H1_TUNNEL_RESPONSE:
  825. CURL_TRC_CF(data, cf, "CONNECT response");
  826. if(data->req.newurl) {
  827. /* not the "final" response, we need to do a follow up request.
  828. * If the other side indicated a connection close, or if someone
  829. * else told us to close this connection, do so now.
  830. */
  831. if(ts->close_connection || conn->bits.close) {
  832. /* Close this filter and the sub-chain, re-connect the
  833. * sub-chain and continue. Closing this filter will
  834. * reset our tunnel state. To avoid recursion, we return
  835. * and expect to be called again.
  836. */
  837. CURL_TRC_CF(data, cf, "CONNECT need to close+open");
  838. infof(data, "Connect me again please");
  839. Curl_conn_cf_close(cf, data);
  840. connkeep(conn, "HTTP proxy CONNECT");
  841. result = Curl_conn_cf_connect(cf->next, data, FALSE, &done);
  842. goto out;
  843. }
  844. else {
  845. /* staying on this connection, reset state */
  846. h1_tunnel_go_state(cf, ts, H1_TUNNEL_INIT, data);
  847. }
  848. }
  849. break;
  850. default:
  851. break;
  852. }
  853. } while(data->req.newurl);
  854. DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
  855. if(data->info.httpproxycode/100 != 2) {
  856. /* a non-2xx response and we have no next url to try. */
  857. Curl_safefree(data->req.newurl);
  858. /* failure, close this connection to avoid reuse */
  859. streamclose(conn, "proxy CONNECT failure");
  860. h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
  861. failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
  862. return CURLE_RECV_ERROR;
  863. }
  864. /* 2xx response, SUCCESS! */
  865. h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
  866. infof(data, "CONNECT tunnel established, response %d",
  867. data->info.httpproxycode);
  868. result = CURLE_OK;
  869. out:
  870. if(result)
  871. h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
  872. return result;
  873. }
  874. static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
  875. struct Curl_easy *data,
  876. bool blocking, bool *done)
  877. {
  878. CURLcode result;
  879. struct h1_tunnel_state *ts = cf->ctx;
  880. if(cf->connected) {
  881. *done = TRUE;
  882. return CURLE_OK;
  883. }
  884. CURL_TRC_CF(data, cf, "connect");
  885. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  886. if(result || !*done)
  887. return result;
  888. *done = FALSE;
  889. if(!ts) {
  890. result = tunnel_init(cf, data, &ts);
  891. if(result)
  892. return result;
  893. cf->ctx = ts;
  894. }
  895. /* TODO: can we do blocking? */
  896. /* We want "seamless" operations through HTTP proxy tunnel */
  897. result = H1_CONNECT(cf, data, ts);
  898. if(result)
  899. goto out;
  900. Curl_safefree(data->state.aptr.proxyuserpwd);
  901. out:
  902. *done = (result == CURLE_OK) && tunnel_is_established(cf->ctx);
  903. if(*done) {
  904. cf->connected = TRUE;
  905. tunnel_free(cf, data);
  906. }
  907. return result;
  908. }
  909. static void cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
  910. struct Curl_easy *data,
  911. struct easy_pollset *ps)
  912. {
  913. struct h1_tunnel_state *ts = cf->ctx;
  914. if(!cf->connected) {
  915. /* If we are not connected, but the filter "below" is
  916. * and not waiting on something, we are tunneling. */
  917. curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
  918. if(ts) {
  919. /* when we've sent a CONNECT to a proxy, we should rather either
  920. wait for the socket to become readable to be able to get the
  921. response headers or if we're still sending the request, wait
  922. for write. */
  923. if(ts->CONNECT.sending == HTTPSEND_REQUEST)
  924. Curl_pollset_set_out_only(data, ps, sock);
  925. else
  926. Curl_pollset_set_in_only(data, ps, sock);
  927. }
  928. else
  929. Curl_pollset_set_out_only(data, ps, sock);
  930. }
  931. }
  932. static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
  933. struct Curl_easy *data)
  934. {
  935. CURL_TRC_CF(data, cf, "destroy");
  936. tunnel_free(cf, data);
  937. }
  938. static void cf_h1_proxy_close(struct Curl_cfilter *cf,
  939. struct Curl_easy *data)
  940. {
  941. CURL_TRC_CF(data, cf, "close");
  942. cf->connected = FALSE;
  943. if(cf->ctx) {
  944. h1_tunnel_go_state(cf, cf->ctx, H1_TUNNEL_INIT, data);
  945. }
  946. if(cf->next)
  947. cf->next->cft->do_close(cf->next, data);
  948. }
  949. struct Curl_cftype Curl_cft_h1_proxy = {
  950. "H1-PROXY",
  951. CF_TYPE_IP_CONNECT,
  952. 0,
  953. cf_h1_proxy_destroy,
  954. cf_h1_proxy_connect,
  955. cf_h1_proxy_close,
  956. Curl_cf_http_proxy_get_host,
  957. cf_h1_proxy_adjust_pollset,
  958. Curl_cf_def_data_pending,
  959. Curl_cf_def_send,
  960. Curl_cf_def_recv,
  961. Curl_cf_def_cntrl,
  962. Curl_cf_def_conn_is_alive,
  963. Curl_cf_def_conn_keep_alive,
  964. Curl_cf_def_query,
  965. };
  966. CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
  967. struct Curl_easy *data)
  968. {
  969. struct Curl_cfilter *cf;
  970. CURLcode result;
  971. (void)data;
  972. result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, NULL);
  973. if(!result)
  974. Curl_conn_cf_insert_after(cf_at, cf);
  975. return result;
  976. }
  977. #endif /* !CURL_DISABLE_PROXY && ! CURL_DISABLE_HTTP */