2023-04-06 15:54:57 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
|
|
|
#if !defined(CURL_DISABLE_PROXY) && !defined(CURL_DISABLE_HTTP)
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#ifdef USE_HYPER
|
|
|
|
#include <hyper.h>
|
|
|
|
#endif
|
|
|
|
#include "urldata.h"
|
|
|
|
#include "dynbuf.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "http.h"
|
2023-09-06 20:43:22 +08:00
|
|
|
#include "http1.h"
|
2023-04-06 15:54:57 +08:00
|
|
|
#include "http_proxy.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "cfilters.h"
|
|
|
|
#include "cf-h1-proxy.h"
|
|
|
|
#include "connect.h"
|
2023-08-03 23:32:25 +08:00
|
|
|
#include "curl_trc.h"
|
2023-04-06 15:54:57 +08:00
|
|
|
#include "curlx.h"
|
|
|
|
#include "vtls/vtls.h"
|
|
|
|
#include "transfer.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
|
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
2023-05-09 18:10:40 +08:00
|
|
|
H1_TUNNEL_INIT, /* init/default/no tunnel state */
|
|
|
|
H1_TUNNEL_CONNECT, /* CONNECT request is being send */
|
|
|
|
H1_TUNNEL_RECEIVE, /* CONNECT answer is being received */
|
|
|
|
H1_TUNNEL_RESPONSE, /* CONNECT response received completely */
|
|
|
|
H1_TUNNEL_ESTABLISHED,
|
|
|
|
H1_TUNNEL_FAILED
|
|
|
|
} h1_tunnel_state;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
/* struct for HTTP CONNECT tunneling */
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state {
|
2023-04-06 15:54:57 +08:00
|
|
|
struct HTTP CONNECT;
|
|
|
|
struct dynbuf rcvbuf;
|
2023-09-06 20:43:22 +08:00
|
|
|
struct dynbuf request_data;
|
|
|
|
size_t nsent;
|
2023-04-06 15:54:57 +08:00
|
|
|
size_t headerlines;
|
|
|
|
enum keeponval {
|
|
|
|
KEEPON_DONE,
|
|
|
|
KEEPON_CONNECT,
|
|
|
|
KEEPON_IGNORE
|
|
|
|
} keepon;
|
|
|
|
curl_off_t cl; /* size of content to read and ignore */
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_state tunnel_state;
|
2023-04-06 15:54:57 +08:00
|
|
|
BIT(chunked_encoding);
|
|
|
|
BIT(close_connection);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
static bool tunnel_is_established(struct h1_tunnel_state *ts)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
2023-05-09 18:10:40 +08:00
|
|
|
return ts && (ts->tunnel_state == H1_TUNNEL_ESTABLISHED);
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
static bool tunnel_is_failed(struct h1_tunnel_state *ts)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
2023-05-09 18:10:40 +08:00
|
|
|
return ts && (ts->tunnel_state == H1_TUNNEL_FAILED);
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
static CURLcode tunnel_reinit(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
struct h1_tunnel_state *ts)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
|
|
|
(void)data;
|
2023-09-06 20:43:22 +08:00
|
|
|
(void)cf;
|
2023-04-06 15:54:57 +08:00
|
|
|
DEBUGASSERT(ts);
|
|
|
|
Curl_dyn_reset(&ts->rcvbuf);
|
2023-09-06 20:43:22 +08:00
|
|
|
Curl_dyn_reset(&ts->request_data);
|
2023-05-09 18:10:40 +08:00
|
|
|
ts->tunnel_state = H1_TUNNEL_INIT;
|
2023-04-06 15:54:57 +08:00
|
|
|
ts->keepon = KEEPON_CONNECT;
|
|
|
|
ts->cl = 0;
|
|
|
|
ts->close_connection = FALSE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
static CURLcode tunnel_init(struct Curl_cfilter *cf,
|
2023-04-06 15:54:57 +08:00
|
|
|
struct Curl_easy *data,
|
2023-09-06 20:43:22 +08:00
|
|
|
struct h1_tunnel_state **pts)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts;
|
2023-04-06 15:54:57 +08:00
|
|
|
CURLcode result;
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
if(cf->conn->handler->flags & PROTOPT_NOTCPPROXY) {
|
|
|
|
failf(data, "%s cannot be done over CONNECT", cf->conn->handler->scheme);
|
2023-04-06 15:54:57 +08:00
|
|
|
return CURLE_UNSUPPORTED_PROTOCOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we might need the upload buffer for streaming a partial request */
|
|
|
|
result = Curl_get_upload_buffer(data);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
ts = calloc(1, sizeof(*ts));
|
|
|
|
if(!ts)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
infof(data, "allocate connect buffer");
|
|
|
|
|
|
|
|
Curl_dyn_init(&ts->rcvbuf, DYN_PROXY_CONNECT_HEADERS);
|
2023-09-06 20:43:22 +08:00
|
|
|
Curl_dyn_init(&ts->request_data, DYN_HTTP_REQUEST);
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
*pts = ts;
|
2023-09-06 20:43:22 +08:00
|
|
|
connkeep(cf->conn, "HTTP proxy CONNECT");
|
|
|
|
return tunnel_reinit(cf, data, ts);
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
static void h1_tunnel_go_state(struct Curl_cfilter *cf,
|
|
|
|
struct h1_tunnel_state *ts,
|
|
|
|
h1_tunnel_state new_state,
|
|
|
|
struct Curl_easy *data)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
|
|
|
if(ts->tunnel_state == new_state)
|
|
|
|
return;
|
|
|
|
/* leaving this one */
|
|
|
|
switch(ts->tunnel_state) {
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_CONNECT:
|
2023-04-06 15:54:57 +08:00
|
|
|
data->req.ignorebody = FALSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* entering this one */
|
|
|
|
switch(new_state) {
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_INIT:
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "new tunnel state 'init'");
|
2023-09-06 20:43:22 +08:00
|
|
|
tunnel_reinit(cf, data, ts);
|
2023-04-06 15:54:57 +08:00
|
|
|
break;
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_CONNECT:
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "new tunnel state 'connect'");
|
2023-05-09 18:10:40 +08:00
|
|
|
ts->tunnel_state = H1_TUNNEL_CONNECT;
|
2023-04-06 15:54:57 +08:00
|
|
|
ts->keepon = KEEPON_CONNECT;
|
|
|
|
Curl_dyn_reset(&ts->rcvbuf);
|
|
|
|
break;
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_RECEIVE:
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "new tunnel state 'receive'");
|
2023-05-09 18:10:40 +08:00
|
|
|
ts->tunnel_state = H1_TUNNEL_RECEIVE;
|
2023-04-06 15:54:57 +08:00
|
|
|
break;
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_RESPONSE:
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "new tunnel state 'response'");
|
2023-05-09 18:10:40 +08:00
|
|
|
ts->tunnel_state = H1_TUNNEL_RESPONSE;
|
2023-04-06 15:54:57 +08:00
|
|
|
break;
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_ESTABLISHED:
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "new tunnel state 'established'");
|
2023-04-06 15:54:57 +08:00
|
|
|
infof(data, "CONNECT phase completed");
|
|
|
|
data->state.authproxy.done = TRUE;
|
|
|
|
data->state.authproxy.multipass = FALSE;
|
|
|
|
/* FALLTHROUGH */
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_FAILED:
|
|
|
|
if(new_state == H1_TUNNEL_FAILED)
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "new tunnel state 'failed'");
|
2023-04-06 15:54:57 +08:00
|
|
|
ts->tunnel_state = new_state;
|
|
|
|
Curl_dyn_reset(&ts->rcvbuf);
|
2023-09-06 20:43:22 +08:00
|
|
|
Curl_dyn_reset(&ts->request_data);
|
2023-04-06 15:54:57 +08:00
|
|
|
/* restore the protocol pointer */
|
|
|
|
data->info.httpcode = 0; /* clear it as it might've been used for the
|
|
|
|
proxy */
|
|
|
|
/* If a proxy-authorization header was used for the proxy, then we should
|
|
|
|
make sure that it isn't accidentally used for the document request
|
|
|
|
after we've connected. So let's free and clear it here. */
|
|
|
|
Curl_safefree(data->state.aptr.proxyuserpwd);
|
|
|
|
#ifdef USE_HYPER
|
|
|
|
data->state.hconnect = FALSE;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tunnel_free(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data)
|
|
|
|
{
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts = cf->ctx;
|
2023-04-06 15:54:57 +08:00
|
|
|
if(ts) {
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
Curl_dyn_free(&ts->rcvbuf);
|
2023-09-06 20:43:22 +08:00
|
|
|
Curl_dyn_free(&ts->request_data);
|
2023-04-06 15:54:57 +08:00
|
|
|
free(ts);
|
|
|
|
cf->ctx = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef USE_HYPER
|
|
|
|
static CURLcode start_CONNECT(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
2023-09-06 20:43:22 +08:00
|
|
|
struct httpreq *req = NULL;
|
|
|
|
int http_minor;
|
2023-04-06 15:54:57 +08:00
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
/* This only happens if we've looped here due to authentication
|
|
|
|
reasons, and we don't really use the newly cloned URL here
|
|
|
|
then. Just free() it. */
|
|
|
|
Curl_safefree(data->req.newurl);
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
result = Curl_http_proxy_create_CONNECT(&req, cf, data, 1);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
infof(data, "Establish HTTP proxy tunnel to %s", req->authority);
|
2023-04-06 15:54:57 +08:00
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
Curl_dyn_reset(&ts->request_data);
|
|
|
|
ts->nsent = 0;
|
2023-04-06 15:54:57 +08:00
|
|
|
ts->headerlines = 0;
|
2023-09-06 20:43:22 +08:00
|
|
|
http_minor = (cf->conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) ? 0 : 1;
|
|
|
|
|
|
|
|
result = Curl_h1_req_write_head(req, http_minor, &ts->request_data);
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
if(result)
|
|
|
|
failf(data, "Failed sending CONNECT to proxy");
|
2023-09-06 20:43:22 +08:00
|
|
|
if(req)
|
|
|
|
Curl_http_req_free(req);
|
2023-04-06 15:54:57 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
static CURLcode send_CONNECT(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts,
|
2023-04-06 15:54:57 +08:00
|
|
|
bool *done)
|
|
|
|
{
|
2023-09-06 20:43:22 +08:00
|
|
|
char *buf = Curl_dyn_ptr(&ts->request_data);
|
|
|
|
size_t request_len = Curl_dyn_len(&ts->request_data);
|
|
|
|
size_t blen = request_len;
|
2023-04-06 15:54:57 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
2023-09-06 20:43:22 +08:00
|
|
|
ssize_t nwritten;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
if(blen <= ts->nsent)
|
|
|
|
goto out; /* we are done */
|
2023-04-06 15:54:57 +08:00
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
blen -= ts->nsent;
|
|
|
|
buf += ts->nsent;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
nwritten = cf->next->cft->do_send(cf->next, data, buf, blen, &result);
|
|
|
|
if(nwritten < 0) {
|
|
|
|
if(result == CURLE_AGAIN) {
|
|
|
|
result = CURLE_OK;
|
|
|
|
}
|
|
|
|
goto out;
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
2023-09-06 20:43:22 +08:00
|
|
|
|
|
|
|
DEBUGASSERT(blen >= (size_t)nwritten);
|
|
|
|
ts->nsent += (size_t)nwritten;
|
|
|
|
Curl_debug(data, CURLINFO_HEADER_OUT, buf, (size_t)nwritten);
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
if(result)
|
|
|
|
failf(data, "Failed sending CONNECT to proxy");
|
2023-09-06 20:43:22 +08:00
|
|
|
*done = (!result && (ts->nsent >= request_len));
|
2023-04-06 15:54:57 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode on_resp_header(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts,
|
2023-04-06 15:54:57 +08:00
|
|
|
const char *header)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct SingleRequest *k = &data->req;
|
|
|
|
(void)cf;
|
|
|
|
|
|
|
|
if((checkprefix("WWW-Authenticate:", header) &&
|
|
|
|
(401 == k->httpcode)) ||
|
|
|
|
(checkprefix("Proxy-authenticate:", header) &&
|
|
|
|
(407 == k->httpcode))) {
|
|
|
|
|
|
|
|
bool proxy = (k->httpcode == 407) ? TRUE : FALSE;
|
|
|
|
char *auth = Curl_copy_header_value(header);
|
|
|
|
if(!auth)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "CONNECT: fwd auth header '%s'", header);
|
2023-04-06 15:54:57 +08:00
|
|
|
result = Curl_http_input_auth(data, proxy, auth);
|
|
|
|
|
|
|
|
free(auth);
|
|
|
|
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if(checkprefix("Content-Length:", header)) {
|
|
|
|
if(k->httpcode/100 == 2) {
|
|
|
|
/* A client MUST ignore any Content-Length or Transfer-Encoding
|
|
|
|
header fields received in a successful response to CONNECT.
|
|
|
|
"Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
|
|
|
|
infof(data, "Ignoring Content-Length in CONNECT %03d response",
|
|
|
|
k->httpcode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
(void)curlx_strtoofft(header + strlen("Content-Length:"),
|
|
|
|
NULL, 10, &ts->cl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(Curl_compareheader(header,
|
|
|
|
STRCONST("Connection:"), STRCONST("close")))
|
|
|
|
ts->close_connection = TRUE;
|
|
|
|
else if(checkprefix("Transfer-Encoding:", header)) {
|
|
|
|
if(k->httpcode/100 == 2) {
|
|
|
|
/* A client MUST ignore any Content-Length or Transfer-Encoding
|
|
|
|
header fields received in a successful response to CONNECT.
|
|
|
|
"Successful" described as: 2xx (Successful). RFC 7231 4.3.6 */
|
|
|
|
infof(data, "Ignoring Transfer-Encoding in "
|
|
|
|
"CONNECT %03d response", k->httpcode);
|
|
|
|
}
|
|
|
|
else if(Curl_compareheader(header,
|
|
|
|
STRCONST("Transfer-Encoding:"),
|
|
|
|
STRCONST("chunked"))) {
|
|
|
|
infof(data, "CONNECT responded chunked");
|
|
|
|
ts->chunked_encoding = TRUE;
|
|
|
|
/* init our chunky engine */
|
|
|
|
Curl_httpchunk_init(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(Curl_compareheader(header,
|
|
|
|
STRCONST("Proxy-Connection:"),
|
|
|
|
STRCONST("close")))
|
|
|
|
ts->close_connection = TRUE;
|
|
|
|
else if(!strncmp(header, "HTTP/1.", 7) &&
|
|
|
|
((header[7] == '0') || (header[7] == '1')) &&
|
|
|
|
(header[8] == ' ') &&
|
|
|
|
ISDIGIT(header[9]) && ISDIGIT(header[10]) && ISDIGIT(header[11]) &&
|
|
|
|
!ISDIGIT(header[12])) {
|
|
|
|
/* store the HTTP code from the proxy */
|
|
|
|
data->info.httpproxycode = k->httpcode = (header[9] - '0') * 100 +
|
|
|
|
(header[10] - '0') * 10 + (header[11] - '0');
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts,
|
2023-04-06 15:54:57 +08:00
|
|
|
bool *done)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct SingleRequest *k = &data->req;
|
|
|
|
curl_socket_t tunnelsocket = Curl_conn_cf_get_socket(cf, data);
|
|
|
|
char *linep;
|
|
|
|
size_t perline;
|
2023-10-23 16:33:07 +08:00
|
|
|
int error, writetype;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
#define SELECT_OK 0
|
|
|
|
#define SELECT_ERROR 1
|
|
|
|
|
|
|
|
error = SELECT_OK;
|
|
|
|
*done = FALSE;
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
if(!Curl_conn_data_pending(data, cf->sockindex))
|
2023-04-06 15:54:57 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
while(ts->keepon) {
|
2023-11-07 00:06:06 +08:00
|
|
|
ssize_t nread;
|
2023-04-06 15:54:57 +08:00
|
|
|
char byte;
|
|
|
|
|
|
|
|
/* Read one byte at a time to avoid a race condition. Wait at most one
|
|
|
|
second before looping to ensure continuous pgrsUpdates. */
|
2023-11-07 00:06:06 +08:00
|
|
|
result = Curl_read(data, tunnelsocket, &byte, 1, &nread);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result == CURLE_AGAIN)
|
|
|
|
/* socket buffer drained, return */
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
if(Curl_pgrsUpdate(data))
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
ts->keepon = KEEPON_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-11-07 00:06:06 +08:00
|
|
|
if(nread <= 0) {
|
2023-04-06 15:54:57 +08:00
|
|
|
if(data->set.proxyauth && data->state.authproxy.avail &&
|
|
|
|
data->state.aptr.proxyuserpwd) {
|
|
|
|
/* proxy auth was requested and there was proxy auth available,
|
|
|
|
then deem this as "mere" proxy disconnect */
|
|
|
|
ts->close_connection = TRUE;
|
|
|
|
infof(data, "Proxy CONNECT connection closed");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error = SELECT_ERROR;
|
|
|
|
failf(data, "Proxy CONNECT aborted");
|
|
|
|
}
|
|
|
|
ts->keepon = KEEPON_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ts->keepon == KEEPON_IGNORE) {
|
|
|
|
/* This means we are currently ignoring a response-body */
|
|
|
|
|
|
|
|
if(ts->cl) {
|
|
|
|
/* A Content-Length based body: simply count down the counter
|
|
|
|
and make sure to break out of the loop when we're done! */
|
|
|
|
ts->cl--;
|
|
|
|
if(ts->cl <= 0) {
|
|
|
|
ts->keepon = KEEPON_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* chunked-encoded body, so we need to do the chunked dance
|
|
|
|
properly to know when the end of the body is reached */
|
|
|
|
CHUNKcode r;
|
|
|
|
CURLcode extra;
|
2023-11-07 00:06:06 +08:00
|
|
|
size_t consumed = 0;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
/* now parse the chunked piece of data so that we can
|
|
|
|
properly tell when the stream ends */
|
2023-11-07 00:06:06 +08:00
|
|
|
r = Curl_httpchunk_read(data, &byte, 1, &consumed, &extra);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(r == CHUNKE_STOP) {
|
|
|
|
/* we're done reading chunks! */
|
|
|
|
infof(data, "chunk reading DONE");
|
|
|
|
ts->keepon = KEEPON_DONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Curl_dyn_addn(&ts->rcvbuf, &byte, 1)) {
|
|
|
|
failf(data, "CONNECT response too large");
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if this is not the end of a header line then continue */
|
|
|
|
if(byte != 0x0a)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ts->headerlines++;
|
|
|
|
linep = Curl_dyn_ptr(&ts->rcvbuf);
|
|
|
|
perline = Curl_dyn_len(&ts->rcvbuf); /* amount of bytes in this line */
|
|
|
|
|
|
|
|
/* output debug if that is requested */
|
|
|
|
Curl_debug(data, CURLINFO_HEADER_IN, linep, perline);
|
|
|
|
|
2023-10-23 16:33:07 +08:00
|
|
|
/* send the header to the callback */
|
|
|
|
writetype = CLIENTWRITE_HEADER | CLIENTWRITE_CONNECT |
|
|
|
|
(ts->headerlines == 1 ? CLIENTWRITE_STATUS : 0);
|
|
|
|
result = Curl_client_write(data, writetype, linep, perline);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
2023-08-03 05:34:48 +08:00
|
|
|
result = Curl_bump_headersize(data, perline, TRUE);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
/* Newlines are CRLF, so the CR is ignored as the line isn't
|
|
|
|
really terminated until the LF comes. Treat a following CR
|
|
|
|
as end-of-headers as well.*/
|
|
|
|
|
|
|
|
if(('\r' == linep[0]) ||
|
|
|
|
('\n' == linep[0])) {
|
|
|
|
/* end of response-headers from the proxy */
|
|
|
|
|
|
|
|
if((407 == k->httpcode) && !data->state.authproblem) {
|
|
|
|
/* If we get a 407 response code with content length
|
|
|
|
when we have no auth problem, we must ignore the
|
|
|
|
whole response-body */
|
|
|
|
ts->keepon = KEEPON_IGNORE;
|
|
|
|
|
|
|
|
if(ts->cl) {
|
|
|
|
infof(data, "Ignore %" CURL_FORMAT_CURL_OFF_T
|
|
|
|
" bytes of response-body", ts->cl);
|
|
|
|
}
|
|
|
|
else if(ts->chunked_encoding) {
|
|
|
|
CHUNKcode r;
|
|
|
|
CURLcode extra;
|
2023-11-07 00:06:06 +08:00
|
|
|
size_t consumed = 0;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
infof(data, "Ignore chunked response-body");
|
|
|
|
|
|
|
|
/* We set ignorebody true here since the chunked decoder
|
|
|
|
function will acknowledge that. Pay attention so that this is
|
|
|
|
cleared again when this function returns! */
|
|
|
|
k->ignorebody = TRUE;
|
|
|
|
|
|
|
|
if(linep[1] == '\n')
|
|
|
|
/* this can only be a LF if the letter at index 0 was a CR */
|
|
|
|
linep++;
|
|
|
|
|
|
|
|
/* now parse the chunked piece of data so that we can properly
|
|
|
|
tell when the stream ends */
|
2023-11-07 00:06:06 +08:00
|
|
|
r = Curl_httpchunk_read(data, linep + 1, 1, &consumed, &extra);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(r == CHUNKE_STOP) {
|
|
|
|
/* we're done reading chunks! */
|
|
|
|
infof(data, "chunk reading DONE");
|
|
|
|
ts->keepon = KEEPON_DONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* without content-length or chunked encoding, we
|
|
|
|
can't keep the connection alive since the close is
|
|
|
|
the end signal so we bail out at once instead */
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "CONNECT: no content-length or chunked");
|
2023-04-06 15:54:57 +08:00
|
|
|
ts->keepon = KEEPON_DONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ts->keepon = KEEPON_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(ts->keepon == KEEPON_IGNORE
|
|
|
|
|| ts->keepon == KEEPON_DONE);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = on_resp_header(cf, data, ts, linep);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Curl_dyn_reset(&ts->rcvbuf);
|
|
|
|
} /* while there's buffer left and loop is requested */
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
result = CURLE_RECV_ERROR;
|
|
|
|
*done = (ts->keepon == KEEPON_DONE);
|
|
|
|
if(!result && *done && data->info.httpproxycode/100 != 2) {
|
|
|
|
/* Deal with the possibly already received authenticate
|
|
|
|
headers. 'newurl' is set to a new URL if we must loop. */
|
|
|
|
result = Curl_http_auth_act(data);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* USE_HYPER */
|
2023-09-06 20:43:22 +08:00
|
|
|
|
|
|
|
static CURLcode CONNECT_host(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
char **pauthority,
|
|
|
|
char **phost_header)
|
|
|
|
{
|
|
|
|
const char *hostname;
|
|
|
|
int port;
|
|
|
|
bool ipv6_ip;
|
|
|
|
CURLcode result;
|
|
|
|
char *authority; /* for CONNECT, the destination host + port */
|
|
|
|
char *host_header = NULL; /* Host: authority */
|
|
|
|
|
|
|
|
result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
authority = aprintf("%s%s%s:%d", ipv6_ip?"[":"", hostname, ipv6_ip?"]":"",
|
|
|
|
port);
|
|
|
|
if(!authority)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* If user is not overriding the Host header later */
|
|
|
|
if(!Curl_checkProxyheaders(data, cf->conn, STRCONST("Host"))) {
|
|
|
|
host_header = aprintf("Host: %s\r\n", authority);
|
|
|
|
if(!host_header) {
|
|
|
|
free(authority);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pauthority = authority;
|
|
|
|
*phost_header = host_header;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2023-04-06 15:54:57 +08:00
|
|
|
/* The Hyper version of CONNECT */
|
|
|
|
static CURLcode start_CONNECT(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
|
|
|
struct connectdata *conn = cf->conn;
|
|
|
|
struct hyptransfer *h = &data->hyp;
|
|
|
|
curl_socket_t tunnelsocket = Curl_conn_cf_get_socket(cf, data);
|
|
|
|
hyper_io *io = NULL;
|
|
|
|
hyper_request *req = NULL;
|
|
|
|
hyper_headers *headers = NULL;
|
|
|
|
hyper_clientconn_options *options = NULL;
|
|
|
|
hyper_task *handshake = NULL;
|
|
|
|
hyper_task *task = NULL; /* for the handshake */
|
|
|
|
hyper_clientconn *client = NULL;
|
|
|
|
hyper_task *sendtask = NULL; /* for the send */
|
2023-09-06 20:43:22 +08:00
|
|
|
char *authority = NULL; /* for CONNECT */
|
|
|
|
char *host_header = NULL; /* Host: */
|
2023-04-06 15:54:57 +08:00
|
|
|
CURLcode result = CURLE_OUT_OF_MEMORY;
|
2023-09-06 20:43:22 +08:00
|
|
|
(void)ts;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
io = hyper_io_new();
|
|
|
|
if(!io) {
|
|
|
|
failf(data, "Couldn't create hyper IO");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* tell Hyper how to read/write network data */
|
|
|
|
hyper_io_set_userdata(io, data);
|
|
|
|
hyper_io_set_read(io, Curl_hyper_recv);
|
|
|
|
hyper_io_set_write(io, Curl_hyper_send);
|
|
|
|
conn->sockfd = tunnelsocket;
|
|
|
|
|
|
|
|
data->state.hconnect = TRUE;
|
|
|
|
|
|
|
|
/* create an executor to poll futures */
|
|
|
|
if(!h->exec) {
|
|
|
|
h->exec = hyper_executor_new();
|
|
|
|
if(!h->exec) {
|
|
|
|
failf(data, "Couldn't create hyper executor");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
options = hyper_clientconn_options_new();
|
|
|
|
if(!options) {
|
|
|
|
failf(data, "Couldn't create hyper client options");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
2023-08-28 12:35:08 +08:00
|
|
|
hyper_clientconn_options_set_preserve_header_case(options, 1);
|
|
|
|
hyper_clientconn_options_set_preserve_header_order(options, 1);
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
hyper_clientconn_options_exec(options, h->exec);
|
|
|
|
|
|
|
|
/* "Both the `io` and the `options` are consumed in this function
|
|
|
|
call" */
|
|
|
|
handshake = hyper_clientconn_handshake(io, options);
|
|
|
|
if(!handshake) {
|
|
|
|
failf(data, "Couldn't create hyper client handshake");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
io = NULL;
|
|
|
|
options = NULL;
|
|
|
|
|
|
|
|
if(HYPERE_OK != hyper_executor_push(h->exec, handshake)) {
|
|
|
|
failf(data, "Couldn't hyper_executor_push the handshake");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
handshake = NULL; /* ownership passed on */
|
|
|
|
|
|
|
|
task = hyper_executor_poll(h->exec);
|
|
|
|
if(!task) {
|
|
|
|
failf(data, "Couldn't hyper_executor_poll the handshake");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
client = hyper_task_value(task);
|
|
|
|
hyper_task_free(task);
|
2023-08-28 12:35:08 +08:00
|
|
|
|
2023-04-06 15:54:57 +08:00
|
|
|
req = hyper_request_new();
|
|
|
|
if(!req) {
|
|
|
|
failf(data, "Couldn't hyper_request_new");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if(hyper_request_set_method(req, (uint8_t *)"CONNECT",
|
|
|
|
strlen("CONNECT"))) {
|
|
|
|
failf(data, "error setting method");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This only happens if we've looped here due to authentication
|
|
|
|
reasons, and we don't really use the newly cloned URL here
|
|
|
|
then. Just free() it. */
|
|
|
|
Curl_safefree(data->req.newurl);
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
result = CONNECT_host(cf, data, &authority, &host_header);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result)
|
|
|
|
goto error;
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
infof(data, "Establish HTTP proxy tunnel to %s", authority);
|
|
|
|
|
|
|
|
if(hyper_request_set_uri(req, (uint8_t *)authority,
|
|
|
|
strlen(authority))) {
|
2023-04-06 15:54:57 +08:00
|
|
|
failf(data, "error setting path");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if(data->set.verbose) {
|
2023-09-06 20:43:22 +08:00
|
|
|
char *se = aprintf("CONNECT %s HTTP/1.1\r\n", authority);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(!se) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Curl_debug(data, CURLINFO_HEADER_OUT, se, strlen(se));
|
|
|
|
free(se);
|
|
|
|
}
|
|
|
|
/* Setup the proxy-authorization header, if any */
|
|
|
|
result = Curl_http_output_auth(data, conn, "CONNECT", HTTPREQ_GET,
|
2023-09-06 20:43:22 +08:00
|
|
|
authority, TRUE);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result)
|
|
|
|
goto error;
|
2023-09-06 20:43:22 +08:00
|
|
|
Curl_safefree(authority);
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
/* default is 1.1 */
|
|
|
|
if((conn->http_proxy.proxytype == CURLPROXY_HTTP_1_0) &&
|
|
|
|
(HYPERE_OK != hyper_request_set_version(req,
|
|
|
|
HYPER_HTTP_VERSION_1_0))) {
|
|
|
|
failf(data, "error setting HTTP version");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
headers = hyper_request_headers(req);
|
|
|
|
if(!headers) {
|
|
|
|
failf(data, "hyper_request_headers");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
2023-09-06 20:43:22 +08:00
|
|
|
if(host_header) {
|
|
|
|
result = Curl_hyper_header(data, headers, host_header);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result)
|
|
|
|
goto error;
|
2023-09-06 20:43:22 +08:00
|
|
|
Curl_safefree(host_header);
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(data->state.aptr.proxyuserpwd) {
|
|
|
|
result = Curl_hyper_header(data, headers,
|
|
|
|
data->state.aptr.proxyuserpwd);
|
|
|
|
if(result)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Curl_checkProxyheaders(data, conn, STRCONST("User-Agent")) &&
|
|
|
|
data->set.str[STRING_USERAGENT]) {
|
|
|
|
struct dynbuf ua;
|
|
|
|
Curl_dyn_init(&ua, DYN_HTTP_REQUEST);
|
|
|
|
result = Curl_dyn_addf(&ua, "User-Agent: %s\r\n",
|
|
|
|
data->set.str[STRING_USERAGENT]);
|
|
|
|
if(result)
|
|
|
|
goto error;
|
|
|
|
result = Curl_hyper_header(data, headers, Curl_dyn_ptr(&ua));
|
|
|
|
if(result)
|
|
|
|
goto error;
|
|
|
|
Curl_dyn_free(&ua);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection"))) {
|
|
|
|
result = Curl_hyper_header(data, headers,
|
|
|
|
"Proxy-Connection: Keep-Alive");
|
|
|
|
if(result)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Curl_add_custom_headers(data, TRUE, headers);
|
|
|
|
if(result)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
sendtask = hyper_clientconn_send(client, req);
|
|
|
|
if(!sendtask) {
|
|
|
|
failf(data, "hyper_clientconn_send");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
2023-08-28 12:35:08 +08:00
|
|
|
req = NULL;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
if(HYPERE_OK != hyper_executor_push(h->exec, sendtask)) {
|
|
|
|
failf(data, "Couldn't hyper_executor_push the send");
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
2023-08-28 12:35:08 +08:00
|
|
|
sendtask = NULL; /* ownership passed on */
|
|
|
|
|
|
|
|
hyper_clientconn_free(client);
|
|
|
|
client = NULL;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
error:
|
2023-09-06 20:43:22 +08:00
|
|
|
free(host_header);
|
|
|
|
free(authority);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(io)
|
|
|
|
hyper_io_free(io);
|
|
|
|
if(options)
|
|
|
|
hyper_clientconn_options_free(options);
|
|
|
|
if(handshake)
|
|
|
|
hyper_task_free(handshake);
|
|
|
|
if(client)
|
|
|
|
hyper_clientconn_free(client);
|
2023-08-28 12:35:08 +08:00
|
|
|
if(req)
|
|
|
|
hyper_request_free(req);
|
|
|
|
|
2023-04-06 15:54:57 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-09-06 20:43:22 +08:00
|
|
|
static CURLcode send_CONNECT(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts,
|
2023-04-06 15:54:57 +08:00
|
|
|
bool *done)
|
|
|
|
{
|
|
|
|
struct hyptransfer *h = &data->hyp;
|
2023-09-06 20:43:22 +08:00
|
|
|
struct connectdata *conn = cf->conn;
|
2023-04-06 15:54:57 +08:00
|
|
|
hyper_task *task = NULL;
|
|
|
|
hyper_error *hypererr = NULL;
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
(void)ts;
|
|
|
|
(void)conn;
|
|
|
|
do {
|
|
|
|
task = hyper_executor_poll(h->exec);
|
|
|
|
if(task) {
|
|
|
|
bool error = hyper_task_type(task) == HYPER_TASK_ERROR;
|
|
|
|
if(error)
|
|
|
|
hypererr = hyper_task_value(task);
|
|
|
|
hyper_task_free(task);
|
|
|
|
if(error) {
|
|
|
|
/* this could probably use a better error code? */
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(task);
|
|
|
|
error:
|
|
|
|
*done = (result == CURLE_OK);
|
|
|
|
if(hypererr) {
|
|
|
|
uint8_t errbuf[256];
|
|
|
|
size_t errlen = hyper_error_print(hypererr, errbuf, sizeof(errbuf));
|
|
|
|
failf(data, "Hyper: %.*s", (int)errlen, errbuf);
|
|
|
|
hyper_error_free(hypererr);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode recv_CONNECT_resp(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts,
|
2023-04-06 15:54:57 +08:00
|
|
|
bool *done)
|
|
|
|
{
|
|
|
|
struct hyptransfer *h = &data->hyp;
|
|
|
|
CURLcode result;
|
|
|
|
int didwhat;
|
|
|
|
|
|
|
|
(void)ts;
|
|
|
|
*done = FALSE;
|
|
|
|
result = Curl_hyper_stream(data, cf->conn, &didwhat, done,
|
|
|
|
CURL_CSELECT_IN | CURL_CSELECT_OUT);
|
|
|
|
if(result || !*done)
|
|
|
|
return result;
|
|
|
|
if(h->exec) {
|
|
|
|
hyper_executor_free(h->exec);
|
|
|
|
h->exec = NULL;
|
|
|
|
}
|
|
|
|
if(h->read_waker) {
|
|
|
|
hyper_waker_free(h->read_waker);
|
|
|
|
h->read_waker = NULL;
|
|
|
|
}
|
|
|
|
if(h->write_waker) {
|
|
|
|
hyper_waker_free(h->write_waker);
|
|
|
|
h->write_waker = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_HYPER */
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
static CURLcode H1_CONNECT(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
struct h1_tunnel_state *ts)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
|
|
|
struct connectdata *conn = cf->conn;
|
|
|
|
CURLcode result;
|
|
|
|
bool done;
|
|
|
|
|
|
|
|
if(tunnel_is_established(ts))
|
|
|
|
return CURLE_OK;
|
|
|
|
if(tunnel_is_failed(ts))
|
|
|
|
return CURLE_RECV_ERROR; /* Need a cfilter close and new bootstrap */
|
|
|
|
|
|
|
|
do {
|
|
|
|
timediff_t check;
|
|
|
|
|
|
|
|
check = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
if(check <= 0) {
|
|
|
|
failf(data, "Proxy CONNECT aborted due to timeout");
|
|
|
|
result = CURLE_OPERATION_TIMEDOUT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(ts->tunnel_state) {
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_INIT:
|
2023-04-06 15:54:57 +08:00
|
|
|
/* Prepare the CONNECT request and make a first attempt to send. */
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "CONNECT start");
|
2023-04-06 15:54:57 +08:00
|
|
|
result = start_CONNECT(cf, data, ts);
|
|
|
|
if(result)
|
|
|
|
goto out;
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, ts, H1_TUNNEL_CONNECT, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_CONNECT:
|
2023-04-06 15:54:57 +08:00
|
|
|
/* see that the request is completely sent */
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "CONNECT send");
|
2023-09-06 20:43:22 +08:00
|
|
|
result = send_CONNECT(cf, data, ts, &done);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result || !done)
|
|
|
|
goto out;
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, ts, H1_TUNNEL_RECEIVE, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_RECEIVE:
|
2023-04-06 15:54:57 +08:00
|
|
|
/* read what is there */
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "CONNECT receive");
|
2023-04-06 15:54:57 +08:00
|
|
|
result = recv_CONNECT_resp(cf, data, ts, &done);
|
|
|
|
if(Curl_pgrsUpdate(data)) {
|
|
|
|
result = CURLE_ABORTED_BY_CALLBACK;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* error or not complete yet. return for more multi-multi */
|
|
|
|
if(result || !done)
|
|
|
|
goto out;
|
|
|
|
/* got it */
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, ts, H1_TUNNEL_RESPONSE, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
case H1_TUNNEL_RESPONSE:
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "CONNECT response");
|
2023-04-06 15:54:57 +08:00
|
|
|
if(data->req.newurl) {
|
|
|
|
/* not the "final" response, we need to do a follow up request.
|
|
|
|
* If the other side indicated a connection close, or if someone
|
|
|
|
* else told us to close this connection, do so now.
|
|
|
|
*/
|
|
|
|
if(ts->close_connection || conn->bits.close) {
|
|
|
|
/* Close this filter and the sub-chain, re-connect the
|
|
|
|
* sub-chain and continue. Closing this filter will
|
|
|
|
* reset our tunnel state. To avoid recursion, we return
|
|
|
|
* and expect to be called again.
|
|
|
|
*/
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "CONNECT need to close+open");
|
2023-04-06 15:54:57 +08:00
|
|
|
infof(data, "Connect me again please");
|
|
|
|
Curl_conn_cf_close(cf, data);
|
|
|
|
connkeep(conn, "HTTP proxy CONNECT");
|
|
|
|
result = Curl_conn_cf_connect(cf->next, data, FALSE, &done);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* staying on this connection, reset state */
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, ts, H1_TUNNEL_INIT, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while(data->req.newurl);
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
DEBUGASSERT(ts->tunnel_state == H1_TUNNEL_RESPONSE);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(data->info.httpproxycode/100 != 2) {
|
|
|
|
/* a non-2xx response and we have no next url to try. */
|
2023-04-13 18:50:04 +08:00
|
|
|
Curl_safefree(data->req.newurl);
|
2023-08-23 20:47:45 +08:00
|
|
|
/* failure, close this connection to avoid reuse */
|
2023-04-06 15:54:57 +08:00
|
|
|
streamclose(conn, "proxy CONNECT failure");
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
failf(data, "CONNECT tunnel failed, response %d", data->req.httpcode);
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
/* 2xx response, SUCCESS! */
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, ts, H1_TUNNEL_ESTABLISHED, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
infof(data, "CONNECT tunnel established, response %d",
|
|
|
|
data->info.httpproxycode);
|
|
|
|
result = CURLE_OK;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if(result)
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, ts, H1_TUNNEL_FAILED, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode cf_h1_proxy_connect(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data,
|
|
|
|
bool blocking, bool *done)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts = cf->ctx;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
|
|
|
if(cf->connected) {
|
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "connect");
|
2023-07-21 01:07:49 +08:00
|
|
|
result = cf->next->cft->do_connect(cf->next, data, blocking, done);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result || !*done)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
*done = FALSE;
|
|
|
|
if(!ts) {
|
2023-09-06 20:43:22 +08:00
|
|
|
result = tunnel_init(cf, data, &ts);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
cf->ctx = ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: can we do blocking? */
|
|
|
|
/* We want "seamless" operations through HTTP proxy tunnel */
|
|
|
|
|
2023-05-09 18:10:40 +08:00
|
|
|
result = H1_CONNECT(cf, data, ts);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(result)
|
|
|
|
goto out;
|
|
|
|
Curl_safefree(data->state.aptr.proxyuserpwd);
|
|
|
|
|
|
|
|
out:
|
|
|
|
*done = (result == CURLE_OK) && tunnel_is_established(cf->ctx);
|
2023-04-27 22:29:45 +08:00
|
|
|
if(*done) {
|
2023-04-06 15:54:57 +08:00
|
|
|
cf->connected = TRUE;
|
|
|
|
tunnel_free(cf, data);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void cf_h1_proxy_adjust_pollset(struct Curl_cfilter *cf,
|
2023-04-06 15:54:57 +08:00
|
|
|
struct Curl_easy *data,
|
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
|
|
|
struct easy_pollset *ps)
|
2023-04-06 15:54:57 +08:00
|
|
|
{
|
2023-05-09 18:10:40 +08:00
|
|
|
struct h1_tunnel_state *ts = cf->ctx;
|
2023-04-06 15:54:57 +08:00
|
|
|
|
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
|
|
|
if(!cf->connected) {
|
2023-04-06 15:54:57 +08:00
|
|
|
/* If we are not connected, but the filter "below" is
|
|
|
|
* and not waiting on something, we are tunneling. */
|
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_socket_t sock = Curl_conn_cf_get_socket(cf, data);
|
2023-04-06 15:54:57 +08:00
|
|
|
if(ts) {
|
|
|
|
/* when we've sent a CONNECT to a proxy, we should rather either
|
|
|
|
wait for the socket to become readable to be able to get the
|
|
|
|
response headers or if we're still sending the request, wait
|
|
|
|
for write. */
|
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
|
|
|
if(ts->CONNECT.sending == HTTPSEND_REQUEST)
|
|
|
|
Curl_pollset_set_out_only(data, ps, sock);
|
|
|
|
else
|
|
|
|
Curl_pollset_set_in_only(data, ps, sock);
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
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
|
|
|
else
|
|
|
|
Curl_pollset_set_out_only(data, ps, sock);
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cf_h1_proxy_destroy(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data)
|
|
|
|
{
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "destroy");
|
2023-04-06 15:54:57 +08:00
|
|
|
tunnel_free(cf, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cf_h1_proxy_close(struct Curl_cfilter *cf,
|
|
|
|
struct Curl_easy *data)
|
|
|
|
{
|
2023-08-03 23:32:25 +08:00
|
|
|
CURL_TRC_CF(data, cf, "close");
|
2023-04-06 15:54:57 +08:00
|
|
|
cf->connected = FALSE;
|
|
|
|
if(cf->ctx) {
|
2023-05-09 18:10:40 +08:00
|
|
|
h1_tunnel_go_state(cf, cf->ctx, H1_TUNNEL_INIT, data);
|
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);
|
2023-04-06 15:54:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct Curl_cftype Curl_cft_h1_proxy = {
|
|
|
|
"H1-PROXY",
|
|
|
|
CF_TYPE_IP_CONNECT,
|
|
|
|
0,
|
|
|
|
cf_h1_proxy_destroy,
|
|
|
|
cf_h1_proxy_connect,
|
|
|
|
cf_h1_proxy_close,
|
|
|
|
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
|
|
|
cf_h1_proxy_adjust_pollset,
|
2023-04-06 15:54:57 +08:00
|
|
|
Curl_cf_def_data_pending,
|
|
|
|
Curl_cf_def_send,
|
|
|
|
Curl_cf_def_recv,
|
|
|
|
Curl_cf_def_cntrl,
|
|
|
|
Curl_cf_def_conn_is_alive,
|
|
|
|
Curl_cf_def_conn_keep_alive,
|
|
|
|
Curl_cf_def_query,
|
|
|
|
};
|
|
|
|
|
|
|
|
CURLcode Curl_cf_h1_proxy_insert_after(struct Curl_cfilter *cf_at,
|
|
|
|
struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
struct Curl_cfilter *cf;
|
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
(void)data;
|
|
|
|
result = Curl_cf_create(&cf, &Curl_cft_h1_proxy, NULL);
|
|
|
|
if(!result)
|
|
|
|
Curl_conn_cf_insert_after(cf_at, cf);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !CURL_DISABLE_PROXY && ! CURL_DISABLE_HTTP */
|