🚧 compiles but probably doesn't work

main
_ 2021-05-15 17:25:13 +00:00
parent 6c34a1ef37
commit af958ac43d
2 changed files with 69 additions and 42 deletions

View File

@ -590,6 +590,23 @@ async fn handle_all (
trace! ("Request path: {}", path); trace! ("Request path: {}", path);
let route = routing::route_url (req.method (), &path); let route = routing::route_url (req.method (), &path);
let route = match route {
Ok (x) => x,
Err (e) => {
use routing::Error;
let response = match e {
Error::BadUriFormat => error_reply (StatusCode::BAD_REQUEST, "Bad URI format")?,
Error::CantPost => {
error! ("Can't POST {}", path);
error_reply (StatusCode::BAD_REQUEST, "Can't POST this")?
},
Error::MethodNotAllowed => error_reply (StatusCode::METHOD_NOT_ALLOWED, "Method not allowed. Are you POST-ing to a GET-only url, or vice versa?")?,
Error::RoutingFailed => error_reply (StatusCode::OK, "URL routing failed")?,
};
return Ok (response);
},
};
let response = match route { let response = match route {
ClientAuditLog => handle_audit_log (state, handlebars).await?, ClientAuditLog => handle_audit_log (state, handlebars).await?,
ClientRelayIsUp => error_reply (StatusCode::OK, "Relay is up")?, ClientRelayIsUp => error_reply (StatusCode::OK, "Relay is up")?,

View File

@ -32,129 +32,139 @@ pub enum Route <'a> {
}, },
} }
pub fn route_url <'a> (method: &Method, path: &'a str) -> Route <'a> { #[derive (Debug, PartialEq)]
if let Some (listen_code) = path.strip_prefix ("/7ZSFUKGV/http_listen/") { pub enum Error {
if method != Method::GET { BadUriFormat,
return Route::ErrorMethodNotAllowed; CantPost,
MethodNotAllowed,
RoutingFailed,
} }
Route::ServerHttpListen { type Result <'a> = std::result::Result <Route <'a>, Error>;
listen_code
pub fn route_url <'a> (method: &Method, path: &'a str) -> Result <'a> {
if let Some (listen_code) = path.strip_prefix ("/7ZSFUKGV/http_listen/") {
if method != Method::GET {
return Err (Error::MethodNotAllowed);
} }
Ok (Route::ServerHttpListen {
listen_code
})
} }
else if let Some (request_code) = path.strip_prefix ("/7ZSFUKGV/http_response/") { else if let Some (request_code) = path.strip_prefix ("/7ZSFUKGV/http_response/") {
if method != Method::POST { if method != Method::POST {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::ServerHttpResponse { Ok (Route::ServerHttpResponse {
request_code request_code
} })
} }
else if path == "/frontend/register" { else if path == "/frontend/register" {
if method != Method::POST { if method != Method::POST {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::RegisterServer Ok (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 { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::ClientServerList Ok (Route::ClientServerList)
} }
else if let Some (idx) = rest.find ('/') { else if let Some (idx) = rest.find ('/') {
let listen_code = &rest [0..idx]; let listen_code = &rest [0..idx];
Route::ClientServerGet { Ok (Route::ClientServerGet {
listen_code, listen_code,
path: &rest [idx..], path: &rest [idx..],
} })
} }
else { else {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::ErrorBadUriFormat Ok (Route::ErrorBadUriFormat)
} }
} }
else if path == "/frontend/unregistered_servers" { else if path == "/frontend/unregistered_servers" {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::ClientUnregisteredServers Ok (Route::ClientUnregisteredServers)
} }
else if path == "/frontend/audit_log" { else if path == "/frontend/audit_log" {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::ClientAuditLog Ok (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 { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::Debug Ok (Route::Debug)
} }
else if rest == "endless_source" { else if rest == "endless_source" {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::DebugEndlessSource (None) Ok (Route::DebugEndlessSource (None))
} }
else if rest == "endless_source_throttled" { else if rest == "endless_source_throttled" {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::DebugEndlessSource (Some (1024 / 64)) Ok (Route::DebugEndlessSource (Some (1024 / 64)))
} }
else if rest == "endless_sink" { else if rest == "endless_sink" {
if method != Method::POST { if method != Method::POST {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::DebugEndlessSink Ok (Route::DebugEndlessSink)
} }
else if rest == "gen_key" { else if rest == "gen_key" {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::DebugGenKey Ok (Route::DebugGenKey)
} }
else { else {
Route::ErrorRoutingFailed Ok (Route::ErrorRoutingFailed)
} }
} }
else if path == "/" { else if path == "/" {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::Root Ok (Route::Root)
} }
else if path == "/frontend/relay_up_check" { else if path == "/frontend/relay_up_check" {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::ClientRelayIsUp Ok (Route::ClientRelayIsUp)
} }
else if path == "/frontend/test_mysterious_error" { else if path == "/frontend/test_mysterious_error" {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::DebugMysteriousError Ok (Route::DebugMysteriousError)
} }
else if let Some (rest) = path.strip_prefix ("/scraper/") { else if let Some (rest) = path.strip_prefix ("/scraper/") {
if method != Method::GET { if method != Method::GET {
return Route::ErrorMethodNotAllowed; return Err (Error::MethodNotAllowed);
} }
Route::Scraper { Ok (Route::Scraper {
rest rest
} })
} }
else { else {
Route::ErrorRoutingFailed Err (Error::RoutingFailed)
} }
} }
@ -165,14 +175,14 @@ mod tests {
#[test] #[test]
fn routing () { fn routing () {
for (input, expected) in vec! [ for (input, expected) in vec! [
("/", Route::Root), ("/", Ok (Route::Root)),
].into_iter () { ].into_iter () {
let actual = route_url (&Method::GET, input); let actual = route_url (&Method::GET, input);
assert_eq! (actual, expected); assert_eq! (actual, expected);
} }
for (input, expected) in vec! [ for (input, expected) in vec! [
("/", Route::ErrorMethodNotAllowed), ("/", Err (Error::MethodNotAllowed)),
].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);