mirror of
https://github.com/YGGverse/nexy.git
synced 2026-03-31 17:25:27 +00:00
improve router debug
This commit is contained in:
parent
8c742ed9a1
commit
c096cace3b
3 changed files with 31 additions and 15 deletions
|
|
@ -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]),
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue