commit 6f490876cd3e5057f5e9bf903a7e8e5701a34923
parent 3a41f40775a803790ba934a35ded010d7ccf9421
Author: Matt Brubeck <mbrubeck@limpet.net>
Date: Thu, 21 May 2020 10:25:43 -0700
Check URL schemes
Diffstat:
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
@@ -23,10 +23,10 @@ openssl req -x509 -newkey rsa:4096 -keyout key.rsa -out cert.pem \
-days 3650 -nodes -subj "/CN=example.com"
```
-4. Run the server. The command line arguments are `agate <address:port> <content_dir> <cert_file> <key_file>`. For example, to listen on the standard Gemini port (1965) on all network interfaces:
+4. Run the server. The command line arguments are `agate <host:port> <content_dir> <cert_file> <key_file>`. For example, to listen on the standard Gemini port (1965) on localhost:
```
-agate 0.0.0.0:1965 path/to/content/ cert.pem key.rsa
+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.)
diff --git a/src/main.rs b/src/main.rs
@@ -45,6 +45,7 @@ lazy_static! {
static ref ARGS: Args = args()
.expect("usage: agate <addr:port> <dir> <cert> <key>");
static ref ACCEPTOR: TlsAcceptor = acceptor().unwrap();
+ static ref BASE: Url = Url::parse(&format!("gemini://{}", ARGS.sock_addr)).unwrap();
}
fn args() -> Option<Args> {
@@ -87,8 +88,17 @@ async fn parse_request(stream: &mut TlsStream<TcpStream>) -> Result<Url> {
let mut stream = async_std::io::BufReader::new(stream);
let mut request = String::new();
stream.read_line(&mut request).await?;
- let url = Url::parse(request.trim())?;
- eprintln!("Got request for {:?}", url);
+ let request = request.trim();
+ eprintln!("Got request for {:?}", request);
+
+ let url = if request.starts_with("//") {
+ BASE.join(request.trim())?
+ } else {
+ Url::parse(request)?
+ };
+ if url.scheme() != "gemini" {
+ Err("unsupported URL scheme")?
+ }
Ok(url)
}