From b7817c529cda65f13731d3ab58ed20bd51e572d9 Mon Sep 17 00:00:00 2001 From: Will Galebach Date: Mon, 19 Feb 2024 11:03:53 +0000 Subject: [PATCH] Updated packages with new IncomingHttpRequest --- Cargo.lock | 4 +- .../app_store/app_store/src/http_api.rs | 107 ++++++++++-------- kinode/packages/chess/chess/Cargo.toml | 2 +- kinode/packages/chess/chess/src/lib.rs | 5 +- 4 files changed, 65 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aaf5b4869..5a80dfed9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -956,7 +956,7 @@ dependencies = [ "anyhow", "base64 0.13.1", "bincode", - "kinode_process_lib 0.6.0 (git+https://github.com/kinode-dao/process_lib?rev=12bf9ee)", + "kinode_process_lib 0.6.0 (git+https://github.com/kinode-dao/process_lib?rev=3232423)", "pleco", "serde", "serde_json", @@ -2679,7 +2679,7 @@ dependencies = [ [[package]] name = "kit" version = "0.1.0" -source = "git+https://github.com/kinode-dao/kit?rev=f96cdaa#f96cdaa022ec8d0ff675bce3b92bd436236881ea" +source = "git+https://github.com/kinode-dao/kit?rev=83d1e86#83d1e86203f5a7d239c7d89aa0375167f1af6cdc" dependencies = [ "anyhow", "autocontext", diff --git a/kinode/packages/app_store/app_store/src/http_api.rs b/kinode/packages/app_store/app_store/src/http_api.rs index 65953bf59..c5b607499 100644 --- a/kinode/packages/app_store/app_store/src/http_api.rs +++ b/kinode/packages/app_store/app_store/src/http_api.rs @@ -47,6 +47,15 @@ pub fn handle_http_request( Ok(()) } +fn get_package_id(url_params: &HashMap) -> anyhow::Result { + let Some(package_id) = url_params.get("id") else { + return Err(anyhow::anyhow!("Missing id")); + }; + + let id = package_id.parse::()?; + Ok(id) +} + fn gen_package_info( id: &PackageId, listing: Option<&PackageListing>, @@ -94,23 +103,10 @@ fn serve_paths( requested_packages: &mut HashMap, req: &IncomingHttpRequest, ) -> anyhow::Result<(StatusCode, Option>, Vec)> { - let path = req.path()?; let method = req.method()?; - // TODO get rid of this workaround when we change `IncomingHttpRequest` - let bound_path: &str = if path.ends_with("auto-update") { - "/apps/:id/auto-update" - } else if path.ends_with("mirror") { - "/apps/:id/mirror" - } else if path.ends_with("caps") { - "/apps/:id/caps" - } else if path.starts_with("/apps/listed/") { - "/apps/listed/:id" - } else if &path == "/apps/listed" || &path == "/apps" { - &path - } else { - "/apps/:id" - }; + let bound_path: &str = req.bound_path(Some(&our.process.to_string())); + let url_params = req.url_params(); // print_to_terminal(0, &format!("HTTP {method} {path} {bound_path}")); @@ -121,7 +117,7 @@ fn serve_paths( return Ok(( StatusCode::METHOD_NOT_ALLOWED, None, - format!("Invalid method {method} for {path}").into_bytes(), + format!("Invalid method {method} for {bound_path}").into_bytes(), )); } let all: Vec = state @@ -140,7 +136,7 @@ fn serve_paths( return Ok(( StatusCode::METHOD_NOT_ALLOWED, None, - format!("Invalid method {method} for {path}").into_bytes(), + format!("Invalid method {method} for {bound_path}").into_bytes(), )); } let all: Vec = state @@ -159,11 +155,14 @@ fn serve_paths( // update a downloaded app: PUT // uninstall/delete a downloaded app: DELETE "/apps/:id" => { - let package_id = path - .split("/") - .last() - .unwrap_or_default() - .parse::()?; + let Ok(package_id) = get_package_id(url_params) else { + return Ok(( + StatusCode::BAD_REQUEST, + None, + format!("Missing id").into_bytes(), + )); + }; + match method { Method::GET => { let Some(pkg) = state.downloaded_packages.get(&package_id) else { @@ -234,18 +233,21 @@ fn serve_paths( _ => Ok(( StatusCode::METHOD_NOT_ALLOWED, None, - format!("Invalid method {method} for {path}").into_bytes(), + format!("Invalid method {method} for {bound_path}").into_bytes(), )), } } // GET detail about a specific listed app // download a listed app: POST "/apps/listed/:id" => { - let package_id = path - .split("/") - .last() - .unwrap_or_default() - .parse::()?; + let Ok(package_id) = get_package_id(url_params) else { + return Ok(( + StatusCode::BAD_REQUEST, + None, + format!("Missing id").into_bytes(), + )); + }; + match method { Method::GET => { let Some(listing) = state.get_listing(&package_id) else { @@ -318,18 +320,21 @@ fn serve_paths( _ => Ok(( StatusCode::METHOD_NOT_ALLOWED, None, - format!("Invalid method {method} for {path}").into_bytes(), + format!("Invalid method {method} for {bound_path}").into_bytes(), )), } } // GET caps for a specific downloaded app // approve capabilities for a downloaded app: POST "/apps/:id/caps" => { - let package_id = path - .split("/") - .nth(2) - .unwrap_or_default() - .parse::()?; + let Ok(package_id) = get_package_id(url_params) else { + return Ok(( + StatusCode::BAD_REQUEST, + None, + format!("Missing id").into_bytes(), + )); + }; + match method { // return the capabilities for that app Method::GET => Ok(match crate::fetch_package_manifest(&package_id) { @@ -356,18 +361,21 @@ fn serve_paths( _ => Ok(( StatusCode::METHOD_NOT_ALLOWED, None, - format!("Invalid method {method} for {path}").into_bytes(), + format!("Invalid method {method} for {bound_path}").into_bytes(), )), } } // start mirroring a downloaded app: PUT // stop mirroring a downloaded app: DELETE "/apps/:id/mirror" => { - let package_id = path - .split("/") - .nth(2) - .unwrap_or_default() - .parse::()?; + let Ok(package_id) = get_package_id(url_params) else { + return Ok(( + StatusCode::BAD_REQUEST, + None, + format!("Missing id").into_bytes(), + )); + }; + match method { // start mirroring an app Method::PUT => { @@ -382,18 +390,21 @@ fn serve_paths( _ => Ok(( StatusCode::METHOD_NOT_ALLOWED, None, - format!("Invalid method {method} for {path}").into_bytes(), + format!("Invalid method {method} for {bound_path}").into_bytes(), )), } } // start auto-updating a downloaded app: PUT // stop auto-updating a downloaded app: DELETE "/apps/:id/auto-update" => { - let package_id = path - .split("/") - .nth(2) - .unwrap_or_default() - .parse::()?; + let Ok(package_id) = get_package_id(url_params) else { + return Ok(( + StatusCode::BAD_REQUEST, + None, + format!("Missing id").into_bytes(), + )); + }; + match method { // start auto-updating an app Method::PUT => { @@ -408,14 +419,14 @@ fn serve_paths( _ => Ok(( StatusCode::METHOD_NOT_ALLOWED, None, - format!("Invalid method {method} for {path}").into_bytes(), + format!("Invalid method {method} for {bound_path}").into_bytes(), )), } } _ => Ok(( StatusCode::NOT_FOUND, None, - format!("Path not found: {}", path).into_bytes(), + format!("Path not found: {bound_path}").into_bytes(), )), } } diff --git a/kinode/packages/chess/chess/Cargo.toml b/kinode/packages/chess/chess/Cargo.toml index 7150a9706..385da11d5 100644 --- a/kinode/packages/chess/chess/Cargo.toml +++ b/kinode/packages/chess/chess/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" anyhow = "1.0" base64 = "0.13" bincode = "1.3.3" -kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "12bf9ee" } +kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "3232423" } pleco = "0.5" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/kinode/packages/chess/chess/src/lib.rs b/kinode/packages/chess/chess/src/lib.rs index 5a02fdfd2..fed53235d 100644 --- a/kinode/packages/chess/chess/src/lib.rs +++ b/kinode/packages/chess/chess/src/lib.rs @@ -193,7 +193,7 @@ fn handle_request(our: &Address, message: &Message, state: &mut ChessState) -> a } } } - http::HttpServerRequest::WebSocketOpen { path, channel_id } => { + http::HttpServerRequest::WebSocketOpen { channel_id, .. } => { // We know this is authenticated and unencrypted because we only // bound one path, the root path. So we know that client // frontend opened a websocket and can send updates @@ -419,7 +419,7 @@ fn handle_http_request( state: &mut ChessState, http_request: &http::IncomingHttpRequest, ) -> anyhow::Result<()> { - if http_request.path()? != "/games" { + if http_request.bound_path(Some(&our.process.to_string())) != "/games" { http::send_response( http::StatusCode::NOT_FOUND, None, @@ -454,6 +454,7 @@ fn handle_http_request( vec![], )); }; + if let Some(game) = state.games.get(game_id) && !game.ended {