2021-04-10 14:17:52 +00:00
|
|
|
use hyper::Method;
|
|
|
|
|
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
pub enum Route <'a> {
|
|
|
|
ClientAuditLog,
|
|
|
|
ClientRelayIsUp,
|
|
|
|
ClientServerGet {
|
|
|
|
listen_code: &'a str,
|
|
|
|
path: &'a str,
|
|
|
|
},
|
|
|
|
ClientServerList,
|
|
|
|
ClientUnregisteredServers,
|
|
|
|
Debug,
|
|
|
|
DebugEndlessSink,
|
2021-04-10 14:38:53 +00:00
|
|
|
DebugEndlessSource (Option <usize>),
|
2021-04-10 14:17:52 +00:00
|
|
|
DebugGenKey,
|
|
|
|
DebugMysteriousError,
|
2021-05-02 21:18:07 +00:00
|
|
|
RegisterServer,
|
2021-04-10 14:17:52 +00:00
|
|
|
Root,
|
|
|
|
Scraper {
|
|
|
|
rest: &'a str,
|
|
|
|
},
|
|
|
|
ServerHttpListen {
|
|
|
|
listen_code: &'a str
|
|
|
|
},
|
|
|
|
ServerHttpResponse {
|
|
|
|
request_code: &'a str,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-05-15 17:25:13 +00:00
|
|
|
#[derive (Debug, PartialEq)]
|
|
|
|
pub enum Error {
|
|
|
|
BadUriFormat,
|
|
|
|
MethodNotAllowed,
|
2021-05-15 17:37:24 +00:00
|
|
|
NotFound,
|
2021-05-15 17:25:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 17:37:24 +00:00
|
|
|
pub fn route_url <'a> (method: &Method, path: &'a str) -> Result <Route <'a>, Error> {
|
2021-05-15 17:14:12 +00:00
|
|
|
if let Some (listen_code) = path.strip_prefix ("/7ZSFUKGV/http_listen/") {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:14:12 +00:00
|
|
|
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::ServerHttpListen {
|
2021-05-15 17:14:12 +00:00
|
|
|
listen_code
|
2021-05-15 17:25:13 +00:00
|
|
|
})
|
2021-05-15 17:14:12 +00:00
|
|
|
}
|
|
|
|
else if let Some (request_code) = path.strip_prefix ("/7ZSFUKGV/http_response/") {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::POST)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::ServerHttpResponse {
|
2021-05-15 17:14:12 +00:00
|
|
|
request_code
|
2021-05-15 17:25:13 +00:00
|
|
|
})
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
2021-05-15 17:14:12 +00:00
|
|
|
else if path == "/frontend/register" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::POST)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::RegisterServer)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if let Some (rest) = path.strip_prefix ("/frontend/servers/") {
|
|
|
|
// DRY T4H76LB3
|
|
|
|
|
|
|
|
if rest.is_empty () {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::ClientServerList)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if let Some (idx) = rest.find ('/') {
|
|
|
|
let listen_code = &rest [0..idx];
|
|
|
|
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::ClientServerGet {
|
2021-04-10 14:17:52 +00:00
|
|
|
listen_code,
|
2021-05-02 22:32:24 +00:00
|
|
|
path: &rest [idx..],
|
2021-05-15 17:25:13 +00:00
|
|
|
})
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
|
|
|
Err (Error::BadUriFormat)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if path == "/frontend/unregistered_servers" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::ClientUnregisteredServers)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if path == "/frontend/audit_log" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::ClientAuditLog)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if let Some (rest) = path.strip_prefix ("/frontend/debug/") {
|
|
|
|
if rest.is_empty () {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::Debug)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if rest == "endless_source" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::DebugEndlessSource (None))
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if rest == "endless_source_throttled" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::DebugEndlessSource (Some (1024 / 64)))
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if rest == "endless_sink" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::POST)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::DebugEndlessSink)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if rest == "gen_key" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::DebugGenKey)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-05-15 17:37:24 +00:00
|
|
|
Err (Error::NotFound)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if path == "/" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::Root)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if path == "/frontend/relay_up_check" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::ClientRelayIsUp)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if path == "/frontend/test_mysterious_error" {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::DebugMysteriousError)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else if let Some (rest) = path.strip_prefix ("/scraper/") {
|
2021-05-15 17:37:24 +00:00
|
|
|
assert_method (method, Method::GET)?;
|
2021-05-15 17:25:13 +00:00
|
|
|
Ok (Route::Scraper {
|
2021-04-10 14:17:52 +00:00
|
|
|
rest
|
2021-05-15 17:25:13 +00:00
|
|
|
})
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-05-15 17:37:24 +00:00
|
|
|
Err (Error::NotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_method <M: PartialEq <Method>> (method: M, expected: Method) -> Result <(), Error>
|
|
|
|
{
|
|
|
|
if method == expected {
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Err (Error::MethodNotAllowed)
|
2021-04-10 14:17:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg (test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn routing () {
|
|
|
|
for (input, expected) in vec! [
|
2021-05-15 17:25:13 +00:00
|
|
|
("/", Ok (Route::Root)),
|
2021-04-10 14:17:52 +00:00
|
|
|
].into_iter () {
|
2021-04-10 14:38:53 +00:00
|
|
|
let actual = route_url (&Method::GET, input);
|
2021-04-10 14:17:52 +00:00
|
|
|
assert_eq! (actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (input, expected) in vec! [
|
2021-05-15 17:25:13 +00:00
|
|
|
("/", Err (Error::MethodNotAllowed)),
|
2021-04-10 14:17:52 +00:00
|
|
|
].into_iter () {
|
2021-04-10 14:38:53 +00:00
|
|
|
let actual = route_url (&Method::POST, input);
|
2021-04-10 14:17:52 +00:00
|
|
|
assert_eq! (actual, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|