index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2022-03-15 22:07:40.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2022-03-15 22:07:40.0 +00:00:00
commit
db3b69ab5c12a71f53b433403ddeda918528b65d [patch]
tree
fce38263ce4aecb19dbe4e8493916103d0ff1869
parent
cba4a8337da7b149f185d564c4f218fb46e94e99
download
db3b69ab5c12a71f53b433403ddeda918528b65d.tar.gz

refactor: minor fallback response tweaks



Diff

 src/fallback_page.rs |  1 +
 src/handler.rs       | 22 ++++++++++++----------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/fallback_page.rs b/src/fallback_page.rs
index 79528b5..51d8576 100644
--- a/src/fallback_page.rs
+++ b/src/fallback_page.rs
@@ -1,6 +1,7 @@
use headers::{AcceptRanges, ContentLength, ContentType, HeaderMapExt};
use hyper::{Body, Response, StatusCode};
use mime_guess::mime;

/// Checks if a fallback response can be generated, i.e. if it is a GET request that would result in a 404 error and a fallback page is configured.
/// If a response can be generated, it is returned, else `None` is returned.
pub fn fallback_response(page_fallback: &str) -> Response<Body> {
diff --git a/src/handler.rs b/src/handler.rs
index 9870485..840db08 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -157,19 +157,21 @@ impl RequestHandler {
                    Ok(resp)
                }
                Err(status) => {
                    // Check for a fallback response
                    if !self.opts.page_fallback.is_empty()
                        && (status == StatusCode::NOT_FOUND)
                        && (method == Method::GET)
                        && status == StatusCode::NOT_FOUND
                        && method == Method::GET
                    {
                        Ok(fallback_page::fallback_response(&self.opts.page_fallback))
                    } else {
                        error_page::error_response(
                            method,
                            &status,
                            &self.opts.page404,
                            &self.opts.page50x,
                        )
                        return Ok(fallback_page::fallback_response(&self.opts.page_fallback));
                    }

                    // Response error
                    error_page::error_response(
                        method,
                        &status,
                        &self.opts.page404,
                        &self.opts.page50x,
                    )
                }
            }
        }