Added support to serve images and use new features from the server.
This commit is contained in:
parent
665bfa68db
commit
a9f605cf0e
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -116,7 +116,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "pukram2html"
|
||||
version = "0.1.0"
|
||||
source = "git+https://gitlab.com/porky11/pukram2html#c30e02c39c1c3d43ba424b21a59baf1d0acf26c0"
|
||||
source = "git+https://gitlab.com/porky11/pukram2html#fd8d848986f80ea9ec173fd0b91966f88ea6fa6d"
|
||||
dependencies = [
|
||||
"formatting",
|
||||
]
|
||||
|
||||
45
src/main.rs
45
src/main.rs
@ -4,7 +4,7 @@ use std::{
|
||||
fs::File,
|
||||
io::{prelude::*, BufReader, Error, ErrorKind, Result},
|
||||
net::{TcpListener, TcpStream},
|
||||
path::Path,
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
@ -183,6 +183,12 @@ impl Context {
|
||||
(Some(pki_path), 1, path.to_string())
|
||||
};
|
||||
|
||||
if relative_path.split('/').any(|name| name == "Images") {
|
||||
let path = path.to_path_buf();
|
||||
pool.execute(move || reply_image(stream, &relative_path, path));
|
||||
return;
|
||||
}
|
||||
|
||||
if !Path::is_file(&pk_path) {
|
||||
fail(stream);
|
||||
return;
|
||||
@ -226,6 +232,43 @@ impl Context {
|
||||
}
|
||||
}
|
||||
|
||||
fn reply_image(mut stream: TcpStream, relative_path: &str, mut path: PathBuf) {
|
||||
let Some((_, ending)) = relative_path.rsplit_once('.') else {
|
||||
fail(stream);
|
||||
return;
|
||||
};
|
||||
|
||||
let ending = ending.to_lowercase();
|
||||
|
||||
let image_type = match ending.as_ref() {
|
||||
"jpg" => "jpeg",
|
||||
"png" | "tiff" | "gif" | "jpeg" => &ending,
|
||||
_ => {
|
||||
fail(stream);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
path.push(relative_path);
|
||||
let Ok(mut file) = File::open(path) else {
|
||||
fail(stream);
|
||||
return;
|
||||
};
|
||||
|
||||
let _ = write!(stream, "HTTP/1.1 200 OK\r\n");
|
||||
let _ = write!(stream, "Content-Type: image/{image_type}\r\n");
|
||||
let _ = write!(stream, "\r\n");
|
||||
|
||||
let mut buf = [0; 0x400];
|
||||
|
||||
while let Ok(len) = file.read(&mut buf) {
|
||||
if len == 0 {
|
||||
break;
|
||||
}
|
||||
let _ = stream.write_all(&buf[0..len]);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_relative_connection(
|
||||
info: Arc<Mutex<SiteInfo>>,
|
||||
mut stream: TcpStream,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user