Skip to content

Commit

Permalink
Merge pull request #16 from G-Core/fix/wasi_http_backend
Browse files Browse the repository at this point in the history
 add server_name as local request authority and remove default ht…
  • Loading branch information
ruslanti authored Aug 1, 2024
2 parents cd21856 + c7c5cc3 commit d0d69df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
16 changes: 9 additions & 7 deletions crates/http-service/src/executor/wasi_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 7 additions & 4 deletions crates/http-service/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ impl<C> BackendRequest for HttpState<C> {
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(|| {
Expand Down

0 comments on commit d0d69df

Please sign in to comment.