Compare commits
No commits in common. "7a392e9c6c18b6e904efc7bf21b59efa069e8608" and "210e24415edb7439fc4d7e0094673c25fe8f4a5d" have entirely different histories.
7a392e9c6c
...
210e24415e
2 changed files with 47 additions and 85 deletions
21
src/http.rs
21
src/http.rs
|
@ -13,7 +13,7 @@ pub enum RequestRange {
|
|||
Suffix(usize),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Method {
|
||||
Get,
|
||||
Head,
|
||||
|
@ -35,7 +35,7 @@ pub enum Status {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Content {
|
||||
mime_type: &'static str,
|
||||
content_type: &'static str,
|
||||
range: Option<(usize, usize, usize)>,
|
||||
bytes: Vec<u8>,
|
||||
}
|
||||
|
@ -93,14 +93,14 @@ impl Response {
|
|||
let mut buffer = format!(
|
||||
"{}\r\nContent-Type: {}\r\nAccept-Ranges: bytes\r\nContent-Length: {}\r\n",
|
||||
self.status.header(),
|
||||
content.mime_type,
|
||||
content.content_type,
|
||||
content.bytes.len(),
|
||||
)
|
||||
.into_bytes();
|
||||
if let Some((start, end, size)) = content.range {
|
||||
buffer.extend_from_slice(
|
||||
format!("Content-Range: bytes {start}-{end}/{size}\r\n").as_bytes(),
|
||||
);
|
||||
format!("Content-Range: bytes {}-{}/{}\r\n", start, end, size).as_bytes(),
|
||||
)
|
||||
}
|
||||
buffer.extend_from_slice(b"\r\n");
|
||||
if !head_only {
|
||||
|
@ -168,7 +168,7 @@ impl Content {
|
|||
}
|
||||
};
|
||||
Self {
|
||||
mime_type: content_type,
|
||||
content_type,
|
||||
range: None,
|
||||
bytes,
|
||||
}
|
||||
|
@ -209,15 +209,6 @@ impl Method {
|
|||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Method {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Method::Get => write!(f, "GET"),
|
||||
Method::Head => write!(f, "HEAD"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RequestRange {
|
||||
fn parse(source: &str) -> Option<Self> {
|
||||
let source = source.strip_prefix("bytes=")?;
|
||||
|
|
111
src/main.rs
111
src/main.rs
|
@ -27,42 +27,40 @@ fn main() {
|
|||
for stream in listener.incoming() {
|
||||
match stream {
|
||||
Ok(stream) => threads.push(thread::spawn(|| handle_connection(stream))),
|
||||
Err(err) => println!("Error with incoming stream: {err}"),
|
||||
Err(err) => println!("Error with incoming stream: {}", err),
|
||||
}
|
||||
threads.retain(|j| !j.is_finished());
|
||||
println!("{} connections open", threads.len());
|
||||
threads = threads.into_iter().filter(|j| !j.is_finished()).collect();
|
||||
println!("{} threads open", threads.len());
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_connection(mut stream: TcpStream) {
|
||||
const MAX_REQUEST_SIZE: usize = 1024 * 4;
|
||||
let Ok(client_ip) = stream.peer_addr() else {
|
||||
let Ok(peer_addr) = stream.peer_addr() else {
|
||||
return;
|
||||
};
|
||||
println!("[{client_ip}] new connection");
|
||||
println!("#### new connection from {peer_addr}");
|
||||
|
||||
let mut buffer = Vec::with_capacity(2048);
|
||||
loop {
|
||||
let mut b = vec![0; 512];
|
||||
let Ok(size) = stream.read(&mut b) else {
|
||||
println!("[{client_ip}] connection broken");
|
||||
println!("failed to read ");
|
||||
return;
|
||||
};
|
||||
if size == 0 {
|
||||
println!("[{client_ip}] connection closed by client");
|
||||
println!("nothing read");
|
||||
return;
|
||||
}
|
||||
b.truncate(size);
|
||||
buffer.extend_from_slice(&b);
|
||||
|
||||
if buffer.len() > MAX_REQUEST_SIZE {
|
||||
println!("[{client_ip}] request over {MAX_REQUEST_SIZE} bytes, closing connection");
|
||||
if buffer.len() > 4096 {
|
||||
println!("request too long");
|
||||
return;
|
||||
}
|
||||
if buffer.ends_with(b"\r\n\r\n") {
|
||||
let request = String::from_utf8_lossy(&buffer).to_string();
|
||||
|
||||
println!("[{client_ip}] received {} bytes", buffer.len());
|
||||
// println!("Received {} bytes from {}", buffer.len(), peer_addr);
|
||||
// println!(
|
||||
// "=======\n{}=======\n\n",
|
||||
// request
|
||||
|
@ -71,51 +69,48 @@ fn handle_connection(mut stream: TcpStream) {
|
|||
// .replace("\\r\\n", "\n")
|
||||
// .replace("\\n", "\n")
|
||||
// );
|
||||
if handle_request(&request, &mut stream) {
|
||||
println!("[{client_ip}] closing connection");
|
||||
if handle_request(request, &mut stream) {
|
||||
println!("closing connection");
|
||||
return;
|
||||
}
|
||||
// println!("keeping connection");
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_request(request: &str, stream: &mut TcpStream) -> bool {
|
||||
let Ok(client_ip) = stream.peer_addr() else {
|
||||
return true;
|
||||
};
|
||||
let request = Request::parse(request);
|
||||
fn handle_request(request: String, stream: &mut TcpStream) -> bool {
|
||||
let request = Request::parse(&request);
|
||||
let response;
|
||||
let mut end_connection = true;
|
||||
|
||||
if let Some(request) = request {
|
||||
println!("[{client_ip}] {} {}", request.method, request.path);
|
||||
let head_only = request.method == Method::Head;
|
||||
let path = request.path.clone();
|
||||
response = get_file(&request)
|
||||
response = get_file(request)
|
||||
.map(|(content, end_of_file)| {
|
||||
end_connection = end_of_file;
|
||||
println!("[{client_ip}] sending file content");
|
||||
Response::new(Status::Ok).with_content(content)
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
println!("[{client_ip}] file not found");
|
||||
Response::new(Status::NotFound)
|
||||
.with_content(Content::text(format!("404 NOT FOUND - '{path}'")))
|
||||
.with_content(Content::text(format!("FILE NOT FOUND - '{}'", path)))
|
||||
})
|
||||
.format(head_only);
|
||||
} else {
|
||||
println!("[{client_ip}] bad request");
|
||||
response = Response::new(Status::BadRequest).format(false);
|
||||
}
|
||||
|
||||
if stream.write_all(&response).is_err() || stream.flush().is_err() {
|
||||
println!("[{client_ip}] failed to send response");
|
||||
}
|
||||
stream
|
||||
.write_all(&response)
|
||||
.unwrap_or_else(|_| println!("failed to respond"));
|
||||
stream
|
||||
.flush()
|
||||
.unwrap_or_else(|_| println!("failed to flush"));
|
||||
end_connection
|
||||
}
|
||||
|
||||
fn get_file(request: &Request) -> Option<(Content, bool)> {
|
||||
fn get_file(request: Request) -> Option<(Content, bool)> {
|
||||
const MAX_SIZE: usize = 1024 * 1024 * 8;
|
||||
|
||||
let current_dir = env::current_dir().unwrap();
|
||||
|
@ -177,37 +172,31 @@ fn generate_index(relative_path: &str, path: &Path) -> Option<Content> {
|
|||
.ok()?
|
||||
.flatten()
|
||||
.filter_map(|d| {
|
||||
let size = if d.file_type().ok()?.is_dir() {
|
||||
None
|
||||
} else {
|
||||
Some(d.metadata().ok()?.len())
|
||||
};
|
||||
|
||||
d.file_name().to_str().map(|s| (s.to_owned(), size))
|
||||
let is_dir = d.file_type().ok()?.is_dir();
|
||||
d.file_name().to_str().map(|s| (s.to_owned(), is_dir))
|
||||
})
|
||||
.collect();
|
||||
dirs.sort_by(|(name_a, size_a), (name_b, size_b)| {
|
||||
size_a
|
||||
.is_some()
|
||||
.cmp(&size_b.is_some())
|
||||
.then(name_a.cmp(name_b))
|
||||
});
|
||||
dirs.sort_by(|(name_a, dir_a), (name_b, dir_b)| dir_b.cmp(dir_a).then(name_a.cmp(name_b)));
|
||||
let list = dirs
|
||||
.into_iter()
|
||||
.map(|(name, size)| {
|
||||
let formatted_size = size.map(format_size).unwrap_or_default();
|
||||
format!(
|
||||
"<tr><td><a href=\"{href}\">{name}{trailing_slash}</a></td><td>{formatted_size}</td></tr>\n",
|
||||
href = PathBuf::from(relative_path).join(&name).display(),
|
||||
trailing_slash = if size.is_some() { "" } else { "/" },
|
||||
)
|
||||
.filter_map(|(name, is_dir)| {
|
||||
let mut s = format!(
|
||||
" <li><a href=\"{}\">{}",
|
||||
PathBuf::from(relative_path).join(&name).display(),
|
||||
name
|
||||
);
|
||||
if is_dir {
|
||||
s.push('/');
|
||||
}
|
||||
s.push_str("</a></li>\n");
|
||||
Some(s)
|
||||
})
|
||||
.fold(String::new(), |mut content, entry| {
|
||||
content.push_str(&entry);
|
||||
content
|
||||
});
|
||||
let parent = if relative_path != "/" {
|
||||
"<tr><td><a href=\"..\">../</a></td><td></td></tr>"
|
||||
r#" <li><a href="..">../</a></li>"#
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
@ -216,31 +205,13 @@ fn generate_index(relative_path: &str, path: &Path) -> Option<Content> {
|
|||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Index of {relative_path}</title>
|
||||
<style>
|
||||
html {{ color-scheme: dark; }}
|
||||
tr:nth-child(odd) {{ background-color: #333; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Index of {relative_path}</h3>
|
||||
<table>
|
||||
{parent}
|
||||
{list}
|
||||
</table>
|
||||
<ul>
|
||||
{parent}{list} </ul>
|
||||
</body>
|
||||
</html>"#,
|
||||
);
|
||||
Some(Content::html(page))
|
||||
}
|
||||
|
||||
fn format_size(bytes: u64) -> String {
|
||||
if bytes < 1024 {
|
||||
format!("{bytes} B")
|
||||
} else if bytes < 1024 * 1024 {
|
||||
format!("{:.1} KiB", bytes as f64 / 1024.0)
|
||||
} else if bytes < 1024 * 1024 * 1024 {
|
||||
format!("{:.1} MiB", bytes as f64 / (1024.0 * 1024.0))
|
||||
} else {
|
||||
format!("{:.1} GiB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue