[edge] Proxy: Pingora data plane with SNI certs, forward-auth, header rules, health checks, ACME
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// 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.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use pingora::connectors::http::Connector;
|
||||
use pingora::http::{RequestHeader, ResponseHeader};
|
||||
use pingora::upstreams::peer::HttpPeer;
|
||||
use pingora::{Error, ErrorType, Result};
|
||||
|
||||
use crate::runtime::UpstreamRt;
|
||||
|
||||
pub struct Fetched {
|
||||
pub header: ResponseHeader,
|
||||
pub body: Bytes,
|
||||
}
|
||||
|
||||
/// Dial timeout, matching Caddy's reverse_proxy default.
|
||||
pub const DIAL_TIMEOUT: Duration = Duration::from_secs(3);
|
||||
|
||||
pub fn peer(u: &UpstreamRt) -> HttpPeer {
|
||||
let mut p = HttpPeer::new(u.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.
|
||||
pub async fn fetch(
|
||||
connector: &Connector,
|
||||
u: &UpstreamRt,
|
||||
req: RequestHeader,
|
||||
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?;
|
||||
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 body = BytesMut::new();
|
||||
while let Some(chunk) = http.read_response_body().await? {
|
||||
if body.len() + chunk.len() > max_body {
|
||||
return Error::e_explain(ErrorType::InvalidHTTPHeader, "response body too large");
|
||||
}
|
||||
body.extend_from_slice(&chunk);
|
||||
}
|
||||
// Not returned to the pool: these calls are rare and a fresh connection is simplest.
|
||||
Ok(Fetched {
|
||||
header,
|
||||
body: body.freeze(),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user