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