cf-h1-proxy.c 32 KB

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