aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-07-09 01:03:10 +0000
committerFuwn <[email protected]>2022-07-09 01:03:10 +0000
commit32d10fa03dff132d825be6f2fe11a8c8e94132b6 (patch)
tree9c41fb843ac682e42173f00569a3e7c358e18e57
parentdocs(cargo): bump version 0.1.15 -> 0.1.16 (diff)
downloadwindmark-32d10fa03dff132d825be6f2fe11a8c8e94132b6.tar.xz
windmark-32d10fa03dff132d825be6f2fe11a8c8e94132b6.zip
feat(response): always allow explicit mime specification
-rw-r--r--Cargo.toml2
-rw-r--r--README.md6
-rw-r--r--examples/windmark.rs11
-rw-r--r--src/response.rs110
-rw-r--r--src/router.rs10
5 files changed, 18 insertions, 121 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1fdd70c..aa105a1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,7 +2,7 @@
[package]
name = "windmark"
-version = "0.1.16"
+version = "0.1.17"
authors = ["Fuwn <[email protected]>"]
edition = "2021"
description = "An elegant and highly performant async Gemini server framework"
diff --git a/README.md b/README.md
index cb9b6e0..4805d8d 100644
--- a/README.md
+++ b/README.md
@@ -15,15 +15,15 @@ the modern age!
# Cargo.toml
[dependencies]
-windmark = "0.1.16"
+windmark = "0.1.17"
tokio = { version = "0.2.4", features = ["full"] }
# If you would like to use the built-in logger (recommended)
-# windmark = { version = "0.1.16", features = ["logger"] }
+# windmark = { version = "0.1.17", features = ["logger"] }
# If you would like to use the built-in MIME dedection when `Success`-ing a file
# (recommended)
-# windmark = { version = "0.1.16", features = ["auto-deduce-mime"] }
+# windmark = { version = "0.1.17", features = ["auto-deduce-mime"] }
```
### Implement a Windmark server
diff --git a/examples/windmark.rs b/examples/windmark.rs
index eaf7b43..80b0cba 100644
--- a/examples/windmark.rs
+++ b/examples/windmark.rs
@@ -58,6 +58,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
router.set_private_key_file("windmark_private.pem");
router.set_certificate_file("windmark_public.pem");
+ #[cfg(feature = "logger")]
router.enable_default_logger(true);
router.set_error_handler(Box::new(move |_| {
error_count += 1;
@@ -185,13 +186,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
"/redirect",
Box::new(|_| Response::PermanentRedirect("gemini://localhost/test".into())),
);
+ #[cfg(feature = "auto-deduce-mime")]
+ router.mount("/auto-file", {
+ Box::new(|_| Response::SuccessFileAuto(include_bytes!("../LICENSE")))
+ });
router.mount("/file", {
- #[cfg(feature = "auto-deduce-mime")]
- {
- Box::new(|_| Response::SuccessFile(include_bytes!("../LICENSE")))
- }
-
- #[cfg(not(feature = "auto-deduce-mime"))]
Box::new(|_| {
Response::SuccessFile(
include_bytes!("../LICENSE"),
diff --git a/src/response.rs b/src/response.rs
index 0459c06..ed320b5 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -24,8 +24,9 @@ pub enum Response<'a> {
SensitiveInput(String),
Success(String),
#[cfg(feature = "auto-deduce-mime")]
- SuccessFile(&'a [u8]),
- #[cfg(not(feature = "auto-deduce-mime"))]
+ /// A successful response where the MIME type of the response is
+ /// automatically deduced from the provided bytes
+ SuccessFileAuto(&'a [u8]),
SuccessFile(&'a [u8], String),
TemporaryRedirect(String),
PermanentRedirect(String),
@@ -44,7 +45,6 @@ pub enum Response<'a> {
CertificateNotValid(String),
}
-#[cfg(not(feature = "auto-deduce-mime"))]
pub(crate) fn to_value_set_status(
response: Response<'_>,
status: &mut i32,
@@ -72,107 +72,9 @@ pub(crate) fn to_value_set_status(
String::from_utf8(value.to_vec()).unwrap()
}
- Response::TemporaryRedirect(value) => {
- *status = 30;
-
- value
- }
- Response::PermanentRedirect(value) => {
- *status = 31;
-
- value
- }
- Response::TemporaryFailure(value) => {
- *status = 40;
-
- value
- }
- 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
- }
- }
-}
-
-#[cfg(feature = "auto-deduce-mime")]
-pub(crate) fn to_value_set_status(
- response: Response<'_>,
- status: &mut i32,
-) -> String {
- match response {
- Response::Input(value) => {
- *status = 10;
-
- value
- }
- Response::SensitiveInput(value) => {
- *status = 11;
-
- value
- }
- Response::Success(value) => {
- *status = 20;
-
- value
- }
- Response::SuccessFile(value) => {
- *status = 21; // Internal status code, not real.
+ #[cfg(feature = "auto-deduce-mime")]
+ Response::SuccessFileAuto(value) => {
+ *status = 22; // Internal status code, not real.
String::from_utf8(value.to_vec()).unwrap()
}
diff --git a/src/router.rs b/src/router.rs
index 3fb1ca5..10a8f92 100644
--- a/src/router.rs
+++ b/src/router.rs
@@ -261,7 +261,6 @@ impl Router {
let mut buffer = [0u8; 1024];
let mut url = Url::parse("gemini://fuwn.me/")?;
let mut response_status = 0;
- #[cfg(not(feature = "auto-deduce-mime"))]
let mut response_mime_type = "".to_string();
let mut footer = String::new();
let mut header = String::new();
@@ -364,7 +363,6 @@ impl Router {
&stream.ssl().peer_certificate(),
),)),
&mut response_status,
- #[cfg(not(feature = "auto-deduce-mime"))]
&mut response_mime_type,
)
} else {
@@ -378,7 +376,6 @@ impl Router {
&stream.ssl().peer_certificate(),
),)),
&mut response_status,
- #[cfg(not(feature = "auto-deduce-mime"))]
&mut response_mime_type,
)
};
@@ -387,7 +384,7 @@ impl Router {
.write_all(
format!(
"{}{}\r\n{}",
- if response_status == 21 {
+ if response_status == 21 || response_status == 22 {
20
} else {
response_status
@@ -399,14 +396,13 @@ impl Router {
self.charset, self.language
),
#[cfg(feature = "auto-deduce-mime")]
- 21 => format!(" {}", tree_magic::from_u8(&*content.as_bytes())),
- #[cfg(not(feature = "auto-deduce-mime"))]
+ 22 => format!(" {}", tree_magic::from_u8(&*content.as_bytes())),
21 => response_mime_type,
_ => format!(" {}", content),
},
match response_status {
20 => format!("{}{}\n{}", header, content, footer),
- 21 => content.to_string(),
+ 21 | 22 => content.to_string(),
_ => "".to_string(),
}
)