139 lines
2.7 KiB
Rust
139 lines
2.7 KiB
Rust
|
use hyper::Method;
|
||
|
|
||
|
#[derive (Debug, PartialEq)]
|
||
|
pub enum Route <'a> {
|
||
|
ClientAuditLog,
|
||
|
ClientRelayIsUp,
|
||
|
ClientServerGet {
|
||
|
listen_code: &'a str,
|
||
|
path: &'a str,
|
||
|
},
|
||
|
ClientServerList,
|
||
|
ClientUnregisteredServers,
|
||
|
Debug,
|
||
|
DebugEndlessSink,
|
||
|
DebugEndlessSource (Option <u64>),
|
||
|
DebugGenKey,
|
||
|
DebugMysteriousError,
|
||
|
ErrorBadUriFormat,
|
||
|
ErrorCantPost,
|
||
|
ErrorMethodNotAllowed,
|
||
|
ErrorRoutingFailed,
|
||
|
Root,
|
||
|
Scraper {
|
||
|
rest: &'a str,
|
||
|
},
|
||
|
ServerHttpListen {
|
||
|
listen_code: &'a str
|
||
|
},
|
||
|
ServerHttpResponse {
|
||
|
request_code: &'a str,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
pub fn route_url (method: Method, path: &str) -> Route {
|
||
|
if method == Method::POST {
|
||
|
return if let Some (request_code) = path.strip_prefix ("/7ZSFUKGV/http_response/") {
|
||
|
Route::ServerHttpResponse {
|
||
|
request_code
|
||
|
}
|
||
|
}
|
||
|
else if path == "/frontend/debug/endless_sink" {
|
||
|
Route::DebugEndlessSink
|
||
|
}
|
||
|
else {
|
||
|
Route::ErrorCantPost
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if let Some (listen_code) = path.strip_prefix ("/7ZSFUKGV/http_listen/") {
|
||
|
Route::ServerHttpListen {
|
||
|
listen_code
|
||
|
}
|
||
|
}
|
||
|
else if let Some (rest) = path.strip_prefix ("/frontend/servers/") {
|
||
|
// DRY T4H76LB3
|
||
|
|
||
|
if rest.is_empty () {
|
||
|
Route::ClientServerList
|
||
|
}
|
||
|
else if let Some (idx) = rest.find ('/') {
|
||
|
let listen_code = &rest [0..idx];
|
||
|
let path = &rest [idx..];
|
||
|
|
||
|
Route::ClientServerGet {
|
||
|
listen_code,
|
||
|
path,
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
Route::ErrorBadUriFormat
|
||
|
}
|
||
|
}
|
||
|
else if path == "/frontend/unregistered_servers" {
|
||
|
Route::ClientUnregisteredServers
|
||
|
}
|
||
|
else if path == "/frontend/audit_log" {
|
||
|
Route::ClientAuditLog
|
||
|
}
|
||
|
else if let Some (rest) = path.strip_prefix ("/frontend/debug/") {
|
||
|
if rest.is_empty () {
|
||
|
Route::Debug
|
||
|
}
|
||
|
else if rest == "endless_source" {
|
||
|
Route::DebugEndlessSource (None)
|
||
|
}
|
||
|
else if rest == "endless_source_throttled" {
|
||
|
Route::DebugEndlessSource (Some (1024 / 64))
|
||
|
}
|
||
|
else if rest == "endless_sink" {
|
||
|
Route::ErrorMethodNotAllowed
|
||
|
}
|
||
|
else if rest == "gen_key" {
|
||
|
Route::DebugGenKey
|
||
|
}
|
||
|
else {
|
||
|
Route::ErrorRoutingFailed
|
||
|
}
|
||
|
}
|
||
|
else if path == "/" {
|
||
|
Route::Root
|
||
|
}
|
||
|
else if path == "/frontend/relay_up_check" {
|
||
|
Route::ClientRelayIsUp
|
||
|
}
|
||
|
else if path == "/frontend/test_mysterious_error" {
|
||
|
Route::DebugMysteriousError
|
||
|
}
|
||
|
else if let Some (rest) = path.strip_prefix ("/scraper/") {
|
||
|
Route::Scraper {
|
||
|
rest
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
Route::ErrorRoutingFailed
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg (test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn routing () {
|
||
|
for (input, expected) in vec! [
|
||
|
("/", Route::Root),
|
||
|
].into_iter () {
|
||
|
let actual = route_url (Method::GET, input);
|
||
|
assert_eq! (actual, expected);
|
||
|
}
|
||
|
|
||
|
for (input, expected) in vec! [
|
||
|
("/", Route::ErrorCantPost),
|
||
|
].into_iter () {
|
||
|
let actual = route_url (Method::POST, input);
|
||
|
assert_eq! (actual, expected);
|
||
|
}
|
||
|
}
|
||
|
}
|