[edge] Fixes: skip 1xx, enforce auth timeout, resolve upstreams per connect, all Upgrade tokens, strip Remote-*
This commit is contained in:
+81
-13
@@ -3,7 +3,14 @@
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! Small HTTP client on Pingora's connector, for forward-auth subrequests and health checks.
|
||||
//!
|
||||
//! - The whole exchange is bounded by one deadline (Pingora's connector ignores the peer's
|
||||
//! read timeout, so the deadline is enforced here with `tokio::time::timeout`).
|
||||
//! - Informational answers (100, 102, 103) are skipped; `101` is returned as the final head
|
||||
//! without a body, for the caller to reject.
|
||||
//! - Every resolved address is tried in order until one accepts the connection.
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
@@ -21,14 +28,17 @@ pub struct Fetched {
|
||||
|
||||
/// Dial timeout, matching Caddy's reverse_proxy default.
|
||||
pub const DIAL_TIMEOUT: Duration = Duration::from_secs(3);
|
||||
/// More 1xx heads than this before a final answer is treated as a broken server.
|
||||
const MAX_INFORMATIONAL: usize = 8;
|
||||
|
||||
pub fn peer(u: &UpstreamRt) -> HttpPeer {
|
||||
let mut p = HttpPeer::new(u.addr, u.tls, u.sni.clone());
|
||||
pub fn peer(addr: SocketAddr, u: &UpstreamRt) -> HttpPeer {
|
||||
let mut p = HttpPeer::new(addr, u.tls, u.sni.clone());
|
||||
p.options.connection_timeout = Some(DIAL_TIMEOUT);
|
||||
p
|
||||
}
|
||||
|
||||
/// Send `req` without a body; read the whole response, at most `max_body` bytes of body.
|
||||
/// Send `req` without a body; read the whole response, at most `max_body` bytes of body,
|
||||
/// all within `timeout`.
|
||||
pub async fn fetch(
|
||||
connector: &Connector,
|
||||
u: &UpstreamRt,
|
||||
@@ -36,18 +46,76 @@ pub async fn fetch(
|
||||
max_body: usize,
|
||||
timeout: Duration,
|
||||
) -> Result<Fetched> {
|
||||
let mut p = peer(u);
|
||||
p.options.read_timeout = Some(timeout);
|
||||
p.options.write_timeout = Some(timeout);
|
||||
p.options.total_connection_timeout = Some(timeout);
|
||||
let (mut http, _reused) = connector.get_http_session(&p).await?;
|
||||
match tokio::time::timeout(timeout, exchange(connector, u, req, max_body, timeout)).await {
|
||||
Ok(r) => r,
|
||||
Err(_) => Error::e_explain(
|
||||
ErrorType::ReadTimedout,
|
||||
format!("no complete answer within {timeout:?}"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async fn exchange(
|
||||
connector: &Connector,
|
||||
u: &UpstreamRt,
|
||||
req: RequestHeader,
|
||||
max_body: usize,
|
||||
timeout: Duration,
|
||||
) -> Result<Fetched> {
|
||||
let addrs = u.resolve().await.map_err(|e| {
|
||||
Error::explain(
|
||||
ErrorType::ConnectNoRoute,
|
||||
format!("cannot resolve {}: {e}", u.authority),
|
||||
)
|
||||
})?;
|
||||
let mut last = None;
|
||||
let mut session = None;
|
||||
for addr in addrs {
|
||||
let mut p = peer(addr, u);
|
||||
p.options.total_connection_timeout = Some(timeout);
|
||||
match connector.get_http_session(&p).await {
|
||||
Ok((s, _reused)) => {
|
||||
session = Some(s);
|
||||
break;
|
||||
}
|
||||
Err(e) => last = Some(e),
|
||||
}
|
||||
}
|
||||
let Some(mut http) = session else {
|
||||
return Err(last.unwrap_or_else(|| {
|
||||
Error::explain(ErrorType::ConnectError, "no address to connect to")
|
||||
}));
|
||||
};
|
||||
http.set_read_timeout(Some(timeout));
|
||||
http.set_write_timeout(Some(timeout));
|
||||
http.write_request_header(Box::new(req)).await?;
|
||||
http.finish_request_body().await?;
|
||||
http.read_response_header().await?;
|
||||
let header = http
|
||||
.response_header()
|
||||
.cloned()
|
||||
.ok_or_else(|| Error::explain(ErrorType::InvalidHTTPHeader, "no response header"))?;
|
||||
|
||||
let mut header = None;
|
||||
for _ in 0..=MAX_INFORMATIONAL {
|
||||
http.read_response_header().await?;
|
||||
let h = http
|
||||
.response_header()
|
||||
.cloned()
|
||||
.ok_or_else(|| Error::explain(ErrorType::InvalidHTTPHeader, "no response header"))?;
|
||||
let s = h.status.as_u16();
|
||||
if (100..200).contains(&s) && s != 101 {
|
||||
continue;
|
||||
}
|
||||
header = Some(h);
|
||||
break;
|
||||
}
|
||||
let header = header.ok_or_else(|| {
|
||||
Error::explain(ErrorType::InvalidHTTPHeader, "only informational answers")
|
||||
})?;
|
||||
if header.status.as_u16() == 101 {
|
||||
// Switching protocols: there is no HTTP body to read.
|
||||
return Ok(Fetched {
|
||||
header,
|
||||
body: Bytes::new(),
|
||||
});
|
||||
}
|
||||
|
||||
let mut body = BytesMut::new();
|
||||
while let Some(chunk) = http.read_response_body().await? {
|
||||
if body.len() + chunk.len() > max_body {
|
||||
|
||||
Reference in New Issue
Block a user