From af958ac43d625b40ab4660e7024410ec201e1fa7 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 15 May 2021 17:25:13 +0000 Subject: [PATCH] :construction: compiles but probably doesn't work --- crates/ptth_relay/src/lib.rs | 17 ++++++ crates/ptth_relay/src/routing.rs | 94 ++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 42 deletions(-) diff --git a/crates/ptth_relay/src/lib.rs b/crates/ptth_relay/src/lib.rs index ea6c9c5..46084bd 100644 --- a/crates/ptth_relay/src/lib.rs +++ b/crates/ptth_relay/src/lib.rs @@ -590,6 +590,23 @@ async fn handle_all ( trace! ("Request path: {}", path); let route = routing::route_url (req.method (), &path); + let route = match route { + Ok (x) => x, + Err (e) => { + use routing::Error; + + let response = match e { + Error::BadUriFormat => error_reply (StatusCode::BAD_REQUEST, "Bad URI format")?, + Error::CantPost => { + error! ("Can't POST {}", path); + error_reply (StatusCode::BAD_REQUEST, "Can't POST this")? + }, + Error::MethodNotAllowed => error_reply (StatusCode::METHOD_NOT_ALLOWED, "Method not allowed. Are you POST-ing to a GET-only url, or vice versa?")?, + Error::RoutingFailed => error_reply (StatusCode::OK, "URL routing failed")?, + }; + return Ok (response); + }, + }; let response = match route { ClientAuditLog => handle_audit_log (state, handlebars).await?, ClientRelayIsUp => error_reply (StatusCode::OK, "Relay is up")?, diff --git a/crates/ptth_relay/src/routing.rs b/crates/ptth_relay/src/routing.rs index a827876..c478c1e 100644 --- a/crates/ptth_relay/src/routing.rs +++ b/crates/ptth_relay/src/routing.rs @@ -32,129 +32,139 @@ pub enum Route <'a> { }, } -pub fn route_url <'a> (method: &Method, path: &'a str) -> Route <'a> { +#[derive (Debug, PartialEq)] +pub enum Error { + BadUriFormat, + CantPost, + MethodNotAllowed, + RoutingFailed, +} + +type Result <'a> = std::result::Result , Error>; + +pub fn route_url <'a> (method: &Method, path: &'a str) -> Result <'a> { if let Some (listen_code) = path.strip_prefix ("/7ZSFUKGV/http_listen/") { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::ServerHttpListen { + Ok (Route::ServerHttpListen { listen_code - } + }) } else if let Some (request_code) = path.strip_prefix ("/7ZSFUKGV/http_response/") { if method != Method::POST { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::ServerHttpResponse { + Ok (Route::ServerHttpResponse { request_code - } + }) } else if path == "/frontend/register" { if method != Method::POST { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::RegisterServer + Ok (Route::RegisterServer) } else if let Some (rest) = path.strip_prefix ("/frontend/servers/") { // DRY T4H76LB3 if rest.is_empty () { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::ClientServerList + Ok (Route::ClientServerList) } else if let Some (idx) = rest.find ('/') { let listen_code = &rest [0..idx]; - Route::ClientServerGet { + Ok (Route::ClientServerGet { listen_code, path: &rest [idx..], - } + }) } else { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::ErrorBadUriFormat + Ok (Route::ErrorBadUriFormat) } } else if path == "/frontend/unregistered_servers" { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::ClientUnregisteredServers + Ok (Route::ClientUnregisteredServers) } else if path == "/frontend/audit_log" { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::ClientAuditLog + Ok (Route::ClientAuditLog) } else if let Some (rest) = path.strip_prefix ("/frontend/debug/") { if rest.is_empty () { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::Debug + Ok (Route::Debug) } else if rest == "endless_source" { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::DebugEndlessSource (None) + Ok (Route::DebugEndlessSource (None)) } else if rest == "endless_source_throttled" { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::DebugEndlessSource (Some (1024 / 64)) + Ok (Route::DebugEndlessSource (Some (1024 / 64))) } else if rest == "endless_sink" { if method != Method::POST { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::DebugEndlessSink + Ok (Route::DebugEndlessSink) } else if rest == "gen_key" { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::DebugGenKey + Ok (Route::DebugGenKey) } else { - Route::ErrorRoutingFailed + Ok (Route::ErrorRoutingFailed) } } else if path == "/" { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::Root + Ok (Route::Root) } else if path == "/frontend/relay_up_check" { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::ClientRelayIsUp + Ok (Route::ClientRelayIsUp) } else if path == "/frontend/test_mysterious_error" { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::DebugMysteriousError + Ok (Route::DebugMysteriousError) } else if let Some (rest) = path.strip_prefix ("/scraper/") { if method != Method::GET { - return Route::ErrorMethodNotAllowed; + return Err (Error::MethodNotAllowed); } - Route::Scraper { + Ok (Route::Scraper { rest - } + }) } else { - Route::ErrorRoutingFailed + Err (Error::RoutingFailed) } } @@ -165,14 +175,14 @@ mod tests { #[test] fn routing () { for (input, expected) in vec! [ - ("/", Route::Root), + ("/", Ok (Route::Root)), ].into_iter () { let actual = route_url (&Method::GET, input); assert_eq! (actual, expected); } for (input, expected) in vec! [ - ("/", Route::ErrorMethodNotAllowed), + ("/", Err (Error::MethodNotAllowed)), ].into_iter () { let actual = route_url (&Method::POST, input); assert_eq! (actual, expected);