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
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]),

View file

@ -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()
}

View file

@ -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,
});
}
}