http_proxy.c 34 KB

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