http_proxy.c 41 KB

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