mirror of
https://github.com/YGGverse/agate.git
synced 2026-04-08 20:45:29 +00:00
Improve request parsing
This commit is contained in:
parent
0d872688f9
commit
039057b8db
2 changed files with 26 additions and 14 deletions
|
|
@ -23,10 +23,10 @@ openssl req -x509 -newkey rsa:4096 -keyout key.rsa -out cert.pem \
|
||||||
-days 3650 -nodes -subj "/CN=example.com"
|
-days 3650 -nodes -subj "/CN=example.com"
|
||||||
```
|
```
|
||||||
|
|
||||||
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:
|
4. Run the server. The command line arguments are `agate <addr:port> <content_dir> <cert_file> <key_file>`. For example, to listen on the standard Gemini port (1965) on all interfaces:
|
||||||
|
|
||||||
```
|
```
|
||||||
agate localhost:1965 path/to/content/ cert.pem key.rsa
|
agate 0.0.0.0: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.
|
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.
|
||||||
|
|
|
||||||
36
src/main.rs
36
src/main.rs
|
|
@ -8,7 +8,7 @@ use {
|
||||||
},
|
},
|
||||||
async_tls::{TlsAcceptor, server::TlsStream},
|
async_tls::{TlsAcceptor, server::TlsStream},
|
||||||
lazy_static::lazy_static,
|
lazy_static::lazy_static,
|
||||||
std::{error::Error, ffi::OsStr, fs::File, io::BufReader, sync::Arc},
|
std::{error::Error, ffi::OsStr, fs::File, io::BufReader, str, sync::Arc},
|
||||||
url::Url,
|
url::Url,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -40,7 +40,6 @@ lazy_static! {
|
||||||
static ref ARGS: Args = args()
|
static ref ARGS: Args = args()
|
||||||
.expect("usage: agate <addr:port> <dir> <cert> <key>");
|
.expect("usage: agate <addr:port> <dir> <cert> <key>");
|
||||||
static ref ACCEPTOR: TlsAcceptor = acceptor().unwrap();
|
static ref ACCEPTOR: TlsAcceptor = acceptor().unwrap();
|
||||||
static ref BASE: Url = Url::parse(&format!("gemini://{}", ARGS.sock_addr)).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn args() -> Option<Args> {
|
fn args() -> Option<Args> {
|
||||||
|
|
@ -71,7 +70,10 @@ async fn connection(stream: TcpStream) -> Result {
|
||||||
use async_std::io::prelude::*;
|
use async_std::io::prelude::*;
|
||||||
let mut stream = ACCEPTOR.accept(stream).await?;
|
let mut stream = ACCEPTOR.accept(stream).await?;
|
||||||
match parse_request(&mut stream).await {
|
match parse_request(&mut stream).await {
|
||||||
Ok(url) => get(&url, &mut stream).await,
|
Ok(url) => {
|
||||||
|
eprintln!("Got request for {:?}", url);
|
||||||
|
get(&url, &mut stream).await
|
||||||
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
stream.write_all(b"59 Invalid request.\r\n").await?;
|
stream.write_all(b"59 Invalid request.\r\n").await?;
|
||||||
Err(e)
|
Err(e)
|
||||||
|
|
@ -81,19 +83,29 @@ async fn connection(stream: TcpStream) -> Result {
|
||||||
|
|
||||||
async fn parse_request(stream: &mut TlsStream<TcpStream>) -> Result<Url> {
|
async fn parse_request(stream: &mut TlsStream<TcpStream>) -> Result<Url> {
|
||||||
let mut stream = async_std::io::BufReader::new(stream);
|
let mut stream = async_std::io::BufReader::new(stream);
|
||||||
let mut request = String::new();
|
let mut request = Vec::new();
|
||||||
stream.read_line(&mut request).await?;
|
stream.read_until(b'\r', &mut request).await?;
|
||||||
let request = request.trim();
|
|
||||||
eprintln!("Got request for {:?}", request);
|
|
||||||
|
|
||||||
let url = if request.starts_with("//") {
|
// Check line ending.
|
||||||
BASE.join(request.trim())?
|
let eol = &mut [0];
|
||||||
} else {
|
stream.read_exact(eol).await?;
|
||||||
Url::parse(request)?
|
if eol != b"\n" {
|
||||||
};
|
Err("CR without LF")?
|
||||||
|
}
|
||||||
|
// Check request length.
|
||||||
|
if request.len() > 1026 {
|
||||||
|
Err("Too long")?
|
||||||
|
}
|
||||||
|
// Handle scheme-relative URLs.
|
||||||
|
if request.starts_with(b"//") {
|
||||||
|
request.splice(..0, "gemini:".bytes());
|
||||||
|
}
|
||||||
|
// Parse URL.
|
||||||
|
let url = Url::parse(str::from_utf8(&request)?.trim_end())?;
|
||||||
if url.scheme() != "gemini" {
|
if url.scheme() != "gemini" {
|
||||||
Err("unsupported URL scheme")?
|
Err("unsupported URL scheme")?
|
||||||
}
|
}
|
||||||
|
// TODO: Validate hostname and port.
|
||||||
Ok(url)
|
Ok(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue