http_proxy.c 32 KB

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