agate

Simple gemini server for static files
git clone https://github.com/mbrubeck/agate.git
Log | Files | Refs | README

commit d10c5122531ac3e7297bd14834cb7bdcd46648ac
parent 56795d098a826fda5ba096b2fac184fa01518d9c
Author: Johann150 <johann.galle@protonmail.com>
Date:   Sun,  7 Nov 2021 15:23:29 +0100

add ability for preamble to directory list

Diffstat:
MREADME.md | 3++-
Msrc/main.rs | 23++++++++++++++++-------
2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md @@ -73,7 +73,8 @@ Agate by default supports TLSv1.2 and TLSv1.3. You can disable support for TLSv1 ### Directory listing You can enable a basic directory listing for a directory by putting a file called `.directory-listing-ok` in that directory. This does not have an effect on sub-directories. -The directory listing will hide files and directories whose name starts with a dot (e.g. the `.directory-listing-ok` file itself or also the `.meta` configuration file). +This file must be UTF-8 encoded text; it may be empty. Any text in the file will be prepended to the directory listing. +The directory listing will hide files and directories whose name starts with a dot (e.g. the `.directory-listing-ok` file itself, the `.meta` configuration file, or the `..` directory). A file called `index.gmi` will always take precedence over a directory listing. diff --git a/src/main.rs b/src/main.rs @@ -529,15 +529,12 @@ impl RequestHandle { if url.path().ends_with('/') || url.path().is_empty() { // if the path ends with a slash or the path is empty, the links will work the same // without a redirect + // use `push` instead of `join` because the changed path is used later path.push("index.gmi"); if !path.exists() { - if path.with_file_name(".directory-listing-ok").exists() { - path.pop(); - return self.list_directory(&path).await; - } else { - self.send_header(51, "Directory index disabled.").await?; - return Ok(()); - } + path.pop(); + // try listing directory + return self.list_directory(&path).await; } } else { // if client is not redirected, links may not work as expected without trailing slash @@ -601,8 +598,20 @@ impl RequestHandle { .add(b'{') .add(b'}'); + // check if directory listing is enabled by geting preamble + let preamble = if let Ok(txt) = std::fs::read_to_string(path.join(".directory-listing-ok")) + { + txt + } else { + self.send_header(51, "Directory index disabled.").await?; + return Ok(()); + }; + log::info!("Listing directory {:?}", path); + self.send_header(20, "text/gemini").await?; + self.stream.write_all(preamble.as_bytes()).await?; + let mut entries = tokio::fs::read_dir(path).await?; let mut lines = vec![]; while let Some(entry) = entries.next_entry().await? {