From c01d0ae46a4a3a1d85cab9a49442283718b1fb88 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 27 Mar 2022 04:16:22 +0000 Subject: feat(response): implement many more responses --- examples/windmark.rs | 3 ++- src/lib.rs | 8 +++---- src/response.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/examples/windmark.rs b/examples/windmark.rs index a733be2..1cccd70 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -24,7 +24,7 @@ extern crate log; use windmark::Response; #[tokio::main] -async fn main() -> std::io::Result<()> { +async fn main() -> Result<(), Box> { windmark::Router::new() .set_private_key_file("windmark_private.pem") .set_certificate_chain_file("windmark_pair.pem") @@ -107,6 +107,7 @@ async fn main() -> std::io::Result<()> { Response::SensitiveInput("What is your password?".into()) } }) + .mount("/error", |_| Response::CertificateNotValid("no".into())) .run() .await } diff --git a/src/lib.rs b/src/lib.rs index 0814667..cb28717 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -328,12 +328,12 @@ impl Router { "{}{}\r\n{}", response_status, match response_status { - 10 | 11 | 40 | 50 | 51 => &*content, - _ => " text/gemini; charset=utf-8", + 20 => " text/gemini; charset=utf-8", + _ => &*content, }, match response_status { - 10 | 11 | 40 | 50 | 51 => "".to_string(), - _ => format!("{}{}{}", header, content, footer), + 20 => format!("{}{}{}", header, content, footer), + _ => "".to_string(), } ) .as_bytes(), diff --git a/src/response.rs b/src/response.rs index 8720eb6..239f8da 100644 --- a/src/response.rs +++ b/src/response.rs @@ -20,9 +20,19 @@ pub enum Response { Input(String), SensitiveInput(String), Success(String), - NotFound(String), TemporaryFailure(String), + ServerUnavailable(String), + CGIError(String), + ProxyError(String), + SlowDown(String), PermanentFailure(String), + NotFound(String), + Gone(String), + ProxyRefused(String), + BadRequest(String), + ClientCertificateRequired(String), + CertificateNotAuthorised(String), + CertificateNotValid(String), } pub(crate) fn to_value_set_status( @@ -50,14 +60,64 @@ pub(crate) fn to_value_set_status( value } - Response::NotFound(value) => { - *status = 51; + Response::ServerUnavailable(value) => { + *status = 41; + + value + } + Response::CGIError(value) => { + *status = 42; + + value + } + Response::ProxyError(value) => { + *status = 43; + + value + } + Response::SlowDown(value) => { + *status = 44; value } Response::PermanentFailure(value) => { *status = 50; + value + } + Response::NotFound(value) => { + *status = 51; + + value + } + Response::Gone(value) => { + *status = 52; + + value + } + Response::ProxyRefused(value) => { + *status = 53; + + value + } + Response::BadRequest(value) => { + *status = 59; + + value + } + Response::ClientCertificateRequired(value) => { + *status = 60; + + value + } + Response::CertificateNotAuthorised(value) => { + *status = 61; + + value + } + Response::CertificateNotValid(value) => { + *status = 62; + value } } -- cgit v1.2.3