Simplified path creation, only keep files open as long as necessary
This commit is contained in:
parent
a2104976ef
commit
b157e2c962
143
src/main.rs
143
src/main.rs
@ -4,6 +4,7 @@ use std::{
|
|||||||
fs::File,
|
fs::File,
|
||||||
io::{prelude::*, BufReader, Error, ErrorKind, Result},
|
io::{prelude::*, BufReader, Error, ErrorKind, Result},
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
|
path::Path,
|
||||||
};
|
};
|
||||||
|
|
||||||
use data_stream::{
|
use data_stream::{
|
||||||
@ -132,14 +133,12 @@ impl Context {
|
|||||||
eprintln!("- Body: {}", request.body);
|
eprintln!("- Body: {}", request.body);
|
||||||
eprintln!();
|
eprintln!();
|
||||||
|
|
||||||
let Ok(current_dir) = env::current_dir() else {
|
let Ok(path) = env::current_dir() else {
|
||||||
fail(stream);
|
fail(stream);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut path = current_dir;
|
let (mut relative_path, _) = request
|
||||||
|
|
||||||
let (relative_path, _) = request
|
|
||||||
.path
|
.path
|
||||||
.split_once('?')
|
.split_once('?')
|
||||||
.unwrap_or((&request.path, Default::default()));
|
.unwrap_or((&request.path, Default::default()));
|
||||||
@ -149,54 +148,47 @@ impl Context {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let relative_path = if relative_path.is_empty() {
|
if let Some(path) = relative_path.strip_prefix('/') {
|
||||||
String::new()
|
relative_path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pk_path = path.clone();
|
||||||
|
let mut data_path = path.clone();
|
||||||
|
|
||||||
|
let (pki_path, start_level, relative_path) = if relative_path.is_empty() {
|
||||||
|
pk_path.push("index.pk");
|
||||||
|
data_path.push("index.dat");
|
||||||
|
|
||||||
|
(None, 0, String::new())
|
||||||
} else {
|
} else {
|
||||||
let path = percent_decode_str(&relative_path[1..]).decode_utf8_lossy();
|
let mut pki_path = path.clone();
|
||||||
|
|
||||||
|
let path = percent_decode_str(relative_path).decode_utf8_lossy();
|
||||||
if path.contains('_') {
|
if path.contains('_') {
|
||||||
let path = path.replace('_', " ");
|
let path = path.replace('_', " ");
|
||||||
let _ = write!(stream, "HTTP/1.1 308 Permanent Redirect\r\n");
|
let _ = write!(stream, "HTTP/1.1 308 Permanent Redirect\r\n");
|
||||||
let _ = write!(stream, "Location: /{path}\r\n\r\n");
|
let _ = write!(stream, "Location: /{path}\r\n\r\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
path.to_string()
|
|
||||||
|
pk_path.push(format!("{path}.pk"));
|
||||||
|
pki_path.push(format!("{path}.pki"));
|
||||||
|
data_path.push(format!("{path}.dat"));
|
||||||
|
|
||||||
|
(Some(pki_path), 1, path.to_string())
|
||||||
};
|
};
|
||||||
|
|
||||||
let (pk_file, pki_file, data_path, start_level) = if !relative_path.is_empty() {
|
if !Path::is_file(&pk_path) {
|
||||||
path.push(&relative_path);
|
fail(stream);
|
||||||
let (pk_extension, pki_extension, data_extension) =
|
return;
|
||||||
if let Some(extension) = path.extension() {
|
}
|
||||||
(
|
|
||||||
format!("{extension:?}.pk"),
|
|
||||||
format!("{extension:?}.pki"),
|
|
||||||
format!("{extension:?}.dat"),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
("pk".to_string(), "pki".to_string(), "dat".to_string())
|
|
||||||
};
|
|
||||||
let pk_path = path.with_extension(pk_extension);
|
|
||||||
let pki_path = path.with_extension(pki_extension);
|
|
||||||
let data_path = path.with_extension(data_extension);
|
|
||||||
|
|
||||||
let (Ok(pk_file), Ok(pki_file)) = (File::open(pk_path), File::open(pki_path)) else {
|
if let Some(pki_path) = &pki_path {
|
||||||
|
if !Path::is_file(pki_path) {
|
||||||
fail(stream);
|
fail(stream);
|
||||||
return;
|
return;
|
||||||
};
|
}
|
||||||
|
}
|
||||||
(pk_file, Some(pki_file), data_path, 1)
|
|
||||||
} else {
|
|
||||||
let mut pk_path = path.clone();
|
|
||||||
pk_path.push("index.pk");
|
|
||||||
let mut data_path = path.clone();
|
|
||||||
data_path.push("index.dat");
|
|
||||||
|
|
||||||
let Ok(pk_file) = File::open(pk_path) else {
|
|
||||||
fail(stream);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
(pk_file, None, data_path, 0)
|
|
||||||
};
|
|
||||||
|
|
||||||
use Entry::*;
|
use Entry::*;
|
||||||
let info = match self.infos.entry(relative_path.clone().into_boxed_str()) {
|
let info = match self.infos.entry(relative_path.clone().into_boxed_str()) {
|
||||||
@ -285,21 +277,18 @@ impl Context {
|
|||||||
|
|
||||||
let path_ref = &path;
|
let path_ref = &path;
|
||||||
|
|
||||||
let handle_entry = |entry: &str, output: &mut TcpStream, level: usize| {
|
let handle_entry = |mut entry: &str, output: &mut TcpStream, level: usize| {
|
||||||
let level = level + 1;
|
let level = level + 1;
|
||||||
let mut path = path_ref.clone();
|
let mut path = path_ref.clone();
|
||||||
let entry = if let Some((entry, _)) = entry.split_once(':') {
|
if let Some((real_entry, _)) = entry.split_once(':') {
|
||||||
entry
|
entry = real_entry
|
||||||
|
}
|
||||||
|
path.push(if relative_path.is_empty() {
|
||||||
|
format!("{entry}.pki")
|
||||||
} else {
|
} else {
|
||||||
entry
|
format!("{relative_path}/{entry}.pki")
|
||||||
};
|
});
|
||||||
path.push(entry);
|
|
||||||
let extension = if let Some(extension) = path.extension() {
|
|
||||||
format!("{extension:?}.pki")
|
|
||||||
} else {
|
|
||||||
"pki".to_string()
|
|
||||||
};
|
|
||||||
path.set_extension(extension);
|
|
||||||
let Ok(file) = File::open(path) else {
|
let Ok(file) = File::open(path) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@ -316,16 +305,20 @@ impl Context {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
convert_extended(
|
if let Ok(pk_file) = File::open(pk_path) {
|
||||||
BufReader::new(pk_file)
|
convert_extended(
|
||||||
.lines()
|
BufReader::new(pk_file)
|
||||||
.map(|line| line.unwrap_or_default()),
|
.lines()
|
||||||
&mut stream,
|
.map(|line| line.unwrap_or_default()),
|
||||||
Settings::default()
|
&mut stream,
|
||||||
.with_handler(handle_entry)
|
Settings::default()
|
||||||
.with_start_level(start_level)
|
.with_handler(handle_entry)
|
||||||
.with_use_textboxes(true),
|
.with_start_level(start_level)
|
||||||
);
|
.with_use_textboxes(true),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
unreachable!();
|
||||||
|
}
|
||||||
|
|
||||||
let parent_path = relative_path
|
let parent_path = relative_path
|
||||||
.rsplit_once('/')
|
.rsplit_once('/')
|
||||||
@ -335,19 +328,23 @@ impl Context {
|
|||||||
let _ = writeln!(stream, "<hr>");
|
let _ = writeln!(stream, "<hr>");
|
||||||
let _ = writeln!(stream, "<a href=\"/{parent_path}\"><< Back</a>");
|
let _ = writeln!(stream, "<a href=\"/{parent_path}\"><< Back</a>");
|
||||||
|
|
||||||
if let Some(pki_file) = pki_file {
|
if let Some(pki_path) = pki_path {
|
||||||
let _ = writeln!(stream, "<h1>Description</h1>");
|
if let Ok(pki_file) = File::open(pki_path) {
|
||||||
|
let _ = writeln!(stream, "<h1>Description</h1>");
|
||||||
|
|
||||||
convert_subheader(
|
convert_subheader(
|
||||||
BufReader::new(pki_file)
|
BufReader::new(pki_file)
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.unwrap_or_default()),
|
.map(|line| line.unwrap_or_default()),
|
||||||
&mut stream,
|
&mut stream,
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = writeln!(stream, "<hr>");
|
let _ = writeln!(stream, "<hr>");
|
||||||
let _ = writeln!(stream, "<a href=\"/{parent_path}\"><< Back</a>");
|
let _ = writeln!(stream, "<a href=\"/{parent_path}\"><< Back</a>");
|
||||||
|
} else {
|
||||||
|
unreachable!();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let html = html! {
|
let html = html! {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user