@@ -203,15 +203,22 @@ impl<'p, 'h> File<'p, 'h> {
203
203
Path :: new ( unsafe { OsStr :: from_encoded_bytes_unchecked ( bytes) } )
204
204
}
205
205
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
+ }
215
222
}
216
223
}
217
224
@@ -264,7 +271,8 @@ impl<F> Rewriter for MapFile<F>
264
271
///
265
272
/// # Panics
266
273
///
267
- /// Panics if `path` is not directory.
274
+ /// Panics if `path` does not exist. See [`file_root_permissive`] for a
275
+ /// non-panicing variant.
268
276
pub fn dir_root ( path : impl AsRef < Path > )
269
277
-> impl for <' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > ) -> FileResponse < ' p , ' h > + Send + Sync + ' static
270
278
{
@@ -295,7 +303,8 @@ pub fn dir_root(path: impl AsRef<Path>)
295
303
///
296
304
/// # Panics
297
305
///
298
- /// Panics if `path` does not exist.
306
+ /// Panics if `path` does not exist. See [`file_root_permissive`] for a
307
+ /// non-panicing variant.
299
308
pub fn file_root ( path : impl AsRef < Path > )
300
309
-> impl for <' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > ) -> FileResponse < ' p , ' h > + Send + Sync + ' static
301
310
{
@@ -318,13 +327,13 @@ pub fn file_root(path: impl AsRef<Path>)
318
327
/// # Example
319
328
///
320
329
/// ```rust,no_run
321
- /// # use rocket::fs::{FileServer, missing_root };
330
+ /// # use rocket::fs::{FileServer, file_root_permissive };
322
331
/// # fn make_server() -> FileServer {
323
332
/// FileServer::empty()
324
- /// .map_file(missing_root ("/tmp/rocket"))
333
+ /// .map_file(file_root_permissive ("/tmp/rocket"))
325
334
/// # }
326
335
/// ```
327
- pub fn missing_root ( path : impl AsRef < Path > )
336
+ pub fn file_root_permissive ( path : impl AsRef < Path > )
328
337
-> impl for <' p , ' h > Fn ( File < ' p , ' h > , & Request < ' _ > ) -> FileResponse < ' p , ' h > + Send + Sync + ' static
329
338
{
330
339
let path = path. as_ref ( ) . to_path_buf ( ) ;
@@ -353,7 +362,8 @@ pub fn filter_dotfiles(file: &File<'_, '_>, _req: &Request<'_>) -> bool {
353
362
354
363
/// Normalize directory accesses to always include a trailing slash.
355
364
///
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
357
367
/// a directory.
358
368
///
359
369
/// # Example
0 commit comments