♻️ refactor: prepare to allow POSTing to servers

main
_ 2021-05-15 17:14:12 +00:00
parent b018f16794
commit 6c34a1ef37
1 changed files with 59 additions and 19 deletions

View File

@ -33,32 +33,36 @@ pub enum Route <'a> {
} }
pub fn route_url <'a> (method: &Method, path: &'a str) -> Route <'a> { pub fn route_url <'a> (method: &Method, path: &'a str) -> Route <'a> {
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 if path == "/frontend/register" {
Route::RegisterServer
}
else {
Route::ErrorCantPost
}
}
if let Some (listen_code) = path.strip_prefix ("/7ZSFUKGV/http_listen/") { if let Some (listen_code) = path.strip_prefix ("/7ZSFUKGV/http_listen/") {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::ServerHttpListen { Route::ServerHttpListen {
listen_code listen_code
} }
} }
else if let Some (request_code) = path.strip_prefix ("/7ZSFUKGV/http_response/") {
if method != Method::POST {
return Route::ErrorMethodNotAllowed;
}
Route::ServerHttpResponse {
request_code
}
}
else if path == "/frontend/register" {
if method != Method::POST {
return Route::ErrorMethodNotAllowed;
}
Route::RegisterServer
}
else if let Some (rest) = path.strip_prefix ("/frontend/servers/") { else if let Some (rest) = path.strip_prefix ("/frontend/servers/") {
// DRY T4H76LB3 // DRY T4H76LB3
if rest.is_empty () { if rest.is_empty () {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::ClientServerList Route::ClientServerList
} }
else if let Some (idx) = rest.find ('/') { else if let Some (idx) = rest.find ('/') {
@ -70,29 +74,53 @@ pub fn route_url <'a> (method: &Method, path: &'a str) -> Route <'a> {
} }
} }
else { else {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::ErrorBadUriFormat Route::ErrorBadUriFormat
} }
} }
else if path == "/frontend/unregistered_servers" { else if path == "/frontend/unregistered_servers" {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::ClientUnregisteredServers Route::ClientUnregisteredServers
} }
else if path == "/frontend/audit_log" { else if path == "/frontend/audit_log" {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::ClientAuditLog Route::ClientAuditLog
} }
else if let Some (rest) = path.strip_prefix ("/frontend/debug/") { else if let Some (rest) = path.strip_prefix ("/frontend/debug/") {
if rest.is_empty () { if rest.is_empty () {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::Debug Route::Debug
} }
else if rest == "endless_source" { else if rest == "endless_source" {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::DebugEndlessSource (None) Route::DebugEndlessSource (None)
} }
else if rest == "endless_source_throttled" { else if rest == "endless_source_throttled" {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::DebugEndlessSource (Some (1024 / 64)) Route::DebugEndlessSource (Some (1024 / 64))
} }
else if rest == "endless_sink" { else if rest == "endless_sink" {
Route::ErrorMethodNotAllowed if method != Method::POST {
return Route::ErrorMethodNotAllowed;
}
Route::DebugEndlessSink
} }
else if rest == "gen_key" { else if rest == "gen_key" {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::DebugGenKey Route::DebugGenKey
} }
else { else {
@ -100,15 +128,27 @@ pub fn route_url <'a> (method: &Method, path: &'a str) -> Route <'a> {
} }
} }
else if path == "/" { else if path == "/" {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::Root Route::Root
} }
else if path == "/frontend/relay_up_check" { else if path == "/frontend/relay_up_check" {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::ClientRelayIsUp Route::ClientRelayIsUp
} }
else if path == "/frontend/test_mysterious_error" { else if path == "/frontend/test_mysterious_error" {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::DebugMysteriousError Route::DebugMysteriousError
} }
else if let Some (rest) = path.strip_prefix ("/scraper/") { else if let Some (rest) = path.strip_prefix ("/scraper/") {
if method != Method::GET {
return Route::ErrorMethodNotAllowed;
}
Route::Scraper { Route::Scraper {
rest rest
} }
@ -132,7 +172,7 @@ mod tests {
} }
for (input, expected) in vec! [ for (input, expected) in vec! [
("/", Route::ErrorCantPost), ("/", Route::ErrorMethodNotAllowed),
].into_iter () { ].into_iter () {
let actual = route_url (&Method::POST, input); let actual = route_url (&Method::POST, input);
assert_eq! (actual, expected); assert_eq! (actual, expected);