From bbfb25e2cce77f9db93f82d73c007a0133060afa Mon Sep 17 00:00:00 2001 From: Ruslan Pislari Date: Mon, 11 May 2026 15:56:05 +0300 Subject: [PATCH 1/2] refactor: improve hostname resolution and clean up imports - Simplified hostname resolution by consistently prioritizing `server_name` and backend configurations. - Reordered and tidied imports for better readability. - Updated `HOST` header propagation logic for accuracy. --- crates/http-service/src/executor/wasi_http.rs | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/crates/http-service/src/executor/wasi_http.rs b/crates/http-service/src/executor/wasi_http.rs index 56d56bc..15e0a35 100644 --- a/crates/http-service/src/executor/wasi_http.rs +++ b/crates/http-service/src/executor/wasi_http.rs @@ -4,19 +4,19 @@ use std::time::Duration; use crate::executor; use crate::executor::HttpExecutor; use crate::state::HttpState; -use ::http::{HeaderMap, Request, Response, Uri, header}; -use anyhow::{Context, anyhow, bail}; +use anyhow::{anyhow, bail, Context}; use async_trait::async_trait; +use ::http::{header, HeaderMap, Request, Response, Uri}; use http_backend::{Backend, SERVER_NAME_HEADER}; use http_body_util::{BodyExt, Full}; use hyper::body::Body; use runtime::util::stats::{StatsTimer, StatsVisitor}; -use runtime::{InstancePre, store::StoreBuilder}; +use runtime::{store::StoreBuilder, InstancePre}; use smol_str::SmolStr; use tracing::Instrument; -use wasmtime_wasi_http::bindings::ProxyPre; use wasmtime_wasi_http::bindings::http::types::Scheme; -use wasmtime_wasi_http::{WasiHttpView, body::HyperOutgoingBody}; +use wasmtime_wasi_http::bindings::ProxyPre; +use wasmtime_wasi_http::{body::HyperOutgoingBody, WasiHttpView}; /// Execute context used by ['HttpService'] #[derive(Clone)] @@ -52,22 +52,24 @@ where // 1. `server_name` request header (if set and valid UTF-8) // 2. backend's configured hostname // 3. fallback to "localhost" - let backend_hostname: SmolStr = parts + let hostname: SmolStr = parts .headers .get(SERVER_NAME_HEADER) .and_then(|v| v.to_str().ok()) .map(SmolStr::from) - .or_else(|| self.backend.hostname()) + .or_else(|| { + self.backend + .hostname() + .map(|backend_hostname| match backend_hostname.find('.') { + None => backend_hostname, + Some(i) => { + let (_, domain) = backend_hostname.split_at(i + 1); + SmolStr::from(domain) + } + }) + }) .unwrap_or(LOCALHOST); - let hostname = match backend_hostname.find('.') { - None => backend_hostname.as_str(), - Some(i) => { - let (_, domain) = backend_hostname.split_at(i + 1); - domain - } - }; - // fix relative uri to absolute if parts.uri.scheme().is_none() { let mut uparts = parts.uri.clone().into_parts(); @@ -108,7 +110,7 @@ where }) .collect(); - propagate_headers.insert(header::HOST, backend_hostname.parse()?); + propagate_headers.insert(header::HOST, hostname.parse()?); let backend_uri = http_backend.uri(); let state = HttpState { From 97b42269b5b8725b90a15e2f0a648bed00d31800 Mon Sep 17 00:00:00 2001 From: Ruslan Pislari Date: Mon, 11 May 2026 16:12:36 +0300 Subject: [PATCH 2/2] refactor: simplify backend hostname resolution and header handling - Replaced hardcoded `LOCALHOST` with dynamic backend hostname resolution. - Added validation for backend hostname configuration and parsing. - Updated `HOST` header propagation using the resolved backend hostname. --- crates/http-service/src/executor/wasi_http.rs | 38 +++++++++---------- crates/http-service/src/lib.rs | 4 +- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/crates/http-service/src/executor/wasi_http.rs b/crates/http-service/src/executor/wasi_http.rs index 15e0a35..342d04d 100644 --- a/crates/http-service/src/executor/wasi_http.rs +++ b/crates/http-service/src/executor/wasi_http.rs @@ -4,19 +4,19 @@ use std::time::Duration; use crate::executor; use crate::executor::HttpExecutor; use crate::state::HttpState; -use anyhow::{anyhow, bail, Context}; +use ::http::{HeaderMap, Request, Response, Uri, header}; +use anyhow::{Context, anyhow, bail}; use async_trait::async_trait; -use ::http::{header, HeaderMap, Request, Response, Uri}; use http_backend::{Backend, SERVER_NAME_HEADER}; use http_body_util::{BodyExt, Full}; use hyper::body::Body; use runtime::util::stats::{StatsTimer, StatsVisitor}; -use runtime::{store::StoreBuilder, InstancePre}; +use runtime::{InstancePre, store::StoreBuilder}; use smol_str::SmolStr; use tracing::Instrument; -use wasmtime_wasi_http::bindings::http::types::Scheme; use wasmtime_wasi_http::bindings::ProxyPre; -use wasmtime_wasi_http::{body::HyperOutgoingBody, WasiHttpView}; +use wasmtime_wasi_http::bindings::http::types::Scheme; +use wasmtime_wasi_http::{WasiHttpView, body::HyperOutgoingBody}; /// Execute context used by ['HttpService'] #[derive(Clone)] @@ -46,8 +46,11 @@ where let (sender, receiver) = tokio::sync::oneshot::channel(); let (mut parts, body) = req.into_parts(); - const LOCALHOST: SmolStr = SmolStr::new_inline("localhost"); - + let backend_hostname = self + .backend + .hostname() + .context("backend hostname must be set")?; + let backend_host_header = backend_hostname.parse().context("invalid hostname")?; // Resolve backend hostname using the following precedence: // 1. `server_name` request header (if set and valid UTF-8) // 2. backend's configured hostname @@ -57,18 +60,13 @@ where .get(SERVER_NAME_HEADER) .and_then(|v| v.to_str().ok()) .map(SmolStr::from) - .or_else(|| { - self.backend - .hostname() - .map(|backend_hostname| match backend_hostname.find('.') { - None => backend_hostname, - Some(i) => { - let (_, domain) = backend_hostname.split_at(i + 1); - SmolStr::from(domain) - } - }) - }) - .unwrap_or(LOCALHOST); + .unwrap_or_else(|| match backend_hostname.find('.') { + None => backend_hostname, + Some(i) => { + let (_, domain) = backend_hostname.split_at(i + 1); + SmolStr::from(domain) + } + }); // fix relative uri to absolute if parts.uri.scheme().is_none() { @@ -110,7 +108,7 @@ where }) .collect(); - propagate_headers.insert(header::HOST, hostname.parse()?); + propagate_headers.insert(header::HOST, backend_host_header); let backend_uri = http_backend.uri(); let state = HttpState { diff --git a/crates/http-service/src/lib.rs b/crates/http-service/src/lib.rs index 350b6eb..46eca34 100644 --- a/crates/http-service/src/lib.rs +++ b/crates/http-service/src/lib.rs @@ -10,9 +10,7 @@ pub use crate::executor::ExecutorFactory; use crate::executor::HttpExecutor; use anyhow::{Context, Error, Result, bail}; use bytes::Bytes; -use http::{ - HeaderMap, HeaderName, HeaderValue, StatusCode, -}; +use http::{HeaderMap, HeaderName, HeaderValue, StatusCode}; use http_backend::SERVER_NAME_HEADER; use http_body_util::{BodyExt, Empty, Full}; use hyper::{body::Body, server::conn::http1, service::service_fn};