add version setting of request forwarded
This commit is contained in:
parent
f24f059d45
commit
c5c7847b92
6 changed files with 43 additions and 9 deletions
|
|
@ -4,6 +4,8 @@ use crate::error::*;
|
|||
pub enum UpstreamOption {
|
||||
OverrideHost,
|
||||
UpgradeInsecureRequests,
|
||||
ConvertToHttp11,
|
||||
ConvertToHttp2,
|
||||
// TODO: Adds more options for heder override
|
||||
}
|
||||
impl TryFrom<&str> for UpstreamOption {
|
||||
|
|
@ -12,6 +14,8 @@ impl TryFrom<&str> for UpstreamOption {
|
|||
match val {
|
||||
"override_host" => Ok(Self::OverrideHost),
|
||||
"upgrade_insecure_requests" => Ok(Self::UpgradeInsecureRequests),
|
||||
"convert_to_http11" => Ok(Self::ConvertToHttp11),
|
||||
"convert_to_http2" => Ok(Self::ConvertToHttp2),
|
||||
_ => Err(RpxyError::Other(anyhow!("Unsupported header option"))),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -224,5 +224,13 @@ fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> std::result::Result<
|
|||
rp_settings.iter().filter(|rpo| rpo.path.is_none()).count() < 2,
|
||||
"Multiple default reverse proxy setting"
|
||||
);
|
||||
ensure!(
|
||||
upstream
|
||||
.iter()
|
||||
.all(|(_, elem)| !(elem.opts.contains(&UpstreamOption::ConvertToHttp11)
|
||||
&& elem.opts.contains(&UpstreamOption::ConvertToHttp2))),
|
||||
"either one of force_http11 or force_http2 can be enabled"
|
||||
);
|
||||
|
||||
Ok(ReverseProxy { upstream })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ use crate::{backend::UpstreamGroup, error::*, globals::Globals, log::*, utils::S
|
|||
use hyper::{
|
||||
client::connect::Connect,
|
||||
header::{self, HeaderValue},
|
||||
http::uri::Scheme,
|
||||
Body, Client, Request, Response, StatusCode, Uri, Version,
|
||||
};
|
||||
use std::{env, net::SocketAddr, sync::Arc};
|
||||
|
|
@ -95,7 +94,7 @@ where
|
|||
error!("Failed to generate destination uri for reverse proxy: {}", e);
|
||||
return self.return_with_error_log(StatusCode::SERVICE_UNAVAILABLE, &mut log_data);
|
||||
};
|
||||
// debug!("Request to be forwarded: {:?}", req_forwarded);
|
||||
debug!("Request to be forwarded: {:?}", req);
|
||||
log_data.xff(&req.headers().get("x-forwarded-for"));
|
||||
log_data.upstream(req.uri());
|
||||
//////
|
||||
|
|
@ -290,10 +289,10 @@ where
|
|||
.insert(header::CONNECTION, HeaderValue::from_str("upgrade")?);
|
||||
}
|
||||
|
||||
// Change version to http/1.1 when destination scheme is http
|
||||
if req.version() != Version::HTTP_11 && upstream_chosen.uri.scheme() == Some(&Scheme::HTTP) {
|
||||
*req.version_mut() = Version::HTTP_11;
|
||||
} else if req.version() == Version::HTTP_3 {
|
||||
apply_upstream_options_to_request_line(req, upstream_group)?;
|
||||
|
||||
// if not specified (force_httpXX_upstream), version is preserved except for http/3
|
||||
if req.version() == Version::HTTP_3 {
|
||||
debug!("HTTP/3 is currently unsupported for request to upstream. Use HTTP/2.");
|
||||
*req.version_mut() = Version::HTTP_2;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ pub(super) fn apply_upstream_options_to_header(
|
|||
.entry(header::UPGRADE_INSECURE_REQUESTS)
|
||||
.or_insert(HeaderValue::from_bytes(&[b'1']).unwrap());
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,24 @@
|
|||
use crate::error::*;
|
||||
use crate::{
|
||||
backend::{UpstreamGroup, UpstreamOption},
|
||||
error::*,
|
||||
};
|
||||
use hyper::{header, Request};
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
// Functions to manipulate request line
|
||||
|
||||
pub(super) fn apply_upstream_options_to_request_line<B>(req: &mut Request<B>, upstream: &UpstreamGroup) -> Result<()> {
|
||||
for opt in upstream.opts.iter() {
|
||||
match opt {
|
||||
UpstreamOption::ConvertToHttp11 => *req.version_mut() = hyper::Version::HTTP_11,
|
||||
UpstreamOption::ConvertToHttp2 => *req.version_mut() = hyper::Version::HTTP_2,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub trait ParseHost {
|
||||
fn parse_host(&self) -> Result<&[u8]>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue