From c7c5cc39e147fafbf0c6b4e99f30e2ac98ad3e9e Mon Sep 17 00:00:00 2001 From: Ruslan Pislari Date: Thu, 1 Aug 2024 11:21:53 +0300 Subject: [PATCH] fix: add server_name as local request authority and remove default http/https port --- Cargo.toml | 2 +- crates/http-service/src/executor/wasi_http.rs | 16 +++++++++------- crates/http-service/src/state.rs | 11 +++++++---- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7bf2b2c..a3ae640 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["crates/*"] resolver = "2" [workspace.package] -version = "0.5.0" +version = "0.5.1" edition = "2021" publish = false authors = ["FastEdge Development Team"] diff --git a/crates/http-service/src/executor/wasi_http.rs b/crates/http-service/src/executor/wasi_http.rs index af4744f..de56061 100644 --- a/crates/http-service/src/executor/wasi_http.rs +++ b/crates/http-service/src/executor/wasi_http.rs @@ -3,7 +3,7 @@ use std::time::{Duration, Instant}; use anyhow::{anyhow, bail, Context}; use async_trait::async_trait; use bytesize::ByteSize; -use http::uri::{Authority, Scheme}; +use http::uri::Scheme; use http::{header, HeaderMap, Response, Uri}; use http_body_util::combinators::BoxBody; use http_body_util::{BodyExt, Full}; @@ -73,12 +73,18 @@ where let (sender, receiver) = tokio::sync::oneshot::channel(); let (mut parts, body) = req.into_parts(); + let server_name = parts + .headers + .get("server_name") + .and_then(|v| v.to_str().ok()) + .ok_or(anyhow!("header Server_name is missing"))?; + // fix relative uri to absolute if parts.uri.scheme().is_none() { let mut uparts = parts.uri.clone().into_parts(); uparts.scheme = Some(Scheme::HTTP); if uparts.authority.is_none() { - uparts.authority = Some(Authority::from_static("localhost")) + uparts.authority = server_name.parse().ok() } parts.uri = Uri::from_parts(uparts)?; } @@ -115,11 +121,7 @@ where }) .collect(); - let server_name = parts - .headers - .get("server_name") - .and_then(|v| v.to_str().ok()) - .ok_or(anyhow!("header Server_name is missing"))?; + propagate_headers.insert(header::HOST, be_base_domain(server_name).parse()?); let backend_uri = http_backend.uri(); diff --git a/crates/http-service/src/state.rs b/crates/http-service/src/state.rs index 6ba13e5..e3e0f25 100644 --- a/crates/http-service/src/state.rs +++ b/crates/http-service/src/state.rs @@ -22,11 +22,14 @@ impl BackendRequest for HttpState { let original_url = head.uri; tracing::trace!("send request original url: {:?}", original_url); let original_host = original_url.authority().map(|a| { - if let Some(port) = a.port() { - format!("{}:{}", a.host(), port) - } else { - a.host().to_string() + match (original_url.scheme_str(), a.port().map(|p| p.as_u16())) { + (None, Some(80)) + | (Some("http"), Some(80)) + | (Some("https"), Some(443)) + | (_, None) => a.host().to_string(), + (_, Some(port)) => format!("{}:{}", a.host(), port), } + }); let original_host = original_host .or_else(|| {