summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-08-19 13:35:55 +0200
committermetamuffin <metamuffin@disroot.org>2024-08-19 13:35:55 +0200
commitad13722e62d96eb84fa72f595bf3cfd3b570155f (patch)
tree69c179cd25ab83b9d237ef3ecb49ab4ccdd8d6d3
parentcb38b78de1b9c61dd3b96e001b10f5abaface0a1 (diff)
downloadgnix-ad13722e62d96eb84fa72f595bf3cfd3b570155f.tar
gnix-ad13722e62d96eb84fa72f595bf3cfd3b570155f.tar.bz2
gnix-ad13722e62d96eb84fa72f595bf3cfd3b570155f.tar.zst
handle h2 host by converting host header before any modules
-rw-r--r--src/main.rs8
-rw-r--r--src/modules/hosts.rs2
2 files changed, 7 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index 8ca86cb..14675b4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -219,9 +219,15 @@ pub async fn serve_stream<T: Unpin + Send + 'static + hyper::rt::Read + hyper::r
async fn service(
state: Arc<State>,
config: Arc<Config>,
- request: Request<Incoming>,
+ mut request: Request<Incoming>,
addr: SocketAddr,
) -> Result<hyper::Response<BoxBody<bytes::Bytes, ServiceError>>, ServiceError> {
+ // copy uri authority used in HTTP/2 to Host header field
+ if let Some(host) = request.uri().authority().map(|a| a.host()) {
+ let host = HeaderValue::from_str(host).map_err(|_| ServiceError::InvalidHeader)?;
+ request.headers_mut().insert(HOST, host);
+ }
+
debug!(
"{addr} ~> {:?} {}",
request.headers().get(HOST),
diff --git a/src/modules/hosts.rs b/src/modules/hosts.rs
index 03a8b57..72d1afd 100644
--- a/src/modules/hosts.rs
+++ b/src/modules/hosts.rs
@@ -26,13 +26,11 @@ impl Node for Hosts {
request: NodeRequest,
) -> Pin<Box<dyn Future<Output = Result<NodeResponse, ServiceError>> + Send + Sync + 'a>> {
Box::pin(async move {
- // use Host header but if missing like in HTTP/2 uses uri authority
let host = request
.headers()
.get(HOST)
.and_then(|e| e.to_str().ok())
.map(remove_port)
- .or(request.uri().authority().map(|a| a.host()))
.ok_or(ServiceError::NoHost)?;
let node = self.0.get(host).ok_or(ServiceError::UnknownHost)?;