commit d6cb2e831b68497610afe63d93a978ed92a824d5
parent 259a190a9c898ce4afeaa47e387b3de88e61fc75
Author: Matt Brubeck <mbrubeck@limpet.net>
Date: Tue, 19 May 2020 22:07:31 -0700
Directory listing
Diffstat:
1 file changed, 26 insertions(+), 3 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -1,7 +1,7 @@
use {
async_std::{
net::{TcpListener, TcpStream},
- prelude::*,
+ stream::StreamExt,
task,
},
async_tls::{TlsAcceptor, server::TlsStream},
@@ -11,7 +11,7 @@ use {
error::Error,
fs::{File, read},
io::BufReader,
- path::PathBuf,
+ path::{Path, PathBuf},
sync::Arc,
},
url::Url,
@@ -68,6 +68,7 @@ fn acceptor() -> Result<TlsAcceptor> {
}
async fn connection(stream: TcpStream) -> Result {
+ use async_std::io::prelude::*;
let mut stream = ACCEPTOR.accept(stream).await?;
let url = match parse_request(&mut stream).await {
Ok(url) => url,
@@ -90,6 +91,7 @@ async fn connection(stream: TcpStream) -> Result {
}
async fn parse_request(stream: &mut TlsStream<TcpStream>) -> Result<Url> {
+ use async_std::io::prelude::*;
let mut stream = async_std::io::BufReader::new(stream);
let mut request = String::new();
stream.read_line(&mut request).await?;
@@ -103,6 +105,27 @@ fn get(url: &Url) -> Result<Vec<u8>> {
if !path.starts_with(&ARGS.content_dir) {
Err("invalid path")?
}
- let response = read(path)?;
+ eprintln!("Got request for {:?}", path);
+ let response = if path.is_dir() {
+ list(&path)?
+ } else {
+ read(&path)?
+ };
Ok(response)
}
+
+fn list(path: &Path) -> Result<Vec<u8>> {
+ use std::io::Write;
+ let mut result = vec![];
+ for entry in path.read_dir()? {
+ let entry = entry?;
+ let file_name = entry.file_name().into_string()
+ .or(Err("non-Unicode path"))?;
+ let path = entry.path();
+ let url = path.strip_prefix(&ARGS.content_dir)?.to_str()
+ .ok_or("non-Unicode path")?;
+ // TODO: Escape whitespace
+ writeln!(&mut result, "=> {} {}", url, file_name)?;
+ }
+ Ok(result)
+}