diff options
author | Martin Fischer <martin@push-f.com> | 2021-07-18 17:31:00 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-07-18 17:34:30 +0200 |
commit | ae8eade8ec0497c5ce23f5a35caba997fd2045f1 (patch) | |
tree | 24112a03ef4f594379224364f850c84fb0760c20 /src/main.rs | |
parent | 119820d6cfc80fd3429fb828891183f66b277d46 (diff) |
refactor: use camino for UTF-8 paths
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 25 |
1 files changed, 11 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs index e8869c7..3383371 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use camino::Utf8Component; +use camino::Utf8Path; +use camino::Utf8PathBuf; use clap::Clap; use controller::Controller; use git2::Commit; @@ -25,9 +28,7 @@ use sputnik::request::SputnikParts; use sputnik::response::SputnikBuilder; use std::convert::Infallible; use std::env; -use std::path::Component; use std::path::Path; -use std::path::PathBuf; use std::sync::Arc; #[cfg(unix)] @@ -331,10 +332,10 @@ async fn build_response<C: Controller>( let mut comps = Vec::new(); // prevent directory traversal attacks - for comp in Path::new(&*unsanitized_path).components() { + for comp in Utf8Path::new(&*unsanitized_path).components() { match comp { - Component::Normal(name) => comps.push(name), - Component::ParentDir => { + Utf8Component::Normal(name) => comps.push(name), + Utf8Component::ParentDir => { return Err(Error::Forbidden("path traversal is forbidden".into())) } _ => {} @@ -343,7 +344,7 @@ async fn build_response<C: Controller>( let params: ActionParam = parts.query::<ActionParam>().unwrap(); - let url_path: PathBuf = comps.iter().collect(); + let url_path: Utf8PathBuf = comps.iter().collect(); let ctx = Context { repo, @@ -367,7 +368,7 @@ async fn build_response<C: Controller>( .map(|r| r.into_commit().unwrap().tree().unwrap()); if ctx.path.components().next().is_some() { - let entr = match tree.and_then(|t| t.get_path(&ctx.path)) { + let entr = match tree.and_then(|t| t.get_path(&ctx.path.as_ref())) { Ok(entr) => entr, Err(_) => { if unsanitized_path.ends_with('/') { @@ -464,7 +465,7 @@ fn action_links<C: Controller>( pub struct Context { repo: Repository, branch: Branch, - path: PathBuf, + path: Utf8PathBuf, } impl Context { @@ -492,10 +493,6 @@ impl Context { parent_commits, ) } - - fn file_name(&self) -> Option<&str> { - self.path.file_name().and_then(|x| x.to_str()) - } } #[derive(PartialEq)] @@ -528,8 +525,8 @@ fn embed_html_as_iframe(input: &str, page: &mut Page, mode: RenderMode) { } } -fn get_renderer(path: &Path) -> Option<fn(&str, &mut Page, RenderMode)> { - match path.extension().map(|e| e.to_str().unwrap()) { +fn get_renderer(path: &Utf8Path) -> Option<fn(&str, &mut Page, RenderMode)> { + match path.extension() { Some("md") => Some(render_markdown), Some("html") => Some(embed_html_as_iframe), _ => None, |