WIP: serve binary files
This commit is contained in:
parent
f61fa2c5e7
commit
d9aef89da7
21
src/main.rs
21
src/main.rs
|
@ -86,6 +86,16 @@ fn respond_success(mut stream: TlsStream<TcpStream>, content: String, query: Opt
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
fn respond_binary(mut stream: TlsStream<TcpStream>, path: &str, mime_type: &str) {
|
||||
let mut f = File::open(path).unwrap();
|
||||
let mut buffer = Vec::new();
|
||||
f.read_to_end(&mut buffer).unwrap();
|
||||
|
||||
let header = format!("20\t{}\r\n", mime_type);
|
||||
stream.write_all(header.as_bytes()).unwrap();
|
||||
stream.write_all(&buffer).unwrap();
|
||||
}
|
||||
|
||||
fn respond_redirect(mut stream: TlsStream<TcpStream>) {
|
||||
let url = random_url();
|
||||
stream
|
||||
|
@ -140,6 +150,7 @@ fn random_url() -> &'static str {
|
|||
"gemini://tilde.pink",
|
||||
"gemini://vger.cloud",
|
||||
"gemini://earthlight.xyz",
|
||||
"gemini://yam655.com",
|
||||
];
|
||||
let url = urls.choose(&mut rand::thread_rng()).unwrap();
|
||||
url.to_owned()
|
||||
|
@ -168,6 +179,7 @@ fn route(stream: TlsStream<TcpStream>, request: String) {
|
|||
(10, String::from("Leave a message."), None)
|
||||
}
|
||||
},
|
||||
"/project_gemini.jpg" => (22, String::from("/project_gemini.jpg"), None),
|
||||
path => (20, String::from(path), None),
|
||||
};
|
||||
|
||||
|
@ -196,6 +208,15 @@ fn route(stream: TlsStream<TcpStream>, request: String) {
|
|||
}
|
||||
}
|
||||
}
|
||||
(22, path, _query) => {
|
||||
let full_path = if path.starts_with("/var/gemini") {
|
||||
path
|
||||
} else {
|
||||
format!("/var/gemini{}", path)
|
||||
};
|
||||
|
||||
respond_binary(stream, &full_path, "image/jpeg")
|
||||
}
|
||||
(31, _, _) => respond_redirect(stream),
|
||||
_ => (),
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue