Skip to content

Commit

Permalink
Auto-detect MIME types
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed May 21, 2020
1 parent db5665b commit ce570fc
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 12 deletions.
117 changes: 114 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "agate"
version = "1.0.1"
version = "1.1.0"
authors = ["Matt Brubeck <mbrubeck@limpet.net>"]
description = "Very simple server for the Gemini hypertext protocol"
keywords = ["server", "gemini", "hypertext", "internet", "protocol"]
Expand All @@ -15,6 +15,7 @@ async-tls = "0.7.0"
async-std = "1.5"
lazy_static = "1.4"
rustls = "0.17.0"
tree_magic = "0.2.3"
url = "2.1"

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -29,7 +29,7 @@ openssl req -x509 -newkey rsa:4096 -keyout key.rsa -out cert.pem \
agate localhost:1965 path/to/content/ cert.pem key.rsa
```

When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If there is a directory at that path, Agate will look for a file named `index.gemini` inside that directory. Currently, Agate sends all responses with the `text/gemini` MIME type. (Support for other MIME types may be added in the future.)
When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If there is a directory at that path, Agate will look for a file named `index.gemini` inside that directory.

[Gemini]: https://gemini.circumlunar.space/
[Rust]: https://www.rust-lang.org/
Expand Down
15 changes: 8 additions & 7 deletions src/main.rs
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit ce570fc

Please sign in to comment.