Changed how binary replies work

This commit is contained in:
p11 2024-05-05 23:57:41 +02:00
parent 4dc33e8c60
commit 6fa0e14469

View File

@ -325,8 +325,10 @@ fn handle_connection(
let path = path.to_path_buf();
let ending = ending.to_lowercase();
match ending.as_ref() {
"jpg" => reply_image(stream, &relative_path, "jpeg", path),
"png" | "tiff" | "gif" | "jpeg" => reply_image(stream, &relative_path, &ending, path),
"jpg" => reply_binary(stream, &relative_path, "image", "jpeg", path),
"png" | "tiff" | "gif" | "jpeg" => {
reply_binary(stream, &relative_path, "image", &ending, path)
}
_ => fail(stream),
};
return;
@ -378,7 +380,13 @@ fn handle_connection(
)
}
fn reply_image(mut stream: TcpStream, relative_path: &str, image_type: &str, mut path: PathBuf) {
fn reply_binary(
mut stream: TcpStream,
relative_path: &str,
data_type: &str,
file_type: &str,
mut path: PathBuf,
) {
path.push(relative_path);
let Ok(mut file) = File::open(path) else {
fail(stream);
@ -386,7 +394,7 @@ fn reply_image(mut stream: TcpStream, relative_path: &str, image_type: &str, mut
};
let _ = write!(stream, "HTTP/1.1 200 OK\r\n");
let _ = write!(stream, "Content-Type: image/{image_type}\r\n");
let _ = write!(stream, "Content-Type: {data_type}/{file_type}\r\n");
let _ = write!(stream, "\r\n");
let mut buf = [0; 0x400];