From 680c8aad6feccd8dca96bfc8db96f5436628154c Mon Sep 17 00:00:00 2001 From: Jose Quintana <1700322+joseluisq@users.noreply.github.com> Date: Thu, 15 Jun 2023 22:30:56 +0200 Subject: [PATCH] feat: `fallback-page` cargo feature (#222) --- Cargo.toml | 4 +++- docs/content/building-from-source.md | 2 ++ src/handler.rs | 10 ++++++++-- src/lib.rs | 4 ++++ src/server.rs | 27 +++++++++++++++++---------- src/settings/cli.rs | 7 ++++--- src/settings/file.rs | 2 ++ src/settings/mod.rs | 5 +++++ 8 files changed, 45 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2436814..3961754 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ doc = false [features] # All features enabled by default -default = ["compression", "http2", "directory-listing", "basic-auth"] +default = ["compression", "http2", "directory-listing", "basic-auth", "fallback-page"] # HTTP2 http2 = ["tokio-rustls", "rustls-pemfile"] # Compression @@ -52,6 +52,8 @@ compression-zstd = ["async-compression/zstd"] directory-listing = ["humansize", "chrono"] # Basic HTTP Authorization basic-auth = ["bcrypt"] +# Fallback Page +fallback-page = [] [dependencies] anyhow = "1.0" diff --git a/docs/content/building-from-source.md b/docs/content/building-from-source.md index bef6f1b..5931b8a 100644 --- a/docs/content/building-from-source.md +++ b/docs/content/building-from-source.md @@ -42,6 +42,8 @@ Feature | Description `directory-listing` | Activates the directory listing feature. [**Basic Authorization**](./features/basic-authentication.md) | `basic-auth` | Activates the Basic HTTP Authorization Schema feature. +[**Fallback Page**](./features/error-pages.md#fallback-page-for-use-with-client-routers) | +`fallback-page` | Activates the Fallback Page feature. ### Disable all default features diff --git a/src/handler.rs b/src/handler.rs index 730bdd6..eed03a7 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -16,10 +16,13 @@ use crate::compression; #[cfg(feature = "basic-auth")] use {crate::basic_auth, hyper::header::WWW_AUTHENTICATE}; +#[cfg(feature = "fallback-page")] +use crate::fallback_page; + use crate::{ control_headers, cors, custom_headers, error_page, exts::http::MethodExt, - fallback_page, redirects, rewrites, security_headers, + redirects, rewrites, security_headers, settings::Advanced, static_files::{self, HandleOpts}, Error, Result, @@ -57,6 +60,8 @@ pub struct RequestHandlerOpts { /// Page for 50x errors. pub page50x: Vec, /// Page fallback feature. + #[cfg(feature = "fallback-page")] + #[cfg_attr(docsrs, doc(cfg(feature = "fallback-page")))] pub page_fallback: Vec, /// Basic auth feature. #[cfg(feature = "basic-auth")] @@ -304,6 +309,7 @@ impl RequestHandler { } Err(status) => { // Check for a fallback response + #[cfg(feature = "fallback-page")] if method.is_get() && status == StatusCode::NOT_FOUND && !self.opts.page_fallback.is_empty() @@ -368,7 +374,7 @@ impl RequestHandler { return Ok(resp); } - // Otherwise return a response error + // Otherwise return an error response error_page::error_response( uri, method, diff --git a/src/lib.rs b/src/lib.rs index 24a6189..8da1c70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -88,6 +88,8 @@ //! `directory-listing` | Activates the directory listing feature. //! [**Basic Authorization**](./features/basic-authentication.md) | //! `basic-auth` | Activates the Basic HTTP Authorization Schema feature. +//! [**Fallback Page**](./features/error-pages.md#fallback-page-for-use-with-client-routers) | +//! `fallback-page` | Activates the Fallback Page feature. //! #![deny(missing_docs)] @@ -121,6 +123,8 @@ pub mod custom_headers; pub mod directory_listing; pub mod error_page; pub mod exts; +#[cfg(feature = "fallback-page")] +#[cfg_attr(docsrs, doc(cfg(feature = "fallback-page")))] pub mod fallback_page; pub mod handler; #[cfg(feature = "http2")] diff --git a/src/server.rs b/src/server.rs index 5f61e0c..d90bd5b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -152,19 +152,25 @@ impl Server { let page404 = helpers::read_bytes_default(&general.page404); let page50x = helpers::read_bytes_default(&general.page50x); - // Fallback page content + // Fallback page option + #[cfg(feature = "fallback-page")] let page_fallback_pbuf = general.page_fallback; + #[cfg(feature = "fallback-page")] let page_fallback = helpers::read_bytes_default(&page_fallback_pbuf); - let page_fallback_enabled = !page_fallback.is_empty(); - let mut page_fallback_opt = ""; - if page_fallback_enabled { - page_fallback_opt = page_fallback_pbuf.to_str().unwrap() + #[cfg(feature = "fallback-page")] + { + let page_fallback_enabled = !page_fallback.is_empty(); + let mut page_fallback_opt = ""; + if page_fallback_enabled { + page_fallback_opt = page_fallback_pbuf.to_str().unwrap() + } + + tracing::info!( + "fallback page: enabled={}, value=\"{}\"", + page_fallback_enabled, + page_fallback_opt + ); } - tracing::info!( - "fallback page: enabled={}, value=\"{}\"", - page_fallback_enabled, - page_fallback_opt - ); // Number of worker threads option let threads = self.worker_threads; @@ -268,6 +274,7 @@ impl Server { cache_control_headers, page404: page404.clone(), page50x: page50x.clone(), + #[cfg(feature = "fallback-page")] page_fallback, #[cfg(feature = "basic-auth")] basic_auth, diff --git a/src/settings/cli.rs b/src/settings/cli.rs index 119db9e..0c560a4 100644 --- a/src/settings/cli.rs +++ b/src/settings/cli.rs @@ -8,8 +8,6 @@ use clap::Parser; use std::path::PathBuf; -use crate::Result; - #[cfg(feature = "directory-listing")] use crate::directory_listing::DirListFmt; @@ -117,6 +115,8 @@ pub struct General { /// HTML file path for 404 errors. If the path is not specified or simply doesn't exist then the server will use a generic HTML error message. pub page404: PathBuf, + #[cfg(feature = "fallback-page")] + #[cfg_attr(docsrs, doc(cfg(feature = "fallback-page")))] #[arg(long, default_value = "", value_parser = value_parser_pathbuf, env = "SERVER_FALLBACK_PAGE")] /// HTML file path that is used for GET requests when the requested path doesn't exist. The fallback page is served with a 200 status code, useful when using client routers. If the path is not specified or simply doesn't exist then this feature will not be active. pub page_fallback: PathBuf, @@ -415,6 +415,7 @@ pub enum Commands { Uninstall {}, } -fn value_parser_pathbuf(s: &str) -> Result { +#[cfg(feature = "fallback-page")] +fn value_parser_pathbuf(s: &str) -> crate::Result { Ok(PathBuf::from(s)) } diff --git a/src/settings/file.rs b/src/settings/file.rs index e3e3d00..bf7fc18 100644 --- a/src/settings/file.rs +++ b/src/settings/file.rs @@ -192,6 +192,8 @@ pub struct General { pub grace_period: Option, /// Page fallback feature. + #[cfg(feature = "fallback-page")] + #[cfg_attr(docsrs, doc(cfg(feature = "fallback-page")))] pub page_fallback: Option, /// Log remote address feature. diff --git a/src/settings/mod.rs b/src/settings/mod.rs index 7f9cfe1..6ee58d6 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -120,7 +120,10 @@ impl Settings { let mut threads_multiplier = opts.threads_multiplier; let mut max_blocking_threads = opts.max_blocking_threads; let mut grace_period = opts.grace_period; + + #[cfg(feature = "fallback-page")] let mut page_fallback = opts.page_fallback; + let mut log_remote_address = opts.log_remote_address; let mut redirect_trailing_slash = opts.redirect_trailing_slash; let mut ignore_hidden_files = opts.ignore_hidden_files; @@ -256,6 +259,7 @@ impl Settings { if let Some(v) = general.grace_period { grace_period = v } + #[cfg(feature = "fallback-page")] if let Some(v) = general.page_fallback { page_fallback = v } @@ -413,6 +417,7 @@ impl Settings { threads_multiplier, max_blocking_threads, grace_period, + #[cfg(feature = "fallback-page")] page_fallback, log_remote_address, redirect_trailing_slash, -- libgit2 1.7.2