http_proxy.c 12 KB

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