add ability for preamble to directory list

This commit is contained in:
Johann150 2021-11-07 15:23:29 +01:00
parent 56795d098a
commit d10c512253
No known key found for this signature in database
GPG key ID: 9EE6577A2A06F8F1
2 changed files with 18 additions and 8 deletions

View file

@ -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.

View file

@ -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? {