support upgrade-insecure-requests option

This commit is contained in:
Jun Kurihara 2022-07-10 04:28:50 +09:00
commit 1a80e405b5
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
8 changed files with 112 additions and 29 deletions

View file

@ -3,6 +3,7 @@ use crate::error::*;
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum UpstreamOption {
OverrideHost,
UpgradeInsecureRequests,
// TODO: Adds more options for heder override
}
impl TryFrom<&str> for UpstreamOption {
@ -10,6 +11,7 @@ impl TryFrom<&str> for UpstreamOption {
fn try_from(val: &str) -> Result<Self> {
match val {
"override_host" => Ok(Self::OverrideHost),
"upgrade_insecure_requests" => Ok(Self::UpgradeInsecureRequests),
_ => Err(anyhow!("Unsupported header option")),
}
}

View file

@ -92,7 +92,7 @@ where
return http_error(StatusCode::SERVICE_UNAVAILABLE);
};
// debug!("Request to be forwarded: {:?}", req_forwarded);
req_forwarded.log(&client_addr, Some("Forwarding"));
req_forwarded.log(&client_addr, Some("(Forwarding)"));
// Forward request to
let mut res_backend = match self.forwarder.request(req_forwarded).await {

View file

@ -16,13 +16,21 @@ pub(super) fn apply_upstream_options_to_header(
upstream: &Upstream,
) -> Result<()> {
for opt in upstream.opts.iter() {
println!("{:?}", opt);
match opt {
UpstreamOption::OverrideHost => {
// overwrite HOST value with upstream hostname (like 192.168.xx.x seen from rpxy)
let upstream_host = upstream_scheme_host.host().ok_or_else(|| anyhow!("none"))?;
headers
.insert(header::HOST, HeaderValue::from_str(upstream_host)?)
.ok_or_else(|| anyhow!("none"))?;
}
UpstreamOption::UpgradeInsecureRequests => {
// add upgrade-insecure-requests in request header if not exist
headers
.entry(header::UPGRADE_INSECURE_REQUESTS)
.or_insert(HeaderValue::from_bytes(&[b'1']).unwrap());
}
}
}