diff --git a/src/lib.rs b/src/lib.rs index 7270684..db08c33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,10 +13,13 @@ pub mod prelude; pub mod relay; pub mod server; -pub fn prefix_match <'a> (hay: &'a str, needle: &str) -> Option <&'a str> +// The arguments are in order so they are in order overall: +// e.g. prefix_match ("/prefix", "/prefix/middle/suffix") -> "/middle/suffix" + +pub fn prefix_match <'a> (prefix: &str, hay: &'a str) -> Option <&'a str> { - if hay.starts_with (needle) { - Some (&hay [needle.len ()..]) + if hay.starts_with (prefix) { + Some (&hay [prefix.len ()..]) } else { None diff --git a/src/relay/mod.rs b/src/relay/mod.rs index 6d017fb..db1f130 100644 --- a/src/relay/mod.rs +++ b/src/relay/mod.rs @@ -467,7 +467,7 @@ async fn handle_all (req: Request , state: Arc ) // This is stuff the server can use. Clients can't // POST right now - return Ok (if let Some (request_code) = prefix_match (path, "/7ZSFUKGV/http_response/") { + return Ok (if let Some (request_code) = prefix_match ("/7ZSFUKGV/http_response/", path) { let request_code = request_code.into (); handle_http_response (req, state, request_code).await } @@ -476,14 +476,14 @@ async fn handle_all (req: Request , state: Arc ) }); } - Ok (if let Some (listen_code) = prefix_match (path, "/7ZSFUKGV/http_listen/") { + Ok (if let Some (listen_code) = prefix_match ("/7ZSFUKGV/http_listen/", path) { let api_key = match api_key { None => return Ok (error_reply (StatusCode::UNAUTHORIZED, "Can't register as server without an API key")), Some (x) => x, }; handle_http_listen (state, listen_code.into (), api_key.as_bytes ()).await } - else if let Some (rest) = prefix_match (path, "/frontend/servers/") { + else if let Some (rest) = prefix_match ("/frontend/servers/", path) { if rest == "" { #[derive (Serialize)] struct ServerEntry <'a> { diff --git a/src/server/file_server.rs b/src/server/file_server.rs index d602c19..3a01bb3 100644 --- a/src/server/file_server.rs +++ b/src/server/file_server.rs @@ -442,7 +442,7 @@ async fn internal_serve_all ( return Favicon; } - let uri = match prefix_match (uri, "/files") { + let uri = match prefix_match ("/files", uri) { Some (x) => x, None => return Root, }; diff --git a/todo.md b/todo.md index 1bdcf0e..afc6fb9 100644 --- a/todo.md +++ b/todo.md @@ -1,7 +1,6 @@ - Not working behind Nginx (Works okay behind Caddy) - Reduce idle memory use? -- Flip match_prefix args - Impl multi-range / multi-part byte serving - Allow spaces in server names - Deny unused HTTP methods for endpoints