2
0

http_proxy.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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_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. /* The last 3 #include files should be in this order */
  42. #include "curl_printf.h"
  43. #include "curl_memory.h"
  44. #include "memdebug.h"
  45. CURLcode Curl_http_proxy_get_destination(struct Curl_cfilter *cf,
  46. const char **phostname,
  47. int *pport, bool *pipv6_ip)
  48. {
  49. DEBUGASSERT(cf);
  50. DEBUGASSERT(cf->conn);
  51. if(cf->conn->bits.conn_to_host)
  52. *phostname = cf->conn->conn_to_host.name;
  53. else if(cf->sockindex == SECONDARYSOCKET)
  54. *phostname = cf->conn->secondaryhostname;
  55. else
  56. *phostname = cf->conn->host.name;
  57. if(cf->sockindex == SECONDARYSOCKET)
  58. *pport = cf->conn->secondary_port;
  59. else if(cf->conn->bits.conn_to_port)
  60. *pport = cf->conn->conn_to_port;
  61. else
  62. *pport = cf->conn->remote_port;
  63. if(*phostname != cf->conn->host.name)
  64. *pipv6_ip = (strchr(*phostname, ':') != NULL);
  65. else
  66. *pipv6_ip = cf->conn->bits.ipv6_ip;
  67. return CURLE_OK;
  68. }
  69. CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,
  70. struct Curl_cfilter *cf,
  71. struct Curl_easy *data,
  72. int http_version_major)
  73. {
  74. const char *hostname = NULL;
  75. char *authority = NULL;
  76. int port;
  77. bool ipv6_ip;
  78. CURLcode result;
  79. struct httpreq *req = NULL;
  80. result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
  81. if(result)
  82. goto out;
  83. authority = aprintf("%s%s%s:%d", ipv6_ip ? "[" : "", hostname,
  84. ipv6_ip ?"]" : "", port);
  85. if(!authority) {
  86. result = CURLE_OUT_OF_MEMORY;
  87. goto out;
  88. }
  89. result = Curl_http_req_make(&req, "CONNECT", sizeof("CONNECT")-1,
  90. NULL, 0, authority, strlen(authority),
  91. NULL, 0);
  92. if(result)
  93. goto out;
  94. /* Setup the proxy-authorization header, if any */
  95. result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
  96. req->authority, TRUE);
  97. if(result)
  98. goto out;
  99. /* If user is not overriding Host: header, we add for HTTP/1.x */
  100. if(http_version_major == 1 &&
  101. !Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
  102. result = Curl_dynhds_cadd(&req->headers, "Host", authority);
  103. if(result)
  104. goto out;
  105. }
  106. if(data->state.aptr.proxyuserpwd) {
  107. result = Curl_dynhds_h1_cadd_line(&req->headers,
  108. data->state.aptr.proxyuserpwd);
  109. if(result)
  110. goto out;
  111. }
  112. if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
  113. data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) {
  114. result = Curl_dynhds_cadd(&req->headers, "User-Agent",
  115. data->set.str[STRING_USERAGENT]);
  116. if(result)
  117. goto out;
  118. }
  119. if(http_version_major == 1 &&
  120. !Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
  121. result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
  122. if(result)
  123. goto out;
  124. }
  125. result = Curl_dynhds_add_custom(data, TRUE, &req->headers);
  126. out:
  127. if(result && req) {
  128. Curl_http_req_free(req);
  129. req = NULL;
  130. }
  131. free(authority);
  132. *preq = req;
  133. return result;
  134. }
  135. struct cf_proxy_ctx {
  136. /* the protocol specific sub-filter we install during connect */
  137. struct Curl_cfilter *cf_protocol;
  138. };
  139. static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
  140. struct Curl_easy *data,
  141. bool blocking, bool *done)
  142. {
  143. struct cf_proxy_ctx *ctx = cf->ctx;
  144. CURLcode result;
  145. if(cf->connected) {
  146. *done = TRUE;
  147. return CURLE_OK;
  148. }
  149. CURL_TRC_CF(data, cf, "connect");
  150. connect_sub:
  151. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  152. if(result || !*done)
  153. return result;
  154. *done = FALSE;
  155. if(!ctx->cf_protocol) {
  156. struct Curl_cfilter *cf_protocol = NULL;
  157. int alpn = Curl_conn_cf_is_ssl(cf->next) ?
  158. cf->conn->proxy_alpn : CURL_HTTP_VERSION_1_1;
  159. /* First time call after the subchain connected */
  160. switch(alpn) {
  161. case CURL_HTTP_VERSION_NONE:
  162. case CURL_HTTP_VERSION_1_0:
  163. case CURL_HTTP_VERSION_1_1:
  164. CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.1");
  165. infof(data, "CONNECT tunnel: HTTP/1.%d negotiated",
  166. (alpn == CURL_HTTP_VERSION_1_0) ? 0 : 1);
  167. result = Curl_cf_h1_proxy_insert_after(cf, data);
  168. if(result)
  169. goto out;
  170. cf_protocol = cf->next;
  171. break;
  172. #ifdef USE_NGHTTP2
  173. case CURL_HTTP_VERSION_2:
  174. CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
  175. infof(data, "CONNECT tunnel: HTTP/2 negotiated");
  176. result = Curl_cf_h2_proxy_insert_after(cf, data);
  177. if(result)
  178. goto out;
  179. cf_protocol = cf->next;
  180. break;
  181. #endif
  182. default:
  183. infof(data, "CONNECT tunnel: unsupported ALPN(%d) negotiated", alpn);
  184. result = CURLE_COULDNT_CONNECT;
  185. goto out;
  186. }
  187. ctx->cf_protocol = cf_protocol;
  188. /* after we installed the filter "below" us, we call connect
  189. * on out sub-chain again.
  190. */
  191. goto connect_sub;
  192. }
  193. else {
  194. /* subchain connected and we had already installed the protocol filter.
  195. * This means the protocol tunnel is established, we are done.
  196. */
  197. DEBUGASSERT(ctx->cf_protocol);
  198. result = CURLE_OK;
  199. }
  200. out:
  201. if(!result) {
  202. cf->connected = TRUE;
  203. *done = TRUE;
  204. }
  205. return result;
  206. }
  207. void Curl_cf_http_proxy_get_host(struct Curl_cfilter *cf,
  208. struct Curl_easy *data,
  209. const char **phost,
  210. const char **pdisplay_host,
  211. int *pport)
  212. {
  213. (void)data;
  214. if(!cf->connected) {
  215. *phost = cf->conn->http_proxy.host.name;
  216. *pdisplay_host = cf->conn->http_proxy.host.dispname;
  217. *pport = (int)cf->conn->http_proxy.port;
  218. }
  219. else {
  220. cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
  221. }
  222. }
  223. static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
  224. struct Curl_easy *data)
  225. {
  226. struct cf_proxy_ctx *ctx = cf->ctx;
  227. (void)data;
  228. CURL_TRC_CF(data, cf, "destroy");
  229. free(ctx);
  230. }
  231. static void http_proxy_cf_close(struct Curl_cfilter *cf,
  232. struct Curl_easy *data)
  233. {
  234. struct cf_proxy_ctx *ctx = cf->ctx;
  235. CURL_TRC_CF(data, cf, "close");
  236. cf->connected = FALSE;
  237. if(ctx->cf_protocol) {
  238. struct Curl_cfilter *f;
  239. /* if someone already removed it, we assume he also
  240. * took care of destroying it. */
  241. for(f = cf->next; f; f = f->next) {
  242. if(f == ctx->cf_protocol) {
  243. /* still in our sub-chain */
  244. Curl_conn_cf_discard_sub(cf, ctx->cf_protocol, data, FALSE);
  245. break;
  246. }
  247. }
  248. ctx->cf_protocol = NULL;
  249. }
  250. if(cf->next)
  251. cf->next->cft->do_close(cf->next, data);
  252. }
  253. struct Curl_cftype Curl_cft_http_proxy = {
  254. "HTTP-PROXY",
  255. CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
  256. 0,
  257. http_proxy_cf_destroy,
  258. http_proxy_cf_connect,
  259. http_proxy_cf_close,
  260. Curl_cf_def_shutdown,
  261. Curl_cf_http_proxy_get_host,
  262. Curl_cf_def_adjust_pollset,
  263. Curl_cf_def_data_pending,
  264. Curl_cf_def_send,
  265. Curl_cf_def_recv,
  266. Curl_cf_def_cntrl,
  267. Curl_cf_def_conn_is_alive,
  268. Curl_cf_def_conn_keep_alive,
  269. Curl_cf_def_query,
  270. };
  271. CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
  272. struct Curl_easy *data)
  273. {
  274. struct Curl_cfilter *cf;
  275. struct cf_proxy_ctx *ctx = NULL;
  276. CURLcode result;
  277. (void)data;
  278. ctx = calloc(1, sizeof(*ctx));
  279. if(!ctx) {
  280. result = CURLE_OUT_OF_MEMORY;
  281. goto out;
  282. }
  283. result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
  284. if(result)
  285. goto out;
  286. ctx = NULL;
  287. Curl_conn_cf_insert_after(cf_at, cf);
  288. out:
  289. free(ctx);
  290. return result;
  291. }
  292. #endif /* ! CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */