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