From 8a301a6d03a39e8d8dd8fdc2326693a4c987c381 Mon Sep 17 00:00:00 2001
From: _ <>
Date: Thu, 29 Oct 2020 13:31:13 +0000
Subject: [PATCH] :construction: Can index directories now, but a lot of stuff
is missing
---
src/bin/server.rs | 33 ++++++++++++++++++++++++++++++---
todo.md | 2 +-
2 files changed, 31 insertions(+), 4 deletions(-)
diff --git a/src/bin/server.rs b/src/bin/server.rs
index 0c946dc..9c724f3 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -83,7 +83,7 @@ impl <'a> ResponseHandle <'a> {
async fn serve_dir (
response_handle: ResponseHandle <'_>,
- dir: ReadDir
+ mut dir: ReadDir
) {
let (tx, rx) = channel (2);
@@ -92,8 +92,35 @@ async fn serve_dir (
tx.send (Ok::<_, Infallible> (String::from ("
").into_bytes ())).await.unwrap ();
- for i in 0u8..10 {
- let s = format! ("- Dir entry {}
\n", i, i);
+ while let Ok (entry) = dir.next_entry ().await {
+ let entry: tokio::fs::DirEntry = match entry {
+ Some (x) => x,
+ None => break,
+ };
+
+ let trailing_slash = match entry.file_type ().await {
+ Ok (t) => if t.is_dir () {
+ "/"
+ }
+ else {
+ ""
+ },
+ Err (_) => "",
+ };
+
+ let file_name = match entry.file_name ().into_string () {
+ Ok (x) => x,
+ Err (_) => {
+ let s = format! ("- (file_name error)
\n");
+ if tx.send (Ok::<_, Infallible> (s.into_bytes ())).await.is_err ()
+ {
+ break;
+ }
+ continue;
+ }
+ };
+
+ let s = format! ("- {}{}
\n", file_name, trailing_slash, file_name, trailing_slash);
if tx.send (Ok::<_, Infallible> (s.into_bytes ())).await.is_err ()
{
break;
diff --git a/todo.md b/todo.md
index 62ef3fb..c7af176 100644
--- a/todo.md
+++ b/todo.md
@@ -1,5 +1,5 @@
-- Index directories
- Handle trailing slash problem for directories
+- Handle URL encoding for spaces in file names
- Set up tokens or something so clients can't trivially
impersonate servers
- Fix possible timing gap when refreshing http_listen (Just have client wait a few seconds?)