If current path does not exist, fail immeditely

This commit is contained in:
p11 2023-07-29 19:33:12 +02:00
parent 07b1bba9ab
commit 665bfa68db

View File

@ -21,6 +21,11 @@ mod request;
use request::Request;
fn main() {
let Ok(path) = env::current_dir() else {
eprintln!("Current directory does not exist!");
return;
};
let mut context = Context::default();
let pool = ThreadPool::new(4);
@ -38,7 +43,7 @@ fn main() {
continue;
};
context.handle_connection(stream, &pool);
context.handle_connection(&path, stream, &pool);
}
}
@ -120,7 +125,7 @@ struct Context {
}
impl Context {
fn handle_connection(&mut self, mut stream: TcpStream, pool: &ThreadPool) {
fn handle_connection(&mut self, path: &Path, mut stream: TcpStream, pool: &ThreadPool) {
let Some(request) = Request::from(&stream) else {
eprintln!("Invalid request!");
return;
@ -138,11 +143,6 @@ impl Context {
eprintln!("- Body: {}", request.body);
eprintln!();
let Ok(path) = env::current_dir() else {
fail(stream);
return;
};
let (mut relative_path, _) = request
.path
.split_once('?')
@ -157,8 +157,8 @@ impl Context {
relative_path = path;
}
let mut pk_path = path.clone();
let mut data_path = path.clone();
let mut pk_path = path.to_path_buf();
let mut data_path = path.to_path_buf();
let (pki_path, start_level, relative_path) = if relative_path.is_empty() {
pk_path.push("index.pk");
@ -166,7 +166,7 @@ impl Context {
(None, 0, String::new())
} else {
let mut pki_path = path.clone();
let mut pki_path = path.to_path_buf();
let path = percent_decode_str(relative_path).decode_utf8_lossy();
if path.contains('_') {
@ -209,6 +209,7 @@ impl Context {
.clone(),
};
let path = path.to_path_buf();
pool.execute(move || {
handle_relative_connection(
info,