From bd7803426e15a45f3bc3c80ddbaa1dcf23dbddb3 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Sun, 8 May 2022 22:43:21 +0200 Subject: [PATCH] feat: include uri on error page tracing log resolves #108 -- the server now will show the requested uri when a file/dir is not found. static-web-server -p 8787 -d docker/public/ -g warn 2022-05-08T20:39:57.864548Z WARN static_web_server::error_page: method=GET uri=/assets/abcdef?sort=1 status=404 error="Not Found" 2022-05-08T20:44:04.214944Z WARN static_web_server::error_page: method=GET uri=/assets/abc status=404 error="Not Found" --- src/error_page.rs | 8 ++++++-- src/handler.rs | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/error_page.rs b/src/error_page.rs index 949f066..e8a665f 100644 --- a/src/error_page.rs +++ b/src/error_page.rs @@ -1,17 +1,21 @@ use headers::{AcceptRanges, ContentLength, ContentType, HeaderMapExt}; -use hyper::{Body, Method, Response, StatusCode}; +use hyper::{Body, Method, Response, StatusCode, Uri}; use mime_guess::mime; use crate::Result; /// It returns a HTTP error response which also handles available `404` or `50x` HTML content. pub fn error_response( + uri: &Uri, method: &Method, status_code: &StatusCode, page404: &[u8], page50x: &[u8], ) -> Result> { - tracing::warn!(method = ?method, status = status_code.as_u16(), error = ?status_code.to_owned()); + tracing::warn!( + method = ?method, uri = ?uri, status = status_code.as_u16(), + error = status_code.canonical_reason().unwrap_or_default() + ); // Check for 4xx/50x status codes and handle their corresponding HTML content let mut error_page_content = String::new(); diff --git a/src/handler.rs b/src/handler.rs index dc02c39..a9ab4f7 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -52,6 +52,7 @@ impl RequestHandler { // Check for disallowed HTTP methods and reject request accordently if !(method == Method::GET || method == Method::HEAD || method == Method::OPTIONS) { return error_page::error_response( + uri, method, &StatusCode::METHOD_NOT_ALLOWED, &self.opts.page404, @@ -69,6 +70,7 @@ impl RequestHandler { Err(err) => { tracing::error!("cors error kind: {:?}", err); return error_page::error_response( + uri, method, &StatusCode::FORBIDDEN, &self.opts.page404, @@ -84,6 +86,7 @@ impl RequestHandler { if let Err(err) = basic_auth::check_request(headers, user_id, password) { tracing::warn!("basic authentication failed {:?}", err); let mut resp = error_page::error_response( + uri, method, &StatusCode::UNAUTHORIZED, &self.opts.page404, @@ -100,6 +103,7 @@ impl RequestHandler { } else { tracing::error!("invalid basic authentication `user_id:password` pairs"); return error_page::error_response( + uri, method, &StatusCode::INTERNAL_SERVER_ERROR, &self.opts.page404, @@ -138,6 +142,7 @@ impl RequestHandler { Err(err) => { tracing::error!("error during body compression: {:?}", err); return error_page::error_response( + uri, method, &StatusCode::INTERNAL_SERVER_ERROR, &self.opts.page404, @@ -175,6 +180,7 @@ impl RequestHandler { // Otherwise return a response error error_page::error_response( + uri, method, &status, &self.opts.page404, -- libgit2 1.7.2