2011-04-04 22:24:37 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-04-04 22:24:37 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-04-04 22:24:37 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2011-04-04 22:24:37 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2011-04-04 22:24:37 +08:00
|
|
|
|
2017-06-18 18:53:54 +08:00
|
|
|
#include "http_proxy.h"
|
|
|
|
|
2023-04-06 15:54:57 +08:00
|
|
|
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY)
|
2011-04-04 22:24:37 +08:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2021-01-04 16:36:41 +08:00
|
|
|
#ifdef USE_HYPER
|
|
|
|
#include <hyper.h>
|
|
|
|
#endif
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "sendf.h"
|
|
|
|
#include "http.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "progress.h"
|
2022-11-11 18:45:34 +08:00
|
|
|
#include "cfilters.h"
|
2023-04-06 15:54:57 +08:00
|
|
|
#include "cf-h1-proxy.h"
|
|
|
|
#include "cf-h2-proxy.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "connect.h"
|
2011-04-04 22:24:37 +08:00
|
|
|
#include "curlx.h"
|
2016-11-17 01:49:15 +08:00
|
|
|
#include "vtls/vtls.h"
|
2021-05-06 19:04:03 +08:00
|
|
|
#include "transfer.h"
|
|
|
|
#include "multiif.h"
|
2011-04-04 22:24:37 +08:00
|
|
|
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
2012-11-17 07:59:42 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2011-04-04 22:24:37 +08:00
|
|
|
|
2022-12-30 16:14:55 +08:00
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
CURLcode Curl_http_proxy_get_destination(struct Curl_cfilter *cf,
|
|
|
|
const char **phostname,
|
|
|
|
int *pport, bool *pipv6_ip)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(cf);
|
|
|
|
DEBUGASSERT(cf->conn);
|
|
|
|
|
|
|
|
if(cf->conn->bits.conn_to_host)
|
|
|
|
*phostname = cf->conn->conn_to_host.name;
|
|
|
|
else if(cf->sockindex == SECONDARYSOCKET)
|
|
|
|
*phostname = cf->conn->secondaryhostname;
|
|
|
|
else
|
|
|
|
*phostname = cf->conn->host.name;
|
|
|
|
|
|
|
|
if(cf->sockindex == SECONDARYSOCKET)
|
|
|
|
*pport = cf->conn->secondary_port;
|
|
|
|
else if(cf->conn->bits.conn_to_port)
|
|
|
|
*pport = cf->conn->conn_to_port;
|
|
|
|
else
|
|
|
|
*pport = cf->conn->remote_port;
|
|
|
|
|
|
|
|
if(*phostname != cf->conn->host.name)
|
|
|
|
*pipv6_ip = (strchr(*phostname, ':') != NULL);
|
|
|
|
else
|
|
|
|
*pipv6_ip = cf->conn->bits.ipv6_ip;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_http_proxy_create_CONNECT(struct httpreq **preq,
|
|
|
|
struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
int http_version_major)
|
|
|
|
{
|
|
|
|
const char *hostname = NULL;
|
|
|
|
char *authority = NULL;
|
|
|
|
int port;
|
|
|
|
bool ipv6_ip;
|
|
|
|
CURLcode result;
|
|
|
|
struct httpreq *req = NULL;
|
|
|
|
|
|
|
|
result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
authority = aprintf("%s%s%s:%d", ipv6_ip?"[":"", hostname,
|
|
|
|
ipv6_ip?"]":"", port);
|
|
|
|
if(!authority) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Curl_http_req_make(&req, "CONNECT", sizeof("CONNECT")-1,
|
|
|
|
NULL, 0, authority, strlen(authority),
|
|
|
|
NULL, 0);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Setup the proxy-authorization header, if any */
|
|
|
|
result = Curl_http_output_auth(data, cf->conn, req->method, HTTPREQ_GET,
|
|
|
|
req->authority, TRUE);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* If user is not overriding Host: header, we add for HTTP/1.x */
|
|
|
|
if(http_version_major == 1 &&
|
|
|
|
!Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
|
|
|
|
result = Curl_dynhds_cadd(&req->headers, "Host", authority);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(data->state.aptr.proxyuserpwd) {
|
|
|
|
result = Curl_dynhds_h1_cadd_line(&req->headers,
|
|
|
|
data->state.aptr.proxyuserpwd);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2024-01-11 06:20:09 +08:00
|
|
|
if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("User-Agent")) &&
|
|
|
|
data->set.str[STRING_USERAGENT] && *data->set.str[STRING_USERAGENT]) {
|
2023-09-06 20:43:22 +08:00
|
|
|
result = Curl_dynhds_cadd(&req->headers, "User-Agent",
|
|
|
|
data->set.str[STRING_USERAGENT]);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(http_version_major == 1 &&
|
|
|
|
!Curl_checkProxyheaders(data, cf->conn, STRCONST("Proxy-Connection"))) {
|
|
|
|
result = Curl_dynhds_cadd(&req->headers, "Proxy-Connection", "Keep-Alive");
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Curl_dynhds_add_custom(data, TRUE, &req->headers);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if(result && req) {
|
|
|
|
Curl_http_req_free(req);
|
|
|
|
req = NULL;
|
|
|
|
}
|
|
|
|
free(authority);
|
|
|
|
*preq = req;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-06 15:54:57 +08:00
|
|
|
struct cf_proxy_ctx {
|
|
|
|
/* the protocol specific sub-filter we install during connect */
|
|
|
|
struct Curl_cfilter *cf_protocol;
|
2022-11-11 18:45:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
bool blocking, bool *done)
|
|
|
|
{
|
2023-04-06 15:54:57 +08:00
|
|
|
struct cf_proxy_ctx *ctx = cf->ctx;
|
2022-11-11 18:45:34 +08:00
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
if(cf->connected) {
|
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
2021-05-27 22:50:24 +08:00
|
|
|
}
|
2021-01-04 16:36:41 +08:00
|
|
|
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "connect");
|
2023-04-06 15:54:57 +08:00
|
|
|
connect_sub:
|
2023-07-21 01:07:49 +08:00
|
|
|
result = cf->next->cft->do_connect(cf->next, data, blocking, done);
|
2022-11-11 18:45:34 +08:00
|
|
|
if(result || !*done)
|
|
|
|
return result;
|
2021-01-04 16:36:41 +08:00
|
|
|
|
2022-11-11 18:45:34 +08:00
|
|
|
*done = FALSE;
|
2023-04-06 15:54:57 +08:00
|
|
|
if(!ctx->cf_protocol) {
|
|
|
|
struct Curl_cfilter *cf_protocol = NULL;
|
|
|
|
int alpn = Curl_conn_cf_is_ssl(cf->next)?
|
|
|
|
cf->conn->proxy_alpn : CURL_HTTP_VERSION_1_1;
|
|
|
|
|
|
|
|
/* First time call after the subchain connected */
|
|
|
|
switch(alpn) {
|
|
|
|
case CURL_HTTP_VERSION_NONE:
|
|
|
|
case CURL_HTTP_VERSION_1_0:
|
|
|
|
case CURL_HTTP_VERSION_1_1:
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "installing subfilter for HTTP/1.1");
|
2023-04-06 15:54:57 +08:00
|
|
|
infof(data, "CONNECT tunnel: HTTP/1.%d negotiated",
|
|
|
|
(alpn == CURL_HTTP_VERSION_1_0)? 0 : 1);
|
|
|
|
result = Curl_cf_h1_proxy_insert_after(cf, data);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
cf_protocol = cf->next;
|
|
|
|
break;
|
|
|
|
#ifdef USE_NGHTTP2
|
|
|
|
case CURL_HTTP_VERSION_2:
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "installing subfilter for HTTP/2");
|
2023-04-06 15:54:57 +08:00
|
|
|
infof(data, "CONNECT tunnel: HTTP/2 negotiated");
|
|
|
|
result = Curl_cf_h2_proxy_insert_after(cf, data);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
cf_protocol = cf->next;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2023-04-07 20:05:00 +08:00
|
|
|
infof(data, "CONNECT tunnel: unsupported ALPN(%d) negotiated", alpn);
|
2023-04-06 15:54:57 +08:00
|
|
|
result = CURLE_COULDNT_CONNECT;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-11-11 18:45:34 +08:00
|
|
|
|
2023-04-06 15:54:57 +08:00
|
|
|
ctx->cf_protocol = cf_protocol;
|
|
|
|
/* after we installed the filter "below" us, we call connect
|
|
|
|
* on out sub-chain again.
|
|
|
|
*/
|
|
|
|
goto connect_sub;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* subchain connected and we had already installed the protocol filter.
|
|
|
|
* This means the protocol tunnel is established, we are done.
|
|
|
|
*/
|
|
|
|
DEBUGASSERT(ctx->cf_protocol);
|
|
|
|
result = CURLE_OK;
|
|
|
|
}
|
2022-11-11 18:45:34 +08:00
|
|
|
|
|
|
|
out:
|
2023-04-06 15:54:57 +08:00
|
|
|
if(!result) {
|
2022-11-11 18:45:34 +08:00
|
|
|
cf->connected = TRUE;
|
2023-04-06 15:54:57 +08:00
|
|
|
*done = TRUE;
|
2022-11-11 18:45:34 +08:00
|
|
|
}
|
2021-01-04 16:36:41 +08:00
|
|
|
return result;
|
|
|
|
}
|
2017-04-25 07:03:17 +08:00
|
|
|
|
2023-04-06 15:54:57 +08:00
|
|
|
void Curl_cf_http_proxy_get_host(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
const char **phost,
|
|
|
|
const char **pdisplay_host,
|
|
|
|
int *pport)
|
2022-11-22 16:55:41 +08:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
if(!cf->connected) {
|
|
|
|
*phost = cf->conn->http_proxy.host.name;
|
|
|
|
*pdisplay_host = cf->conn->http_proxy.host.dispname;
|
|
|
|
*pport = (int)cf->conn->http_proxy.port;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 18:45:34 +08:00
|
|
|
static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data)
|
|
|
|
{
|
2023-04-06 15:54:57 +08:00
|
|
|
struct cf_proxy_ctx *ctx = cf->ctx;
|
|
|
|
|
|
|
|
(void)data;
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "destroy");
|
2023-04-06 15:54:57 +08:00
|
|
|
free(ctx);
|
2022-11-11 18:45:34 +08:00
|
|
|
}
|
2017-04-25 07:03:17 +08:00
|
|
|
|
2022-11-11 18:45:34 +08:00
|
|
|
static void http_proxy_cf_close(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data)
|
|
|
|
{
|
2023-04-06 15:54:57 +08:00
|
|
|
struct cf_proxy_ctx *ctx = cf->ctx;
|
|
|
|
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "close");
|
2022-11-11 18:45:34 +08:00
|
|
|
cf->connected = FALSE;
|
2023-04-06 15:54:57 +08:00
|
|
|
if(ctx->cf_protocol) {
|
|
|
|
struct Curl_cfilter *f;
|
|
|
|
/* if someone already removed it, we assume he also
|
|
|
|
* took care of destroying it. */
|
|
|
|
for(f = cf->next; f; f = f->next) {
|
|
|
|
if(f == ctx->cf_protocol) {
|
|
|
|
/* still in our sub-chain */
|
|
|
|
Curl_conn_cf_discard_sub(cf, ctx->cf_protocol, data, FALSE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->cf_protocol = NULL;
|
2022-11-11 18:45:34 +08:00
|
|
|
}
|
2023-04-06 15:54:57 +08:00
|
|
|
if(cf->next)
|
2023-07-21 01:07:49 +08:00
|
|
|
cf->next->cft->do_close(cf->next, data);
|
2017-04-25 07:03:17 +08:00
|
|
|
}
|
|
|
|
|
2022-11-11 18:45:34 +08:00
|
|
|
|
2023-01-11 17:30:42 +08:00
|
|
|
struct Curl_cftype Curl_cft_http_proxy = {
|
2022-11-11 18:45:34 +08:00
|
|
|
"HTTP-PROXY",
|
2024-03-22 20:07:25 +08:00
|
|
|
CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
|
2023-01-11 17:30:42 +08:00
|
|
|
0,
|
2022-11-11 18:45:34 +08:00
|
|
|
http_proxy_cf_destroy,
|
|
|
|
http_proxy_cf_connect,
|
2022-11-22 16:55:41 +08:00
|
|
|
http_proxy_cf_close,
|
2023-04-06 15:54:57 +08:00
|
|
|
Curl_cf_http_proxy_get_host,
|
lib: introduce struct easy_poll_set for poll information
Connection filter had a `get_select_socks()` method, inspired by the
various `getsocks` functions involved during the lifetime of a
transfer. These, depending on transfer state (CONNECT/DO/DONE/ etc.),
return sockets to monitor and flag if this shall be done for POLLIN
and/or POLLOUT.
Due to this design, sockets and flags could only be added, not
removed. This led to problems in filters like HTTP/2 where flow control
prohibits the sending of data until the peer increases the flow
window. The general transfer loop wants to write, adds POLLOUT, the
socket is writeable but no data can be written.
This leads to cpu busy loops. To prevent that, HTTP/2 did set the
`SEND_HOLD` flag of such a blocked transfer, so the transfer loop cedes
further attempts. This works if only one such filter is involved. If a
HTTP/2 transfer goes through a HTTP/2 proxy, two filters are
setting/clearing this flag and may step on each other's toes.
Connection filters `get_select_socks()` is replaced by
`adjust_pollset()`. They get passed a `struct easy_pollset` that keeps
up to `MAX_SOCKSPEREASYHANDLE` sockets and their `POLLIN|POLLOUT`
flags. This struct is initialized in `multi_getsock()` by calling the
various `getsocks()` implementations based on transfer state, as before.
After protocol handlers/transfer loop have set the sockets and flags
they want, the `easy_pollset` is *always* passed to the filters. Filters
"higher" in the chain are called first, starting at the first
not-yet-connection one. Each filter may add sockets and/or change
flags. When all flags are removed, the socket itself is removed from the
pollset.
Example:
* transfer wants to send, adds POLLOUT
* http/2 filter has a flow control block, removes POLLOUT and adds
POLLIN (it is waiting on a WINDOW_UPDATE from the server)
* TLS filter is connected and changes nothing
* h2-proxy filter also has a flow control block on its tunnel stream,
removes POLLOUT and adds POLLIN also.
* socket filter is connected and changes nothing
* The resulting pollset is then mixed together with all other transfers
and their pollsets, just as before.
Use of `SEND_HOLD` is no longer necessary in the filters.
All filters are adapted for the changed method. The handling in
`multi.c` has been adjusted, but its state handling the the protocol
handlers' `getsocks` method are untouched.
The most affected filters are http/2, ngtcp2, quiche and h2-proxy. TLS
filters needed to be adjusted for the connecting handshake read/write
handling.
No noticeable difference in performance was detected in local scorecard
runs.
Closes #11833
2023-09-04 18:06:07 +08:00
|
|
|
Curl_cf_def_adjust_pollset,
|
2022-11-11 18:45:34 +08:00
|
|
|
Curl_cf_def_data_pending,
|
|
|
|
Curl_cf_def_send,
|
|
|
|
Curl_cf_def_recv,
|
2022-12-30 16:14:55 +08:00
|
|
|
Curl_cf_def_cntrl,
|
|
|
|
Curl_cf_def_conn_is_alive,
|
|
|
|
Curl_cf_def_conn_keep_alive,
|
|
|
|
Curl_cf_def_query,
|
2022-11-11 18:45:34 +08:00
|
|
|
};
|
|
|
|
|
2022-12-30 16:14:55 +08:00
|
|
|
CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
|
|
|
|
struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
struct Curl_cfilter *cf;
|
2023-04-06 15:54:57 +08:00
|
|
|
struct cf_proxy_ctx *ctx = NULL;
|
2022-12-30 16:14:55 +08:00
|
|
|
CURLcode result;
|
2022-12-27 19:10:45 +08:00
|
|
|
|
2022-12-30 16:14:55 +08:00
|
|
|
(void)data;
|
2023-04-06 15:54:57 +08:00
|
|
|
ctx = calloc(1, sizeof(*ctx));
|
2022-12-30 16:14:55 +08:00
|
|
|
if(!ctx) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
2023-04-06 15:54:57 +08:00
|
|
|
result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
|
2022-12-30 16:14:55 +08:00
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
ctx = NULL;
|
|
|
|
Curl_conn_cf_insert_after(cf_at, cf);
|
|
|
|
|
|
|
|
out:
|
2023-04-06 15:54:57 +08:00
|
|
|
free(ctx);
|
2022-11-14 23:44:12 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-06 15:54:57 +08:00
|
|
|
#endif /* ! CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */
|