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,
|
|
|
|
ErrorBadUriFormat,
|
|
|
|
ErrorCantPost,
|
|
|
|
ErrorMethodNotAllowed,
|
|
|
|
ErrorRoutingFailed,
|
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-04-10 14:38:53 +00:00
|
|
|
pub fn route_url <'a> (method: &Method, path: &'a str) -> Route <'a> {
|
2021-04-10 14:17:52 +00:00
|
|
|
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
|
|
|
|
}
|
2021-05-02 21:18:07 +00:00
|
|
|
else if path == "/frontend/register" {
|
|
|
|
Route::RegisterServer
|
|
|
|
}
|
2021-04-10 14:17:52 +00:00
|
|
|
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 () {
|
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! [
|
|
|
|
("/", Route::ErrorCantPost),
|
|
|
|
].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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|