Skip to content

Commit

Permalink
fix: adding requestor field for wasi-http
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanti committed Aug 21, 2024
1 parent c68fde4 commit 879a966
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 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.3"
version = "0.5.4"
edition = "2021"
publish = false
authors = ["FastEdge Development Team"]
Expand Down
31 changes: 17 additions & 14 deletions crates/http-service/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ where
body,
};

let properties = Self::get_properties(&parts.headers);
let properties = get_properties(&parts.headers);

let store_builder = self.store_builder.to_owned().with_properties(properties);
let wasi_nn = self.store_builder.make_wasi_nn_ctx()?;
Expand Down Expand Up @@ -192,19 +192,7 @@ where
builder.body(body).map(|r| (r, used)).map_err(anyhow::Error::msg)
}

fn get_properties(headers: &HeaderMap<HeaderValue>) -> HashMap<String, String> {
let mut properties = HashMap::new();
if let Some(client_ip) = headers.get(X_REAL_IP).and_then(|v| v.to_str().ok()) {
properties.insert("client_ip".to_owned(), client_ip.to_owned());
}
if let Some(traceparent) = headers.get(TRACEPARENT).and_then(|v| v.to_str().ok()) {
properties.insert("traceparent".to_owned(), traceparent.to_owned());
}
if let Some(requestor) = headers.get(X_CDN_REQUESTOR).and_then(|v| v.to_str().ok()) {
properties.insert("requestor".to_owned(), requestor.to_owned());
}
properties
}

}

fn to_fastedge_http_method(method: &Method) -> Result<fastedge::http::Method> {
Expand All @@ -219,3 +207,18 @@ fn to_fastedge_http_method(method: &Method) -> Result<fastedge::http::Method> {
method => bail!("unsupported method: {}", method),
})
}


pub(crate) fn get_properties(headers: &HeaderMap<HeaderValue>) -> HashMap<String, String> {
let mut properties = HashMap::new();
if let Some(client_ip) = headers.get(X_REAL_IP).and_then(|v| v.to_str().ok()) {
properties.insert("client_ip".to_owned(), client_ip.to_owned());
}
if let Some(traceparent) = headers.get(TRACEPARENT).and_then(|v| v.to_str().ok()) {
properties.insert("traceparent".to_owned(), traceparent.to_owned());
}
if let Some(requestor) = headers.get(X_CDN_REQUESTOR).and_then(|v| v.to_str().ok()) {
properties.insert("requestor".to_owned(), requestor.to_owned());
}
properties
}
5 changes: 3 additions & 2 deletions crates/http-service/src/executor/wasi_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use dictionary::Dictionary;
use http_backend::Backend;
use runtime::store::StoreBuilder;
use runtime::InstancePre;

use crate::executor;
use crate::executor::HttpExecutor;
use crate::state::HttpState;

Expand Down Expand Up @@ -100,7 +100,8 @@ where
let body = Full::new(body).map_err(|never| match never {});
let body = body.boxed();

let store_builder = self.store_builder.to_owned(); //.with_properties(properties);
let properties = executor::get_properties(&parts.headers);
let store_builder = self.store_builder.to_owned().with_properties(properties);
let wasi_nn = self
.store_builder
.make_wasi_nn_ctx()
Expand Down
4 changes: 2 additions & 2 deletions crates/http-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ fn app_name_from_request(req: &Request<impl Body>) -> Result<SmolStr> {
bail!("app name not found in URL".to_string());
}

return match path.find('/') {
match path.find('/') {
None => Ok(SmolStr::from(path)),
Some(i) => {
let (prefix, _) = path.split_at(i);
Expand All @@ -478,7 +478,7 @@ fn app_name_from_request(req: &Request<impl Body>) -> Result<SmolStr> {
Ok(SmolStr::from(prefix))
}
}
};
}
}

fn app_res_headers(app_cfg: App) -> HeaderMap {
Expand Down

0 comments on commit 879a966

Please sign in to comment.