should be possible to POST to servers now

main
_ 2021-05-15 17:37:24 +00:00
parent af958ac43d
commit 0f7c95f79f
3 changed files with 40 additions and 78 deletions

View File

@ -28,9 +28,13 @@ impl TryFrom <hyper::Method> for Method {
type Error = Error; type Error = Error;
fn try_from (x: hyper::Method) -> Result <Self, Error> { fn try_from (x: hyper::Method) -> Result <Self, Error> {
use hyper::Method;
match x { match x {
hyper::Method::GET => Ok (Self::Get), Method::GET => Ok (Self::Get),
hyper::Method::HEAD => Ok (Self::Head), Method::HEAD => Ok (Self::Head),
Method::POST => Ok (Self::Post),
Method::PUT => Ok (Self::Put),
_ => Err (Error::UnsupportedMethod), _ => Err (Error::UnsupportedMethod),
} }
} }

View File

@ -129,10 +129,8 @@ async fn handle_http_request (
let user = get_user_name (&req); let user = get_user_name (&req);
let req = match http_serde::RequestParts::from_hyper (req.method, uri.clone (), req.headers) { let req = http_serde::RequestParts::from_hyper (req.method, uri.clone (), req.headers)
Ok (x) => x, .map_err (|_| BadRequest)?;
Err (_) => return Err (BadRequest),
};
let (tx, rx) = oneshot::channel (); let (tx, rx) = oneshot::channel ();
@ -597,12 +595,8 @@ async fn handle_all (
let response = match e { let response = match e {
Error::BadUriFormat => error_reply (StatusCode::BAD_REQUEST, "Bad URI format")?, 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::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")?, Error::NotFound => error_reply (StatusCode::OK, "URL routing failed")?,
}; };
return Ok (response); return Ok (response);
}, },
@ -628,13 +622,6 @@ async fn handle_all (
DebugEndlessSource (throttle) => handle_endless_source (1, throttle).await?, DebugEndlessSource (throttle) => handle_endless_source (1, throttle).await?,
DebugGenKey => handle_gen_scraper_key (state).await?, DebugGenKey => handle_gen_scraper_key (state).await?,
DebugMysteriousError => return Err (RequestError::Mysterious), DebugMysteriousError => return Err (RequestError::Mysterious),
ErrorBadUriFormat => error_reply (StatusCode::BAD_REQUEST, "Bad URI format")?,
ErrorCantPost => {
error! ("Can't POST {}", path);
error_reply (StatusCode::BAD_REQUEST, "Can't POST this")?
},
ErrorMethodNotAllowed => error_reply (StatusCode::METHOD_NOT_ALLOWED, "Method not allowed. Are you POST-ing to a GET-only url, or vice versa?")?,
ErrorRoutingFailed => error_reply (StatusCode::OK, "URL routing failed")?,
RegisterServer => { RegisterServer => {
match handle_register_server (req, state).await { match handle_register_server (req, state).await {
Ok (_) => Response::builder () Ok (_) => Response::builder ()

View File

@ -15,10 +15,6 @@ pub enum Route <'a> {
DebugEndlessSource (Option <usize>), DebugEndlessSource (Option <usize>),
DebugGenKey, DebugGenKey,
DebugMysteriousError, DebugMysteriousError,
ErrorBadUriFormat,
ErrorCantPost,
ErrorMethodNotAllowed,
ErrorRoutingFailed,
RegisterServer, RegisterServer,
Root, Root,
Scraper { Scraper {
@ -35,44 +31,33 @@ pub enum Route <'a> {
#[derive (Debug, PartialEq)] #[derive (Debug, PartialEq)]
pub enum Error { pub enum Error {
BadUriFormat, BadUriFormat,
CantPost,
MethodNotAllowed, MethodNotAllowed,
RoutingFailed, NotFound,
} }
type Result <'a> = std::result::Result <Route <'a>, Error>; pub fn route_url <'a> (method: &Method, path: &'a str) -> Result <Route <'a>, Error> {
pub fn route_url <'a> (method: &Method, path: &'a str) -> Result <'a> {
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 { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::ServerHttpListen { Ok (Route::ServerHttpListen {
listen_code 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 { assert_method (method, Method::POST)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::ServerHttpResponse { Ok (Route::ServerHttpResponse {
request_code request_code
}) })
} }
else if path == "/frontend/register" { else if path == "/frontend/register" {
if method != Method::POST { assert_method (method, Method::POST)?;
return Err (Error::MethodNotAllowed);
}
Ok (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 { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::ClientServerList) Ok (Route::ClientServerList)
} }
else if let Some (idx) = rest.find ('/') { else if let Some (idx) = rest.find ('/') {
@ -84,87 +69,73 @@ pub fn route_url <'a> (method: &Method, path: &'a str) -> Result <'a> {
}) })
} }
else { else {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed); Err (Error::BadUriFormat)
}
Ok (Route::ErrorBadUriFormat)
} }
} }
else if path == "/frontend/unregistered_servers" { else if path == "/frontend/unregistered_servers" {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::ClientUnregisteredServers) Ok (Route::ClientUnregisteredServers)
} }
else if path == "/frontend/audit_log" { else if path == "/frontend/audit_log" {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (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 { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::Debug) Ok (Route::Debug)
} }
else if rest == "endless_source" { else if rest == "endless_source" {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::DebugEndlessSource (None)) Ok (Route::DebugEndlessSource (None))
} }
else if rest == "endless_source_throttled" { else if rest == "endless_source_throttled" {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (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 { assert_method (method, Method::POST)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::DebugEndlessSink) Ok (Route::DebugEndlessSink)
} }
else if rest == "gen_key" { else if rest == "gen_key" {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::DebugGenKey) Ok (Route::DebugGenKey)
} }
else { else {
Ok (Route::ErrorRoutingFailed) Err (Error::NotFound)
} }
} }
else if path == "/" { else if path == "/" {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::Root) Ok (Route::Root)
} }
else if path == "/frontend/relay_up_check" { else if path == "/frontend/relay_up_check" {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::ClientRelayIsUp) Ok (Route::ClientRelayIsUp)
} }
else if path == "/frontend/test_mysterious_error" { else if path == "/frontend/test_mysterious_error" {
if method != Method::GET { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (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 { assert_method (method, Method::GET)?;
return Err (Error::MethodNotAllowed);
}
Ok (Route::Scraper { Ok (Route::Scraper {
rest rest
}) })
} }
else { else {
Err (Error::RoutingFailed) Err (Error::NotFound)
}
}
fn assert_method <M: PartialEq <Method>> (method: M, expected: Method) -> Result <(), Error>
{
if method == expected {
Ok (())
}
else {
Err (Error::MethodNotAllowed)
} }
} }