diff options
| author | Fuwn <[email protected]> | 2022-07-09 01:41:19 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-07-09 01:41:19 +0000 |
| commit | d806185bdd41cd89159f5ab729710d84bbae2265 (patch) | |
| tree | 9d984c80f8f0de526523a320d562f8c3c94748e6 | |
| parent | chore(cargo): republish with --all-features (diff) | |
| download | windmark-0.1.19.tar.xz windmark-0.1.19.zip | |
feat(response): success with mime responsev0.1.19
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | examples/windmark.rs | 6 | ||||
| -rw-r--r-- | src/response.rs | 9 | ||||
| -rw-r--r-- | src/router.rs | 10 |
5 files changed, 26 insertions, 7 deletions
@@ -2,7 +2,7 @@ [package] name = "windmark" -version = "0.1.18" +version = "0.1.19" authors = ["Fuwn <[email protected]>"] edition = "2021" description = "An elegant and highly performant async Gemini server framework" @@ -15,15 +15,15 @@ the modern age! # Cargo.toml [dependencies] -windmark = "0.1.18" +windmark = "0.1.19" tokio = { version = "0.2.4", features = ["full"] } # If you would like to use the built-in logger (recommended) -# windmark = { version = "0.1.18", features = ["logger"] } +# windmark = { version = "0.1.19", features = ["logger"] } # If you would like to use the built-in MIME dedection when `Success`-ing a file # (recommended) -# windmark = { version = "0.1.18", features = ["auto-deduce-mime"] } +# windmark = { version = "0.1.19", features = ["auto-deduce-mime"] } ``` ### Implement a Windmark server diff --git a/examples/windmark.rs b/examples/windmark.rs index 80b0cba..736eb54 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -103,6 +103,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { }), ); router.mount( + "/specific-mime", + Box::new(|_| { + Response::SuccessWithMime("hi".to_string(), "text/plain".to_string()) + }), + ); + router.mount( "/ip", Box::new(|context| { Response::Success( diff --git a/src/response.rs b/src/response.rs index ed320b5..806eebb 100644 --- a/src/response.rs +++ b/src/response.rs @@ -23,6 +23,9 @@ pub enum Response<'a> { Input(String), SensitiveInput(String), Success(String), + /// A successful response where the MIME type of the response is manually + /// specific by the user + SuccessWithMime(String, String), #[cfg(feature = "auto-deduce-mime")] /// A successful response where the MIME type of the response is /// automatically deduced from the provided bytes @@ -66,6 +69,12 @@ pub(crate) fn to_value_set_status( value } + Response::SuccessWithMime(value, value_mime) => { + *status = 23; + *mime = value_mime; + + value + } Response::SuccessFile(value, value_mime) => { *status = 21; // Internal status code, not real. *mime = value_mime; diff --git a/src/router.rs b/src/router.rs index 10a8f92..d4663f7 100644 --- a/src/router.rs +++ b/src/router.rs @@ -384,7 +384,10 @@ impl Router { .write_all( format!( "{}{}\r\n{}", - if response_status == 21 || response_status == 22 { + if response_status == 21 + || response_status == 22 + || response_status == 23 + { 20 } else { response_status @@ -395,14 +398,15 @@ impl Router { " text/gemini; charset={}; lang={}", self.charset, self.language ), + 21 => response_mime_type, #[cfg(feature = "auto-deduce-mime")] 22 => format!(" {}", tree_magic::from_u8(&*content.as_bytes())), - 21 => response_mime_type, + 23 => response_mime_type, _ => format!(" {}", content), }, match response_status { 20 => format!("{}{}\n{}", header, content, footer), - 21 | 22 => content.to_string(), + 21 | 22 | 23 => content.to_string(), _ => "".to_string(), } ) |