index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-06-23 20:41:08.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-06-23 20:41:08.0 +00:00:00
commit
2a699e48a7f51e891066f6c2c4aeed3d57c98677 [patch]
tree
e22ad5bc9f1990abaf87466b72018c2f840be6ea
parent
5ba6cd39f34ff2b4b8d28e9cc791b82754a0403f
download
2a699e48a7f51e891066f6c2c4aeed3d57c98677.tar.gz

refactor: follow symlinks during directory listing



Diff

 src/static_files.rs | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/src/static_files.rs b/src/static_files.rs
index 65544ab..ea68bbc 100644
--- a/src/static_files.rs
+++ b/src/static_files.rs
@@ -60,7 +60,7 @@ pub async fn handle_request(
            return Ok(resp);
        }

        return directory_listing(method, (current_path.to_string(), path)).await;
        return directory_listing(method, (current_path.to_owned(), path)).await;
    }

    file_reply(headers, (path, meta, auto_index)).await
@@ -151,23 +151,37 @@ async fn read_directory_entries(
    let mut files_count: usize = 0;
    while let Some(entry) = entries.next_entry().await? {
        let meta = entry.metadata().await?;
        let filesize = meta.len();

        let mut filesize_str = filesize
            .file_size(file_size_opts::DECIMAL)
            .map_err(anyhow::Error::msg)?;

        let mut name = entry
            .file_name()
            .into_string()
            .map_err(|err| anyhow::anyhow!(err.into_string().unwrap_or_default()))?;

        let mut filesize_str = String::from("-");

        if meta.is_dir() {
            name = format!("{}/", name);
            filesize_str = String::from("-");
            dirs_count += 1;
        } else {
        } else if meta.is_file() {
            filesize_str = meta
                .len()
                .file_size(file_size_opts::DECIMAL)
                .map_err(anyhow::Error::msg)?;
            files_count += 1;
        } else if meta.file_type().is_symlink() {
            let m = tokio::fs::symlink_metadata(entry.path().canonicalize()?).await?;
            if m.is_dir() {
                name = format!("{}/", name);
                dirs_count += 1;
            } else {
                filesize_str = meta
                    .len()
                    .file_size(file_size_opts::DECIMAL)
                    .map_err(anyhow::Error::msg)?;
                files_count += 1;
            }
        } else {
            continue;
        }

        let uri = format!("{}{}", base_path, name);
@@ -184,8 +198,7 @@ async fn read_directory_entries(
        );
    }

    let current_path = percent_decode_str(&base_path).decode_utf8()?.to_string();

    let current_path = percent_decode_str(&base_path).decode_utf8()?.to_owned();
    let dirs_str = if dirs_count == 1 {
        "directory"
    } else {