cmake/Utilities/cmcurl/lib/http_proxy.c

231 lines
6.2 KiB
C
Raw Normal View History

2015-04-27 22:25:09 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2023-07-02 19:51:09 +02:00
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
2015-04-27 22:25:09 +02:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
2015-04-27 22:25:09 +02: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.
*
2022-11-16 20:14:03 +01:00
* SPDX-License-Identifier: curl
*
2015-04-27 22:25:09 +02:00
***************************************************************************/
#include "curl_setup.h"
2018-01-26 17:06:56 +01:00
#include "http_proxy.h"
2023-07-02 19:51:09 +02:00
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY)
2015-04-27 22:25:09 +02:00
#include <curl/curl.h>
2021-09-14 00:13:48 +02:00
#ifdef USE_HYPER
#include <hyper.h>
#endif
2015-04-27 22:25:09 +02:00
#include "sendf.h"
#include "http.h"
#include "url.h"
#include "select.h"
#include "progress.h"
2023-05-23 16:38:00 +02:00
#include "cfilters.h"
2023-07-02 19:51:09 +02:00
#include "cf-h1-proxy.h"
#include "cf-h2-proxy.h"
2015-04-27 22:25:09 +02:00
#include "connect.h"
#include "curlx.h"
2017-07-20 19:35:53 +02:00
#include "vtls/vtls.h"
2021-09-14 00:13:48 +02:00
#include "transfer.h"
#include "multiif.h"
2015-04-27 22:25:09 +02:00
2016-09-11 17:22:32 +02:00
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
2015-04-27 22:25:09 +02:00
#include "curl_memory.h"
#include "memdebug.h"
2023-05-23 16:38:00 +02:00
2023-07-02 19:51:09 +02:00
struct cf_proxy_ctx {
/* the protocol specific sub-filter we install during connect */
struct Curl_cfilter *cf_protocol;
2023-05-23 16:38:00 +02:00
};
2023-07-02 19:51:09 +02:00
static CURLcode http_proxy_cf_connect(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool blocking, bool *done)
2015-04-27 22:25:09 +02:00
{
2023-07-02 19:51:09 +02:00
struct cf_proxy_ctx *ctx = cf->ctx;
2015-04-27 22:25:09 +02:00
CURLcode result;
2023-07-02 19:51:09 +02:00
if(cf->connected) {
*done = TRUE;
2023-05-23 16:38:00 +02:00
return CURLE_OK;
}
2021-09-14 00:13:48 +02:00
2023-07-02 19:51:09 +02:00
DEBUGF(LOG_CF(data, cf, "connect"));
connect_sub:
result = cf->next->cft->connect(cf->next, data, blocking, done);
2023-05-23 16:38:00 +02:00
if(result || !*done)
return result;
2021-09-14 00:13:48 +02:00
2023-07-02 19:51:09 +02:00
*done = FALSE;
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:
DEBUGF(LOG_CF(data, cf, "installing subfilter for HTTP/1.1"));
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);
2022-03-29 21:10:50 +02:00
if(result)
2023-05-23 16:38:00 +02:00
goto out;
2023-07-02 19:51:09 +02:00
cf_protocol = cf->next;
break;
#ifdef USE_NGHTTP2
case CURL_HTTP_VERSION_2:
DEBUGF(LOG_CF(data, cf, "installing subfilter for HTTP/2"));
infof(data, "CONNECT tunnel: HTTP/2 negotiated");
result = Curl_cf_h2_proxy_insert_after(cf, data);
if(result)
2023-05-23 16:38:00 +02:00
goto out;
2023-07-02 19:51:09 +02:00
cf_protocol = cf->next;
2021-09-14 00:13:48 +02:00
break;
2023-07-02 19:51:09 +02:00
#endif
2023-05-23 16:38:00 +02:00
default:
2023-07-02 19:51:09 +02:00
DEBUGF(LOG_CF(data, cf, "installing subfilter for default HTTP/1.1"));
infof(data, "CONNECT tunnel: unsupported ALPN(%d) negotiated", alpn);
result = CURLE_COULDNT_CONNECT;
goto out;
2022-08-04 22:12:04 +02:00
}
2023-07-02 19:51:09 +02:00
ctx->cf_protocol = cf_protocol;
/* after we installed the filter "below" us, we call connect
* on out sub-chain again.
*/
goto connect_sub;
2023-05-23 16:38:00 +02:00
}
2023-07-02 19:51:09 +02:00
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;
2023-05-23 16:38:00 +02:00
}
out:
2023-07-02 19:51:09 +02:00
if(!result) {
2023-05-23 16:38:00 +02:00
cf->connected = TRUE;
2023-07-02 19:51:09 +02:00
*done = TRUE;
2021-09-14 00:13:48 +02:00
}
return result;
}
2017-07-20 19:35:53 +02:00
2023-07-02 19:51:09 +02: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)
2023-05-23 16:38:00 +02: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);
}
}
static void http_proxy_cf_destroy(struct Curl_cfilter *cf,
struct Curl_easy *data)
{
2023-07-02 19:51:09 +02:00
struct cf_proxy_ctx *ctx = cf->ctx;
(void)data;
DEBUGF(LOG_CF(data, cf, "destroy"));
free(ctx);
2023-05-23 16:38:00 +02:00
}
static void http_proxy_cf_close(struct Curl_cfilter *cf,
struct Curl_easy *data)
2018-01-26 17:06:56 +01:00
{
2023-07-02 19:51:09 +02:00
struct cf_proxy_ctx *ctx = cf->ctx;
DEBUGF(LOG_CF(data, cf, "close"));
2023-05-23 16:38:00 +02:00
cf->connected = FALSE;
2023-07-02 19:51:09 +02: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;
2018-01-26 17:06:56 +01:00
}
2023-07-02 19:51:09 +02:00
if(cf->next)
cf->next->cft->close(cf->next, data);
2018-01-26 17:06:56 +01:00
}
2017-07-20 19:35:53 +02:00
2023-07-02 19:51:09 +02:00
struct Curl_cftype Curl_cft_http_proxy = {
2023-05-23 16:38:00 +02:00
"HTTP-PROXY",
CF_TYPE_IP_CONNECT,
2023-07-02 19:51:09 +02:00
0,
2023-05-23 16:38:00 +02:00
http_proxy_cf_destroy,
http_proxy_cf_connect,
http_proxy_cf_close,
2023-07-02 19:51:09 +02:00
Curl_cf_http_proxy_get_host,
Curl_cf_def_get_select_socks,
2023-05-23 16:38:00 +02:00
Curl_cf_def_data_pending,
Curl_cf_def_send,
Curl_cf_def_recv,
2023-07-02 19:51:09 +02:00
Curl_cf_def_cntrl,
Curl_cf_def_conn_is_alive,
Curl_cf_def_conn_keep_alive,
Curl_cf_def_query,
2023-05-23 16:38:00 +02:00
};
2023-07-02 19:51:09 +02:00
CURLcode Curl_cf_http_proxy_insert_after(struct Curl_cfilter *cf_at,
struct Curl_easy *data)
2017-07-20 19:35:53 +02:00
{
2023-05-23 16:38:00 +02:00
struct Curl_cfilter *cf;
2023-07-02 19:51:09 +02:00
struct cf_proxy_ctx *ctx = NULL;
2017-07-20 19:35:53 +02:00
CURLcode result;
2023-05-23 16:38:00 +02:00
2023-07-02 19:51:09 +02:00
(void)data;
ctx = calloc(1, sizeof(*ctx));
if(!ctx) {
result = CURLE_OUT_OF_MEMORY;
goto out;
2023-05-23 16:38:00 +02:00
}
2023-07-02 19:51:09 +02:00
result = Curl_cf_create(&cf, &Curl_cft_http_proxy, ctx);
if(result)
goto out;
ctx = NULL;
Curl_conn_cf_insert_after(cf_at, cf);
2023-05-23 16:38:00 +02:00
2023-07-02 19:51:09 +02:00
out:
free(ctx);
2023-05-23 16:38:00 +02:00
return result;
2018-01-26 17:06:56 +01:00
}
2017-07-20 19:35:53 +02:00
2023-07-02 19:51:09 +02:00
#endif /* ! CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */