[core] Share links and repo layout: HMAC tokens with epoch, prefix check, exact hosts/<hostname>.toml, error codes; edge config v2
This commit is contained in:
@@ -44,18 +44,62 @@ pub fn host_path(hostname: &str) -> String {
|
||||
|
||||
/// Check one path of the repository tree (as listed recursively, `/`-separated).
|
||||
pub fn check_path(path: &str, kind: EntryKind) -> Result<RoutePath, ValidationError> {
|
||||
// Tests first: not written yet.
|
||||
let _ = (kind, reason::PATH_LAYOUT, check_hostname);
|
||||
match path.strip_prefix("hosts/").and_then(|r| r.strip_suffix(".toml")) {
|
||||
Some(h) => Ok(RoutePath::Host { hostname: h.to_owned() }),
|
||||
None => Ok(RoutePath::Site),
|
||||
let bad = |reason| ValidationError::UnexpectedFile {
|
||||
file: path.to_owned(),
|
||||
reason,
|
||||
};
|
||||
if kind != EntryKind::File {
|
||||
return Err(bad(reason::PATH_KIND));
|
||||
}
|
||||
if path == SITE_FILE {
|
||||
return Ok(RoutePath::Site);
|
||||
}
|
||||
let Some(rest) = path.strip_prefix("hosts/") else {
|
||||
return Err(bad(reason::PATH_LAYOUT));
|
||||
};
|
||||
if rest.contains(['/', '\\']) {
|
||||
return Err(bad(reason::PATH_SUBDIR));
|
||||
}
|
||||
let Some(hostname) = rest.strip_suffix(".toml") else {
|
||||
return Err(bad(reason::PATH_NAME));
|
||||
};
|
||||
if check_hostname(hostname).is_err() {
|
||||
return Err(bad(reason::PATH_NAME));
|
||||
}
|
||||
Ok(RoutePath::Host {
|
||||
hostname: hostname.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Check a whole tree listing.
|
||||
/// Check a whole tree listing. Returns every problem; at most [`MAX_HOST_FILES`] host files.
|
||||
pub fn check_tree<'a>(
|
||||
entries: impl IntoIterator<Item = (&'a str, EntryKind)>,
|
||||
) -> Result<(), Vec<ValidationError>> {
|
||||
let _ = entries.into_iter().count();
|
||||
Ok(())
|
||||
let mut errors = Vec::new();
|
||||
let mut hosts = 0usize;
|
||||
let mut site = false;
|
||||
for (path, kind) in entries {
|
||||
match check_path(path, kind) {
|
||||
Ok(RoutePath::Site) => site = true,
|
||||
Ok(RoutePath::Host { .. }) => hosts += 1,
|
||||
Err(e) => errors.push(e),
|
||||
}
|
||||
}
|
||||
if !site {
|
||||
errors.push(ValidationError::MissingField {
|
||||
file: SITE_FILE.into(),
|
||||
field: "(the file itself)".into(),
|
||||
});
|
||||
}
|
||||
if hosts > MAX_HOST_FILES {
|
||||
errors.push(ValidationError::UnexpectedFile {
|
||||
file: HOSTS_DIR.into(),
|
||||
reason: "more than 1000 host files",
|
||||
});
|
||||
}
|
||||
if errors.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(errors)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user