Auto-detect MIME types

This commit is contained in:
Matt Brubeck 2020-05-21 15:28:07 -07:00
parent db5665b783
commit ce570fc8c4
4 changed files with 125 additions and 12 deletions

View file

@ -8,12 +8,7 @@ use {
},
async_tls::{TlsAcceptor, server::TlsStream},
lazy_static::lazy_static,
std::{
error::Error,
fs::File,
io::BufReader,
sync::Arc,
},
std::{error::Error, ffi::OsStr, fs::File, io::BufReader, sync::Arc},
url::Url,
};
@ -118,7 +113,13 @@ async fn get(url: &Url, stream: &mut TlsStream<TcpStream>) -> Result {
}
match async_std::fs::read(&path).await {
Ok(body) => {
stream.write_all(b"20 text/gemini\r\n").await?;
if path.extension() == Some(OsStr::new("gemini")) {
stream.write_all(b"20 text/gemini\r\n").await?;
} else {
let mime = tree_magic::from_u8(&body);
let header = format!("20 {}\r\n", mime);
stream.write_all(header.as_bytes()).await?;
}
stream.write_all(&body).await?;
}
Err(e) => {