http_proxy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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_HTTP) && !defined(CURL_DISABLE_PROXY)
  27. #include <curl/curl.h>
  28. #include "sendf.h"
  29. #include "http.h"
  30. #include "url.h"
  31. #include "select.h"
  32. #include "progress.h"
  33. #include "cfilters.h"
  34. #include "cf-h1-proxy.h"
  35. #include "cf-h2-proxy.h"
  36. #include "connect.h"
  37. #include "curlx.h"
  38. #include "vtls/vtls.h"
  39. #include "transfer.h"
  40. #include "multiif.h"
  41. #include "vauth/vauth.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. static bool hd_name_eq(const char *n1, size_t n1len,
  47. const char *n2, size_t n2len)
  48. {
  49. return (n1len == n2len) ? strncasecompare(n1, n2, n1len) : FALSE;
  50. }
  51. static CURLcode dynhds_add_custom(struct Curl_easy *data,
  52. bool is_connect, int httpversion,
  53. struct dynhds *hds)
  54. {
  55. struct connectdata *conn = data->conn;
  56. char *ptr;
  57. struct curl_slist *h[2];
  58. struct curl_slist *headers;
  59. int numlists = 1; /* by default */
  60. int i;
  61. enum Curl_proxy_use proxy;
  62. if(is_connect)
  63. proxy = HEADER_CONNECT;
  64. else
  65. proxy = conn->bits.httpproxy && !conn->bits.tunnel_proxy ?
  66. HEADER_PROXY : HEADER_SERVER;
  67. switch(proxy) {
  68. case HEADER_SERVER:
  69. h[0] = data->set.headers;
  70. break;
  71. case HEADER_PROXY:
  72. h[0] = data->set.headers;
  73. if(data->set.sep_headers) {
  74. h[1] = data->set.proxyheaders;
  75. numlists++;
  76. }
  77. break;
  78. case HEADER_CONNECT:
  79. if(data->set.sep_headers)
  80. h[0] = data->set.proxyheaders;
  81. else
  82. h[0] = data->set.headers;
  83. break;
  84. }
  85. /* loop through one or two lists */
  86. for(i = 0; i < numlists; i++) {
  87. for(headers = h[i]; headers; headers = headers->next) {
  88. const char *name, *value;
  89. size_t namelen, valuelen;
  90. /* There are 2 quirks in place for custom headers:
  91. * 1. setting only 'name:' to suppress a header from being sent
  92. * 2. setting only 'name;' to send an empty (illegal) header
  93. */
  94. ptr = strchr(headers->data, ':');
  95. if(ptr) {
  96. name = headers->data;
  97. namelen = ptr - headers->data;
  98. ptr++; /* pass the colon */
  99. while(*ptr && ISSPACE(*ptr))
  100. ptr++;
  101. if(*ptr) {
  102. value = ptr;
  103. valuelen = strlen(value);
  104. }
  105. else {
  106. /* quirk #1, suppress this header */
  107. continue;
  108. }
  109. }
  110. else {
  111. ptr = strchr(headers->data, ';');
  112. if(!ptr) {
  113. /* neither : nor ; in provided header value. We seem
  114. * to ignore this silently */
  115. continue;
  116. }
  117. name = headers->data;
  118. namelen = ptr - headers->data;
  119. ptr++; /* pass the semicolon */
  120. while(*ptr && ISSPACE(*ptr))
  121. ptr++;
  122. if(!*ptr) {
  123. /* quirk #2, send an empty header */
  124. value = "";
  125. valuelen = 0;
  126. }
  127. else {
  128. /* this may be used for something else in the future,
  129. * ignore this for now */
  130. continue;
  131. }
  132. }
  133. DEBUGASSERT(name && value);
  134. if(data->state.aptr.host &&
  135. /* a Host: header was sent already, do not pass on any custom Host:
  136. header as that will produce *two* in the same request! */
  137. hd_name_eq(name, namelen, STRCONST("Host:")))
  138. ;
  139. else if(data->state.httpreq == HTTPREQ_POST_FORM &&
  140. /* this header (extended by formdata.c) is sent later */
  141. hd_name_eq(name, namelen, STRCONST("Content-Type:")))
  142. ;
  143. else if(data->state.httpreq == HTTPREQ_POST_MIME &&
  144. /* this header is sent later */
  145. hd_name_eq(name, namelen, STRCONST("Content-Type:")))
  146. ;
  147. else if(data->req.authneg &&
  148. /* while doing auth neg, do not allow the custom length since
  149. we will force length zero then */
  150. hd_name_eq(name, namelen, STRCONST("Content-Length:")))
  151. ;
  152. else if(data->state.aptr.te &&
  153. /* when asking for Transfer-Encoding, do not pass on a custom
  154. Connection: */
  155. hd_name_eq(name, namelen, STRCONST("Connection:")))
  156. ;
  157. else if((httpversion >= 20) &&
  158. hd_name_eq(name, namelen, STRCONST("Transfer-Encoding:")))
  159. /* HTTP/2 and HTTP/3 do not support chunked requests */
  160. ;
  161. else if((hd_name_eq(name, namelen, STRCONST("Authorization:")) ||
  162. hd_name_eq(name, namelen, STRCONST("Cookie:"))) &&
  163. /* be careful of sending this potentially sensitive header to
  164. other hosts */
  165. !Curl_auth_allowed_to_host(data))
  166. ;
  167. else {
  168. CURLcode result;
  169. result = Curl_dynhds_add(hds, name, namelen, value, valuelen);
  170. if(result)
  171. return result;
  172. }
  173. }
  174. }
  175. return CURLE_OK;
  176. }
  177. CURLcode Curl_http_proxy_get_destination(struct Curl_cfilter *cf,
  178. const char **phostname,
  179. int *pport, bool *pipv6_ip)
  180. {
  181. DEBUGASSERT(cf);
  182. DEBUGASSERT(cf->conn);
  183. if(cf->conn->bits.conn_to_host)
  184. *phostname = cf->conn->conn_to_host.name;
  185. else if(cf->sockindex == SECONDARYSOCKET)
  186. *phostname = cf->conn->secondaryhostname;
  187. else
  188. *phostname = cf->conn->host.name;
  189. if(cf->sockindex == SECONDARYSOCKET)
  190. *pport = cf->conn->secondary_port;
  191. else if(cf->conn->bits.conn_to_port)
  192. *pport = cf->conn->conn_to_port;
  193. else
  194. *pport = cf->conn->remote_port;
  195. if(*phostname != cf->conn->host.name)
  196. *pipv6_ip = (strchr(*phostname, ':') != NULL);
  197. else
  198. *pipv6_ip = cf->conn->bits.ipv6_ip;
  199. return CURLE_OK;
  200. }
  201. struct cf_proxy_ctx {
  202. /* the protocol specific sub-filter we install during connect */
  203. struct Curl_cfilter *cf_protocol;
  204. int httpversion; /* HTTP version used to CONNECT */
  205. };
  206. CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,
  207. struct Curl_cfilter *cf,
  208. struct Curl_easy *data,
  209. int http_version_major)
  210. {
  211. struct cf_proxy_ctx *ctx = cf->ctx;
  212. const char *hostname = NULL;
  213. char *authority = NULL;
  214. int port;
  215. bool ipv6_ip;
  216. CURLcode result;
  217. struct httpreq *req = NULL;
  218. result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
  219. if(result)
  220. goto out;
  221. authority = aprintf("%s%s%s:%d", ipv6_ip ? "[" : "", hostname,
  222. ipv6_ip ?"]" : "", port);
  223. if(!authority) {
  224. result = CURLE_OUT_OF_MEMORY;
  225. goto out;
  226. }
  227. result = Curl_http_req_make(&req, "CONNECT", sizeof("CONNECT")-1,
  228. NULL, 0, authority, strlen(authority),
  229. NULL, 0);
  230. if(result)
  231. goto out;
  232. /* Setup the proxy-authorization header, if any */
  233. result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
  234. req->authority, TRUE);
  235. if(result)
  236. goto out;
  237. /* If user is not overriding Host: header, we add for HTTP/1.x */
  238. if(http_version_major == 1 &&
  239. !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
  240. result = Curl_dynhds_cadd(&req->headers, "Host", authority);
  241. if(result)
  242. goto out;
  243. }
  244. if(data->state.aptr.proxyuserpwd) {
  245. result = Curl_dynhds_h1_cadd_line(&req->headers,
  246. data->state.aptr.proxyuserpwd);
  247. if(result)
  248. goto out;
  249. }
  250. if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
  251. data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) {
  252. result = Curl_dynhds_cadd(&req->headers, "User-Agent",
  253. data->set.str[STRING_USERAGENT]);
  254. if(result)
  255. goto out;
  256. }
  257. if(http_version_major == 1 &&
  258. !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
  259. result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
  260. if(result)
  261. goto out;
  262. }
  263. result = dynhds_add_custom(data, TRUE, ctx->httpversion, &req->headers);
  264. out:
  265. if(result && req) {
  266. Curl_http_req_free(req);
  267. req = NULL;
  268. }
  269. free(authority);
  270. *preq = req;
  271. return result;
  272. }
  273. static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
  274. struct Curl_easy *data,
  275. bool blocking, bool *done)
  276. {
  277. struct cf_proxy_ctx *ctx = cf->ctx;
  278. CURLcode result;
  279. if(cf->connected) {
  280. *done = TRUE;
  281. return CURLE_OK;
  282. }
  283. CURL_TRC_CF(data, cf, "connect");
  284. connect_sub:
  285. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  286. if(result || !*done)
  287. return result;
  288. *done = FALSE;
  289. if(!ctx->cf_protocol) {
  290. struct Curl_cfilter *cf_protocol = NULL;
  291. int httpversion = 0;
  292. int alpn = Curl_conn_cf_is_ssl(cf->next) ?
  293. cf->conn->proxy_alpn : CURL_HTTP_VERSION_1_1;
  294. /* First time call after the subchain connected */
  295. switch(alpn) {
  296. case CURL_HTTP_VERSION_NONE:
  297. case CURL_HTTP_VERSION_1_0:
  298. case CURL_HTTP_VERSION_1_1:
  299. CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.1");
  300. infof(data, "CONNECT tunnel: HTTP/1.%d negotiated",
  301. (alpn == CURL_HTTP_VERSION_1_0) ? 0 : 1);
  302. result = Curl_cf_h1_proxy_insert_after(cf, data);
  303. if(result)
  304. goto out;
  305. cf_protocol = cf->next;
  306. httpversion = (alpn == CURL_HTTP_VERSION_1_0) ? 10 : 11;
  307. break;
  308. #ifdef USE_NGHTTP2
  309. case CURL_HTTP_VERSION_2:
  310. CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
  311. infof(data, "CONNECT tunnel: HTTP/2 negotiated");
  312. result = Curl_cf_h2_proxy_insert_after(cf, data);
  313. if(result)
  314. goto out;
  315. cf_protocol = cf->next;
  316. httpversion = 20;
  317. break;
  318. #endif
  319. default:
  320. infof(data, "CONNECT tunnel: unsupported ALPN(%d) negotiated", alpn);
  321. result = CURLE_COULDNT_CONNECT;
  322. goto out;
  323. }
  324. ctx->cf_protocol = cf_protocol;
  325. ctx->httpversion = httpversion;
  326. /* after we installed the filter "below" us, we call connect
  327. * on out sub-chain again.
  328. */
  329. goto connect_sub;
  330. }
  331. else {
  332. /* subchain connected and we had already installed the protocol filter.
  333. * This means the protocol tunnel is established, we are done.
  334. */
  335. DEBUGASSERT(ctx->cf_protocol);
  336. result = CURLE_OK;
  337. }
  338. out:
  339. if(!result) {
  340. cf->connected = TRUE;
  341. *done = TRUE;
  342. }
  343. return result;
  344. }
  345. void Curl_cf_http_proxy_get_host(struct Curl_cfilter *cf,
  346. struct Curl_easy *data,
  347. const char **phost,
  348. const char **pdisplay_host,
  349. int *pport)
  350. {
  351. (void)data;
  352. if(!cf->connected) {
  353. *phost = cf->conn->http_proxy.host.name;
  354. *pdisplay_host = cf->conn->http_proxy.host.dispname;
  355. *pport = (int)cf->conn->http_proxy.port;
  356. }
  357. else {
  358. cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
  359. }
  360. }
  361. static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
  362. struct Curl_easy *data)
  363. {
  364. struct cf_proxy_ctx *ctx = cf->ctx;
  365. (void)data;
  366. CURL_TRC_CF(data, cf, "destroy");
  367. free(ctx);
  368. }
  369. static void http_proxy_cf_close(struct Curl_cfilter *cf,
  370. struct Curl_easy *data)
  371. {
  372. struct cf_proxy_ctx *ctx = cf->ctx;
  373. CURL_TRC_CF(data, cf, "close");
  374. cf->connected = FALSE;
  375. if(ctx->cf_protocol) {
  376. struct Curl_cfilter *f;
  377. /* if someone already removed it, we assume he also
  378. * took care of destroying it. */
  379. for(f = cf->next; f; f = f->next) {
  380. if(f == ctx->cf_protocol) {
  381. /* still in our sub-chain */
  382. Curl_conn_cf_discard_sub(cf, ctx->cf_protocol, data, FALSE);
  383. break;
  384. }
  385. }
  386. ctx->cf_protocol = NULL;
  387. }
  388. if(cf->next)
  389. cf->next->cft->do_close(cf->next, data);
  390. }
  391. struct Curl_cftype Curl_cft_http_proxy = {
  392. "HTTP-PROXY",
  393. CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
  394. 0,
  395. http_proxy_cf_destroy,
  396. http_proxy_cf_connect,
  397. http_proxy_cf_close,
  398. Curl_cf_def_shutdown,
  399. Curl_cf_http_proxy_get_host,
  400. Curl_cf_def_adjust_pollset,
  401. Curl_cf_def_data_pending,
  402. Curl_cf_def_send,
  403. Curl_cf_def_recv,
  404. Curl_cf_def_cntrl,
  405. Curl_cf_def_conn_is_alive,
  406. Curl_cf_def_conn_keep_alive,
  407. Curl_cf_def_query,
  408. };
  409. CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
  410. struct Curl_easy *data)
  411. {
  412. struct Curl_cfilter *cf;
  413. struct cf_proxy_ctx *ctx = NULL;
  414. CURLcode result;
  415. (void)data;
  416. ctx = calloc(1, sizeof(*ctx));
  417. if(!ctx) {
  418. result = CURLE_OUT_OF_MEMORY;
  419. goto out;
  420. }
  421. result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
  422. if(result)
  423. goto out;
  424. ctx = NULL;
  425. Curl_conn_cf_insert_after(cf_at, cf);
  426. out:
  427. free(ctx);
  428. return result;
  429. }
  430. #endif /* ! CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */