From c096cace3bef1067cb6a1f35ca725ce1c5e3b254 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 4 Jul 2025 12:15:13 +0300 Subject: [PATCH] improve router debug --- src/response.rs | 6 +++++- src/server/connection.rs | 19 +++++++++++++------ src/session/public.rs | 21 +++++++++++++-------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/response.rs b/src/response.rs index 0ac77a2..801b633 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,7 +1,10 @@ +use std::path::PathBuf; + /// Internal server response types pub enum Response<'a> { AccessDenied { - path: std::path::PathBuf, + canonical: PathBuf, + path: PathBuf, query: &'a str, }, InternalServerError { @@ -10,6 +13,7 @@ pub enum Response<'a> { }, NotFound { error: String, + path: PathBuf, query: &'a str, }, File(&'a [u8]), diff --git a/src/server/connection.rs b/src/server/connection.rs index b53ae60..50df73b 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -141,19 +141,26 @@ impl Connection { ); self.session.template.internal_server_error() } - Response::AccessDenied { query, path } => { + Response::AccessDenied { + canonical, + path, + query, + } => { eprintln!( - "[{}] < [{}] access to `{query}` denied (resolved path: `{}`).", + "[{}] < [{}] access denied: `{query}` (path: `{}` / canonical path: `{}`)", self.address.server, self.address.client, - path.to_string_lossy() + path.to_string_lossy(), + canonical.to_string_lossy() ); self.session.template.access_denied() } - Response::NotFound { query, error } => { + Response::NotFound { error, path, query } => { eprintln!( - "[{}] < [{}] requested resource `{query}` not found: {error}.", - self.address.server, self.address.client + "[{}] < [{}] not found: `{query}` (`{}`) reason: {error}", + self.address.server, + self.address.client, + path.to_string_lossy() ); self.session.template.not_found() } diff --git a/src/session/public.rs b/src/session/public.rs index 5cb11c7..8c48828 100644 --- a/src/session/public.rs +++ b/src/session/public.rs @@ -45,19 +45,24 @@ impl Public { pub fn request(&self, query: &str, mut callback: impl FnMut(Response) -> bool) -> bool { let p = { // access restriction zone, change carefully! - let mut p = PathBuf::from(&self.public_dir); - p.push(query.trim_matches('/')); - match p.canonicalize() { - Ok(path) => { - if !path.starts_with(&self.public_dir) { - return callback(Response::AccessDenied { query, path }); + let mut path = PathBuf::from(&self.public_dir); + path.push(query.trim_matches('/')); + match path.canonicalize() { + Ok(canonical) => { + if !canonical.starts_with(&self.public_dir) { + return callback(Response::AccessDenied { + canonical, + path, + query, + }); } - path + canonical } Err(e) => { return callback(Response::NotFound { - query, error: e.to_string(), + path, + query, }); } }