Skip to content

Commit

Permalink
Format for style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
the10thWiz committed May 10, 2024
1 parent 30610e0 commit c050d07
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions core/lib/src/fs/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ impl fmt::Debug for DebugListRewrite<'_> {

pub trait Rewrite: Send + Sync + Any {
/// Modify RewritablePath as needed.
fn rewrite(&self, req: &Request<'_>, path: FileServerResponse, root: &Path) -> FileServerResponse;
fn rewrite(&self, req: &Request<'_>, path: FileServerResponse, root: &Path)
-> FileServerResponse;
/// Allow multiple of the same rewrite
fn allow_multiple(&self) -> bool { false }
fn name(&self) -> &'static str{ type_name::<Self>()}
Expand Down Expand Up @@ -129,9 +130,12 @@ pub enum FileServerResponse {
// These might have to remain as basic options (always processed first)
struct DotFiles;
impl Rewrite for DotFiles {
fn rewrite(&self, _req: &Request<'_>, path: FileServerResponse, _root: &Path) -> FileServerResponse {
fn rewrite(&self, _req: &Request<'_>, path: FileServerResponse, _root: &Path)
-> FileServerResponse
{
match path {
FileServerResponse::Hidden { name, reason: HiddenReason::DotFile } => FileServerResponse::File { name, modified: None, headers: HeaderMap::new() },
FileServerResponse::Hidden { name, reason: HiddenReason::DotFile } =>
FileServerResponse::File { name, modified: None, headers: HeaderMap::new() },
path => path,
}
}
Expand All @@ -145,35 +149,37 @@ impl Rewrite for DotFiles {

struct Index(&'static str);
impl Rewrite for Index {
fn rewrite(&self, _req: &Request<'_>, path: FileServerResponse, root: &Path) -> FileServerResponse {
// if path.file_name_path.is_dir() {
// path.file_name_path.push(self.0);
// // TODO: handle file_data_path
// }
fn rewrite(&self, _req: &Request<'_>, path: FileServerResponse, root: &Path)
-> FileServerResponse
{
match path {
FileServerResponse::File { name, modified, headers } if root.join(&name).is_dir() => FileServerResponse::File { name: name.join(self.0), modified, headers },
FileServerResponse::File { name, modified, headers } if root.join(&name).is_dir() =>
FileServerResponse::File { name: name.join(self.0), modified, headers },
path => path,
}
}
}
// Actually, curiously, this already just works as-is (the only thing that prevents it is the startup check)
// Actually, curiously, this already just works as-is (the only thing that prevents
// it is the startup check)
struct IndexFile;
impl Rewrite for IndexFile {
fn rewrite(&self, _req: &Request<'_>, path: FileServerResponse, _root: &Path) -> FileServerResponse {
fn rewrite(&self, _req: &Request<'_>, path: FileServerResponse, _root: &Path)
-> FileServerResponse
{
match path {
// FileServerResponse::File { name, modified, headers } if _root.is_file() && name.iter().count() == 0 => {
// FileServerResponse::File { name, modified, headers }
// }
path => path,
}
}
}

struct NormalizeDirs;
impl Rewrite for NormalizeDirs {
fn rewrite(&self, req: &Request<'_>, path: FileServerResponse, root: &Path) -> FileServerResponse {
fn rewrite(&self, req: &Request<'_>, path: FileServerResponse, root: &Path)
-> FileServerResponse
{
match path {
FileServerResponse::File { name, .. } if !req.uri().path().ends_with('/') && root.join(&name).is_dir() =>
FileServerResponse::File { name, .. } if !req.uri().path().ends_with('/') &&
root.join(&name).is_dir() =>
FileServerResponse::PermanentRedirect {
to: req.uri().map_path(|p| format!("{}/", p))
.expect("adding a trailing slash to a known good path => valid path")
Expand Down Expand Up @@ -301,10 +307,14 @@ impl FileServer {
}

pub fn rewrite(mut self, rewrite: impl Rewrite) -> Self {
if rewrite.allow_multiple() || !self.rewrites.iter().any(|f| f.as_ref().type_id() == rewrite.type_id()) {
if rewrite.allow_multiple() ||
!self.rewrites .iter().any(|f| f.as_ref().type_id() == rewrite.type_id()) {
self.rewrites.push(Arc::new(rewrite));
} else {
error!("Attempted to insert multiple of the same rewrite `{}` on a FileServer", rewrite.name());
error!(
"Attempted to insert multiple of the same rewrite `{}` on a FileServer",
rewrite.name()
);
}
self
}
Expand Down Expand Up @@ -346,8 +356,10 @@ impl Handler for FileServer {
.and_then(|segments| segments.to_path_buf_dotfiles().ok());
// .map(|path| self.root.join(path));
let mut response = match path {
Some((name, false)) => FileServerResponse::File { name, modified: None, headers: HeaderMap::new() },
Some((name, true)) => FileServerResponse::Hidden { name, reason: HiddenReason::DotFile },
Some((name, false)) =>
FileServerResponse::File { name, modified: None, headers: HeaderMap::new() },
Some((name, true)) =>
FileServerResponse::Hidden { name, reason: HiddenReason::DotFile },
None => return Outcome::forward(data, Status::NotFound),
};
println!("initial: {response:?}");
Expand All @@ -372,9 +384,14 @@ impl Handler for FileServer {
r
}).or_forward((data, Status::NotFound))
},
FileServerResponse::Hidden { .. } | FileServerResponse::NotFound { ..} => Outcome::forward(data, Status::NotFound),
FileServerResponse::PermanentRedirect { to } => Redirect::permanent(to).respond_to(req).or_forward((data, Status::InternalServerError)),
FileServerResponse::TemporaryRedirect { to } => Redirect::temporary(to).respond_to(req).or_forward((data, Status::InternalServerError)),
FileServerResponse::Hidden { .. } | FileServerResponse::NotFound { ..} =>
Outcome::forward(data, Status::NotFound),
FileServerResponse::PermanentRedirect { to } => Redirect::permanent(to)
.respond_to(req)
.or_forward((data, Status::InternalServerError)),
FileServerResponse::TemporaryRedirect { to } => Redirect::temporary(to)
.respond_to(req)
.or_forward((data, Status::InternalServerError)),
}
}
}
Expand Down

0 comments on commit c050d07

Please sign in to comment.