http_proxy.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. #include "http_proxy.h"
  26. #if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
  27. #include <curl/curl.h>
  28. #ifdef USE_HYPER
  29. #include <hyper.h>
  30. #endif
  31. #include "sendf.h"
  32. #include "http.h"
  33. #include "url.h"
  34. #include "select.h"
  35. #include "progress.h"
  36. #include "connect.h"
  37. #include "curlx.h"
  38. #include "vtls/vtls.h"
  39. #include "transfer.h"
  40. #include "multiif.h"
  41. /* The last 3 #include files should be in this order */
  42. #include "curl_printf.h"
  43. #include "curl_memory.h"
  44. #include "memdebug.h"
  45. /*
  46. * Perform SSL initialization for HTTPS proxy. Sets
  47. * proxy_ssl_connected connection bit when complete. Can be
  48. * called multiple times.
  49. */
  50. static CURLcode https_proxy_connect(struct Curl_easy *data, int sockindex)
  51. {
  52. #ifdef USE_SSL
  53. struct connectdata *conn = data->conn;
  54. CURLcode result = CURLE_OK;
  55. DEBUGASSERT(conn->http_proxy.proxytype == CURLPROXY_HTTPS);
  56. if(!conn->bits.proxy_ssl_connected[sockindex]) {
  57. /* perform SSL initialization for this socket */
  58. result =
  59. Curl_ssl_connect_nonblocking(data, conn, TRUE, sockindex,
  60. &conn->bits.proxy_ssl_connected[sockindex]);
  61. if(result)
  62. /* a failed connection is marked for closure to prevent (bad) re-use or
  63. similar */
  64. connclose(conn, "TLS handshake failed");
  65. }
  66. return result;
  67. #else
  68. (void) data;
  69. (void) sockindex;
  70. return CURLE_NOT_BUILT_IN;
  71. #endif
  72. }
  73. CURLcode Curl_proxy_connect(struct Curl_easy *data, int sockindex)
  74. {
  75. struct connectdata *conn = data->conn;
  76. if(conn->http_proxy.proxytype == CURLPROXY_HTTPS) {
  77. const CURLcode result = https_proxy_connect(data, sockindex);
  78. if(result)
  79. return result;
  80. if(!conn->bits.proxy_ssl_connected[sockindex])
  81. return result; /* wait for HTTPS proxy SSL initialization to complete */
  82. }
  83. if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
  84. #ifndef CURL_DISABLE_PROXY
  85. /* for [protocol] tunneled through HTTP proxy */
  86. const char *hostname;
  87. int remote_port;
  88. CURLcode result;
  89. /* We want "seamless" operations through HTTP proxy tunnel */
  90. /* for the secondary socket (FTP), use the "connect to host"
  91. * but ignore the "connect to port" (use the secondary port)
  92. */
  93. if(conn->bits.conn_to_host)
  94. hostname = conn->conn_to_host.name;
  95. else if(sockindex == SECONDARYSOCKET)
  96. hostname = conn->secondaryhostname;
  97. else
  98. hostname = conn->host.name;
  99. if(sockindex == SECONDARYSOCKET)
  100. remote_port = conn->secondary_port;
  101. else if(conn->bits.conn_to_port)
  102. remote_port = conn->conn_to_port;
  103. else
  104. remote_port = conn->remote_port;
  105. result = Curl_proxyCONNECT(data, sockindex, hostname, remote_port);
  106. if(CURLE_OK != result)
  107. return result;
  108. Curl_safefree(data->state.aptr.proxyuserpwd);
  109. #else
  110. return CURLE_NOT_BUILT_IN;
  111. #endif
  112. }
  113. /* no HTTP tunnel proxy, just return */
  114. return CURLE_OK;
  115. }
  116. bool Curl_connect_complete(struct connectdata *conn)
  117. {
  118. return !conn->connect_state ||
  119. (conn->connect_state->tunnel_state >= TUNNEL_COMPLETE);
  120. }
  121. bool Curl_connect_ongoing(struct connectdata *conn)
  122. {
  123. return conn->connect_state &&
  124. (conn->connect_state->tunnel_state <= TUNNEL_COMPLETE);
  125. }
  126. /* when we've sent a CONNECT to a proxy, we should rather either wait for the
  127. socket to become readable to be able to get the response headers or if
  128. we're still sending the request, wait for write. */
  129. int Curl_connect_getsock(struct connectdata *conn)
  130. {
  131. struct HTTP *http;
  132. DEBUGASSERT(conn);
  133. DEBUGASSERT(conn->connect_state);
  134. http = &conn->connect_state->http_proxy;
  135. if(http->sending == HTTPSEND_REQUEST)
  136. return GETSOCK_WRITESOCK(0);
  137. return GETSOCK_READSOCK(0);
  138. }
  139. static CURLcode connect_init(struct Curl_easy *data, bool reinit)
  140. {
  141. struct http_connect_state *s;
  142. struct connectdata *conn = data->conn;
  143. if(conn->handler->flags & PROTOPT_NOTCPPROXY) {
  144. failf(data, "%s cannot be done over CONNECT", conn->handler->scheme);
  145. return CURLE_UNSUPPORTED_PROTOCOL;
  146. }
  147. if(!reinit) {
  148. CURLcode result;
  149. DEBUGASSERT(!conn->connect_state);
  150. /* we might need the upload buffer for streaming a partial request */
  151. result = Curl_get_upload_buffer(data);
  152. if(result)
  153. return result;
  154. s = calloc(1, sizeof(struct http_connect_state));
  155. if(!s)
  156. return CURLE_OUT_OF_MEMORY;
  157. infof(data, "allocate connect buffer");
  158. conn->connect_state = s;
  159. Curl_dyn_init(&s->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
  160. /* Curl_proxyCONNECT is based on a pointer to a struct HTTP at the
  161. * member conn->proto.http; we want [protocol] through HTTP and we have
  162. * to change the member temporarily for connecting to the HTTP
  163. * proxy. After Curl_proxyCONNECT we have to set back the member to the
  164. * original pointer
  165. *
  166. * This function might be called several times in the multi interface case
  167. * if the proxy's CONNECT response is not instant.
  168. */
  169. s->prot_save = data->req.p.http;
  170. data->req.p.http = &s->http_proxy;
  171. connkeep(conn, "HTTP proxy CONNECT");
  172. }
  173. else {
  174. DEBUGASSERT(conn->connect_state);
  175. s = conn->connect_state;
  176. Curl_dyn_reset(&s->rcvbuf);
  177. }
  178. s->tunnel_state = TUNNEL_INIT;
  179. s->keepon = KEEPON_CONNECT;
  180. s->cl = 0;
  181. s->close_connection = FALSE;
  182. return CURLE_OK;
  183. }
  184. void Curl_connect_done(struct Curl_easy *data)
  185. {
  186. struct connectdata *conn = data->conn;
  187. struct http_connect_state *s = conn->connect_state;
  188. if(s && (s->tunnel_state != TUNNEL_EXIT)) {
  189. s->tunnel_state = TUNNEL_EXIT;
  190. Curl_dyn_free(&s->rcvbuf);
  191. Curl_dyn_free(&s->req);
  192. /* restore the protocol pointer */
  193. data->req.p.http = s->prot_save;
  194. data->info.httpcode = 0; /* clear it as it might've been used for the
  195. proxy */
  196. data->req.ignorebody = FALSE;
  197. #ifdef USE_HYPER
  198. data->state.hconnect = FALSE;
  199. #endif
  200. infof(data, "CONNECT phase completed");
  201. }
  202. }
  203. static CURLcode CONNECT_host(struct Curl_easy *data,
  204. struct connectdata *conn,
  205. const char *hostname,
  206. int remote_port,
  207. char **connecthostp,
  208. char **hostp)
  209. {
  210. char *hostheader; /* for CONNECT */
  211. char *host = NULL; /* Host: */
  212. bool ipv6_ip = conn->bits.ipv6_ip;
  213. /* the hostname may be different */
  214. if(hostname != conn->host.name)
  215. ipv6_ip = (strchr(hostname, ':') != NULL);
  216. hostheader = /* host:port with IPv6 support */
  217. aprintf("%s%s%s:%d", ipv6_ip?"[":"", hostname, ipv6_ip?"]":"",
  218. remote_port);
  219. if(!hostheader)
  220. return CURLE_OUT_OF_MEMORY;
  221. if(!Curl_checkProxyheaders(data, conn, STRCONST("Host"))) {
  222. host = aprintf("Host: %s\r\n", hostheader);
  223. if(!host) {
  224. free(hostheader);
  225. return CURLE_OUT_OF_MEMORY;
  226. }
  227. }
  228. *connecthostp = hostheader;
  229. *hostp = host;
  230. return CURLE_OK;
  231. }
  232. #ifndef USE_HYPER
  233. static CURLcode CONNECT(struct Curl_easy *data,
  234. int sockindex,
  235. const char *hostname,
  236. int remote_port)
  237. {
  238. int subversion = 0;
  239. struct SingleRequest *k = &data->req;
  240. CURLcode result;
  241. struct connectdata *conn = data->conn;
  242. curl_socket_t tunnelsocket = conn->sock[sockindex];
  243. struct http_connect_state *s = conn->connect_state;
  244. struct HTTP *http = data->req.p.http;
  245. char *linep;
  246. size_t perline;
  247. #define SELECT_OK 0
  248. #define SELECT_ERROR 1
  249. if(Curl_connect_complete(conn))
  250. return CURLE_OK; /* CONNECT is already completed */
  251. conn->bits.proxy_connect_closed = FALSE;
  252. do {
  253. timediff_t check;
  254. if(TUNNEL_INIT == s->tunnel_state) {
  255. /* BEGIN CONNECT PHASE */
  256. struct dynbuf *req = &s->req;
  257. char *hostheader = NULL;
  258. char *host = NULL;
  259. infof(data, "Establish HTTP proxy tunnel to %s:%d",
  260. hostname, remote_port);
  261. /* This only happens if we've looped here due to authentication
  262. reasons, and we don't really use the newly cloned URL here
  263. then. Just free() it. */
  264. Curl_safefree(data->req.newurl);
  265. /* initialize send-buffer */
  266. Curl_dyn_init(req, DYN_HTTP_REQUEST);
  267. result = CONNECT_host(data, conn,
  268. hostname, remote_port, &hostheader, &host);
  269. if(result)
  270. return result;
  271. /* Setup the proxy-authorization header, if any */
  272. result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
  273. hostheader, TRUE);
  274. if(!result) {
  275. const char *httpv =
  276. (conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ? "1.0" : "1.1";
  277. result =
  278. Curl_dyn_addf(req,
  279. "CONNECT %s HTTP/%s\r\n"
  280. "%s" /* Host: */
  281. "%s", /* Proxy-Authorization */
  282. hostheader,
  283. httpv,
  284. host?host:"",
  285. data->state.aptr.proxyuserpwd?
  286. data->state.aptr.proxyuserpwd:"");
  287. if(!result && !Curl_checkProxyheaders(data,
  288. conn, STRCONST("User-Agent")) &&
  289. data->set.str[STRING_USERAGENT])
  290. result = Curl_dyn_addf(req, "User-Agent: %s\r\n",
  291. data->set.str[STRING_USERAGENT]);
  292. if(!result && !Curl_checkProxyheaders(data, conn,
  293. STRCONST("Proxy-Connection")))
  294. result = Curl_dyn_addn(req,
  295. STRCONST("Proxy-Connection: Keep-Alive\r\n"));
  296. if(!result)
  297. result = Curl_add_custom_headers(data, TRUE, req);
  298. if(!result)
  299. /* CRLF terminate the request */
  300. result = Curl_dyn_addn(req, STRCONST("\r\n"));
  301. if(!result) {
  302. /* Send the connect request to the proxy */
  303. result = Curl_buffer_send(req, data, &data->info.request_size, 0,
  304. sockindex);
  305. s->headerlines = 0;
  306. }
  307. if(result)
  308. failf(data, "Failed sending CONNECT to proxy");
  309. }
  310. free(host);
  311. free(hostheader);
  312. if(result)
  313. return result;
  314. s->tunnel_state = TUNNEL_CONNECT;
  315. } /* END CONNECT PHASE */
  316. check = Curl_timeleft(data, NULL, TRUE);
  317. if(check <= 0) {
  318. failf(data, "Proxy CONNECT aborted due to timeout");
  319. return CURLE_OPERATION_TIMEDOUT;
  320. }
  321. if(!Curl_conn_data_pending(conn, sockindex) && !http->sending)
  322. /* return so we'll be called again polling-style */
  323. return CURLE_OK;
  324. /* at this point, the tunnel_connecting phase is over. */
  325. if(http->sending == HTTPSEND_REQUEST) {
  326. if(!s->nsend) {
  327. size_t fillcount;
  328. k->upload_fromhere = data->state.ulbuf;
  329. result = Curl_fillreadbuffer(data, data->set.upload_buffer_size,
  330. &fillcount);
  331. if(result)
  332. return result;
  333. s->nsend = fillcount;
  334. }
  335. if(s->nsend) {
  336. ssize_t bytes_written;
  337. /* write to socket (send away data) */
  338. result = Curl_write(data,
  339. conn->writesockfd, /* socket to send to */
  340. k->upload_fromhere, /* buffer pointer */
  341. s->nsend, /* buffer size */
  342. &bytes_written); /* actually sent */
  343. if(!result)
  344. /* send to debug callback! */
  345. Curl_debug(data, CURLINFO_HEADER_OUT,
  346. k->upload_fromhere, bytes_written);
  347. s->nsend -= bytes_written;
  348. k->upload_fromhere += bytes_written;
  349. return result;
  350. }
  351. http->sending = HTTPSEND_NADA;
  352. /* if nothing left to send, continue */
  353. }
  354. { /* READING RESPONSE PHASE */
  355. int error = SELECT_OK;
  356. while(s->keepon) {
  357. ssize_t gotbytes;
  358. char byte;
  359. /* Read one byte at a time to avoid a race condition. Wait at most one
  360. second before looping to ensure continuous pgrsUpdates. */
  361. result = Curl_read(data, tunnelsocket, &byte, 1, &gotbytes);
  362. if(result == CURLE_AGAIN)
  363. /* socket buffer drained, return */
  364. return CURLE_OK;
  365. if(Curl_pgrsUpdate(data))
  366. return CURLE_ABORTED_BY_CALLBACK;
  367. if(result) {
  368. s->keepon = KEEPON_DONE;
  369. break;
  370. }
  371. else if(gotbytes <= 0) {
  372. if(data->set.proxyauth && data->state.authproxy.avail &&
  373. data->state.aptr.proxyuserpwd) {
  374. /* proxy auth was requested and there was proxy auth available,
  375. then deem this as "mere" proxy disconnect */
  376. conn->bits.proxy_connect_closed = TRUE;
  377. infof(data, "Proxy CONNECT connection closed");
  378. }
  379. else {
  380. error = SELECT_ERROR;
  381. failf(data, "Proxy CONNECT aborted");
  382. }
  383. s->keepon = KEEPON_DONE;
  384. break;
  385. }
  386. if(s->keepon == KEEPON_IGNORE) {
  387. /* This means we are currently ignoring a response-body */
  388. if(s->cl) {
  389. /* A Content-Length based body: simply count down the counter
  390. and make sure to break out of the loop when we're done! */
  391. s->cl--;
  392. if(s->cl <= 0) {
  393. s->keepon = KEEPON_DONE;
  394. s->tunnel_state = TUNNEL_COMPLETE;
  395. break;
  396. }
  397. }
  398. else {
  399. /* chunked-encoded body, so we need to do the chunked dance
  400. properly to know when the end of the body is reached */
  401. CHUNKcode r;
  402. CURLcode extra;
  403. ssize_t tookcareof = 0;
  404. /* now parse the chunked piece of data so that we can
  405. properly tell when the stream ends */
  406. r = Curl_httpchunk_read(data, &byte, 1, &tookcareof, &extra);
  407. if(r == CHUNKE_STOP) {
  408. /* we're done reading chunks! */
  409. infof(data, "chunk reading DONE");
  410. s->keepon = KEEPON_DONE;
  411. /* we did the full CONNECT treatment, go COMPLETE */
  412. s->tunnel_state = TUNNEL_COMPLETE;
  413. }
  414. }
  415. continue;
  416. }
  417. if(Curl_dyn_addn(&s->rcvbuf, &byte, 1)) {
  418. failf(data, "CONNECT response too large");
  419. return CURLE_RECV_ERROR;
  420. }
  421. /* if this is not the end of a header line then continue */
  422. if(byte != 0x0a)
  423. continue;
  424. s->headerlines++;
  425. linep = Curl_dyn_ptr(&s->rcvbuf);
  426. perline = Curl_dyn_len(&s->rcvbuf); /* amount of bytes in this line */
  427. /* output debug if that is requested */
  428. Curl_debug(data, CURLINFO_HEADER_IN, linep, perline);
  429. if(!data->set.suppress_connect_headers) {
  430. /* send the header to the callback */
  431. int writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
  432. (data->set.include_header ? CLIENTWRITE_BODY : 0) |
  433. (s->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
  434. result = Curl_client_write(data, writetype, linep, perline);
  435. if(result)
  436. return result;
  437. }
  438. data->info.header_size += (long)perline;
  439. /* Newlines are CRLF, so the CR is ignored as the line isn't
  440. really terminated until the LF comes. Treat a following CR
  441. as end-of-headers as well.*/
  442. if(('\r' == linep[0]) ||
  443. ('\n' == linep[0])) {
  444. /* end of response-headers from the proxy */
  445. if((407 == k->httpcode) && !data->state.authproblem) {
  446. /* If we get a 407 response code with content length
  447. when we have no auth problem, we must ignore the
  448. whole response-body */
  449. s->keepon = KEEPON_IGNORE;
  450. if(s->cl) {
  451. infof(data, "Ignore %" CURL_FORMAT_CURL_OFF_T
  452. " bytes of response-body", s->cl);
  453. }
  454. else if(s->chunked_encoding) {
  455. CHUNKcode r;
  456. CURLcode extra;
  457. infof(data, "Ignore chunked response-body");
  458. /* We set ignorebody true here since the chunked decoder
  459. function will acknowledge that. Pay attention so that this is
  460. cleared again when this function returns! */
  461. k->ignorebody = TRUE;
  462. if(linep[1] == '\n')
  463. /* this can only be a LF if the letter at index 0 was a CR */
  464. linep++;
  465. /* now parse the chunked piece of data so that we can properly
  466. tell when the stream ends */
  467. r = Curl_httpchunk_read(data, linep + 1, 1, &gotbytes,
  468. &extra);
  469. if(r == CHUNKE_STOP) {
  470. /* we're done reading chunks! */
  471. infof(data, "chunk reading DONE");
  472. s->keepon = KEEPON_DONE;
  473. /* we did the full CONNECT treatment, go to COMPLETE */
  474. s->tunnel_state = TUNNEL_COMPLETE;
  475. }
  476. }
  477. else {
  478. /* without content-length or chunked encoding, we
  479. can't keep the connection alive since the close is
  480. the end signal so we bail out at once instead */
  481. s->keepon = KEEPON_DONE;
  482. }
  483. }
  484. else
  485. s->keepon = KEEPON_DONE;
  486. if(s->keepon == KEEPON_DONE && !s->cl)
  487. /* we did the full CONNECT treatment, go to COMPLETE */
  488. s->tunnel_state = TUNNEL_COMPLETE;
  489. DEBUGASSERT(s->keepon == KEEPON_IGNORE || s->keepon == KEEPON_DONE);
  490. continue;
  491. }
  492. if((checkprefix("WWW-Authenticate:", linep) &&
  493. (401 == k->httpcode)) ||
  494. (checkprefix("Proxy-authenticate:", linep) &&
  495. (407 == k->httpcode))) {
  496. bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
  497. char *auth = Curl_copy_header_value(linep);
  498. if(!auth)
  499. return CURLE_OUT_OF_MEMORY;
  500. result = Curl_http_input_auth(data, proxy, auth);
  501. free(auth);
  502. if(result)
  503. return result;
  504. }
  505. else if(checkprefix("Content-Length:", linep)) {
  506. if(k->httpcode/100 == 2) {
  507. /* A client MUST ignore any Content-Length or Transfer-Encoding
  508. header fields received in a successful response to CONNECT.
  509. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  510. infof(data, "Ignoring Content-Length in CONNECT %03d response",
  511. k->httpcode);
  512. }
  513. else {
  514. (void)curlx_strtoofft(linep +
  515. strlen("Content-Length:"), NULL, 10, &s->cl);
  516. }
  517. }
  518. else if(Curl_compareheader(linep,
  519. STRCONST("Connection:"), STRCONST("close")))
  520. s->close_connection = TRUE;
  521. else if(checkprefix("Transfer-Encoding:", linep)) {
  522. if(k->httpcode/100 == 2) {
  523. /* A client MUST ignore any Content-Length or Transfer-Encoding
  524. header fields received in a successful response to CONNECT.
  525. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  526. infof(data, "Ignoring Transfer-Encoding in "
  527. "CONNECT %03d response", k->httpcode);
  528. }
  529. else if(Curl_compareheader(linep,
  530. STRCONST("Transfer-Encoding:"),
  531. STRCONST("chunked"))) {
  532. infof(data, "CONNECT responded chunked");
  533. s->chunked_encoding = TRUE;
  534. /* init our chunky engine */
  535. Curl_httpchunk_init(data);
  536. }
  537. }
  538. else if(Curl_compareheader(linep,
  539. STRCONST("Proxy-Connection:"),
  540. STRCONST("close")))
  541. s->close_connection = TRUE;
  542. else if(2 == sscanf(linep, "HTTP/1.%d %d",
  543. &subversion,
  544. &k->httpcode)) {
  545. /* store the HTTP code from the proxy */
  546. data->info.httpproxycode = k->httpcode;
  547. }
  548. Curl_dyn_reset(&s->rcvbuf);
  549. } /* while there's buffer left and loop is requested */
  550. if(Curl_pgrsUpdate(data))
  551. return CURLE_ABORTED_BY_CALLBACK;
  552. if(error)
  553. return CURLE_RECV_ERROR;
  554. if(data->info.httpproxycode/100 != 2) {
  555. /* Deal with the possibly already received authenticate
  556. headers. 'newurl' is set to a new URL if we must loop. */
  557. result = Curl_http_auth_act(data);
  558. if(result)
  559. return result;
  560. if(conn->bits.close)
  561. /* the connection has been marked for closure, most likely in the
  562. Curl_http_auth_act() function and thus we can kill it at once
  563. below */
  564. s->close_connection = TRUE;
  565. }
  566. if(s->close_connection && data->req.newurl) {
  567. /* Connection closed by server. Don't use it anymore */
  568. Curl_closesocket(data, conn, conn->sock[sockindex]);
  569. conn->sock[sockindex] = CURL_SOCKET_BAD;
  570. break;
  571. }
  572. } /* END READING RESPONSE PHASE */
  573. /* If we are supposed to continue and request a new URL, which basically
  574. * means the HTTP authentication is still going on so if the tunnel
  575. * is complete we start over in INIT state */
  576. if(data->req.newurl && (TUNNEL_COMPLETE == s->tunnel_state)) {
  577. connect_init(data, TRUE); /* reinit */
  578. }
  579. } while(data->req.newurl);
  580. if(data->info.httpproxycode/100 != 2) {
  581. if(s->close_connection && data->req.newurl) {
  582. conn->bits.proxy_connect_closed = TRUE;
  583. infof(data, "Connect me again please");
  584. Curl_connect_done(data);
  585. }
  586. else {
  587. free(data->req.newurl);
  588. data->req.newurl = NULL;
  589. /* failure, close this connection to avoid re-use */
  590. streamclose(conn, "proxy CONNECT failure");
  591. }
  592. /* to back to init state */
  593. s->tunnel_state = TUNNEL_INIT;
  594. if(conn->bits.proxy_connect_closed)
  595. /* this is not an error, just part of the connection negotiation */
  596. return CURLE_OK;
  597. Curl_dyn_free(&s->rcvbuf);
  598. failf(data, "Received HTTP code %d from proxy after CONNECT",
  599. data->req.httpcode);
  600. return CURLE_RECV_ERROR;
  601. }
  602. s->tunnel_state = TUNNEL_COMPLETE;
  603. /* If a proxy-authorization header was used for the proxy, then we should
  604. make sure that it isn't accidentally used for the document request
  605. after we've connected. So let's free and clear it here. */
  606. Curl_safefree(data->state.aptr.proxyuserpwd);
  607. data->state.aptr.proxyuserpwd = NULL;
  608. data->state.authproxy.done = TRUE;
  609. data->state.authproxy.multipass = FALSE;
  610. infof(data, "Proxy replied %d to CONNECT request",
  611. data->info.httpproxycode);
  612. data->req.ignorebody = FALSE; /* put it (back) to non-ignore state */
  613. conn->bits.rewindaftersend = FALSE; /* make sure this isn't set for the
  614. document request */
  615. Curl_dyn_free(&s->rcvbuf);
  616. return CURLE_OK;
  617. }
  618. #else
  619. /* The Hyper version of CONNECT */
  620. static CURLcode CONNECT(struct Curl_easy *data,
  621. int sockindex,
  622. const char *hostname,
  623. int remote_port)
  624. {
  625. struct connectdata *conn = data->conn;
  626. struct hyptransfer *h = &data->hyp;
  627. curl_socket_t tunnelsocket = conn->sock[sockindex];
  628. struct http_connect_state *s = conn->connect_state;
  629. CURLcode result = CURLE_OUT_OF_MEMORY;
  630. hyper_io *io = NULL;
  631. hyper_request *req = NULL;
  632. hyper_headers *headers = NULL;
  633. hyper_clientconn_options *options = NULL;
  634. hyper_task *handshake = NULL;
  635. hyper_task *task = NULL; /* for the handshake */
  636. hyper_task *sendtask = NULL; /* for the send */
  637. hyper_clientconn *client = NULL;
  638. hyper_error *hypererr = NULL;
  639. char *hostheader = NULL; /* for CONNECT */
  640. char *host = NULL; /* Host: */
  641. if(Curl_connect_complete(conn))
  642. return CURLE_OK; /* CONNECT is already completed */
  643. conn->bits.proxy_connect_closed = FALSE;
  644. do {
  645. switch(s->tunnel_state) {
  646. case TUNNEL_INIT:
  647. /* BEGIN CONNECT PHASE */
  648. io = hyper_io_new();
  649. if(!io) {
  650. failf(data, "Couldn't create hyper IO");
  651. result = CURLE_OUT_OF_MEMORY;
  652. goto error;
  653. }
  654. /* tell Hyper how to read/write network data */
  655. hyper_io_set_userdata(io, data);
  656. hyper_io_set_read(io, Curl_hyper_recv);
  657. hyper_io_set_write(io, Curl_hyper_send);
  658. conn->sockfd = tunnelsocket;
  659. data->state.hconnect = TRUE;
  660. /* create an executor to poll futures */
  661. if(!h->exec) {
  662. h->exec = hyper_executor_new();
  663. if(!h->exec) {
  664. failf(data, "Couldn't create hyper executor");
  665. result = CURLE_OUT_OF_MEMORY;
  666. goto error;
  667. }
  668. }
  669. options = hyper_clientconn_options_new();
  670. hyper_clientconn_options_set_preserve_header_case(options, 1);
  671. hyper_clientconn_options_set_preserve_header_order(options, 1);
  672. if(!options) {
  673. failf(data, "Couldn't create hyper client options");
  674. result = CURLE_OUT_OF_MEMORY;
  675. goto error;
  676. }
  677. hyper_clientconn_options_exec(options, h->exec);
  678. /* "Both the `io` and the `options` are consumed in this function
  679. call" */
  680. handshake = hyper_clientconn_handshake(io, options);
  681. if(!handshake) {
  682. failf(data, "Couldn't create hyper client handshake");
  683. result = CURLE_OUT_OF_MEMORY;
  684. goto error;
  685. }
  686. io = NULL;
  687. options = NULL;
  688. if(HYPERE_OK != hyper_executor_push(h->exec, handshake)) {
  689. failf(data, "Couldn't hyper_executor_push the handshake");
  690. result = CURLE_OUT_OF_MEMORY;
  691. goto error;
  692. }
  693. handshake = NULL; /* ownership passed on */
  694. task = hyper_executor_poll(h->exec);
  695. if(!task) {
  696. failf(data, "Couldn't hyper_executor_poll the handshake");
  697. result = CURLE_OUT_OF_MEMORY;
  698. goto error;
  699. }
  700. client = hyper_task_value(task);
  701. hyper_task_free(task);
  702. req = hyper_request_new();
  703. if(!req) {
  704. failf(data, "Couldn't hyper_request_new");
  705. result = CURLE_OUT_OF_MEMORY;
  706. goto error;
  707. }
  708. if(hyper_request_set_method(req, (uint8_t *)"CONNECT",
  709. strlen("CONNECT"))) {
  710. failf(data, "error setting method");
  711. result = CURLE_OUT_OF_MEMORY;
  712. goto error;
  713. }
  714. infof(data, "Establish HTTP proxy tunnel to %s:%d",
  715. hostname, remote_port);
  716. /* This only happens if we've looped here due to authentication
  717. reasons, and we don't really use the newly cloned URL here
  718. then. Just free() it. */
  719. Curl_safefree(data->req.newurl);
  720. result = CONNECT_host(data, conn, hostname, remote_port,
  721. &hostheader, &host);
  722. if(result)
  723. goto error;
  724. if(hyper_request_set_uri(req, (uint8_t *)hostheader,
  725. strlen(hostheader))) {
  726. failf(data, "error setting path");
  727. result = CURLE_OUT_OF_MEMORY;
  728. goto error;
  729. }
  730. if(data->set.verbose) {
  731. char *se = aprintf("CONNECT %s HTTP/1.1\r\n", hostheader);
  732. if(!se) {
  733. result = CURLE_OUT_OF_MEMORY;
  734. goto error;
  735. }
  736. Curl_debug(data, CURLINFO_HEADER_OUT, se, strlen(se));
  737. free(se);
  738. }
  739. /* Setup the proxy-authorization header, if any */
  740. result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
  741. hostheader, TRUE);
  742. if(result)
  743. goto error;
  744. Curl_safefree(hostheader);
  745. /* default is 1.1 */
  746. if((conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) &&
  747. (HYPERE_OK != hyper_request_set_version(req,
  748. HYPER_HTTP_VERSION_1_0))) {
  749. failf(data, "error setting HTTP version");
  750. result = CURLE_OUT_OF_MEMORY;
  751. goto error;
  752. }
  753. headers = hyper_request_headers(req);
  754. if(!headers) {
  755. failf(data, "hyper_request_headers");
  756. result = CURLE_OUT_OF_MEMORY;
  757. goto error;
  758. }
  759. if(host) {
  760. result = Curl_hyper_header(data, headers, host);
  761. if(result)
  762. goto error;
  763. Curl_safefree(host);
  764. }
  765. if(data->state.aptr.proxyuserpwd) {
  766. result = Curl_hyper_header(data, headers,
  767. data->state.aptr.proxyuserpwd);
  768. if(result)
  769. goto error;
  770. }
  771. if(!Curl_checkProxyheaders(data, conn, STRCONST("User-Agent")) &&
  772. data->set.str[STRING_USERAGENT]) {
  773. struct dynbuf ua;
  774. Curl_dyn_init(&ua, DYN_HTTP_REQUEST);
  775. result = Curl_dyn_addf(&ua, "User-Agent: %s\r\n",
  776. data->set.str[STRING_USERAGENT]);
  777. if(result)
  778. goto error;
  779. result = Curl_hyper_header(data, headers, Curl_dyn_ptr(&ua));
  780. if(result)
  781. goto error;
  782. Curl_dyn_free(&ua);
  783. }
  784. if(!Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection"))) {
  785. result = Curl_hyper_header(data, headers,
  786. "Proxy-Connection: Keep-Alive");
  787. if(result)
  788. goto error;
  789. }
  790. result = Curl_add_custom_headers(data, TRUE, headers);
  791. if(result)
  792. goto error;
  793. sendtask = hyper_clientconn_send(client, req);
  794. if(!sendtask) {
  795. failf(data, "hyper_clientconn_send");
  796. result = CURLE_OUT_OF_MEMORY;
  797. goto error;
  798. }
  799. if(HYPERE_OK != hyper_executor_push(h->exec, sendtask)) {
  800. failf(data, "Couldn't hyper_executor_push the send");
  801. result = CURLE_OUT_OF_MEMORY;
  802. goto error;
  803. }
  804. hyper_clientconn_free(client);
  805. do {
  806. task = hyper_executor_poll(h->exec);
  807. if(task) {
  808. bool error = hyper_task_type(task) == HYPER_TASK_ERROR;
  809. if(error)
  810. hypererr = hyper_task_value(task);
  811. hyper_task_free(task);
  812. if(error) {
  813. /* this could probably use a better error code? */
  814. result = CURLE_OUT_OF_MEMORY;
  815. goto error;
  816. }
  817. }
  818. } while(task);
  819. s->tunnel_state = TUNNEL_CONNECT;
  820. /* FALLTHROUGH */
  821. case TUNNEL_CONNECT: {
  822. int didwhat;
  823. bool done = FALSE;
  824. result = Curl_hyper_stream(data, conn, &didwhat, &done,
  825. CURL_CSELECT_IN | CURL_CSELECT_OUT);
  826. if(result)
  827. goto error;
  828. if(!done)
  829. break;
  830. s->tunnel_state = TUNNEL_COMPLETE;
  831. if(h->exec) {
  832. hyper_executor_free(h->exec);
  833. h->exec = NULL;
  834. }
  835. if(h->read_waker) {
  836. hyper_waker_free(h->read_waker);
  837. h->read_waker = NULL;
  838. }
  839. if(h->write_waker) {
  840. hyper_waker_free(h->write_waker);
  841. h->write_waker = NULL;
  842. }
  843. }
  844. break;
  845. default:
  846. break;
  847. }
  848. if(conn->bits.close && data->req.newurl) {
  849. /* Connection closed by server. Don't use it anymore */
  850. Curl_closesocket(data, conn, conn->sock[sockindex]);
  851. conn->sock[sockindex] = CURL_SOCKET_BAD;
  852. break;
  853. }
  854. /* If we are supposed to continue and request a new URL, which basically
  855. * means the HTTP authentication is still going on so if the tunnel
  856. * is complete we start over in INIT state */
  857. if(data->req.newurl && (TUNNEL_COMPLETE == s->tunnel_state)) {
  858. infof(data, "CONNECT request done, loop to make another");
  859. connect_init(data, TRUE); /* reinit */
  860. }
  861. } while(data->req.newurl);
  862. result = CURLE_OK;
  863. if(s->tunnel_state == TUNNEL_COMPLETE) {
  864. if(data->info.httpproxycode/100 != 2) {
  865. if(conn->bits.close && data->req.newurl) {
  866. conn->bits.proxy_connect_closed = TRUE;
  867. infof(data, "Connect me again please");
  868. Curl_connect_done(data);
  869. }
  870. else {
  871. free(data->req.newurl);
  872. data->req.newurl = NULL;
  873. /* failure, close this connection to avoid re-use */
  874. streamclose(conn, "proxy CONNECT failure");
  875. Curl_closesocket(data, conn, conn->sock[sockindex]);
  876. conn->sock[sockindex] = CURL_SOCKET_BAD;
  877. }
  878. /* to back to init state */
  879. s->tunnel_state = TUNNEL_INIT;
  880. if(!conn->bits.proxy_connect_closed) {
  881. failf(data, "Received HTTP code %d from proxy after CONNECT",
  882. data->req.httpcode);
  883. result = CURLE_RECV_ERROR;
  884. }
  885. }
  886. }
  887. error:
  888. free(host);
  889. free(hostheader);
  890. if(io)
  891. hyper_io_free(io);
  892. if(options)
  893. hyper_clientconn_options_free(options);
  894. if(handshake)
  895. hyper_task_free(handshake);
  896. if(hypererr) {
  897. uint8_t errbuf[256];
  898. size_t errlen = hyper_error_print(hypererr, errbuf, sizeof(errbuf));
  899. failf(data, "Hyper: %.*s", (int)errlen, errbuf);
  900. hyper_error_free(hypererr);
  901. }
  902. return result;
  903. }
  904. #endif
  905. void Curl_connect_free(struct Curl_easy *data)
  906. {
  907. struct connectdata *conn = data->conn;
  908. struct http_connect_state *s = conn->connect_state;
  909. if(s) {
  910. free(s);
  911. conn->connect_state = NULL;
  912. }
  913. }
  914. /*
  915. * Curl_proxyCONNECT() requires that we're connected to an HTTP proxy. This
  916. * function will issue the necessary commands to get a seamless tunnel through
  917. * this proxy. After that, the socket can be used just as a normal socket.
  918. */
  919. CURLcode Curl_proxyCONNECT(struct Curl_easy *data,
  920. int sockindex,
  921. const char *hostname,
  922. int remote_port)
  923. {
  924. CURLcode result;
  925. struct connectdata *conn = data->conn;
  926. if(!conn->connect_state) {
  927. result = connect_init(data, FALSE);
  928. if(result)
  929. return result;
  930. }
  931. result = CONNECT(data, sockindex, hostname, remote_port);
  932. if(result || Curl_connect_complete(conn))
  933. Curl_connect_done(data);
  934. return result;
  935. }
  936. #else
  937. void Curl_connect_free(struct Curl_easy *data)
  938. {
  939. (void)data;
  940. }
  941. #endif /* CURL_DISABLE_PROXY */