2
0

cf-h1-proxy.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. #include "urldata.h"
  28. #include "dynbuf.h"
  29. #include "sendf.h"
  30. #include "http.h"
  31. #include "http1.h"
  32. #include "http_proxy.h"
  33. #include "url.h"
  34. #include "select.h"
  35. #include "progress.h"
  36. #include "cfilters.h"
  37. #include "cf-h1-proxy.h"
  38. #include "connect.h"
  39. #include "curl_trc.h"
  40. #include "curlx.h"
  41. #include "vtls/vtls.h"
  42. #include "transfer.h"
  43. #include "multiif.h"
  44. /* The last 3 #include files should be in this order */
  45. #include "curl_printf.h"
  46. #include "curl_memory.h"
  47. #include "memdebug.h"
  48. typedef enum {
  49. H1_TUNNEL_INIT, /* init/default/no tunnel state */
  50. H1_TUNNEL_CONNECT, /* CONNECT request is being send */
  51. H1_TUNNEL_RECEIVE, /* CONNECT answer is being received */
  52. H1_TUNNEL_RESPONSE, /* CONNECT response received completely */
  53. H1_TUNNEL_ESTABLISHED,
  54. H1_TUNNEL_FAILED
  55. } h1_tunnel_state;
  56. /* struct for HTTP CONNECT tunneling */
  57. struct h1_tunnel_state {
  58. struct dynbuf rcvbuf;
  59. struct dynbuf request_data;
  60. size_t nsent;
  61. size_t headerlines;
  62. struct Curl_chunker ch;
  63. enum keeponval {
  64. KEEPON_DONE,
  65. KEEPON_CONNECT,
  66. KEEPON_IGNORE
  67. } keepon;
  68. curl_off_t cl; /* size of content to read and ignore */
  69. h1_tunnel_state tunnel_state;
  70. BIT(chunked_encoding);
  71. BIT(close_connection);
  72. };
  73. static bool tunnel_is_established(struct h1_tunnel_state *ts)
  74. {
  75. return ts && (ts->tunnel_state == H1_TUNNEL_ESTABLISHED);
  76. }
  77. static bool tunnel_is_failed(struct h1_tunnel_state *ts)
  78. {
  79. return ts && (ts->tunnel_state == H1_TUNNEL_FAILED);
  80. }
  81. static CURLcode tunnel_reinit(struct Curl_cfilter *cf,
  82. struct Curl_easy *data,
  83. struct h1_tunnel_state *ts)
  84. {
  85. (void)data;
  86. (void)cf;
  87. DEBUGASSERT(ts);
  88. Curl_dyn_reset(&ts->rcvbuf);
  89. Curl_dyn_reset(&ts->request_data);
  90. ts->tunnel_state = H1_TUNNEL_INIT;
  91. ts->keepon = KEEPON_CONNECT;
  92. ts->cl = 0;
  93. ts->close_connection = FALSE;
  94. return CURLE_OK;
  95. }
  96. static CURLcode tunnel_init(struct Curl_cfilter *cf,
  97. struct Curl_easy *data,
  98. struct h1_tunnel_state **pts)
  99. {
  100. struct h1_tunnel_state *ts;
  101. if(cf->conn->handler->flags & PROTOPT_NOTCPPROXY) {
  102. failf(data, "%s cannot be done over CONNECT", cf->conn->handler->scheme);
  103. return CURLE_UNSUPPORTED_PROTOCOL;
  104. }
  105. ts = calloc(1, sizeof(*ts));
  106. if(!ts)
  107. return CURLE_OUT_OF_MEMORY;
  108. infof(data, "allocate connect buffer");
  109. Curl_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
  110. Curl_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
  111. Curl_httpchunk_init(data, &ts->ch, TRUE);
  112. *pts = ts;
  113. connkeep(cf->conn, "HTTP proxy CONNECT");
  114. return tunnel_reinit(cf, data, ts);
  115. }
  116. static void h1_tunnel_go_state(struct Curl_cfilter *cf,
  117. struct h1_tunnel_state *ts,
  118. h1_tunnel_state new_state,
  119. struct Curl_easy *data)
  120. {
  121. if(ts->tunnel_state == new_state)
  122. return;
  123. /* entering this one */
  124. switch(new_state) {
  125. case H1_TUNNEL_INIT:
  126. CURL_TRC_CF(data, cf, "new tunnel state 'init'");
  127. tunnel_reinit(cf, data, ts);
  128. break;
  129. case H1_TUNNEL_CONNECT:
  130. CURL_TRC_CF(data, cf, "new tunnel state 'connect'");
  131. ts->tunnel_state = H1_TUNNEL_CONNECT;
  132. ts->keepon = KEEPON_CONNECT;
  133. Curl_dyn_reset(&ts->rcvbuf);
  134. break;
  135. case H1_TUNNEL_RECEIVE:
  136. CURL_TRC_CF(data, cf, "new tunnel state 'receive'");
  137. ts->tunnel_state = H1_TUNNEL_RECEIVE;
  138. break;
  139. case H1_TUNNEL_RESPONSE:
  140. CURL_TRC_CF(data, cf, "new tunnel state 'response'");
  141. ts->tunnel_state = H1_TUNNEL_RESPONSE;
  142. break;
  143. case H1_TUNNEL_ESTABLISHED:
  144. CURL_TRC_CF(data, cf, "new tunnel state 'established'");
  145. infof(data, "CONNECT phase completed");
  146. data->state.authproxy.done = TRUE;
  147. data->state.authproxy.multipass = FALSE;
  148. FALLTHROUGH();
  149. case H1_TUNNEL_FAILED:
  150. if(new_state == H1_TUNNEL_FAILED)
  151. CURL_TRC_CF(data, cf, "new tunnel state 'failed'");
  152. ts->tunnel_state = new_state;
  153. Curl_dyn_reset(&ts->rcvbuf);
  154. Curl_dyn_reset(&ts->request_data);
  155. /* restore the protocol pointer */
  156. data->info.httpcode = 0; /* clear it as it might've been used for the
  157. proxy */
  158. /* If a proxy-authorization header was used for the proxy, then we should
  159. make sure that it is not accidentally used for the document request
  160. after we have connected. So let's free and clear it here. */
  161. Curl_safefree(data->state.aptr.proxyuserpwd);
  162. break;
  163. }
  164. }
  165. static void tunnel_free(struct Curl_cfilter *cf,
  166. struct Curl_easy *data)
  167. {
  168. if(cf) {
  169. struct h1_tunnel_state *ts = cf->ctx;
  170. if(ts) {
  171. h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
  172. Curl_dyn_free(&ts->rcvbuf);
  173. Curl_dyn_free(&ts->request_data);
  174. Curl_httpchunk_free(data, &ts->ch);
  175. free(ts);
  176. cf->ctx = NULL;
  177. }
  178. }
  179. }
  180. static bool tunnel_want_send(struct h1_tunnel_state *ts)
  181. {
  182. return (ts->tunnel_state == H1_TUNNEL_CONNECT);
  183. }
  184. static CURLcode start_CONNECT(struct Curl_cfilter *cf,
  185. struct Curl_easy *data,
  186. struct h1_tunnel_state *ts)
  187. {
  188. struct httpreq *req = NULL;
  189. int http_minor;
  190. CURLcode result;
  191. /* This only happens if we have looped here due to authentication
  192. reasons, and we do not really use the newly cloned URL here
  193. then. Just free() it. */
  194. Curl_safefree(data->req.newurl);
  195. result = Curl_http_proxy_create_CONNECT(&req, cf, data, 1);
  196. if(result)
  197. goto out;
  198. infof(data, "Establish HTTP proxy tunnel to %s", req->authority);
  199. Curl_dyn_reset(&ts->request_data);
  200. ts->nsent = 0;
  201. ts->headerlines = 0;
  202. http_minor = (cf->conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ? 0 : 1;
  203. result = Curl_h1_req_write_head(req, http_minor, &ts->request_data);
  204. if(!result)
  205. result = Curl_creader_set_null(data);
  206. out:
  207. if(result)
  208. failf(data, "Failed sending CONNECT to proxy");
  209. if(req)
  210. Curl_http_req_free(req);
  211. return result;
  212. }
  213. static CURLcode send_CONNECT(struct Curl_cfilter *cf,
  214. struct Curl_easy *data,
  215. struct h1_tunnel_state *ts,
  216. bool *done)
  217. {
  218. char *buf = Curl_dyn_ptr(&ts->request_data);
  219. size_t request_len = Curl_dyn_len(&ts->request_data);
  220. size_t blen = request_len;
  221. CURLcode result = CURLE_OK;
  222. ssize_t nwritten;
  223. if(blen <= ts->nsent)
  224. goto out; /* we are done */
  225. blen -= ts->nsent;
  226. buf += ts->nsent;
  227. nwritten = cf->next->cft->do_send(cf->next, data, buf, blen, FALSE, &result);
  228. if(nwritten < 0) {
  229. if(result == CURLE_AGAIN) {
  230. result = CURLE_OK;
  231. }
  232. goto out;
  233. }
  234. DEBUGASSERT(blen >= (size_t)nwritten);
  235. ts->nsent += (size_t)nwritten;
  236. Curl_debug(data, CURLINFO_HEADER_OUT, buf, (size_t)nwritten);
  237. out:
  238. if(result)
  239. failf(data, "Failed sending CONNECT to proxy");
  240. *done = (!result && (ts->nsent >= request_len));
  241. return result;
  242. }
  243. static CURLcode on_resp_header(struct Curl_cfilter *cf,
  244. struct Curl_easy *data,
  245. struct h1_tunnel_state *ts,
  246. const char *header)
  247. {
  248. CURLcode result = CURLE_OK;
  249. struct SingleRequest *k = &data->req;
  250. (void)cf;
  251. if((checkprefix("WWW-Authenticate:", header) &&
  252. (401 == k->httpcode)) ||
  253. (checkprefix("Proxy-authenticate:", header) &&
  254. (407 == k->httpcode))) {
  255. bool proxy = (k->httpcode == 407);
  256. char *auth = Curl_copy_header_value(header);
  257. if(!auth)
  258. return CURLE_OUT_OF_MEMORY;
  259. CURL_TRC_CF(data, cf, "CONNECT: fwd auth header '%s'", header);
  260. result = Curl_http_input_auth(data, proxy, auth);
  261. free(auth);
  262. if(result)
  263. return result;
  264. }
  265. else if(checkprefix("Content-Length:", header)) {
  266. if(k->httpcode/100 == 2) {
  267. /* A client MUST ignore any Content-Length or Transfer-Encoding
  268. header fields received in a successful response to CONNECT.
  269. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  270. infof(data, "Ignoring Content-Length in CONNECT %03d response",
  271. k->httpcode);
  272. }
  273. else {
  274. (void)curlx_strtoofft(header + strlen("Content-Length:"),
  275. NULL, 10, &ts->cl);
  276. }
  277. }
  278. else if(Curl_compareheader(header,
  279. STRCONST("Connection:"), STRCONST("close")))
  280. ts->close_connection = TRUE;
  281. else if(checkprefix("Transfer-Encoding:", header)) {
  282. if(k->httpcode/100 == 2) {
  283. /* A client MUST ignore any Content-Length or Transfer-Encoding
  284. header fields received in a successful response to CONNECT.
  285. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  286. infof(data, "Ignoring Transfer-Encoding in "
  287. "CONNECT %03d response", k->httpcode);
  288. }
  289. else if(Curl_compareheader(header,
  290. STRCONST("Transfer-Encoding:"),
  291. STRCONST("chunked"))) {
  292. infof(data, "CONNECT responded chunked");
  293. ts->chunked_encoding = TRUE;
  294. /* reset our chunky engine */
  295. Curl_httpchunk_reset(data, &ts->ch, TRUE);
  296. }
  297. }
  298. else if(Curl_compareheader(header,
  299. STRCONST("Proxy-Connection:"),
  300. STRCONST("close")))
  301. ts->close_connection = TRUE;
  302. else if(!strncmp(header, "HTTP/1.", 7) &&
  303. ((header[7] == '0') || (header[7] == '1')) &&
  304. (header[8] == ' ') &&
  305. ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
  306. !ISDIGIT(header[12])) {
  307. /* store the HTTP code from the proxy */
  308. data->info.httpproxycode = k->httpcode = (header[9] - '0') * 100 +
  309. (header[10] - '0') * 10 + (header[11] - '0');
  310. }
  311. return result;
  312. }
  313. static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
  314. struct Curl_easy *data,
  315. struct h1_tunnel_state *ts,
  316. bool *done)
  317. {
  318. CURLcode result = CURLE_OK;
  319. struct SingleRequest *k = &data->req;
  320. char *linep;
  321. size_t line_len;
  322. int error, writetype;
  323. #define SELECT_OK 0
  324. #define SELECT_ERROR 1
  325. error = SELECT_OK;
  326. *done = FALSE;
  327. if(!Curl_conn_data_pending(data, cf->sockindex))
  328. return CURLE_OK;
  329. while(ts->keepon) {
  330. ssize_t nread;
  331. char byte;
  332. /* Read one byte at a time to avoid a race condition. Wait at most one
  333. second before looping to ensure continuous pgrsUpdates. */
  334. result = Curl_conn_recv(data, cf->sockindex, &byte, 1, &nread);
  335. if(result == CURLE_AGAIN)
  336. /* socket buffer drained, return */
  337. return CURLE_OK;
  338. if(Curl_pgrsUpdate(data))
  339. return CURLE_ABORTED_BY_CALLBACK;
  340. if(result) {
  341. ts->keepon = KEEPON_DONE;
  342. break;
  343. }
  344. if(nread <= 0) {
  345. if(data->set.proxyauth && data->state.authproxy.avail &&
  346. data->state.aptr.proxyuserpwd) {
  347. /* proxy auth was requested and there was proxy auth available,
  348. then deem this as "mere" proxy disconnect */
  349. ts->close_connection = TRUE;
  350. infof(data, "Proxy CONNECT connection closed");
  351. }
  352. else {
  353. error = SELECT_ERROR;
  354. failf(data, "Proxy CONNECT aborted");
  355. }
  356. ts->keepon = KEEPON_DONE;
  357. break;
  358. }
  359. if(ts->keepon == KEEPON_IGNORE) {
  360. /* This means we are currently ignoring a response-body */
  361. if(ts->cl) {
  362. /* A Content-Length based body: simply count down the counter
  363. and make sure to break out of the loop when we are done! */
  364. ts->cl--;
  365. if(ts->cl <= 0) {
  366. ts->keepon = KEEPON_DONE;
  367. break;
  368. }
  369. }
  370. else if(ts->chunked_encoding) {
  371. /* chunked-encoded body, so we need to do the chunked dance
  372. properly to know when the end of the body is reached */
  373. size_t consumed = 0;
  374. /* now parse the chunked piece of data so that we can
  375. properly tell when the stream ends */
  376. result = Curl_httpchunk_read(data, &ts->ch, &byte, 1, &consumed);
  377. if(result)
  378. return result;
  379. if(Curl_httpchunk_is_done(data, &ts->ch)) {
  380. /* we are done reading chunks! */
  381. infof(data, "chunk reading DONE");
  382. ts->keepon = KEEPON_DONE;
  383. }
  384. }
  385. continue;
  386. }
  387. if(Curl_dyn_addn(&ts->rcvbuf, &byte, 1)) {
  388. failf(data, "CONNECT response too large");
  389. return CURLE_RECV_ERROR;
  390. }
  391. /* if this is not the end of a header line then continue */
  392. if(byte != 0x0a)
  393. continue;
  394. ts->headerlines++;
  395. linep = Curl_dyn_ptr(&ts->rcvbuf);
  396. line_len = Curl_dyn_len(&ts->rcvbuf); /* amount of bytes in this line */
  397. /* output debug if that is requested */
  398. Curl_debug(data, CURLINFO_HEADER_IN, linep, line_len);
  399. /* send the header to the callback */
  400. writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
  401. (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
  402. result = Curl_client_write(data, writetype, linep, line_len);
  403. if(result)
  404. return result;
  405. result = Curl_bump_headersize(data, line_len, TRUE);
  406. if(result)
  407. return result;
  408. /* Newlines are CRLF, so the CR is ignored as the line is not
  409. really terminated until the LF comes. Treat a following CR
  410. as end-of-headers as well.*/
  411. if(('\r' == linep[0]) ||
  412. ('\n' == linep[0])) {
  413. /* end of response-headers from the proxy */
  414. if((407 == k->httpcode) && !data->state.authproblem) {
  415. /* If we get a 407 response code with content length
  416. when we have no auth problem, we must ignore the
  417. whole response-body */
  418. ts->keepon = KEEPON_IGNORE;
  419. if(ts->cl) {
  420. infof(data, "Ignore %" FMT_OFF_T " bytes of response-body", ts->cl);
  421. }
  422. else if(ts->chunked_encoding) {
  423. infof(data, "Ignore chunked response-body");
  424. }
  425. else {
  426. /* without content-length or chunked encoding, we
  427. cannot keep the connection alive since the close is
  428. the end signal so we bail out at once instead */
  429. CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
  430. ts->keepon = KEEPON_DONE;
  431. }
  432. }
  433. else {
  434. ts->keepon = KEEPON_DONE;
  435. }
  436. DEBUGASSERT(ts->keepon == KEEPON_IGNORE
  437. || ts->keepon == KEEPON_DONE);
  438. continue;
  439. }
  440. result = on_resp_header(cf, data, ts, linep);
  441. if(result)
  442. return result;
  443. Curl_dyn_reset(&ts->rcvbuf);
  444. } /* while there is buffer left and loop is requested */
  445. if(error)
  446. result = CURLE_RECV_ERROR;
  447. *done = (ts->keepon == KEEPON_DONE);
  448. if(!result && *done && data->info.httpproxycode/100 != 2) {
  449. /* Deal with the possibly already received authenticate
  450. headers. 'newurl' is set to a new URL if we must loop. */
  451. result = Curl_http_auth_act(data);
  452. }
  453. return result;
  454. }
  455. static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
  456. struct Curl_easy *data,
  457. struct h1_tunnel_state *ts)
  458. {
  459. struct connectdata *conn = cf->conn;
  460. CURLcode result;
  461. bool done;
  462. if(tunnel_is_established(ts))
  463. return CURLE_OK;
  464. if(tunnel_is_failed(ts))
  465. return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
  466. do {
  467. timediff_t check;
  468. check = Curl_timeleft(data, NULL, TRUE);
  469. if(check <= 0) {
  470. failf(data, "Proxy CONNECT aborted due to timeout");
  471. result = CURLE_OPERATION_TIMEDOUT;
  472. goto out;
  473. }
  474. switch(ts->tunnel_state) {
  475. case H1_TUNNEL_INIT:
  476. /* Prepare the CONNECT request and make a first attempt to send. */
  477. CURL_TRC_CF(data, cf, "CONNECT start");
  478. result = start_CONNECT(cf, data, ts);
  479. if(result)
  480. goto out;
  481. h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
  482. FALLTHROUGH();
  483. case H1_TUNNEL_CONNECT:
  484. /* see that the request is completely sent */
  485. CURL_TRC_CF(data, cf, "CONNECT send");
  486. result = send_CONNECT(cf, data, ts, &done);
  487. if(result || !done)
  488. goto out;
  489. h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
  490. FALLTHROUGH();
  491. case H1_TUNNEL_RECEIVE:
  492. /* read what is there */
  493. CURL_TRC_CF(data, cf, "CONNECT receive");
  494. result = recv_CONNECT_resp(cf, data, ts, &done);
  495. if(Curl_pgrsUpdate(data)) {
  496. result = CURLE_ABORTED_BY_CALLBACK;
  497. goto out;
  498. }
  499. /* error or not complete yet. return for more multi-multi */
  500. if(result || !done)
  501. goto out;
  502. /* got it */
  503. h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
  504. FALLTHROUGH();
  505. case H1_TUNNEL_RESPONSE:
  506. CURL_TRC_CF(data, cf, "CONNECT response");
  507. if(data->req.newurl) {
  508. /* not the "final" response, we need to do a follow up request.
  509. * If the other side indicated a connection close, or if someone
  510. * else told us to close this connection, do so now.
  511. */
  512. Curl_req_soft_reset(&data->req, data);
  513. if(ts->close_connection || conn->bits.close) {
  514. /* Close this filter and the sub-chain, re-connect the
  515. * sub-chain and continue. Closing this filter will
  516. * reset our tunnel state. To avoid recursion, we return
  517. * and expect to be called again.
  518. */
  519. CURL_TRC_CF(data, cf, "CONNECT need to close+open");
  520. infof(data, "Connect me again please");
  521. Curl_conn_cf_close(cf, data);
  522. connkeep(conn, "HTTP proxy CONNECT");
  523. result = Curl_conn_cf_connect(cf->next, data, FALSE, &done);
  524. goto out;
  525. }
  526. else {
  527. /* staying on this connection, reset state */
  528. h1_tunnel_go_state(cf, ts, H1_TUNNEL_INIT, data);
  529. }
  530. }
  531. break;
  532. default:
  533. break;
  534. }
  535. } while(data->req.newurl);
  536. DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
  537. if(data->info.httpproxycode/100 != 2) {
  538. /* a non-2xx response and we have no next URL to try. */
  539. Curl_safefree(data->req.newurl);
  540. /* failure, close this connection to avoid reuse */
  541. streamclose(conn, "proxy CONNECT failure");
  542. h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
  543. failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
  544. return CURLE_RECV_ERROR;
  545. }
  546. /* 2xx response, SUCCESS! */
  547. h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
  548. infof(data, "CONNECT tunnel established, response %d",
  549. data->info.httpproxycode);
  550. result = CURLE_OK;
  551. out:
  552. if(result)
  553. h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
  554. return result;
  555. }
  556. static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
  557. struct Curl_easy *data,
  558. bool blocking, bool *done)
  559. {
  560. CURLcode result;
  561. struct h1_tunnel_state *ts = cf->ctx;
  562. if(cf->connected) {
  563. *done = TRUE;
  564. return CURLE_OK;
  565. }
  566. CURL_TRC_CF(data, cf, "connect");
  567. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  568. if(result || !*done)
  569. return result;
  570. *done = FALSE;
  571. if(!ts) {
  572. result = tunnel_init(cf, data, &ts);
  573. if(result)
  574. return result;
  575. cf->ctx = ts;
  576. }
  577. /* TODO: can we do blocking? */
  578. /* We want "seamless" operations through HTTP proxy tunnel */
  579. result = H1_CONNECT(cf, data, ts);
  580. if(result)
  581. goto out;
  582. Curl_safefree(data->state.aptr.proxyuserpwd);
  583. out:
  584. *done = (result == CURLE_OK) && tunnel_is_established(cf->ctx);
  585. if(*done) {
  586. cf->connected = TRUE;
  587. /* The real request will follow the CONNECT, reset request partially */
  588. Curl_req_soft_reset(&data->req, data);
  589. Curl_client_reset(data);
  590. Curl_pgrsSetUploadCounter(data, 0);
  591. Curl_pgrsSetDownloadCounter(data, 0);
  592. tunnel_free(cf, data);
  593. }
  594. return result;
  595. }
  596. static void cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
  597. struct Curl_easy *data,
  598. struct easy_pollset *ps)
  599. {
  600. struct h1_tunnel_state *ts = cf->ctx;
  601. if(!cf->connected) {
  602. /* If we are not connected, but the filter "below" is
  603. * and not waiting on something, we are tunneling. */
  604. curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
  605. if(ts) {
  606. /* when we have sent a CONNECT to a proxy, we should rather either
  607. wait for the socket to become readable to be able to get the
  608. response headers or if we are still sending the request, wait
  609. for write. */
  610. if(tunnel_want_send(ts))
  611. Curl_pollset_set_out_only(data, ps, sock);
  612. else
  613. Curl_pollset_set_in_only(data, ps, sock);
  614. }
  615. else
  616. Curl_pollset_set_out_only(data, ps, sock);
  617. }
  618. }
  619. static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
  620. struct Curl_easy *data)
  621. {
  622. CURL_TRC_CF(data, cf, "destroy");
  623. tunnel_free(cf, data);
  624. }
  625. static void cf_h1_proxy_close(struct Curl_cfilter *cf,
  626. struct Curl_easy *data)
  627. {
  628. CURL_TRC_CF(data, cf, "close");
  629. if(cf) {
  630. cf->connected = FALSE;
  631. if(cf->ctx) {
  632. h1_tunnel_go_state(cf, cf->ctx, H1_TUNNEL_INIT, data);
  633. }
  634. if(cf->next)
  635. cf->next->cft->do_close(cf->next, data);
  636. }
  637. }
  638. struct Curl_cftype Curl_cft_h1_proxy = {
  639. "H1-PROXY",
  640. CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
  641. 0,
  642. cf_h1_proxy_destroy,
  643. cf_h1_proxy_connect,
  644. cf_h1_proxy_close,
  645. Curl_cf_def_shutdown,
  646. Curl_cf_http_proxy_get_host,
  647. cf_h1_proxy_adjust_pollset,
  648. Curl_cf_def_data_pending,
  649. Curl_cf_def_send,
  650. Curl_cf_def_recv,
  651. Curl_cf_def_cntrl,
  652. Curl_cf_def_conn_is_alive,
  653. Curl_cf_def_conn_keep_alive,
  654. Curl_cf_def_query,
  655. };
  656. CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
  657. struct Curl_easy *data)
  658. {
  659. struct Curl_cfilter *cf;
  660. CURLcode result;
  661. (void)data;
  662. result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, NULL);
  663. if(!result)
  664. Curl_conn_cf_insert_after(cf_at, cf);
  665. return result;
  666. }
  667. #endif /* !CURL_DISABLE_PROXY && ! CURL_DISABLE_HTTP */