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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "http_proxy.h"
  24. #if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
  25. #include <curl/curl.h>
  26. #ifdef USE_HYPER
  27. #include <hyper.h>
  28. #endif
  29. #include "sendf.h"
  30. #include "http.h"
  31. #include "url.h"
  32. #include "select.h"
  33. #include "progress.h"
  34. #include "connect.h"
  35. #include "curlx.h"
  36. #include "vtls/vtls.h"
  37. #include "transfer.h"
  38. #include "multiif.h"
  39. /* The last 3 #include files should be in this order */
  40. #include "curl_printf.h"
  41. #include "curl_memory.h"
  42. #include "memdebug.h"
  43. /*
  44. * Perform SSL initialization for HTTPS proxy. Sets
  45. * proxy_ssl_connected connection bit when complete. Can be
  46. * called multiple times.
  47. */
  48. static CURLcode https_proxy_connect(struct Curl_easy *data, int sockindex)
  49. {
  50. #ifdef USE_SSL
  51. struct connectdata *conn = data->conn;
  52. CURLcode result = CURLE_OK;
  53. DEBUGASSERT(conn->http_proxy.proxytype == CURLPROXY_HTTPS);
  54. if(!conn->bits.proxy_ssl_connected[sockindex]) {
  55. /* perform SSL initialization for this socket */
  56. result =
  57. Curl_ssl_connect_nonblocking(data, conn, TRUE, sockindex,
  58. &conn->bits.proxy_ssl_connected[sockindex]);
  59. if(result)
  60. /* a failed connection is marked for closure to prevent (bad) re-use or
  61. similar */
  62. connclose(conn, "TLS handshake failed");
  63. }
  64. return result;
  65. #else
  66. (void) data;
  67. (void) sockindex;
  68. return CURLE_NOT_BUILT_IN;
  69. #endif
  70. }
  71. CURLcode Curl_proxy_connect(struct Curl_easy *data, int sockindex)
  72. {
  73. struct connectdata *conn = data->conn;
  74. if(conn->http_proxy.proxytype == CURLPROXY_HTTPS) {
  75. const CURLcode result = https_proxy_connect(data, sockindex);
  76. if(result)
  77. return result;
  78. if(!conn->bits.proxy_ssl_connected[sockindex])
  79. return result; /* wait for HTTPS proxy SSL initialization to complete */
  80. }
  81. if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
  82. #ifndef CURL_DISABLE_PROXY
  83. /* for [protocol] tunneled through HTTP proxy */
  84. const char *hostname;
  85. int remote_port;
  86. CURLcode result;
  87. /* We want "seamless" operations through HTTP proxy tunnel */
  88. /* for the secondary socket (FTP), use the "connect to host"
  89. * but ignore the "connect to port" (use the secondary port)
  90. */
  91. if(conn->bits.conn_to_host)
  92. hostname = conn->conn_to_host.name;
  93. else if(sockindex == SECONDARYSOCKET)
  94. hostname = conn->secondaryhostname;
  95. else
  96. hostname = conn->host.name;
  97. if(sockindex == SECONDARYSOCKET)
  98. remote_port = conn->secondary_port;
  99. else if(conn->bits.conn_to_port)
  100. remote_port = conn->conn_to_port;
  101. else
  102. remote_port = conn->remote_port;
  103. result = Curl_proxyCONNECT(data, sockindex, hostname, remote_port);
  104. if(CURLE_OK != result)
  105. return result;
  106. Curl_safefree(data->state.aptr.proxyuserpwd);
  107. #else
  108. return CURLE_NOT_BUILT_IN;
  109. #endif
  110. }
  111. /* no HTTP tunnel proxy, just return */
  112. return CURLE_OK;
  113. }
  114. bool Curl_connect_complete(struct connectdata *conn)
  115. {
  116. return !conn->connect_state ||
  117. (conn->connect_state->tunnel_state >= TUNNEL_COMPLETE);
  118. }
  119. bool Curl_connect_ongoing(struct connectdata *conn)
  120. {
  121. return conn->connect_state &&
  122. (conn->connect_state->tunnel_state <= TUNNEL_COMPLETE);
  123. }
  124. /* when we've sent a CONNECT to a proxy, we should rather either wait for the
  125. socket to become readable to be able to get the response headers or if
  126. we're still sending the request, wait for write. */
  127. int Curl_connect_getsock(struct connectdata *conn)
  128. {
  129. struct HTTP *http;
  130. DEBUGASSERT(conn);
  131. DEBUGASSERT(conn->connect_state);
  132. http = &conn->connect_state->http_proxy;
  133. if(http->sending == HTTPSEND_REQUEST)
  134. return GETSOCK_WRITESOCK(0);
  135. return GETSOCK_READSOCK(0);
  136. }
  137. static CURLcode connect_init(struct Curl_easy *data, bool reinit)
  138. {
  139. struct http_connect_state *s;
  140. struct connectdata *conn = data->conn;
  141. if(conn->handler->flags & PROTOPT_NOTCPPROXY) {
  142. failf(data, "%s cannot be done over CONNECT", conn->handler->scheme);
  143. return CURLE_UNSUPPORTED_PROTOCOL;
  144. }
  145. if(!reinit) {
  146. CURLcode result;
  147. DEBUGASSERT(!conn->connect_state);
  148. /* we might need the upload buffer for streaming a partial request */
  149. result = Curl_get_upload_buffer(data);
  150. if(result)
  151. return result;
  152. s = calloc(1, sizeof(struct http_connect_state));
  153. if(!s)
  154. return CURLE_OUT_OF_MEMORY;
  155. infof(data, "allocate connect buffer");
  156. conn->connect_state = s;
  157. Curl_dyn_init(&s->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
  158. /* Curl_proxyCONNECT is based on a pointer to a struct HTTP at the
  159. * member conn->proto.http; we want [protocol] through HTTP and we have
  160. * to change the member temporarily for connecting to the HTTP
  161. * proxy. After Curl_proxyCONNECT we have to set back the member to the
  162. * original pointer
  163. *
  164. * This function might be called several times in the multi interface case
  165. * if the proxy's CONNECT response is not instant.
  166. */
  167. s->prot_save = data->req.p.http;
  168. data->req.p.http = &s->http_proxy;
  169. connkeep(conn, "HTTP proxy CONNECT");
  170. }
  171. else {
  172. DEBUGASSERT(conn->connect_state);
  173. s = conn->connect_state;
  174. Curl_dyn_reset(&s->rcvbuf);
  175. }
  176. s->tunnel_state = TUNNEL_INIT;
  177. s->keepon = KEEPON_CONNECT;
  178. s->cl = 0;
  179. s->close_connection = FALSE;
  180. return CURLE_OK;
  181. }
  182. void Curl_connect_done(struct Curl_easy *data)
  183. {
  184. struct connectdata *conn = data->conn;
  185. struct http_connect_state *s = conn->connect_state;
  186. if(s && (s->tunnel_state != TUNNEL_EXIT)) {
  187. s->tunnel_state = TUNNEL_EXIT;
  188. Curl_dyn_free(&s->rcvbuf);
  189. Curl_dyn_free(&s->req);
  190. /* restore the protocol pointer, if not already done */
  191. if(s->prot_save)
  192. data->req.p.http = s->prot_save;
  193. s->prot_save = NULL;
  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. result = 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 a 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 */