mirror of
https://github.com/YGGverse/agate.git
synced 2026-04-09 04:55:27 +00:00
Code cleanup
This commit is contained in:
parent
2c4118b590
commit
713c8ca837
1 changed files with 16 additions and 17 deletions
33
src/main.rs
33
src/main.rs
|
|
@ -48,20 +48,18 @@ fn args() -> Option<Args> {
|
||||||
async fn handle_request(stream: TcpStream) -> Result {
|
async fn handle_request(stream: TcpStream) -> Result {
|
||||||
// Perform handshake.
|
// Perform handshake.
|
||||||
static TLS: Lazy<TlsAcceptor> = Lazy::new(|| acceptor().unwrap());
|
static TLS: Lazy<TlsAcceptor> = Lazy::new(|| acceptor().unwrap());
|
||||||
let mut stream = TLS.accept(stream).await?;
|
let stream = &mut TLS.accept(stream).await?;
|
||||||
|
|
||||||
match parse_request(&mut stream).await {
|
let url = match parse_request(stream).await {
|
||||||
Ok(url) => {
|
Ok(url) => url,
|
||||||
eprintln!("Got request for {:?}", url);
|
|
||||||
if let Err(e) = send_response(&url, &mut stream).await {
|
|
||||||
respond(&mut stream, "51", &["Not found, sorry."]).await?;
|
|
||||||
return Err(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
respond(&mut stream, "59", &["Invalid request."]).await?;
|
respond(stream, "59", &["Invalid request."]).await?;
|
||||||
return Err(e)
|
return Err(e)
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
if let Err(e) = send_response(url, stream).await {
|
||||||
|
respond(stream, "51", &["Not found, sorry."]).await?;
|
||||||
|
return Err(e)
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -80,7 +78,7 @@ fn acceptor() -> Result<TlsAcceptor> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the URL requested by the client.
|
/// Return the URL requested by the client.
|
||||||
async fn parse_request<R: Read + Unpin>(mut stream: R) -> Result<Url> {
|
async fn parse_request<R: Read + Unpin>(stream: &mut R) -> Result<Url> {
|
||||||
// Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we
|
// Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we
|
||||||
// can use a fixed-sized buffer on the stack, avoiding allocations and
|
// can use a fixed-sized buffer on the stack, avoiding allocations and
|
||||||
// copying, and stopping bad clients from making us use too much memory.
|
// copying, and stopping bad clients from making us use too much memory.
|
||||||
|
|
@ -112,11 +110,12 @@ async fn parse_request<R: Read + Unpin>(mut stream: R) -> Result<Url> {
|
||||||
if url.scheme() != "gemini" {
|
if url.scheme() != "gemini" {
|
||||||
Err("unsupported URL scheme")?
|
Err("unsupported URL scheme")?
|
||||||
}
|
}
|
||||||
|
eprintln!("Got request for {:?}", url);
|
||||||
Ok(url)
|
Ok(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send the client the file located at the requested URL.
|
/// Send the client the file located at the requested URL.
|
||||||
async fn send_response<W: Write + Unpin>(url: &Url, mut stream: W) -> Result {
|
async fn send_response<W: Write + Unpin>(url: Url, stream: &mut W) -> Result {
|
||||||
let mut path = std::path::PathBuf::from(&ARGS.content_dir);
|
let mut path = std::path::PathBuf::from(&ARGS.content_dir);
|
||||||
if let Some(segments) = url.path_segments() {
|
if let Some(segments) = url.path_segments() {
|
||||||
path.extend(segments);
|
path.extend(segments);
|
||||||
|
|
@ -126,7 +125,7 @@ async fn send_response<W: Write + Unpin>(url: &Url, mut stream: W) -> Result {
|
||||||
path.push("index.gmi");
|
path.push("index.gmi");
|
||||||
} else {
|
} else {
|
||||||
// Send a redirect when the URL for a directory has no trailing slash.
|
// Send a redirect when the URL for a directory has no trailing slash.
|
||||||
return respond(&mut stream, "31", &[url.as_str(), "/"]).await;
|
return respond(stream, "31", &[url.as_str(), "/"]).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -135,18 +134,18 @@ async fn send_response<W: Write + Unpin>(url: &Url, mut stream: W) -> Result {
|
||||||
|
|
||||||
// Send header.
|
// Send header.
|
||||||
if path.extension() == Some(OsStr::new("gmi")) {
|
if path.extension() == Some(OsStr::new("gmi")) {
|
||||||
respond(&mut stream, "20", &["text/gemini"]).await?;
|
respond(stream, "20", &["text/gemini"]).await?;
|
||||||
} else {
|
} else {
|
||||||
let mime = mime_guess::from_path(&path).first_or_octet_stream();
|
let mime = mime_guess::from_path(&path).first_or_octet_stream();
|
||||||
respond(&mut stream, "20", &[mime.essence_str()]).await?;
|
respond(stream, "20", &[mime.essence_str()]).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send body.
|
// Send body.
|
||||||
async_std::io::copy(&mut file, &mut stream).await?;
|
async_std::io::copy(&mut file, stream).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn respond<W: Write + Unpin>(mut stream: W, status: &str, meta: &[&str]) -> Result {
|
async fn respond<W: Write + Unpin>(stream: &mut W, status: &str, meta: &[&str]) -> Result {
|
||||||
stream.write_all(status.as_bytes()).await?;
|
stream.write_all(status.as_bytes()).await?;
|
||||||
stream.write_all(b" ").await?;
|
stream.write_all(b" ").await?;
|
||||||
for m in meta {
|
for m in meta {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue