Skip to content

Commit

Permalink
Update naming and documentation
Browse files Browse the repository at this point in the history
- Change name of `missing_root`
- Update documentation to reference non-panicing
  variant
- Fix issue caused by attempting to open directories
  • Loading branch information
the10thWiz committed Jun 11, 2024
1 parent 63fafdf commit 0abf8c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
40 changes: 25 additions & 15 deletions core/lib/src/fs/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,22 @@ impl<'p, 'h> File<'p, 'h> {
Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(bytes) })
}

NamedFile::open(strip_trailing_slash(self.path.as_ref()))
.await
.respond_to(req)
.map(|mut r| {
for header in self.headers {
r.adjoin_raw_header(header.name.as_str().to_owned(), header.value);
}
r
}).or_forward((data, Status::NotFound))
let path = strip_trailing_slash(self.path.as_ref());
// Fun fact, on Linux attempting to open a directory works, it just errors
// when you attempt to read it.
if path.is_file() {
NamedFile::open(path)
.await
.respond_to(req)
.map(|mut r| {
for header in self.headers {
r.adjoin_raw_header(header.name.as_str().to_owned(), header.value);
}
r
}).or_forward((data, Status::NotFound))
} else {
Outcome::forward(data, Status::NotFound)
}
}
}

Expand Down Expand Up @@ -264,7 +271,8 @@ impl<F> Rewriter for MapFile<F>
///
/// # Panics
///
/// Panics if `path` is not directory.
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
/// non-panicing variant.
pub fn dir_root(path: impl AsRef<Path>)
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static
{
Expand Down Expand Up @@ -295,7 +303,8 @@ pub fn dir_root(path: impl AsRef<Path>)
///
/// # Panics
///
/// Panics if `path` does not exist.
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
/// non-panicing variant.
pub fn file_root(path: impl AsRef<Path>)
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static
{
Expand All @@ -318,13 +327,13 @@ pub fn file_root(path: impl AsRef<Path>)
/// # Example
///
/// ```rust,no_run
/// # use rocket::fs::{FileServer, missing_root};
/// # use rocket::fs::{FileServer, file_root_permissive};
/// # fn make_server() -> FileServer {
/// FileServer::empty()
/// .map_file(missing_root("/tmp/rocket"))
/// .map_file(file_root_permissive("/tmp/rocket"))
/// # }
/// ```
pub fn missing_root(path: impl AsRef<Path>)
pub fn file_root_permissive(path: impl AsRef<Path>)
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static
{
let path = path.as_ref().to_path_buf();
Expand Down Expand Up @@ -353,7 +362,8 @@ pub fn filter_dotfiles(file: &File<'_, '_>, _req: &Request<'_>) -> bool {

/// Normalize directory accesses to always include a trailing slash.
///
/// Must be used after `dir_root`, since it needs the full path to check whether it is
/// Should normally be used after `dir_root` (or another rewrite that adds
/// a root), since it needs the full path to check whether a path points to
/// a directory.
///
/// # Example
Expand Down
4 changes: 2 additions & 2 deletions core/lib/tests/file_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rocket::{Rocket, Route, Build};
use rocket::http::Status;
use rocket::local::blocking::Client;
use rocket::fs::{
dir_root, file_root, filter_dotfiles, index, missing_root, normalize_dirs, relative, FileServer
dir_root, file_root, filter_dotfiles, index, file_root_permissive, normalize_dirs, relative, FileServer
};

fn static_root() -> &'static Path {
Expand Down Expand Up @@ -66,7 +66,7 @@ fn rocket() -> Rocket<Build> {
"/missing_root",
FileServer::empty()
.filter_file(filter_dotfiles)
.map_file(missing_root(root.join("no_file")))
.map_file(file_root_permissive(root.join("no_file")))

)
}
Expand Down

0 comments on commit 0abf8c3

Please sign in to comment.