Skip to content

Commit 0abf8c3

Browse files
committed
Update naming and documentation
- Change name of `missing_root` - Update documentation to reference non-panicing variant - Fix issue caused by attempting to open directories
1 parent 63fafdf commit 0abf8c3

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

core/lib/src/fs/server.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,22 @@ impl<'p, 'h> File<'p, 'h> {
203203
Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(bytes) })
204204
}
205205

206-
NamedFile::open(strip_trailing_slash(self.path.as_ref()))
207-
.await
208-
.respond_to(req)
209-
.map(|mut r| {
210-
for header in self.headers {
211-
r.adjoin_raw_header(header.name.as_str().to_owned(), header.value);
212-
}
213-
r
214-
}).or_forward((data, Status::NotFound))
206+
let path = strip_trailing_slash(self.path.as_ref());
207+
// Fun fact, on Linux attempting to open a directory works, it just errors
208+
// when you attempt to read it.
209+
if path.is_file() {
210+
NamedFile::open(path)
211+
.await
212+
.respond_to(req)
213+
.map(|mut r| {
214+
for header in self.headers {
215+
r.adjoin_raw_header(header.name.as_str().to_owned(), header.value);
216+
}
217+
r
218+
}).or_forward((data, Status::NotFound))
219+
} else {
220+
Outcome::forward(data, Status::NotFound)
221+
}
215222
}
216223
}
217224

@@ -264,7 +271,8 @@ impl<F> Rewriter for MapFile<F>
264271
///
265272
/// # Panics
266273
///
267-
/// Panics if `path` is not directory.
274+
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
275+
/// non-panicing variant.
268276
pub fn dir_root(path: impl AsRef<Path>)
269277
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static
270278
{
@@ -295,7 +303,8 @@ pub fn dir_root(path: impl AsRef<Path>)
295303
///
296304
/// # Panics
297305
///
298-
/// Panics if `path` does not exist.
306+
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
307+
/// non-panicing variant.
299308
pub fn file_root(path: impl AsRef<Path>)
300309
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static
301310
{
@@ -318,13 +327,13 @@ pub fn file_root(path: impl AsRef<Path>)
318327
/// # Example
319328
///
320329
/// ```rust,no_run
321-
/// # use rocket::fs::{FileServer, missing_root};
330+
/// # use rocket::fs::{FileServer, file_root_permissive};
322331
/// # fn make_server() -> FileServer {
323332
/// FileServer::empty()
324-
/// .map_file(missing_root("/tmp/rocket"))
333+
/// .map_file(file_root_permissive("/tmp/rocket"))
325334
/// # }
326335
/// ```
327-
pub fn missing_root(path: impl AsRef<Path>)
336+
pub fn file_root_permissive(path: impl AsRef<Path>)
328337
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static
329338
{
330339
let path = path.as_ref().to_path_buf();
@@ -353,7 +362,8 @@ pub fn filter_dotfiles(file: &File<'_, '_>, _req: &Request<'_>) -> bool {
353362

354363
/// Normalize directory accesses to always include a trailing slash.
355364
///
356-
/// Must be used after `dir_root`, since it needs the full path to check whether it is
365+
/// Should normally be used after `dir_root` (or another rewrite that adds
366+
/// a root), since it needs the full path to check whether a path points to
357367
/// a directory.
358368
///
359369
/// # Example

core/lib/tests/file_server.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rocket::{Rocket, Route, Build};
55
use rocket::http::Status;
66
use rocket::local::blocking::Client;
77
use rocket::fs::{
8-
dir_root, file_root, filter_dotfiles, index, missing_root, normalize_dirs, relative, FileServer
8+
dir_root, file_root, filter_dotfiles, index, file_root_permissive, normalize_dirs, relative, FileServer
99
};
1010

1111
fn static_root() -> &'static Path {
@@ -66,7 +66,7 @@ fn rocket() -> Rocket<Build> {
6666
"/missing_root",
6767
FileServer::empty()
6868
.filter_file(filter_dotfiles)
69-
.map_file(missing_root(root.join("no_file")))
69+
.map_file(file_root_permissive(root.join("no_file")))
7070

7171
)
7272
}

0 commit comments

Comments
 (0)