diff --git a/src/response.rs b/src/response.rs index 801b633..d4a2c1f 100644 --- a/src/response.rs +++ b/src/response.rs @@ -9,6 +9,7 @@ pub enum Response<'a> { }, InternalServerError { error: String, + path: Option, query: Option<&'a str>, }, NotFound { diff --git a/src/server/connection.rs b/src/server/connection.rs index e973661..ba38ae5 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -73,11 +73,12 @@ impl Connection { } } Err(e) => match self.response(Response::InternalServerError { - query: None, error: format!( "[{}] < [{}] failed to handle incoming request: `{e}`", self.address.server, self.address.client ), + path: None, + query: None, }) { Ok(sent) => { t += sent; @@ -134,10 +135,12 @@ impl Connection { ) } } - Response::InternalServerError { query, error } => { + Response::InternalServerError { error, path, query } => { eprintln!( - "[{}] > [{}] `{query:?}`: internal server error: `{error}`", - self.address.server, self.address.client + "[{}] > [{}] `{query:?}` (`{:?}`): internal server error: `{error}`", + self.address.server, + self.address.client, + path.map(|p| p.to_string_lossy().to_string()), ); self.session.template.internal_server_error() } diff --git a/src/session/public.rs b/src/session/public.rs index 8c48828..3690e1c 100644 --- a/src/session/public.rs +++ b/src/session/public.rs @@ -43,7 +43,7 @@ impl Public { } pub fn request(&self, query: &str, mut callback: impl FnMut(Response) -> bool) -> bool { - let p = { + let path = { // access restriction zone, change carefully! let mut path = PathBuf::from(&self.public_dir); path.push(query.trim_matches('/')); @@ -67,20 +67,21 @@ impl Public { } } }; - match fs::metadata(&p) { + match fs::metadata(&path) { Ok(t) => match (t.is_dir(), t.is_file()) { - (true, _) => callback(match self.list(&p) { + (true, _) => callback(match self.list(&path) { Ok(data) => Response::Directory { query, data, - is_root: p == self.public_dir, + is_root: path == self.public_dir, }, Err(e) => Response::InternalServerError { - query: Some(query), error: e.to_string(), + path: Some(path), + query: Some(query), }, }), - (_, true) => match fs::File::open(p) { + (_, true) => match fs::File::open(&path) { Ok(mut f) => loop { let mut b = vec![0; self.read_chunk]; match f.read(&mut b) { @@ -92,22 +93,25 @@ impl Public { } Err(e) => { return callback(Response::InternalServerError { - query: Some(query), error: format!("failed to read response chunk: `{e}`"), + path: Some(path), + query: Some(query), }); } } }, Err(e) => callback(Response::InternalServerError { - query: Some(query), error: format!("failed to read response: `{e}`"), + path: Some(path), + query: Some(query), }), }, _ => panic!(), // unexpected }, Err(e) => callback(Response::InternalServerError { - query: Some(query), error: format!("failed to read storage: `{e}`"), + path: Some(path), + query: Some(query), }), } }