cf-h1-proxy.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
  26. #include <curl/curl.h>
  27. #ifdef USE_HYPER
  28. #include <hyper.h>
  29. #endif
  30. #include "urldata.h"
  31. #include "dynbuf.h"
  32. #include "sendf.h"
  33. #include "http.h"
  34. #include "http_proxy.h"
  35. #include "url.h"
  36. #include "select.h"
  37. #include "progress.h"
  38. #include "cfilters.h"
  39. #include "cf-h1-proxy.h"
  40. #include "connect.h"
  41. #include "curl_log.h"
  42. #include "curlx.h"
  43. #include "vtls/vtls.h"
  44. #include "transfer.h"
  45. #include "multiif.h"
  46. /* The last 3 #include files should be in this order */
  47. #include "curl_printf.h"
  48. #include "curl_memory.h"
  49. #include "memdebug.h"
  50. typedef enum {
  51. TUNNEL_INIT, /* init/default/no tunnel state */
  52. TUNNEL_CONNECT, /* CONNECT request is being send */
  53. TUNNEL_RECEIVE, /* CONNECT answer is being received */
  54. TUNNEL_RESPONSE, /* CONNECT response received completely */
  55. TUNNEL_ESTABLISHED,
  56. TUNNEL_FAILED
  57. } tunnel_state;
  58. /* struct for HTTP CONNECT tunneling */
  59. struct tunnel_state {
  60. int sockindex;
  61. const char *hostname;
  62. int remote_port;
  63. struct HTTP CONNECT;
  64. struct dynbuf rcvbuf;
  65. struct dynbuf req;
  66. size_t nsend;
  67. size_t headerlines;
  68. enum keeponval {
  69. KEEPON_DONE,
  70. KEEPON_CONNECT,
  71. KEEPON_IGNORE
  72. } keepon;
  73. curl_off_t cl; /* size of content to read and ignore */
  74. tunnel_state tunnel_state;
  75. BIT(chunked_encoding);
  76. BIT(close_connection);
  77. };
  78. static bool tunnel_is_established(struct tunnel_state *ts)
  79. {
  80. return ts && (ts->tunnel_state == TUNNEL_ESTABLISHED);
  81. }
  82. static bool tunnel_is_failed(struct tunnel_state *ts)
  83. {
  84. return ts && (ts->tunnel_state == TUNNEL_FAILED);
  85. }
  86. static CURLcode tunnel_reinit(struct tunnel_state *ts,
  87. struct connectdata *conn,
  88. struct Curl_easy *data)
  89. {
  90. (void)data;
  91. DEBUGASSERT(ts);
  92. Curl_dyn_reset(&ts->rcvbuf);
  93. Curl_dyn_reset(&ts->req);
  94. ts->tunnel_state = TUNNEL_INIT;
  95. ts->keepon = KEEPON_CONNECT;
  96. ts->cl = 0;
  97. ts->close_connection = FALSE;
  98. if(conn->bits.conn_to_host)
  99. ts->hostname = conn->conn_to_host.name;
  100. else if(ts->sockindex == SECONDARYSOCKET)
  101. ts->hostname = conn->secondaryhostname;
  102. else
  103. ts->hostname = conn->host.name;
  104. if(ts->sockindex == SECONDARYSOCKET)
  105. ts->remote_port = conn->secondary_port;
  106. else if(conn->bits.conn_to_port)
  107. ts->remote_port = conn->conn_to_port;
  108. else
  109. ts->remote_port = conn->remote_port;
  110. return CURLE_OK;
  111. }
  112. static CURLcode tunnel_init(struct tunnel_state **pts,
  113. struct Curl_easy *data,
  114. struct connectdata *conn,
  115. int sockindex)
  116. {
  117. struct tunnel_state *ts;
  118. CURLcode result;
  119. if(conn->handler->flags & PROTOPT_NOTCPPROXY) {
  120. failf(data, "%s cannot be done over CONNECT", conn->handler->scheme);
  121. return CURLE_UNSUPPORTED_PROTOCOL;
  122. }
  123. /* we might need the upload buffer for streaming a partial request */
  124. result = Curl_get_upload_buffer(data);
  125. if(result)
  126. return result;
  127. ts = calloc(1, sizeof(*ts));
  128. if(!ts)
  129. return CURLE_OUT_OF_MEMORY;
  130. ts->sockindex = sockindex;
  131. infof(data, "allocate connect buffer");
  132. Curl_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
  133. Curl_dyn_init(&ts->req, DYN_HTTP_REQUEST);
  134. *pts = ts;
  135. connkeep(conn, "HTTP proxy CONNECT");
  136. return tunnel_reinit(ts, conn, data);
  137. }
  138. static void tunnel_go_state(struct Curl_cfilter *cf,
  139. struct tunnel_state *ts,
  140. tunnel_state new_state,
  141. struct Curl_easy *data)
  142. {
  143. if(ts->tunnel_state == new_state)
  144. return;
  145. /* leaving this one */
  146. switch(ts->tunnel_state) {
  147. case TUNNEL_CONNECT:
  148. data->req.ignorebody = FALSE;
  149. break;
  150. default:
  151. break;
  152. }
  153. /* entering this one */
  154. switch(new_state) {
  155. case TUNNEL_INIT:
  156. DEBUGF(LOG_CF(data, cf, "new tunnel state 'init'"));
  157. tunnel_reinit(ts, cf->conn, data);
  158. break;
  159. case TUNNEL_CONNECT:
  160. DEBUGF(LOG_CF(data, cf, "new tunnel state 'connect'"));
  161. ts->tunnel_state = TUNNEL_CONNECT;
  162. ts->keepon = KEEPON_CONNECT;
  163. Curl_dyn_reset(&ts->rcvbuf);
  164. break;
  165. case TUNNEL_RECEIVE:
  166. DEBUGF(LOG_CF(data, cf, "new tunnel state 'receive'"));
  167. ts->tunnel_state = TUNNEL_RECEIVE;
  168. break;
  169. case TUNNEL_RESPONSE:
  170. DEBUGF(LOG_CF(data, cf, "new tunnel state 'response'"));
  171. ts->tunnel_state = TUNNEL_RESPONSE;
  172. break;
  173. case TUNNEL_ESTABLISHED:
  174. DEBUGF(LOG_CF(data, cf, "new tunnel state 'established'"));
  175. infof(data, "CONNECT phase completed");
  176. data->state.authproxy.done = TRUE;
  177. data->state.authproxy.multipass = FALSE;
  178. /* FALLTHROUGH */
  179. case TUNNEL_FAILED:
  180. if(new_state == TUNNEL_FAILED)
  181. DEBUGF(LOG_CF(data, cf, "new tunnel state 'failed'"));
  182. ts->tunnel_state = new_state;
  183. Curl_dyn_reset(&ts->rcvbuf);
  184. Curl_dyn_reset(&ts->req);
  185. /* restore the protocol pointer */
  186. data->info.httpcode = 0; /* clear it as it might've been used for the
  187. proxy */
  188. /* If a proxy-authorization header was used for the proxy, then we should
  189. make sure that it isn't accidentally used for the document request
  190. after we've connected. So let's free and clear it here. */
  191. Curl_safefree(data->state.aptr.proxyuserpwd);
  192. data->state.aptr.proxyuserpwd = NULL;
  193. #ifdef USE_HYPER
  194. data->state.hconnect = FALSE;
  195. #endif
  196. break;
  197. }
  198. }
  199. static void tunnel_free(struct Curl_cfilter *cf,
  200. struct Curl_easy *data)
  201. {
  202. struct tunnel_state *ts = cf->ctx;
  203. if(ts) {
  204. tunnel_go_state(cf, ts, TUNNEL_FAILED, data);
  205. Curl_dyn_free(&ts->rcvbuf);
  206. Curl_dyn_free(&ts->req);
  207. free(ts);
  208. cf->ctx = NULL;
  209. }
  210. }
  211. static CURLcode CONNECT_host(struct Curl_easy *data,
  212. struct connectdata *conn,
  213. const char *hostname,
  214. int remote_port,
  215. char **connecthostp,
  216. char **hostp)
  217. {
  218. char *hostheader; /* for CONNECT */
  219. char *host = NULL; /* Host: */
  220. bool ipv6_ip = conn->bits.ipv6_ip;
  221. /* the hostname may be different */
  222. if(hostname != conn->host.name)
  223. ipv6_ip = (strchr(hostname, ':') != NULL);
  224. hostheader = /* host:port with IPv6 support */
  225. aprintf("%s%s%s:%d", ipv6_ip?"[":"", hostname, ipv6_ip?"]":"",
  226. remote_port);
  227. if(!hostheader)
  228. return CURLE_OUT_OF_MEMORY;
  229. if(!Curl_checkProxyheaders(data, conn, STRCONST("Host"))) {
  230. host = aprintf("Host: %s\r\n", hostheader);
  231. if(!host) {
  232. free(hostheader);
  233. return CURLE_OUT_OF_MEMORY;
  234. }
  235. }
  236. *connecthostp = hostheader;
  237. *hostp = host;
  238. return CURLE_OK;
  239. }
  240. #ifndef USE_HYPER
  241. static CURLcode start_CONNECT(struct Curl_cfilter *cf,
  242. struct Curl_easy *data,
  243. struct tunnel_state *ts)
  244. {
  245. struct connectdata *conn = cf->conn;
  246. char *hostheader = NULL;
  247. char *host = NULL;
  248. const char *httpv;
  249. CURLcode result;
  250. infof(data, "Establish HTTP proxy tunnel to %s:%d",
  251. ts->hostname, ts->remote_port);
  252. /* This only happens if we've looped here due to authentication
  253. reasons, and we don't really use the newly cloned URL here
  254. then. Just free() it. */
  255. Curl_safefree(data->req.newurl);
  256. result = CONNECT_host(data, conn,
  257. ts->hostname, ts->remote_port,
  258. &hostheader, &host);
  259. if(result)
  260. goto out;
  261. /* Setup the proxy-authorization header, if any */
  262. result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
  263. hostheader, TRUE);
  264. if(result)
  265. goto out;
  266. httpv = (conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ? "1.0" : "1.1";
  267. result =
  268. Curl_dyn_addf(&ts->req,
  269. "CONNECT %s HTTP/%s\r\n"
  270. "%s" /* Host: */
  271. "%s", /* Proxy-Authorization */
  272. hostheader,
  273. httpv,
  274. host?host:"",
  275. data->state.aptr.proxyuserpwd?
  276. data->state.aptr.proxyuserpwd:"");
  277. if(result)
  278. goto out;
  279. if(!Curl_checkProxyheaders(data, conn, STRCONST("User-Agent"))
  280. && data->set.str[STRING_USERAGENT])
  281. result = Curl_dyn_addf(&ts->req, "User-Agent: %s\r\n",
  282. data->set.str[STRING_USERAGENT]);
  283. if(result)
  284. goto out;
  285. if(!Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection")))
  286. result = Curl_dyn_addn(&ts->req,
  287. STRCONST("Proxy-Connection: Keep-Alive\r\n"));
  288. if(result)
  289. goto out;
  290. result = Curl_add_custom_headers(data, TRUE, &ts->req);
  291. if(result)
  292. goto out;
  293. /* CRLF terminate the request */
  294. result = Curl_dyn_addn(&ts->req, STRCONST("\r\n"));
  295. if(result)
  296. goto out;
  297. /* Send the connect request to the proxy */
  298. result = Curl_buffer_send(&ts->req, data, &ts->CONNECT,
  299. &data->info.request_size, 0,
  300. ts->sockindex);
  301. ts->headerlines = 0;
  302. out:
  303. if(result)
  304. failf(data, "Failed sending CONNECT to proxy");
  305. free(host);
  306. free(hostheader);
  307. return result;
  308. }
  309. static CURLcode send_CONNECT(struct Curl_easy *data,
  310. struct connectdata *conn,
  311. struct tunnel_state *ts,
  312. bool *done)
  313. {
  314. struct SingleRequest *k = &data->req;
  315. struct HTTP *http = &ts->CONNECT;
  316. CURLcode result = CURLE_OK;
  317. if(http->sending != HTTPSEND_REQUEST)
  318. goto out;
  319. if(!ts->nsend) {
  320. size_t fillcount;
  321. k->upload_fromhere = data->state.ulbuf;
  322. result = Curl_fillreadbuffer(data, data->set.upload_buffer_size,
  323. &fillcount);
  324. if(result)
  325. goto out;
  326. ts->nsend = fillcount;
  327. }
  328. if(ts->nsend) {
  329. ssize_t bytes_written;
  330. /* write to socket (send away data) */
  331. result = Curl_write(data,
  332. conn->writesockfd, /* socket to send to */
  333. k->upload_fromhere, /* buffer pointer */
  334. ts->nsend, /* buffer size */
  335. &bytes_written); /* actually sent */
  336. if(result)
  337. goto out;
  338. /* send to debug callback! */
  339. Curl_debug(data, CURLINFO_HEADER_OUT,
  340. k->upload_fromhere, bytes_written);
  341. ts->nsend -= bytes_written;
  342. k->upload_fromhere += bytes_written;
  343. }
  344. if(!ts->nsend)
  345. http->sending = HTTPSEND_NADA;
  346. out:
  347. if(result)
  348. failf(data, "Failed sending CONNECT to proxy");
  349. *done = (http->sending != HTTPSEND_REQUEST);
  350. return result;
  351. }
  352. static CURLcode on_resp_header(struct Curl_cfilter *cf,
  353. struct Curl_easy *data,
  354. struct tunnel_state *ts,
  355. const char *header)
  356. {
  357. CURLcode result = CURLE_OK;
  358. struct SingleRequest *k = &data->req;
  359. (void)cf;
  360. if((checkprefix("WWW-Authenticate:", header) &&
  361. (401 == k->httpcode)) ||
  362. (checkprefix("Proxy-authenticate:", header) &&
  363. (407 == k->httpcode))) {
  364. bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
  365. char *auth = Curl_copy_header_value(header);
  366. if(!auth)
  367. return CURLE_OUT_OF_MEMORY;
  368. DEBUGF(LOG_CF(data, cf, "CONNECT: fwd auth header '%s'", header));
  369. result = Curl_http_input_auth(data, proxy, auth);
  370. free(auth);
  371. if(result)
  372. return result;
  373. }
  374. else if(checkprefix("Content-Length:", header)) {
  375. if(k->httpcode/100 == 2) {
  376. /* A client MUST ignore any Content-Length or Transfer-Encoding
  377. header fields received in a successful response to CONNECT.
  378. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  379. infof(data, "Ignoring Content-Length in CONNECT %03d response",
  380. k->httpcode);
  381. }
  382. else {
  383. (void)curlx_strtoofft(header + strlen("Content-Length:"),
  384. NULL, 10, &ts->cl);
  385. }
  386. }
  387. else if(Curl_compareheader(header,
  388. STRCONST("Connection:"), STRCONST("close")))
  389. ts->close_connection = TRUE;
  390. else if(checkprefix("Transfer-Encoding:", header)) {
  391. if(k->httpcode/100 == 2) {
  392. /* A client MUST ignore any Content-Length or Transfer-Encoding
  393. header fields received in a successful response to CONNECT.
  394. "Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
  395. infof(data, "Ignoring Transfer-Encoding in "
  396. "CONNECT %03d response", k->httpcode);
  397. }
  398. else if(Curl_compareheader(header,
  399. STRCONST("Transfer-Encoding:"),
  400. STRCONST("chunked"))) {
  401. infof(data, "CONNECT responded chunked");
  402. ts->chunked_encoding = TRUE;
  403. /* init our chunky engine */
  404. Curl_httpchunk_init(data);
  405. }
  406. }
  407. else if(Curl_compareheader(header,
  408. STRCONST("Proxy-Connection:"),
  409. STRCONST("close")))
  410. ts->close_connection = TRUE;
  411. else if(!strncmp(header, "HTTP/1.", 7) &&
  412. ((header[7] == '0') || (header[7] == '1')) &&
  413. (header[8] == ' ') &&
  414. ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
  415. !ISDIGIT(header[12])) {
  416. /* store the HTTP code from the proxy */
  417. data->info.httpproxycode = k->httpcode = (header[9] - '0') * 100 +
  418. (header[10] - '0') * 10 + (header[11] - '0');
  419. }
  420. return result;
  421. }
  422. static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
  423. struct Curl_easy *data,
  424. struct tunnel_state *ts,
  425. bool *done)
  426. {
  427. CURLcode result = CURLE_OK;
  428. struct SingleRequest *k = &data->req;
  429. curl_socket_t tunnelsocket = Curl_conn_cf_get_socket(cf, data);
  430. char *linep;
  431. size_t perline;
  432. int error;
  433. #define SELECT_OK 0
  434. #define SELECT_ERROR 1
  435. error = SELECT_OK;
  436. *done = FALSE;
  437. if(!Curl_conn_data_pending(data, ts->sockindex))
  438. return CURLE_OK;
  439. while(ts->keepon) {
  440. ssize_t gotbytes;
  441. char byte;
  442. /* Read one byte at a time to avoid a race condition. Wait at most one
  443. second before looping to ensure continuous pgrsUpdates. */
  444. result = Curl_read(data, tunnelsocket, &byte, 1, &gotbytes);
  445. if(result == CURLE_AGAIN)
  446. /* socket buffer drained, return */
  447. return CURLE_OK;
  448. if(Curl_pgrsUpdate(data))
  449. return CURLE_ABORTED_BY_CALLBACK;
  450. if(result) {
  451. ts->keepon = KEEPON_DONE;
  452. break;
  453. }
  454. if(gotbytes <= 0) {
  455. if(data->set.proxyauth && data->state.authproxy.avail &&
  456. data->state.aptr.proxyuserpwd) {
  457. /* proxy auth was requested and there was proxy auth available,
  458. then deem this as "mere" proxy disconnect */
  459. ts->close_connection = TRUE;
  460. infof(data, "Proxy CONNECT connection closed");
  461. }
  462. else {
  463. error = SELECT_ERROR;
  464. failf(data, "Proxy CONNECT aborted");
  465. }
  466. ts->keepon = KEEPON_DONE;
  467. break;
  468. }
  469. if(ts->keepon == KEEPON_IGNORE) {
  470. /* This means we are currently ignoring a response-body */
  471. if(ts->cl) {
  472. /* A Content-Length based body: simply count down the counter
  473. and make sure to break out of the loop when we're done! */
  474. ts->cl--;
  475. if(ts->cl <= 0) {
  476. ts->keepon = KEEPON_DONE;
  477. break;
  478. }
  479. }
  480. else {
  481. /* chunked-encoded body, so we need to do the chunked dance
  482. properly to know when the end of the body is reached */
  483. CHUNKcode r;
  484. CURLcode extra;
  485. ssize_t tookcareof = 0;
  486. /* now parse the chunked piece of data so that we can
  487. properly tell when the stream ends */
  488. r = Curl_httpchunk_read(data, &byte, 1, &tookcareof, &extra);
  489. if(r == CHUNKE_STOP) {
  490. /* we're done reading chunks! */
  491. infof(data, "chunk reading DONE");
  492. ts->keepon = KEEPON_DONE;
  493. }
  494. }
  495. continue;
  496. }
  497. if(Curl_dyn_addn(&ts->rcvbuf, &byte, 1)) {
  498. failf(data, "CONNECT response too large");
  499. return CURLE_RECV_ERROR;
  500. }
  501. /* if this is not the end of a header line then continue */
  502. if(byte != 0x0a)
  503. continue;
  504. ts->headerlines++;
  505. linep = Curl_dyn_ptr(&ts->rcvbuf);
  506. perline = Curl_dyn_len(&ts->rcvbuf); /* amount of bytes in this line */
  507. /* output debug if that is requested */
  508. Curl_debug(data, CURLINFO_HEADER_IN, linep, perline);
  509. if(!data->set.suppress_connect_headers) {
  510. /* send the header to the callback */
  511. int writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
  512. (data->set.include_header ? CLIENTWRITE_BODY : 0) |
  513. (ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
  514. result = Curl_client_write(data, writetype, linep, perline);
  515. if(result)
  516. return result;
  517. }
  518. data->info.header_size += (long)perline;
  519. /* Newlines are CRLF, so the CR is ignored as the line isn't
  520. really terminated until the LF comes. Treat a following CR
  521. as end-of-headers as well.*/
  522. if(('\r' == linep[0]) ||
  523. ('\n' == linep[0])) {
  524. /* end of response-headers from the proxy */
  525. if((407 == k->httpcode) && !data->state.authproblem) {
  526. /* If we get a 407 response code with content length
  527. when we have no auth problem, we must ignore the
  528. whole response-body */
  529. ts->keepon = KEEPON_IGNORE;
  530. if(ts->cl) {
  531. infof(data, "Ignore %" CURL_FORMAT_CURL_OFF_T
  532. " bytes of response-body", ts->cl);
  533. }
  534. else if(ts->chunked_encoding) {
  535. CHUNKcode r;
  536. CURLcode extra;
  537. infof(data, "Ignore chunked response-body");
  538. /* We set ignorebody true here since the chunked decoder
  539. function will acknowledge that. Pay attention so that this is
  540. cleared again when this function returns! */
  541. k->ignorebody = TRUE;
  542. if(linep[1] == '\n')
  543. /* this can only be a LF if the letter at index 0 was a CR */
  544. linep++;
  545. /* now parse the chunked piece of data so that we can properly
  546. tell when the stream ends */
  547. r = Curl_httpchunk_read(data, linep + 1, 1, &gotbytes,
  548. &extra);
  549. if(r == CHUNKE_STOP) {
  550. /* we're done reading chunks! */
  551. infof(data, "chunk reading DONE");
  552. ts->keepon = KEEPON_DONE;
  553. }
  554. }
  555. else {
  556. /* without content-length or chunked encoding, we
  557. can't keep the connection alive since the close is
  558. the end signal so we bail out at once instead */
  559. DEBUGF(LOG_CF(data, cf, "CONNECT: no content-length or chunked"));
  560. ts->keepon = KEEPON_DONE;
  561. }
  562. }
  563. else {
  564. ts->keepon = KEEPON_DONE;
  565. }
  566. DEBUGASSERT(ts->keepon == KEEPON_IGNORE
  567. || ts->keepon == KEEPON_DONE);
  568. continue;
  569. }
  570. result = on_resp_header(cf, data, ts, linep);
  571. if(result)
  572. return result;
  573. Curl_dyn_reset(&ts->rcvbuf);
  574. } /* while there's buffer left and loop is requested */
  575. if(error)
  576. result = CURLE_RECV_ERROR;
  577. *done = (ts->keepon == KEEPON_DONE);
  578. if(!result && *done && data->info.httpproxycode/100 != 2) {
  579. /* Deal with the possibly already received authenticate
  580. headers. 'newurl' is set to a new URL if we must loop. */
  581. result = Curl_http_auth_act(data);
  582. }
  583. return result;
  584. }
  585. #else /* USE_HYPER */
  586. /* The Hyper version of CONNECT */
  587. static CURLcode start_CONNECT(struct Curl_cfilter *cf,
  588. struct Curl_easy *data,
  589. struct tunnel_state *ts)
  590. {
  591. struct connectdata *conn = cf->conn;
  592. struct hyptransfer *h = &data->hyp;
  593. curl_socket_t tunnelsocket = Curl_conn_cf_get_socket(cf, data);
  594. hyper_io *io = NULL;
  595. hyper_request *req = NULL;
  596. hyper_headers *headers = NULL;
  597. hyper_clientconn_options *options = NULL;
  598. hyper_task *handshake = NULL;
  599. hyper_task *task = NULL; /* for the handshake */
  600. hyper_clientconn *client = NULL;
  601. hyper_task *sendtask = NULL; /* for the send */
  602. char *hostheader = NULL; /* for CONNECT */
  603. char *host = NULL; /* Host: */
  604. CURLcode result = CURLE_OUT_OF_MEMORY;
  605. io = hyper_io_new();
  606. if(!io) {
  607. failf(data, "Couldn't create hyper IO");
  608. result = CURLE_OUT_OF_MEMORY;
  609. goto error;
  610. }
  611. /* tell Hyper how to read/write network data */
  612. hyper_io_set_userdata(io, data);
  613. hyper_io_set_read(io, Curl_hyper_recv);
  614. hyper_io_set_write(io, Curl_hyper_send);
  615. conn->sockfd = tunnelsocket;
  616. data->state.hconnect = TRUE;
  617. /* create an executor to poll futures */
  618. if(!h->exec) {
  619. h->exec = hyper_executor_new();
  620. if(!h->exec) {
  621. failf(data, "Couldn't create hyper executor");
  622. result = CURLE_OUT_OF_MEMORY;
  623. goto error;
  624. }
  625. }
  626. options = hyper_clientconn_options_new();
  627. hyper_clientconn_options_set_preserve_header_case(options, 1);
  628. hyper_clientconn_options_set_preserve_header_order(options, 1);
  629. if(!options) {
  630. failf(data, "Couldn't create hyper client options");
  631. result = CURLE_OUT_OF_MEMORY;
  632. goto error;
  633. }
  634. hyper_clientconn_options_exec(options, h->exec);
  635. /* "Both the `io` and the `options` are consumed in this function
  636. call" */
  637. handshake = hyper_clientconn_handshake(io, options);
  638. if(!handshake) {
  639. failf(data, "Couldn't create hyper client handshake");
  640. result = CURLE_OUT_OF_MEMORY;
  641. goto error;
  642. }
  643. io = NULL;
  644. options = NULL;
  645. if(HYPERE_OK != hyper_executor_push(h->exec, handshake)) {
  646. failf(data, "Couldn't hyper_executor_push the handshake");
  647. result = CURLE_OUT_OF_MEMORY;
  648. goto error;
  649. }
  650. handshake = NULL; /* ownership passed on */
  651. task = hyper_executor_poll(h->exec);
  652. if(!task) {
  653. failf(data, "Couldn't hyper_executor_poll the handshake");
  654. result = CURLE_OUT_OF_MEMORY;
  655. goto error;
  656. }
  657. client = hyper_task_value(task);
  658. hyper_task_free(task);
  659. req = hyper_request_new();
  660. if(!req) {
  661. failf(data, "Couldn't hyper_request_new");
  662. result = CURLE_OUT_OF_MEMORY;
  663. goto error;
  664. }
  665. if(hyper_request_set_method(req, (uint8_t *)"CONNECT",
  666. strlen("CONNECT"))) {
  667. failf(data, "error setting method");
  668. result = CURLE_OUT_OF_MEMORY;
  669. goto error;
  670. }
  671. infof(data, "Establish HTTP proxy tunnel to %s:%d",
  672. ts->hostname, ts->remote_port);
  673. /* This only happens if we've looped here due to authentication
  674. reasons, and we don't really use the newly cloned URL here
  675. then. Just free() it. */
  676. Curl_safefree(data->req.newurl);
  677. result = CONNECT_host(data, conn, ts->hostname, ts->remote_port,
  678. &hostheader, &host);
  679. if(result)
  680. goto error;
  681. if(hyper_request_set_uri(req, (uint8_t *)hostheader,
  682. strlen(hostheader))) {
  683. failf(data, "error setting path");
  684. result = CURLE_OUT_OF_MEMORY;
  685. goto error;
  686. }
  687. if(data->set.verbose) {
  688. char *se = aprintf("CONNECT %s HTTP/1.1\r\n", hostheader);
  689. if(!se) {
  690. result = CURLE_OUT_OF_MEMORY;
  691. goto error;
  692. }
  693. Curl_debug(data, CURLINFO_HEADER_OUT, se, strlen(se));
  694. free(se);
  695. }
  696. /* Setup the proxy-authorization header, if any */
  697. result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
  698. hostheader, TRUE);
  699. if(result)
  700. goto error;
  701. Curl_safefree(hostheader);
  702. /* default is 1.1 */
  703. if((conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) &&
  704. (HYPERE_OK != hyper_request_set_version(req,
  705. HYPER_HTTP_VERSION_1_0))) {
  706. failf(data, "error setting HTTP version");
  707. result = CURLE_OUT_OF_MEMORY;
  708. goto error;
  709. }
  710. headers = hyper_request_headers(req);
  711. if(!headers) {
  712. failf(data, "hyper_request_headers");
  713. result = CURLE_OUT_OF_MEMORY;
  714. goto error;
  715. }
  716. if(host) {
  717. result = Curl_hyper_header(data, headers, host);
  718. if(result)
  719. goto error;
  720. Curl_safefree(host);
  721. }
  722. if(data->state.aptr.proxyuserpwd) {
  723. result = Curl_hyper_header(data, headers,
  724. data->state.aptr.proxyuserpwd);
  725. if(result)
  726. goto error;
  727. }
  728. if(!Curl_checkProxyheaders(data, conn, STRCONST("User-Agent")) &&
  729. data->set.str[STRING_USERAGENT]) {
  730. struct dynbuf ua;
  731. Curl_dyn_init(&ua, DYN_HTTP_REQUEST);
  732. result = Curl_dyn_addf(&ua, "User-Agent: %s\r\n",
  733. data->set.str[STRING_USERAGENT]);
  734. if(result)
  735. goto error;
  736. result = Curl_hyper_header(data, headers, Curl_dyn_ptr(&ua));
  737. if(result)
  738. goto error;
  739. Curl_dyn_free(&ua);
  740. }
  741. if(!Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection"))) {
  742. result = Curl_hyper_header(data, headers,
  743. "Proxy-Connection: Keep-Alive");
  744. if(result)
  745. goto error;
  746. }
  747. result = Curl_add_custom_headers(data, TRUE, headers);
  748. if(result)
  749. goto error;
  750. sendtask = hyper_clientconn_send(client, req);
  751. if(!sendtask) {
  752. failf(data, "hyper_clientconn_send");
  753. result = CURLE_OUT_OF_MEMORY;
  754. goto error;
  755. }
  756. if(HYPERE_OK != hyper_executor_push(h->exec, sendtask)) {
  757. failf(data, "Couldn't hyper_executor_push the send");
  758. result = CURLE_OUT_OF_MEMORY;
  759. goto error;
  760. }
  761. error:
  762. free(host);
  763. free(hostheader);
  764. if(io)
  765. hyper_io_free(io);
  766. if(options)
  767. hyper_clientconn_options_free(options);
  768. if(handshake)
  769. hyper_task_free(handshake);
  770. if(client)
  771. hyper_clientconn_free(client);
  772. return result;
  773. }
  774. static CURLcode send_CONNECT(struct Curl_easy *data,
  775. struct connectdata *conn,
  776. struct tunnel_state *ts,
  777. bool *done)
  778. {
  779. struct hyptransfer *h = &data->hyp;
  780. hyper_task *task = NULL;
  781. hyper_error *hypererr = NULL;
  782. CURLcode result = CURLE_OK;
  783. (void)ts;
  784. (void)conn;
  785. do {
  786. task = hyper_executor_poll(h->exec);
  787. if(task) {
  788. bool error = hyper_task_type(task) == HYPER_TASK_ERROR;
  789. if(error)
  790. hypererr = hyper_task_value(task);
  791. hyper_task_free(task);
  792. if(error) {
  793. /* this could probably use a better error code? */
  794. result = CURLE_OUT_OF_MEMORY;
  795. goto error;
  796. }
  797. }
  798. } while(task);
  799. error:
  800. *done = (result == CURLE_OK);
  801. if(hypererr) {
  802. uint8_t errbuf[256];
  803. size_t errlen = hyper_error_print(hypererr, errbuf, sizeof(errbuf));
  804. failf(data, "Hyper: %.*s", (int)errlen, errbuf);
  805. hyper_error_free(hypererr);
  806. }
  807. return result;
  808. }
  809. static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
  810. struct Curl_easy *data,
  811. struct tunnel_state *ts,
  812. bool *done)
  813. {
  814. struct hyptransfer *h = &data->hyp;
  815. CURLcode result;
  816. int didwhat;
  817. (void)ts;
  818. *done = FALSE;
  819. result = Curl_hyper_stream(data, cf->conn, &didwhat, done,
  820. CURL_CSELECT_IN | CURL_CSELECT_OUT);
  821. if(result || !*done)
  822. return result;
  823. if(h->exec) {
  824. hyper_executor_free(h->exec);
  825. h->exec = NULL;
  826. }
  827. if(h->read_waker) {
  828. hyper_waker_free(h->read_waker);
  829. h->read_waker = NULL;
  830. }
  831. if(h->write_waker) {
  832. hyper_waker_free(h->write_waker);
  833. h->write_waker = NULL;
  834. }
  835. return result;
  836. }
  837. #endif /* USE_HYPER */
  838. static CURLcode CONNECT(struct Curl_cfilter *cf,
  839. struct Curl_easy *data,
  840. struct tunnel_state *ts)
  841. {
  842. struct connectdata *conn = cf->conn;
  843. CURLcode result;
  844. bool done;
  845. if(tunnel_is_established(ts))
  846. return CURLE_OK;
  847. if(tunnel_is_failed(ts))
  848. return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
  849. do {
  850. timediff_t check;
  851. check = Curl_timeleft(data, NULL, TRUE);
  852. if(check <= 0) {
  853. failf(data, "Proxy CONNECT aborted due to timeout");
  854. result = CURLE_OPERATION_TIMEDOUT;
  855. goto out;
  856. }
  857. switch(ts->tunnel_state) {
  858. case TUNNEL_INIT:
  859. /* Prepare the CONNECT request and make a first attempt to send. */
  860. DEBUGF(LOG_CF(data, cf, "CONNECT start"));
  861. result = start_CONNECT(cf, data, ts);
  862. if(result)
  863. goto out;
  864. tunnel_go_state(cf, ts, TUNNEL_CONNECT, data);
  865. /* FALLTHROUGH */
  866. case TUNNEL_CONNECT:
  867. /* see that the request is completely sent */
  868. DEBUGF(LOG_CF(data, cf, "CONNECT send"));
  869. result = send_CONNECT(data, cf->conn, ts, &done);
  870. if(result || !done)
  871. goto out;
  872. tunnel_go_state(cf, ts, TUNNEL_RECEIVE, data);
  873. /* FALLTHROUGH */
  874. case TUNNEL_RECEIVE:
  875. /* read what is there */
  876. DEBUGF(LOG_CF(data, cf, "CONNECT receive"));
  877. result = recv_CONNECT_resp(cf, data, ts, &done);
  878. if(Curl_pgrsUpdate(data)) {
  879. result = CURLE_ABORTED_BY_CALLBACK;
  880. goto out;
  881. }
  882. /* error or not complete yet. return for more multi-multi */
  883. if(result || !done)
  884. goto out;
  885. /* got it */
  886. tunnel_go_state(cf, ts, TUNNEL_RESPONSE, data);
  887. /* FALLTHROUGH */
  888. case TUNNEL_RESPONSE:
  889. DEBUGF(LOG_CF(data, cf, "CONNECT response"));
  890. if(data->req.newurl) {
  891. /* not the "final" response, we need to do a follow up request.
  892. * If the other side indicated a connection close, or if someone
  893. * else told us to close this connection, do so now.
  894. */
  895. if(ts->close_connection || conn->bits.close) {
  896. /* Close this filter and the sub-chain, re-connect the
  897. * sub-chain and continue. Closing this filter will
  898. * reset our tunnel state. To avoid recursion, we return
  899. * and expect to be called again.
  900. */
  901. DEBUGF(LOG_CF(data, cf, "CONNECT need to close+open"));
  902. infof(data, "Connect me again please");
  903. Curl_conn_cf_close(cf, data);
  904. connkeep(conn, "HTTP proxy CONNECT");
  905. result = Curl_conn_cf_connect(cf->next, data, FALSE, &done);
  906. goto out;
  907. }
  908. else {
  909. /* staying on this connection, reset state */
  910. tunnel_go_state(cf, ts, TUNNEL_INIT, data);
  911. }
  912. }
  913. break;
  914. default:
  915. break;
  916. }
  917. } while(data->req.newurl);
  918. DEBUGASSERT(ts->tunnel_state == TUNNEL_RESPONSE);
  919. if(data->info.httpproxycode/100 != 2) {
  920. /* a non-2xx response and we have no next url to try. */
  921. free(data->req.newurl);
  922. data->req.newurl = NULL;
  923. /* failure, close this connection to avoid re-use */
  924. streamclose(conn, "proxy CONNECT failure");
  925. tunnel_go_state(cf, ts, TUNNEL_FAILED, data);
  926. failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
  927. return CURLE_RECV_ERROR;
  928. }
  929. /* 2xx response, SUCCESS! */
  930. tunnel_go_state(cf, ts, TUNNEL_ESTABLISHED, data);
  931. infof(data, "CONNECT tunnel established, response %d",
  932. data->info.httpproxycode);
  933. result = CURLE_OK;
  934. out:
  935. if(result)
  936. tunnel_go_state(cf, ts, TUNNEL_FAILED, data);
  937. return result;
  938. }
  939. static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
  940. struct Curl_easy *data,
  941. bool blocking, bool *done)
  942. {
  943. CURLcode result;
  944. struct tunnel_state *ts = cf->ctx;
  945. if(cf->connected) {
  946. *done = TRUE;
  947. return CURLE_OK;
  948. }
  949. DEBUGF(LOG_CF(data, cf, "connect"));
  950. result = cf->next->cft->connect(cf->next, data, blocking, done);
  951. if(result || !*done)
  952. return result;
  953. *done = FALSE;
  954. if(!ts) {
  955. result = tunnel_init(&ts, data, cf->conn, cf->sockindex);
  956. if(result)
  957. return result;
  958. cf->ctx = ts;
  959. }
  960. /* TODO: can we do blocking? */
  961. /* We want "seamless" operations through HTTP proxy tunnel */
  962. result = CONNECT(cf, data, ts);
  963. if(result)
  964. goto out;
  965. Curl_safefree(data->state.aptr.proxyuserpwd);
  966. out:
  967. *done = (result == CURLE_OK) && tunnel_is_established(cf->ctx);
  968. if (*done) {
  969. cf->connected = TRUE;
  970. tunnel_free(cf, data);
  971. }
  972. return result;
  973. }
  974. static int cf_h1_proxy_get_select_socks(struct Curl_cfilter *cf,
  975. struct Curl_easy *data,
  976. curl_socket_t *socks)
  977. {
  978. struct tunnel_state *ts = cf->ctx;
  979. int fds;
  980. fds = cf->next->cft->get_select_socks(cf->next, data, socks);
  981. if(!fds && cf->next->connected && !cf->connected) {
  982. /* If we are not connected, but the filter "below" is
  983. * and not waiting on something, we are tunneling. */
  984. socks[0] = Curl_conn_cf_get_socket(cf, data);
  985. if(ts) {
  986. /* when we've sent a CONNECT to a proxy, we should rather either
  987. wait for the socket to become readable to be able to get the
  988. response headers or if we're still sending the request, wait
  989. for write. */
  990. if(ts->CONNECT.sending == HTTPSEND_REQUEST) {
  991. return GETSOCK_WRITESOCK(0);
  992. }
  993. return GETSOCK_READSOCK(0);
  994. }
  995. return GETSOCK_WRITESOCK(0);
  996. }
  997. return fds;
  998. }
  999. static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
  1000. struct Curl_easy *data)
  1001. {
  1002. DEBUGF(LOG_CF(data, cf, "destroy"));
  1003. tunnel_free(cf, data);
  1004. }
  1005. static void cf_h1_proxy_close(struct Curl_cfilter *cf,
  1006. struct Curl_easy *data)
  1007. {
  1008. DEBUGF(LOG_CF(data, cf, "close"));
  1009. cf->connected = FALSE;
  1010. if(cf->ctx) {
  1011. tunnel_go_state(cf, cf->ctx, TUNNEL_INIT, data);
  1012. }
  1013. if(cf->next)
  1014. cf->next->cft->close(cf->next, data);
  1015. }
  1016. struct Curl_cftype Curl_cft_h1_proxy = {
  1017. "H1-PROXY",
  1018. CF_TYPE_IP_CONNECT,
  1019. 0,
  1020. cf_h1_proxy_destroy,
  1021. cf_h1_proxy_connect,
  1022. cf_h1_proxy_close,
  1023. Curl_cf_http_proxy_get_host,
  1024. cf_h1_proxy_get_select_socks,
  1025. Curl_cf_def_data_pending,
  1026. Curl_cf_def_send,
  1027. Curl_cf_def_recv,
  1028. Curl_cf_def_cntrl,
  1029. Curl_cf_def_conn_is_alive,
  1030. Curl_cf_def_conn_keep_alive,
  1031. Curl_cf_def_query,
  1032. };
  1033. CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
  1034. struct Curl_easy *data)
  1035. {
  1036. struct Curl_cfilter *cf;
  1037. CURLcode result;
  1038. (void)data;
  1039. result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, NULL);
  1040. if(!result)
  1041. Curl_conn_cf_insert_after(cf_at, cf);
  1042. return result;
  1043. }
  1044. #endif /* !CURL_DISABLE_PROXY && ! CURL_DISABLE_HTTP */