http_proxy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #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 "cf-h1-proxy.h"
  38. #include "cf-h2-proxy.h"
  39. #include "connect.h"
  40. #include "curlx.h"
  41. #include "vtls/vtls.h"
  42. #include "transfer.h"
  43. #include "multiif.h"
  44. /* The last 3 #include files should be in this order */
  45. #include "curl_printf.h"
  46. #include "curl_memory.h"
  47. #include "memdebug.h"
  48. struct cf_proxy_ctx {
  49. /* the protocol specific sub-filter we install during connect */
  50. struct Curl_cfilter *cf_protocol;
  51. };
  52. static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
  53. struct Curl_easy *data,
  54. bool blocking, bool *done)
  55. {
  56. struct cf_proxy_ctx *ctx = cf->ctx;
  57. CURLcode result;
  58. if(cf->connected) {
  59. *done = TRUE;
  60. return CURLE_OK;
  61. }
  62. DEBUGF(LOG_CF(data, cf, "connect"));
  63. connect_sub:
  64. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  65. if(result || !*done)
  66. return result;
  67. *done = FALSE;
  68. if(!ctx->cf_protocol) {
  69. struct Curl_cfilter *cf_protocol = NULL;
  70. int alpn = Curl_conn_cf_is_ssl(cf->next)?
  71. cf->conn->proxy_alpn : CURL_HTTP_VERSION_1_1;
  72. /* First time call after the subchain connected */
  73. switch(alpn) {
  74. case CURL_HTTP_VERSION_NONE:
  75. case CURL_HTTP_VERSION_1_0:
  76. case CURL_HTTP_VERSION_1_1:
  77. DEBUGF(LOG_CF(data, cf, "installing subfilter for HTTP/1.1"));
  78. infof(data, "CONNECT tunnel: HTTP/1.%d negotiated",
  79. (alpn == CURL_HTTP_VERSION_1_0)? 0 : 1);
  80. result = Curl_cf_h1_proxy_insert_after(cf, data);
  81. if(result)
  82. goto out;
  83. cf_protocol = cf->next;
  84. break;
  85. #ifdef USE_NGHTTP2
  86. case CURL_HTTP_VERSION_2:
  87. DEBUGF(LOG_CF(data, cf, "installing subfilter for HTTP/2"));
  88. infof(data, "CONNECT tunnel: HTTP/2 negotiated");
  89. result = Curl_cf_h2_proxy_insert_after(cf, data);
  90. if(result)
  91. goto out;
  92. cf_protocol = cf->next;
  93. break;
  94. #endif
  95. default:
  96. DEBUGF(LOG_CF(data, cf, "installing subfilter for default HTTP/1.1"));
  97. infof(data, "CONNECT tunnel: unsupported ALPN(%d) negotiated", alpn);
  98. result = CURLE_COULDNT_CONNECT;
  99. goto out;
  100. }
  101. ctx->cf_protocol = cf_protocol;
  102. /* after we installed the filter "below" us, we call connect
  103. * on out sub-chain again.
  104. */
  105. goto connect_sub;
  106. }
  107. else {
  108. /* subchain connected and we had already installed the protocol filter.
  109. * This means the protocol tunnel is established, we are done.
  110. */
  111. DEBUGASSERT(ctx->cf_protocol);
  112. result = CURLE_OK;
  113. }
  114. out:
  115. if(!result) {
  116. cf->connected = TRUE;
  117. *done = TRUE;
  118. }
  119. return result;
  120. }
  121. void Curl_cf_http_proxy_get_host(struct Curl_cfilter *cf,
  122. struct Curl_easy *data,
  123. const char **phost,
  124. const char **pdisplay_host,
  125. int *pport)
  126. {
  127. (void)data;
  128. if(!cf->connected) {
  129. *phost = cf->conn->http_proxy.host.name;
  130. *pdisplay_host = cf->conn->http_proxy.host.dispname;
  131. *pport = (int)cf->conn->http_proxy.port;
  132. }
  133. else {
  134. cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
  135. }
  136. }
  137. static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
  138. struct Curl_easy *data)
  139. {
  140. struct cf_proxy_ctx *ctx = cf->ctx;
  141. (void)data;
  142. DEBUGF(LOG_CF(data, cf, "destroy"));
  143. free(ctx);
  144. }
  145. static void http_proxy_cf_close(struct Curl_cfilter *cf,
  146. struct Curl_easy *data)
  147. {
  148. struct cf_proxy_ctx *ctx = cf->ctx;
  149. DEBUGF(LOG_CF(data, cf, "close"));
  150. cf->connected = FALSE;
  151. if(ctx->cf_protocol) {
  152. struct Curl_cfilter *f;
  153. /* if someone already removed it, we assume he also
  154. * took care of destroying it. */
  155. for(f = cf->next; f; f = f->next) {
  156. if(f == ctx->cf_protocol) {
  157. /* still in our sub-chain */
  158. Curl_conn_cf_discard_sub(cf, ctx->cf_protocol, data, FALSE);
  159. break;
  160. }
  161. }
  162. ctx->cf_protocol = NULL;
  163. }
  164. if(cf->next)
  165. cf->next->cft->do_close(cf->next, data);
  166. }
  167. struct Curl_cftype Curl_cft_http_proxy = {
  168. "HTTP-PROXY",
  169. CF_TYPE_IP_CONNECT,
  170. 0,
  171. http_proxy_cf_destroy,
  172. http_proxy_cf_connect,
  173. http_proxy_cf_close,
  174. Curl_cf_http_proxy_get_host,
  175. Curl_cf_def_get_select_socks,
  176. Curl_cf_def_data_pending,
  177. Curl_cf_def_send,
  178. Curl_cf_def_recv,
  179. Curl_cf_def_cntrl,
  180. Curl_cf_def_conn_is_alive,
  181. Curl_cf_def_conn_keep_alive,
  182. Curl_cf_def_query,
  183. };
  184. CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
  185. struct Curl_easy *data)
  186. {
  187. struct Curl_cfilter *cf;
  188. struct cf_proxy_ctx *ctx = NULL;
  189. CURLcode result;
  190. (void)data;
  191. ctx = calloc(1, sizeof(*ctx));
  192. if(!ctx) {
  193. result = CURLE_OUT_OF_MEMORY;
  194. goto out;
  195. }
  196. result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
  197. if(result)
  198. goto out;
  199. ctx = NULL;
  200. Curl_conn_cf_insert_after(cf_at, cf);
  201. out:
  202. free(ctx);
  203. return result;
  204. }
  205. #endif /* ! CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */