cf-h1-proxy.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 dynbuf rcvbuf;
  62. struct dynbuf request_data;
  63. size_t nsent;
  64. size_t headerlines;
  65. struct Curl_chunker ch;
  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. if(cf->conn->handler->flags & PROTOPT_NOTCPPROXY) {
  105. failf(data, "%s cannot be done over CONNECT", cf->conn->handler->scheme);
  106. return CURLE_UNSUPPORTED_PROTOCOL;
  107. }
  108. ts = calloc(1, sizeof(*ts));
  109. if(!ts)
  110. return CURLE_OUT_OF_MEMORY;
  111. infof(data, "allocate connect buffer");
  112. Curl_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
  113. Curl_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
  114. Curl_httpchunk_init(data, &ts->ch, TRUE);
  115. *pts = ts;
  116. connkeep(cf->conn, "HTTP proxy CONNECT");
  117. return tunnel_reinit(cf, data, ts);
  118. }
  119. static void h1_tunnel_go_state(struct Curl_cfilter *cf,
  120. struct h1_tunnel_state *ts,
  121. h1_tunnel_state new_state,
  122. struct Curl_easy *data)
  123. {
  124. if(ts->tunnel_state == new_state)
  125. return;
  126. /* entering this one */
  127. switch(new_state) {
  128. case H1_TUNNEL_INIT:
  129. CURL_TRC_CF(data, cf, "new tunnel state 'init'");
  130. tunnel_reinit(cf, data, ts);
  131. break;
  132. case H1_TUNNEL_CONNECT:
  133. CURL_TRC_CF(data, cf, "new tunnel state 'connect'");
  134. ts->tunnel_state = H1_TUNNEL_CONNECT;
  135. ts->keepon = KEEPON_CONNECT;
  136. Curl_dyn_reset(&ts->rcvbuf);
  137. break;
  138. case H1_TUNNEL_RECEIVE:
  139. CURL_TRC_CF(data, cf, "new tunnel state 'receive'");
  140. ts->tunnel_state = H1_TUNNEL_RECEIVE;
  141. break;
  142. case H1_TUNNEL_RESPONSE:
  143. CURL_TRC_CF(data, cf, "new tunnel state 'response'");
  144. ts->tunnel_state = H1_TUNNEL_RESPONSE;
  145. break;
  146. case H1_TUNNEL_ESTABLISHED:
  147. CURL_TRC_CF(data, cf, "new tunnel state 'established'");
  148. infof(data, "CONNECT phase completed");
  149. data->state.authproxy.done = TRUE;
  150. data->state.authproxy.multipass = FALSE;
  151. FALLTHROUGH();
  152. case H1_TUNNEL_FAILED:
  153. if(new_state == H1_TUNNEL_FAILED)
  154. CURL_TRC_CF(data, cf, "new tunnel state 'failed'");
  155. ts->tunnel_state = new_state;
  156. Curl_dyn_reset(&ts->rcvbuf);
  157. Curl_dyn_reset(&ts->request_data);
  158. /* restore the protocol pointer */
  159. data->info.httpcode = 0; /* clear it as it might've been used for the
  160. proxy */
  161. /* If a proxy-authorization header was used for the proxy, then we should
  162. make sure that it is not accidentally used for the document request
  163. after we have connected. So let's free and clear it here. */
  164. Curl_safefree(data->state.aptr.proxyuserpwd);
  165. #ifdef USE_HYPER
  166. data->state.hconnect = FALSE;
  167. #endif
  168. break;
  169. }
  170. }
  171. static void tunnel_free(struct Curl_cfilter *cf,
  172. struct Curl_easy *data)
  173. {
  174. if(cf) {
  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. }
  186. static bool tunnel_want_send(struct h1_tunnel_state *ts)
  187. {
  188. return (ts->tunnel_state == H1_TUNNEL_CONNECT);
  189. }
  190. #ifndef USE_HYPER
  191. static CURLcode start_CONNECT(struct Curl_cfilter *cf,
  192. struct Curl_easy *data,
  193. struct h1_tunnel_state *ts)
  194. {
  195. struct httpreq *req = NULL;
  196. int http_minor;
  197. CURLcode result;
  198. /* This only happens if we have looped here due to authentication
  199. reasons, and we do not really use the newly cloned URL here
  200. then. Just free() it. */
  201. Curl_safefree(data->req.newurl);
  202. result = Curl_http_proxy_create_CONNECT(&req, cf, data, 1);
  203. if(result)
  204. goto out;
  205. infof(data, "Establish HTTP proxy tunnel to %s", req->authority);
  206. Curl_dyn_reset(&ts->request_data);
  207. ts->nsent = 0;
  208. ts->headerlines = 0;
  209. http_minor = (cf->conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ? 0 : 1;
  210. result = Curl_h1_req_write_head(req, http_minor, &ts->request_data);
  211. if(!result)
  212. result = Curl_creader_set_null(data);
  213. out:
  214. if(result)
  215. failf(data, "Failed sending CONNECT to proxy");
  216. if(req)
  217. Curl_http_req_free(req);
  218. return result;
  219. }
  220. static CURLcode send_CONNECT(struct Curl_cfilter *cf,
  221. struct Curl_easy *data,
  222. struct h1_tunnel_state *ts,
  223. bool *done)
  224. {
  225. char *buf = Curl_dyn_ptr(&ts->request_data);
  226. size_t request_len = Curl_dyn_len(&ts->request_data);
  227. size_t blen = request_len;
  228. CURLcode result = CURLE_OK;
  229. ssize_t nwritten;
  230. if(blen <= ts->nsent)
  231. goto out; /* we are done */
  232. blen -= ts->nsent;
  233. buf += ts->nsent;
  234. nwritten = cf->next->cft->do_send(cf->next, data, buf, blen, FALSE, &result);
  235. if(nwritten < 0) {
  236. if(result == CURLE_AGAIN) {
  237. result = CURLE_OK;
  238. }
  239. goto out;
  240. }
  241. DEBUGASSERT(blen >= (size_t)nwritten);
  242. ts->nsent += (size_t)nwritten;
  243. Curl_debug(data, CURLINFO_HEADER_OUT, buf, (size_t)nwritten);
  244. out:
  245. if(result)
  246. failf(data, "Failed sending CONNECT to proxy");
  247. *done = (!result && (ts->nsent >= request_len));
  248. return result;
  249. }
  250. static CURLcode on_resp_header(struct Curl_cfilter *cf,
  251. struct Curl_easy *data,
  252. struct h1_tunnel_state *ts,
  253. const char *header)
  254. {
  255. CURLcode result = CURLE_OK;
  256. struct SingleRequest *k = &data->req;
  257. (void)cf;
  258. if((checkprefix("WWW-Authenticate:", header) &&
  259. (401 == k->httpcode)) ||
  260. (checkprefix("Proxy-authenticate:", header) &&
  261. (407 == k->httpcode))) {
  262. bool proxy = (k->httpcode == 407);
  263. char *auth = Curl_copy_header_value(header);
  264. if(!auth)
  265. return CURLE_OUT_OF_MEMORY;
  266. CURL_TRC_CF(data, cf, "CONNECT: fwd auth header '%s'", header);
  267. result = Curl_http_input_auth(data, proxy, auth);
  268. free(auth);
  269. if(result)
  270. return result;
  271. }
  272. else if(checkprefix("Content-Length:", header)) {
  273. if(k->httpcode/100 == 2) {
  274. /* A client MUST ignore any Content-Length or Transfer-Encoding
  275. header fields received in a successful response to CONNECT.
  276. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  277. infof(data, "Ignoring Content-Length in CONNECT %03d response",
  278. k->httpcode);
  279. }
  280. else {
  281. (void)curlx_strtoofft(header + strlen("Content-Length:"),
  282. NULL, 10, &ts->cl);
  283. }
  284. }
  285. else if(Curl_compareheader(header,
  286. STRCONST("Connection:"), STRCONST("close")))
  287. ts->close_connection = TRUE;
  288. else if(checkprefix("Transfer-Encoding:", header)) {
  289. if(k->httpcode/100 == 2) {
  290. /* A client MUST ignore any Content-Length or Transfer-Encoding
  291. header fields received in a successful response to CONNECT.
  292. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  293. infof(data, "Ignoring Transfer-Encoding in "
  294. "CONNECT %03d response", k->httpcode);
  295. }
  296. else if(Curl_compareheader(header,
  297. STRCONST("Transfer-Encoding:"),
  298. STRCONST("chunked"))) {
  299. infof(data, "CONNECT responded chunked");
  300. ts->chunked_encoding = TRUE;
  301. /* reset our chunky engine */
  302. Curl_httpchunk_reset(data, &ts->ch, TRUE);
  303. }
  304. }
  305. else if(Curl_compareheader(header,
  306. STRCONST("Proxy-Connection:"),
  307. STRCONST("close")))
  308. ts->close_connection = TRUE;
  309. else if(!strncmp(header, "HTTP/1.", 7) &&
  310. ((header[7] == '0') || (header[7] == '1')) &&
  311. (header[8] == ' ') &&
  312. ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
  313. !ISDIGIT(header[12])) {
  314. /* store the HTTP code from the proxy */
  315. data->info.httpproxycode = k->httpcode = (header[9] - '0') * 100 +
  316. (header[10] - '0') * 10 + (header[11] - '0');
  317. }
  318. return result;
  319. }
  320. static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
  321. struct Curl_easy *data,
  322. struct h1_tunnel_state *ts,
  323. bool *done)
  324. {
  325. CURLcode result = CURLE_OK;
  326. struct SingleRequest *k = &data->req;
  327. char *linep;
  328. size_t line_len;
  329. int error, writetype;
  330. #define SELECT_OK 0
  331. #define SELECT_ERROR 1
  332. error = SELECT_OK;
  333. *done = FALSE;
  334. if(!Curl_conn_data_pending(data, cf->sockindex))
  335. return CURLE_OK;
  336. while(ts->keepon) {
  337. ssize_t nread;
  338. char byte;
  339. /* Read one byte at a time to avoid a race condition. Wait at most one
  340. second before looping to ensure continuous pgrsUpdates. */
  341. result = Curl_conn_recv(data, cf->sockindex, &byte, 1, &nread);
  342. if(result == CURLE_AGAIN)
  343. /* socket buffer drained, return */
  344. return CURLE_OK;
  345. if(Curl_pgrsUpdate(data))
  346. return CURLE_ABORTED_BY_CALLBACK;
  347. if(result) {
  348. ts->keepon = KEEPON_DONE;
  349. break;
  350. }
  351. if(nread <= 0) {
  352. if(data->set.proxyauth && data->state.authproxy.avail &&
  353. data->state.aptr.proxyuserpwd) {
  354. /* proxy auth was requested and there was proxy auth available,
  355. then deem this as "mere" proxy disconnect */
  356. ts->close_connection = TRUE;
  357. infof(data, "Proxy CONNECT connection closed");
  358. }
  359. else {
  360. error = SELECT_ERROR;
  361. failf(data, "Proxy CONNECT aborted");
  362. }
  363. ts->keepon = KEEPON_DONE;
  364. break;
  365. }
  366. if(ts->keepon == KEEPON_IGNORE) {
  367. /* This means we are currently ignoring a response-body */
  368. if(ts->cl) {
  369. /* A Content-Length based body: simply count down the counter
  370. and make sure to break out of the loop when we are done! */
  371. ts->cl--;
  372. if(ts->cl <= 0) {
  373. ts->keepon = KEEPON_DONE;
  374. break;
  375. }
  376. }
  377. else if(ts->chunked_encoding) {
  378. /* chunked-encoded body, so we need to do the chunked dance
  379. properly to know when the end of the body is reached */
  380. size_t consumed = 0;
  381. /* now parse the chunked piece of data so that we can
  382. properly tell when the stream ends */
  383. result = Curl_httpchunk_read(data, &ts->ch, &byte, 1, &consumed);
  384. if(result)
  385. return result;
  386. if(Curl_httpchunk_is_done(data, &ts->ch)) {
  387. /* we are done reading chunks! */
  388. infof(data, "chunk reading DONE");
  389. ts->keepon = KEEPON_DONE;
  390. }
  391. }
  392. continue;
  393. }
  394. if(Curl_dyn_addn(&ts->rcvbuf, &byte, 1)) {
  395. failf(data, "CONNECT response too large");
  396. return CURLE_RECV_ERROR;
  397. }
  398. /* if this is not the end of a header line then continue */
  399. if(byte != 0x0a)
  400. continue;
  401. ts->headerlines++;
  402. linep = Curl_dyn_ptr(&ts->rcvbuf);
  403. line_len = Curl_dyn_len(&ts->rcvbuf); /* amount of bytes in this line */
  404. /* output debug if that is requested */
  405. Curl_debug(data, CURLINFO_HEADER_IN, linep, line_len);
  406. /* send the header to the callback */
  407. writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
  408. (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
  409. result = Curl_client_write(data, writetype, linep, line_len);
  410. if(result)
  411. return result;
  412. result = Curl_bump_headersize(data, line_len, TRUE);
  413. if(result)
  414. return result;
  415. /* Newlines are CRLF, so the CR is ignored as the line is not
  416. really terminated until the LF comes. Treat a following CR
  417. as end-of-headers as well.*/
  418. if(('\r' == linep[0]) ||
  419. ('\n' == linep[0])) {
  420. /* end of response-headers from the proxy */
  421. if((407 == k->httpcode) && !data->state.authproblem) {
  422. /* If we get a 407 response code with content length
  423. when we have no auth problem, we must ignore the
  424. whole response-body */
  425. ts->keepon = KEEPON_IGNORE;
  426. if(ts->cl) {
  427. infof(data, "Ignore %" FMT_OFF_T " 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. cannot 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 is 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,
  478. ipv6_ip ? "]" : "", 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 have looped here due to authentication
  582. reasons, and we do not 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 have 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 are 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. if(cf) {
  931. cf->connected = FALSE;
  932. if(cf->ctx) {
  933. h1_tunnel_go_state(cf, cf->ctx, H1_TUNNEL_INIT, data);
  934. }
  935. if(cf->next)
  936. cf->next->cft->do_close(cf->next, data);
  937. }
  938. }
  939. struct Curl_cftype Curl_cft_h1_proxy = {
  940. "H1-PROXY",
  941. CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
  942. 0,
  943. cf_h1_proxy_destroy,
  944. cf_h1_proxy_connect,
  945. cf_h1_proxy_close,
  946. Curl_cf_def_shutdown,
  947. Curl_cf_http_proxy_get_host,
  948. cf_h1_proxy_adjust_pollset,
  949. Curl_cf_def_data_pending,
  950. Curl_cf_def_send,
  951. Curl_cf_def_recv,
  952. Curl_cf_def_cntrl,
  953. Curl_cf_def_conn_is_alive,
  954. Curl_cf_def_conn_keep_alive,
  955. Curl_cf_def_query,
  956. };
  957. CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
  958. struct Curl_easy *data)
  959. {
  960. struct Curl_cfilter *cf;
  961. CURLcode result;
  962. (void)data;
  963. result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, NULL);
  964. if(!result)
  965. Curl_conn_cf_insert_after(cf_at, cf);
  966. return result;
  967. }
  968. #endif /* !CURL_DISABLE_PROXY && ! CURL_DISABLE_HTTP */