♻️ Removing unwraps

main
_ 2020-11-29 17:08:33 +00:00
parent bf96d400b2
commit 5c0d7ea998
1 changed files with 14 additions and 15 deletions

View File

@ -152,18 +152,18 @@ impl RelayState {
} }
fn ok_reply <B: Into <Body>> (b: B) fn ok_reply <B: Into <Body>> (b: B)
-> Response <Body> -> Result <Response <Body>, http::Error>
{ {
Response::builder ().status (StatusCode::OK).body (b.into ()).unwrap () Response::builder ().status (StatusCode::OK).body (b.into ())
} }
fn error_reply (status: StatusCode, b: &str) fn error_reply (status: StatusCode, b: &str)
-> Response <Body> -> Result <Response <Body>, http::Error>
{ {
Response::builder () Response::builder ()
.status (status) .status (status)
.header ("content-type", "text/plain") .header ("content-type", "text/plain")
.body (format! ("{}\n", b).into ()).unwrap () .body (format! ("{}\n", b).into ())
} }
// Servers will come here and either handle queued requests from parked clients, // Servers will come here and either handle queued requests from parked clients,
@ -174,7 +174,7 @@ async fn handle_http_listen (
watcher_code: String, watcher_code: String,
api_key: &[u8], api_key: &[u8],
) )
-> Response <Body> -> Result <Response <Body>, http::Error>
{ {
let trip_error = error_reply (StatusCode::UNAUTHORIZED, "Bad X-ApiKey"); let trip_error = error_reply (StatusCode::UNAUTHORIZED, "Bad X-ApiKey");
@ -253,7 +253,7 @@ async fn handle_http_response (
state: Arc <RelayState>, state: Arc <RelayState>,
req_id: String, req_id: String,
) )
-> Response <Body> -> Result <Response <Body>, http::Error>
{ {
let (parts, mut body) = req.into_parts (); let (parts, mut body) = req.into_parts ();
let resp_parts: http_serde::ResponseParts = rmp_serde::from_read_ref (&base64::decode (parts.headers.get (ptth_core::PTTH_MAGIC_HEADER).unwrap ()).unwrap ()).unwrap (); let resp_parts: http_serde::ResponseParts = rmp_serde::from_read_ref (&base64::decode (parts.headers.get (ptth_core::PTTH_MAGIC_HEADER).unwrap ()).unwrap ()).unwrap ();
@ -350,7 +350,7 @@ async fn handle_http_request (
state: Arc <RelayState>, state: Arc <RelayState>,
watcher_code: String watcher_code: String
) )
-> Response <Body> -> Result <Response <Body>, http::Error>
{ {
{ {
let config = state.config.read ().await; let config = state.config.read ().await;
@ -446,7 +446,6 @@ async fn handle_http_request (
debug! ("Unparked request {}", req_id); debug! ("Unparked request {}", req_id);
resp.body (body) resp.body (body)
.unwrap ()
}, },
Ok (Err (ShuttingDownError::ShuttingDown)) => { Ok (Err (ShuttingDownError::ShuttingDown)) => {
error_reply (StatusCode::GATEWAY_TIMEOUT, "Relay shutting down") error_reply (StatusCode::GATEWAY_TIMEOUT, "Relay shutting down")
@ -568,7 +567,7 @@ async fn handle_server_list_internal (state: &Arc <RelayState>)
async fn handle_server_list ( async fn handle_server_list (
state: Arc <RelayState> state: Arc <RelayState>
) -> Response <Body> ) -> Result <Response <Body>, http::Error>
{ {
let page = handle_server_list_internal (&state).await; let page = handle_server_list_internal (&state).await;
@ -578,7 +577,7 @@ async fn handle_server_list (
#[instrument (level = "trace", skip (req, state))] #[instrument (level = "trace", skip (req, state))]
async fn handle_all (req: Request <Body>, state: Arc <RelayState>) async fn handle_all (req: Request <Body>, state: Arc <RelayState>)
-> Result <Response <Body>, Infallible> -> Result <Response <Body>, http::Error>
{ {
let path = req.uri ().path (); let path = req.uri ().path ();
//println! ("{}", path); //println! ("{}", path);
@ -591,18 +590,18 @@ async fn handle_all (req: Request <Body>, state: Arc <RelayState>)
// This is stuff the server can use. Clients can't // This is stuff the server can use. Clients can't
// POST right now // POST right now
return Ok (if let Some (request_code) = prefix_match ("/7ZSFUKGV/http_response/", path) { return if let Some (request_code) = prefix_match ("/7ZSFUKGV/http_response/", path) {
let request_code = request_code.into (); let request_code = request_code.into ();
handle_http_response (req, state, request_code).await handle_http_response (req, state, request_code).await
} }
else { else {
error_reply (StatusCode::BAD_REQUEST, "Can't POST this") error_reply (StatusCode::BAD_REQUEST, "Can't POST this")
}); };
} }
Ok (if let Some (listen_code) = prefix_match ("/7ZSFUKGV/http_listen/", path) { if let Some (listen_code) = prefix_match ("/7ZSFUKGV/http_listen/", path) {
let api_key = match api_key { let api_key = match api_key {
None => return Ok (error_reply (StatusCode::UNAUTHORIZED, "Can't register as server without an API key")), None => return error_reply (StatusCode::UNAUTHORIZED, "Can't register as server without an API key"),
Some (x) => x, Some (x) => x,
}; };
handle_http_listen (state, listen_code.into (), api_key.as_bytes ()).await handle_http_listen (state, listen_code.into (), api_key.as_bytes ()).await
@ -631,7 +630,7 @@ async fn handle_all (req: Request <Body>, state: Arc <RelayState>)
} }
else { else {
error_reply (StatusCode::OK, "Hi") error_reply (StatusCode::OK, "Hi")
}) }
} }
pub fn load_templates (asset_root: &Path) pub fn load_templates (asset_root: &Path)