From db50846909f757770d1be2b4ffa8e5b0cfb95420 Mon Sep 17 00:00:00 2001 From: Will Galebach Date: Thu, 15 Feb 2024 15:59:40 +0000 Subject: [PATCH 1/4] Incoming HTTP request updates --- Cargo.lock | 1 + kinode/Cargo.toml | 1 + kinode/src/http/server.rs | 12 +++++++++++- kinode/src/http/utils.rs | 4 ++++ lib/src/http/server_types.rs | 8 +++++--- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7285c74a..253110fd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3139,6 +3139,7 @@ dependencies = [ "open", "public-ip", "rand 0.8.5", + "regex", "reqwest", "ring 0.16.20", "rmp-serde", diff --git a/kinode/Cargo.toml b/kinode/Cargo.toml index 7da207a90..092504c59 100644 --- a/kinode/Cargo.toml +++ b/kinode/Cargo.toml @@ -82,3 +82,4 @@ warp = "0.3.5" wasmtime = "15.0.1" wasmtime-wasi = "15.0.1" zip = "0.6" +regex = "1.10" diff --git a/kinode/src/http/server.rs b/kinode/src/http/server.rs index 0453af366..77e523b71 100644 --- a/kinode/src/http/server.rs +++ b/kinode/src/http/server.rs @@ -41,6 +41,7 @@ type WsPathBindings = Arc>>; struct BoundPath { pub app: ProcessId, + pub path: String, pub secure_subdomain: Option, pub authenticated: bool, pub local_only: bool, @@ -185,17 +186,19 @@ pub async fn http_server( let jwt_secret_bytes = Arc::new(jwt_secret_bytes); let http_response_senders: HttpResponseSenders = Arc::new(DashMap::new()); let ws_senders: WebSocketSenders = Arc::new(DashMap::new()); + let path = format!("/rpc:distro:sys/message"); // add RPC path let mut bindings_map: Router = Router::new(); let rpc_bound_path = BoundPath { app: ProcessId::new(Some("rpc"), "distro", "sys"), + path: path.clone(), secure_subdomain: None, // TODO maybe RPC should have subdomain? authenticated: false, local_only: true, static_content: None, }; - bindings_map.add("/rpc:distro:sys/message", rpc_bound_path); + bindings_map.add(&path, rpc_bound_path); let path_bindings: PathBindings = Arc::new(RwLock::new(bindings_map)); // ws path bindings @@ -584,6 +587,7 @@ async fn http_handler( } } else { // otherwise, make a message to the correct app + let url_params: HashMap = route.params().into_iter().map(|(key, value)| (key.to_string(), value.to_string())).collect(); ( KernelMessage { id, @@ -607,7 +611,9 @@ async fn http_handler( host.unwrap_or(Authority::from_static("localhost")), original_path ), + bound_path: bound_path.path.clone(), headers: serialized_headers, + url_params, query_params, })) .unwrap(), @@ -1138,6 +1144,7 @@ async fn handle_app_message( &normalize_path(&path), BoundPath { app: km.source.process.clone(), + path: path.clone(), secure_subdomain: None, authenticated, local_only, @@ -1160,6 +1167,7 @@ async fn handle_app_message( &normalize_path(&path), BoundPath { app: km.source.process.clone(), + path: path.clone(), secure_subdomain: None, authenticated, local_only, @@ -1183,6 +1191,7 @@ async fn handle_app_message( &normalize_path(&path), BoundPath { app: km.source.process.clone(), + path: path.clone(), secure_subdomain: Some(subdomain), authenticated: true, local_only: false, @@ -1205,6 +1214,7 @@ async fn handle_app_message( &normalize_path(&path), BoundPath { app: km.source.process.clone(), + path: path.clone(), secure_subdomain: Some(subdomain), authenticated: true, local_only: false, diff --git a/kinode/src/http/utils.rs b/kinode/src/http/utils.rs index 3f7f3c1f4..76cfec540 100644 --- a/kinode/src/http/utils.rs +++ b/kinode/src/http/utils.rs @@ -69,6 +69,10 @@ pub fn auth_cookie_valid(our_node: &str, cookie: &str, jwt_secret: &[u8]) -> boo } pub fn normalize_path(path: &str) -> String { + if path.starts_with("regex") { + return path.strip_prefix("regex").unwrap_or("").to_string(); + } + match path.strip_suffix('/') { Some(new) => new.to_string(), None => path.to_string(), diff --git a/lib/src/http/server_types.rs b/lib/src/http/server_types.rs index ed0a90f53..12d50843b 100644 --- a/lib/src/http/server_types.rs +++ b/lib/src/http/server_types.rs @@ -29,10 +29,12 @@ pub enum HttpServerRequest { #[derive(Debug, Serialize, Deserialize)] pub struct IncomingHttpRequest { - pub source_socket_addr: Option, // will parse to SocketAddr - pub method: String, // will parse to http::Method - pub url: String, // will parse to url::Url + pub source_socket_addr: Option, // will parse to SocketAddr + pub method: String, // will parse to http::Method + pub url: String, // will parse to url::Url + pub bound_path: String, // the path that was originally bound pub headers: HashMap, + pub url_params: HashMap, // comes from route-recognizer pub query_params: HashMap, // BODY is stored in the lazy_load_blob, as bytes } From f9039b5a28dc6c60505816fbeee0defee3a3305c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:00:05 +0000 Subject: [PATCH 2/4] Format Rust code using rustfmt --- kinode/src/http/server.rs | 6 +++++- lib/src/http/server_types.rs | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/kinode/src/http/server.rs b/kinode/src/http/server.rs index 77e523b71..51213efba 100644 --- a/kinode/src/http/server.rs +++ b/kinode/src/http/server.rs @@ -587,7 +587,11 @@ async fn http_handler( } } else { // otherwise, make a message to the correct app - let url_params: HashMap = route.params().into_iter().map(|(key, value)| (key.to_string(), value.to_string())).collect(); + let url_params: HashMap = route + .params() + .into_iter() + .map(|(key, value)| (key.to_string(), value.to_string())) + .collect(); ( KernelMessage { id, diff --git a/lib/src/http/server_types.rs b/lib/src/http/server_types.rs index 12d50843b..363b00e43 100644 --- a/lib/src/http/server_types.rs +++ b/lib/src/http/server_types.rs @@ -29,10 +29,10 @@ pub enum HttpServerRequest { #[derive(Debug, Serialize, Deserialize)] pub struct IncomingHttpRequest { - pub source_socket_addr: Option, // will parse to SocketAddr - pub method: String, // will parse to http::Method - pub url: String, // will parse to url::Url - pub bound_path: String, // the path that was originally bound + pub source_socket_addr: Option, // will parse to SocketAddr + pub method: String, // will parse to http::Method + pub url: String, // will parse to url::Url + pub bound_path: String, // the path that was originally bound pub headers: HashMap, pub url_params: HashMap, // comes from route-recognizer pub query_params: HashMap, From ae4a149984772c837270eab42e7da3aac9c970ea Mon Sep 17 00:00:00 2001 From: Will Galebach Date: Thu, 15 Feb 2024 16:10:17 +0000 Subject: [PATCH 3/4] remove regex --- kinode/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/kinode/Cargo.toml b/kinode/Cargo.toml index 092504c59..7da207a90 100644 --- a/kinode/Cargo.toml +++ b/kinode/Cargo.toml @@ -82,4 +82,3 @@ warp = "0.3.5" wasmtime = "15.0.1" wasmtime-wasi = "15.0.1" zip = "0.6" -regex = "1.10" From f541f83d1eccee53f8b870ddfe07d14be90468cf Mon Sep 17 00:00:00 2001 From: Will Galebach Date: Thu, 15 Feb 2024 16:11:29 +0000 Subject: [PATCH 4/4] Remove unnecessary bit --- kinode/src/http/utils.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/kinode/src/http/utils.rs b/kinode/src/http/utils.rs index 76cfec540..3f7f3c1f4 100644 --- a/kinode/src/http/utils.rs +++ b/kinode/src/http/utils.rs @@ -69,10 +69,6 @@ pub fn auth_cookie_valid(our_node: &str, cookie: &str, jwt_secret: &[u8]) -> boo } pub fn normalize_path(path: &str) -> String { - if path.starts_with("regex") { - return path.strip_prefix("regex").unwrap_or("").to_string(); - } - match path.strip_suffix('/') { Some(new) => new.to_string(), None => path.to_string(),