1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::borrow::Cow;

use crate::{response, Data, Request, Response};
use crate::outcome::IntoOutcome;
use crate::http::{uri::Segments, HeaderMap, Method, ContentType, Status};
use crate::route::{Route, Handler, Outcome};
use crate::response::Responder;
use crate::util::Formatter;
use crate::fs::rewrite::*;

/// Custom handler for serving static files.
///
/// This handler makes is simple to serve static files from a directory on the
/// local file system. To use it, construct a `FileServer` using
/// [`FileServer::new()`], then `mount` the handler.
///
/// ```rust,no_run
/// # #[macro_use] extern crate rocket;
/// use rocket::fs::FileServer;
///
/// #[launch]
/// fn rocket() -> _ {
///     rocket::build()
///         .mount("/", FileServer::new("/www/static"))
/// }
/// ```
///
/// When mounted, the handler serves files from the specified path. If a
/// requested file does not exist, the handler _forwards_ the request with a
/// `404` status.
///
/// By default, the route has a rank of `10` which can be changed with
/// [`FileServer::rank()`].
///
/// # Customization
///
/// `FileServer` works through a pipeline of _rewrites_ in which a requested
/// path is transformed into a `PathBuf` via [`Segments::to_path_buf()`] and
/// piped through a series of [`Rewriter`]s to obtain a final [`Rewrite`] which
/// is then used to generate a final response. See [`Rewriter`] for complete
/// details on implementing your own `Rewriter`s.
///
/// # Example
///
/// Serve files from the `/static` directory on the local file system at the
/// `/public` path:
///
/// ```rust,no_run
/// # #[macro_use] extern crate rocket;
/// use rocket::fs::FileServer;
///
/// #[launch]
/// fn rocket() -> _ {
///     rocket::build().mount("/public", FileServer::new("/static"))
/// }
/// ```
///
/// Requests for files at `/public/<path..>` will be handled by returning the
/// contents of `/static/<path..>`. Requests for directories will return the
/// contents of `index.html`.
///
/// ## Relative Paths
///
/// In the example above, `/static` is an absolute path. If your static files
/// are stored relative to your crate and your project is managed by Cargo, use
/// the [`relative!`] macro to obtain a path that is relative to your crate's
/// root. For example, to serve files in the `static` subdirectory of your crate
/// at `/`, you might write:
///
/// ```rust,no_run
/// # #[macro_use] extern crate rocket;
/// use rocket::fs::{FileServer, relative};
///
/// #[launch]
/// fn rocket() -> _ {
///     rocket::build().mount("/", FileServer::new(relative!("static")))
/// }
/// ```
///
/// [`relative!`]: crate::fs::relative!
#[derive(Clone)]
pub struct FileServer {
    rewrites: Vec<Arc<dyn Rewriter>>,
    rank: isize,
}

impl FileServer {
    /// The default rank use by `FileServer` routes.
    const DEFAULT_RANK: isize = 10;

    /// Constructs a new `FileServer` that serves files from the file system
    /// `path` with the following rewrites:
    ///
    /// - `|f, _| f.is_visible()`: Serve only visible files (hide dotfiles).
    /// - [`Prefix::checked(path)`]: Prefix requests with `path`.
    /// - [`TrailingDirs`]: Ensure directory have a trailing slash.
    /// - [`DirIndex::unconditional("index.html")`]: Serve `$dir/index.html` for
    ///   requests to directory `$dir`.
    ///
    /// If you don't want to serve index files or want a different index file,
    /// use [`Self::without_index`]. To customize the entire request to file
    /// path rewrite pipeline, use [`Self::identity`].
    ///
    /// [`Prefix::checked(path)`]: crate::fs::rewrite::Prefix::checked
    /// [`TrailingDirs`]: crate::fs::rewrite::TrailingDirs
    /// [`DirIndex::unconditional("index.html")`]: DirIndex::unconditional()
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # #[macro_use] extern crate rocket;
    /// use rocket::fs::FileServer;
    ///
    /// #[launch]
    /// fn rocket() -> _ {
    ///     rocket::build()
    ///         .mount("/", FileServer::new("/www/static"))
    /// }
    /// ```
    pub fn new<P: AsRef<Path>>(path: P) -> Self {
        Self::identity()
            .filter(|f, _| f.is_visible())
            .rewrite(Prefix::checked(path))
            .rewrite(TrailingDirs)
            .rewrite(DirIndex::unconditional("index.html"))
    }

    /// Exactly like [`FileServer::new()`] except it _does not_ serve directory
    /// index files via [`DirIndex`]. It rewrites with the following:
    ///
    /// - `|f, _| f.is_visible()`: Serve only visible files (hide dotfiles).
    /// - [`Prefix::checked(path)`]: Prefix requests with `path`.
    /// - [`TrailingDirs`]: Ensure directory have a trailing slash.
    ///
    /// # Example
    ///
    /// Constructs a default file server to serve files from `./static` using
    /// `index.txt` as the index file if `index.html` doesn't exist.
    ///
    /// ```rust,no_run
    /// # #[macro_use] extern crate rocket;
    /// use rocket::fs::{FileServer, rewrite::DirIndex};
    ///
    /// #[launch]
    /// fn rocket() -> _ {
    ///     let server = FileServer::new("static")
    ///         .rewrite(DirIndex::if_exists("index.html"))
    ///         .rewrite(DirIndex::unconditional("index.txt"));
    ///
    ///     rocket::build()
    ///         .mount("/", server)
    /// }
    /// ```
    ///
    /// [`Prefix::checked(path)`]: crate::fs::rewrite::Prefix::checked
    /// [`TrailingDirs`]: crate::fs::rewrite::TrailingDirs
    pub fn without_index<P: AsRef<Path>>(path: P) -> Self {
        Self::identity()
            .filter(|f, _| f.is_visible())
            .rewrite(Prefix::checked(path))
            .rewrite(TrailingDirs)
    }

    /// Constructs a new `FileServer` with no rewrites.
    ///
    /// Without any rewrites, a `FileServer` will try to serve the requested
    /// file from the current working directory. In other words, it represents
    /// the identity rewrite. For example, a request `GET /foo/bar` will be
    /// passed through unmodified and thus `./foo/bar` will be served. This is
    /// very unlikely to be what you want.
    ///
    /// Prefer to use [`FileServer::new()`] or [`FileServer::without_index()`]
    /// whenever possible and otherwise use one or more of the rewrites in
    /// [`rocket::fs::rewrite`] or your own custom rewrites.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # #[macro_use] extern crate rocket;
    /// use rocket::fs::{FileServer, rewrite};
    ///
    /// #[launch]
    /// fn rocket() -> _ {
    ///     // A file server that serves exactly one file: /www/foo.html. The
    ///     // file is served irrespective of what's requested.
    ///     let server = FileServer::identity()
    ///         .rewrite(rewrite::File::checked("/www/foo.html"));
    ///
    ///     rocket::build()
    ///         .mount("/", server)
    /// }
    /// ```
    pub fn identity() -> Self {
        Self {
            rewrites: vec![],
            rank: Self::DEFAULT_RANK
        }
    }

    /// Sets the rank of the route emitted by the `FileServer` to `rank`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use rocket::fs::FileServer;
    /// # fn make_server() -> FileServer {
    /// FileServer::identity()
    ///    .rank(5)
    /// # }
    pub fn rank(mut self, rank: isize) -> Self {
        self.rank = rank;
        self
    }

    /// Add `rewriter` to the rewrite pipeline.
    ///
    /// # Example
    ///
    /// Redirect filtered requests (`None`) to `/`.
    ///
    /// ```rust,no_run
    /// # #[macro_use] extern crate rocket;
    /// use rocket::fs::{FileServer, rewrite::Rewrite};
    /// use rocket::{request::Request, response::Redirect};
    ///
    /// fn redir_missing<'r>(p: Option<Rewrite<'r>>, _req: &Request<'_>) -> Option<Rewrite<'r>> {
    ///     Some(p.unwrap_or_else(|| Redirect::temporary(uri!("/")).into()))
    /// }
    ///
    /// #[launch]
    /// fn rocket() -> _ {
    ///     rocket::build()
    ///         .mount("/", FileServer::new("static").rewrite(redir_missing))
    /// }
    /// ```
    ///
    /// Note that `redir_missing` is not a closure in this example. Making it a closure
    /// causes compilation to fail with a lifetime error. It really shouldn't but it does.
    pub fn rewrite<R: Rewriter>(mut self, rewriter: R) -> Self {
        self.rewrites.push(Arc::new(rewriter));
        self
    }

    /// Adds a rewriter to the pipeline that returns `Some` only when the
    /// function `f` returns `true`, filtering out all other files.
    ///
    /// # Example
    ///
    /// Allow all files that don't have a file name or have a file name other
    /// than "hidden".
    ///
    /// ```rust,no_run
    /// # #[macro_use] extern crate rocket;
    /// use rocket::fs::FileServer;
    ///
    /// #[launch]
    /// fn rocket() -> _ {
    ///     let server = FileServer::new("static")
    ///         .filter(|f, _| f.path.file_name() != Some("hidden".as_ref()));
    ///
    ///     rocket::build()
    ///         .mount("/", server)
    /// }
    /// ```
    pub fn filter<F: Send + Sync + 'static>(self, f: F) -> Self
        where F: Fn(&File<'_>, &Request<'_>) -> bool
    {
        struct Filter<F>(F);

        impl<F> Rewriter for Filter<F>
            where F: Fn(&File<'_>, &Request<'_>) -> bool + Send + Sync + 'static
        {
            fn rewrite<'r>(&self, f: Option<Rewrite<'r>>, r: &Request<'_>) -> Option<Rewrite<'r>> {
                f.and_then(|f| match f {
                    Rewrite::File(f) if self.0(&f, r) => Some(Rewrite::File(f)),
                    _ => None,
                })
            }
        }

        self.rewrite(Filter(f))
    }

    /// Adds a rewriter to the pipeline that maps the current `File` to another
    /// `Rewrite` using `f`. If the current `Rewrite` is a `Redirect`, it is
    /// passed through without calling `f`.
    ///
    /// # Example
    ///
    /// Append `index.txt` to every path.
    ///
    /// ```rust,no_run
    /// # #[macro_use] extern crate rocket;
    /// use rocket::fs::FileServer;
    ///
    /// #[launch]
    /// fn rocket() -> _ {
    ///     let server = FileServer::new("static")
    ///         .map(|f, _| f.map_path(|p| p.join("index.txt")).into());
    ///
    ///     rocket::build()
    ///         .mount("/", server)
    /// }
    /// ```
    pub fn map<F: Send + Sync + 'static>(self, f: F) -> Self
        where F: for<'r> Fn(File<'r>, &Request<'_>) -> Rewrite<'r>
    {
        struct Map<F>(F);

        impl<F> Rewriter for Map<F>
            where F: for<'r> Fn(File<'r>, &Request<'_>) -> Rewrite<'r> + Send + Sync + 'static
        {
            fn rewrite<'r>(&self, f: Option<Rewrite<'r>>, r: &Request<'_>) -> Option<Rewrite<'r>> {
                f.map(|f| match f {
                    Rewrite::File(f) => self.0(f, r),
                    Rewrite::Redirect(r) => Rewrite::Redirect(r),
                })
            }
        }

        self.rewrite(Map(f))
    }
}

impl From<FileServer> for Vec<Route> {
    fn from(server: FileServer) -> Self {
        let mut route = Route::ranked(server.rank, Method::Get, "/<path..>", server);
        route.name = Some("FileServer".into());
        vec![route]
    }
}

#[crate::async_trait]
impl Handler for FileServer {
    async fn handle<'r>(&self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> {
        use crate::http::uri::fmt::Path as UriPath;
        let path: Option<PathBuf> = req.segments::<Segments<'_, UriPath>>(0..).ok()
            .and_then(|segments| segments.to_path_buf(true).ok());

        let mut response = path.map(|p| Rewrite::File(File::new(p)));
        for rewrite in &self.rewrites {
            response = rewrite.rewrite(response, req);
        }

        let (outcome, status) = match response {
            Some(Rewrite::File(f)) => (f.open().await.respond_to(req), Status::NotFound),
            Some(Rewrite::Redirect(r)) => (r.respond_to(req), Status::InternalServerError),
            None => return Outcome::forward(data, Status::NotFound),
        };

        outcome.or_forward((data, status))
    }
}

impl fmt::Debug for FileServer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FileServer")
            .field("rewrites", &Formatter(|f| write!(f, "<{} rewrites>", self.rewrites.len())))
            .field("rank", &self.rank)
            .finish()
    }
}

impl<'r> File<'r> {
    async fn open(self) -> std::io::Result<NamedFile<'r>> {
        let file = tokio::fs::File::open(&self.path).await?;
        let metadata = file.metadata().await?;
        if metadata.is_dir() {
            return Err(std::io::Error::other("is a directory"));
        }

        Ok(NamedFile {
            file,
            len: metadata.len(),
            path: self.path,
            headers: self.headers,
        })
    }
}

struct NamedFile<'r> {
    file: tokio::fs::File,
    len: u64,
    path: Cow<'r, Path>,
    headers: HeaderMap<'r>,
}

// Do we want to allow the user to rewrite the Content-Type?
impl<'r> Responder<'r, 'r> for NamedFile<'r> {
    fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> {
        let mut response = Response::new();
        response.set_header_map(self.headers);
        if !response.headers().contains("Content-Type") {
            self.path.extension()
                .and_then(|ext| ext.to_str())
                .and_then(ContentType::from_extension)
                .map(|content_type| response.set_header(content_type));
        }

        response.set_sized_body(self.len as usize, self.file);
        Ok(response)
    }
}