serve hidden files if there is a .meta config for them

This commit is contained in:
Johann150 2021-02-11 18:46:20 +01:00
parent bd9ed3255a
commit 49813d0c68
No known key found for this signature in database
GPG key ID: 9EE6577A2A06F8F1
4 changed files with 55 additions and 42 deletions

View file

@ -19,6 +19,7 @@ Thank you to @gegeweb for contributing to this release.
* The configuration files are now parsed as YAML. The syntax only changes in that a space is now required behind the colon. * The configuration files are now parsed as YAML. The syntax only changes in that a space is now required behind the colon.
* The changelog is now also kept in this file in addition to the GitHub releases. * The changelog is now also kept in this file in addition to the GitHub releases.
* Certificate chain and key file are now only loaded once at startup, certificate changes need a restart to take effect. * Certificate chain and key file are now only loaded once at startup, certificate changes need a restart to take effect.
* Hidden files are now served if there is an explicit setting in a `.meta` file for them, regardless of the `--serve-secret` flag.
### Fixed ### Fixed
* The Syntax for the IPv6 address in the README has been corrected. * The Syntax for the IPv6 address in the README has been corrected.

View file

@ -48,7 +48,7 @@ agate --content path/to/content/ \
All of the command-line arguments are optional. Run `agate --help` to see the default values used when arguments are omitted. All of the command-line arguments are optional. Run `agate --help` to see the default values used when arguments are omitted.
When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If any segment of the requested path starts with a dot, agate will respond with a status code 52, whether the file exists or not (this behaviour can be disabled with `--serve-secret`). If there is a directory at that path, Agate will look for a file named `index.gmi` inside that directory. When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If any segment of the requested path starts with a dot, agate will respond with a status code 52, whether the file exists or not. This behaviour can be disabled with `--serve-secret` or by an entry for the specific file in the `.meta` configuration file (see Meta-Presets). If there is a directory at that path, Agate will look for a file named `index.gmi` inside that directory.
## Configuration ## Configuration

View file

@ -331,16 +331,25 @@ impl RequestHandle {
path.push(url.host_str().expect("no hostname")); path.push(url.host_str().expect("no hostname"));
} }
if let Some(segments) = url.path_segments() { if let Some(mut segments) = url.path_segments() {
for segment in segments { // append percent-decoded path segments
if !ARGS.serve_secret && segment.starts_with('.') { path.extend(
// Do not serve anything that looks like a hidden file. segments
.clone()
.map(|segment| Ok(percent_decode_str(segment).decode_utf8()?.into_owned()))
.collect::<Result<Vec<_>>>()?,
);
// check if hiding files is disabled
if !ARGS.serve_secret
// there is a configuration for this file, assume it should be served
&& !self.metadata.lock().await.exists(&path)
// check if file or directory is hidden
&& segments.any(|segment| segment.starts_with('.'))
{
return self return self
.send_header(52, "If I told you, it would not be a secret.") .send_header(52, "If I told you, it would not be a secret.")
.await; .await;
} }
path.push(&*percent_decode_str(segment).decode_utf8()?);
}
} }
if let Ok(metadata) = tokio::fs::metadata(&path).await { if let Ok(metadata) = tokio::fs::metadata(&path).await {

View file

@ -1,5 +1,5 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::time::SystemTime; use std::time::SystemTime;
use yaml_rust::YamlLoader; use yaml_rust::YamlLoader;
@ -67,21 +67,22 @@ impl FileOptions {
} }
} }
/// Checks wether the database for the respective directory is still /// Checks wether the database for the directory of the specified file is
/// up to date. /// still up to date and re-reads it if outdated or not yet read.
/// Will only return true if the database should be (re)read, i.e. it will fn update(&mut self, dir: &Path) {
/// return false if there is no database file in the specified directory. let mut dir = if super::ARGS.central_config {
fn check_outdated(&self, db_dir: &PathBuf) -> bool { super::ARGS.content_dir.clone()
let mut db = db_dir.clone(); } else {
db.push(SIDECAR_FILENAME); dir.parent().expect("no parent directory").to_path_buf()
let db = db.as_path(); };
dir.push(SIDECAR_FILENAME);
if let Ok(metadata) = db.metadata() { let should_read = if let Ok(metadata) = dir.as_path().metadata() {
if !metadata.is_file() { if !metadata.is_file() {
// it exists, but it is a directory // it exists, but it is a directory
false false
} else if let (Ok(modified), Some(last_read)) = } else if let (Ok(modified), Some(last_read)) =
(metadata.modified(), self.databases_read.get(db_dir)) (metadata.modified(), self.databases_read.get(&dir))
{ {
// check that it was last modified before the read // check that it was last modified before the read
// if the times are the same, we might have read the old file // if the times are the same, we might have read the old file
@ -95,18 +96,17 @@ impl FileOptions {
} else { } else {
// the file probably does not exist // the file probably does not exist
false false
};
if should_read {
self.read_database(&dir);
} }
} }
/// (Re)reads a specific sidecar file that resides in the specified /// (Re)reads a specified sidecar file.
/// directory. The function takes a directory to minimize path
/// alterations "on the fly".
/// This function will allways try to read the file, even if it is current. /// This function will allways try to read the file, even if it is current.
fn read_database(&mut self, db_dir: &PathBuf) { fn read_database(&mut self, db: &PathBuf) {
log::trace!("reading database for {:?}", db_dir); log::trace!("reading database {:?}", db);
let mut db = db_dir.clone();
db.push(SIDECAR_FILENAME);
let db = db.as_path();
if let Ok(contents) = std::fs::read_to_string(db) { if let Ok(contents) = std::fs::read_to_string(db) {
let docs = match YamlLoader::load_from_str(&contents) { let docs = match YamlLoader::load_from_str(&contents) {
@ -137,7 +137,8 @@ impl FileOptions {
}; };
// generate workspace-unique path // generate workspace-unique path
let mut path = db_dir.clone(); let mut path = db.clone();
path.pop();
path.push(rel_path); path.push(rel_path);
// parse the preset // parse the preset
@ -180,8 +181,10 @@ impl FileOptions {
log::error!("no YAML document {:?}", db); log::error!("no YAML document {:?}", db);
return; return;
}; };
self.databases_read self.databases_read.insert(
.insert(db_dir.clone(), SystemTime::now()); db.as_path().parent().unwrap().to_path_buf(),
SystemTime::now(),
);
} else { } else {
log::error!("could not read configuration file {:?}", db); log::error!("could not read configuration file {:?}", db);
} }
@ -192,17 +195,17 @@ impl FileOptions {
/// The file path should consistenly be either absolute or relative to the /// The file path should consistenly be either absolute or relative to the
/// working/content directory. If inconsisten file paths are used, this can /// working/content directory. If inconsisten file paths are used, this can
/// lead to loading and storing sidecar files multiple times. /// lead to loading and storing sidecar files multiple times.
pub fn get(&mut self, file: &PathBuf) -> PresetMeta { pub fn get(&mut self, file: &Path) -> PresetMeta {
let dir = if super::ARGS.central_config { self.update(file);
super::ARGS.content_dir.clone()
} else {
file.parent().expect("no parent directory").to_path_buf()
};
if self.check_outdated(&dir) {
self.read_database(&dir);
}
self.file_meta.get(file).unwrap_or(&self.default).clone() self.file_meta.get(file).unwrap_or(&self.default).clone()
} }
/// Returns true if a configuration exists in a configuration file.
/// Returns false if no or only the default value exists.
pub fn exists(&mut self, file: &Path) -> bool {
self.update(file);
self.file_meta.contains_key(file)
}
} }