improve router debug

This commit is contained in:
yggverse 2025-07-04 12:15:13 +03:00
parent 8c742ed9a1
commit c096cace3b
3 changed files with 31 additions and 15 deletions

View file

@ -1,7 +1,10 @@
use std::path::PathBuf;
/// Internal server response types /// Internal server response types
pub enum Response<'a> { pub enum Response<'a> {
AccessDenied { AccessDenied {
path: std::path::PathBuf, canonical: PathBuf,
path: PathBuf,
query: &'a str, query: &'a str,
}, },
InternalServerError { InternalServerError {
@ -10,6 +13,7 @@ pub enum Response<'a> {
}, },
NotFound { NotFound {
error: String, error: String,
path: PathBuf,
query: &'a str, query: &'a str,
}, },
File(&'a [u8]), File(&'a [u8]),

View file

@ -141,19 +141,26 @@ impl Connection {
); );
self.session.template.internal_server_error() self.session.template.internal_server_error()
} }
Response::AccessDenied { query, path } => { Response::AccessDenied {
canonical,
path,
query,
} => {
eprintln!( eprintln!(
"[{}] < [{}] access to `{query}` denied (resolved path: `{}`).", "[{}] < [{}] access denied: `{query}` (path: `{}` / canonical path: `{}`)",
self.address.server, self.address.server,
self.address.client, self.address.client,
path.to_string_lossy() path.to_string_lossy(),
canonical.to_string_lossy()
); );
self.session.template.access_denied() self.session.template.access_denied()
} }
Response::NotFound { query, error } => { Response::NotFound { error, path, query } => {
eprintln!( eprintln!(
"[{}] < [{}] requested resource `{query}` not found: {error}.", "[{}] < [{}] not found: `{query}` (`{}`) reason: {error}",
self.address.server, self.address.client self.address.server,
self.address.client,
path.to_string_lossy()
); );
self.session.template.not_found() self.session.template.not_found()
} }

View file

@ -45,19 +45,24 @@ impl Public {
pub fn request(&self, query: &str, mut callback: impl FnMut(Response) -> bool) -> bool { pub fn request(&self, query: &str, mut callback: impl FnMut(Response) -> bool) -> bool {
let p = { let p = {
// access restriction zone, change carefully! // access restriction zone, change carefully!
let mut p = PathBuf::from(&self.public_dir); let mut path = PathBuf::from(&self.public_dir);
p.push(query.trim_matches('/')); path.push(query.trim_matches('/'));
match p.canonicalize() { match path.canonicalize() {
Ok(path) => { Ok(canonical) => {
if !path.starts_with(&self.public_dir) { if !canonical.starts_with(&self.public_dir) {
return callback(Response::AccessDenied { query, path }); return callback(Response::AccessDenied {
canonical,
path,
query,
});
} }
path canonical
} }
Err(e) => { Err(e) => {
return callback(Response::NotFound { return callback(Response::NotFound {
query,
error: e.to_string(), error: e.to_string(),
path,
query,
}); });
} }
} }