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
//! A crate for embedding static assets into warp webservers.
#![warn(rust_2018_idioms, missing_docs)]

use headers::{
    AcceptRanges, ContentLength, ContentRange, ContentType, HeaderMapExt, IfModifiedSince, IfRange,
    IfUnmodifiedSince, LastModified, Range,
};
use http::{HeaderMap, StatusCode};
use hyper::Body;
use once_cell::sync::Lazy;
use std::future::ready;
use std::{
    path::{Path, PathBuf},
    time::SystemTime,
};
use warp::{
    filters::header::headers_cloned,
    path::{tail, Tail},
    reject::{self, Rejection},
    reply::Response,
    Filter,
};

#[doc(hidden)]
pub use include_dir::include_dir;

#[doc(hidden)]
pub use include_dir::{Dir, File};

/// Creates a [Filter][warp::Filter] that serves a directory at the base `$path` joined by
/// the request path.
///
/// It behaves much like warp's [fs::dir][warp::filters::fs::dir], but instead of serving
/// files from the filesystem at runtime, the directory to be served will be embedded into
/// your binary at compile time.
///
/// If the provided path is relative, it will be relative to the project root (as per the
/// `CARGO_MANIFEST_DIR` environment variable).
///
/// # Examples
// This test is ignored because we don't actually want to include /www/static while building
// doctests...
/// ```ignore
/// use static_dir::static_dir;
/// use warp::Filter;
///
/// // Matches requests that start with `/static`, and then uses the
/// // rest of that path to lookup and serve a file from `/www/static`.
/// let route = warp::path("static").and(static_dir!("/www/static"));
///
/// // For example:
/// // - `GET /static/app.js` would serve the file `/www/static/app.js`
/// // - `GET /static/css/app.css` would serve the file `/www/static/css/app.css`
/// ```
// The following makes sure the macro compiles when invoked.
/// ```
/// # use static_dir::static_dir;
/// # use warp::Filter;
/// #
/// # let route = warp::path("static").and(static_dir!("src"));
/// ```
#[macro_export]
macro_rules! static_dir {
    ($path:literal) => {{
        static DIR: $crate::Dir<'static> = $crate::include_dir!($path);

        $crate::dir(&DIR)
    }};
}

/// Creates a [Filter][warp::Filter] that serves an included directory.
///
/// See the documentation of [static_dir] for details.
pub fn dir(
    dir: &'static Dir<'_>,
) -> impl Filter<Extract = (Response,), Error = Rejection> + Clone + 'static {
    warp::get()
        // use the unmatched tail to decide which path we're serving
        .and(tail().and_then(move |tail| ready(parse_path(tail, dir))))
        // extract conditional information for cached / range responses
        .and(headers_cloned().map(|h: HeaderMap| Conditionals {
            if_modified_since: h.typed_get(),
            if_unmodified_since: h.typed_get(),
            if_range: h.typed_get(),
            range: h.typed_get(),
        }))
        // reply with the file's content, if it exists
        .and_then(move |path, conds| ready(reply(dir, path, conds)))
}

fn parse_path(tail: Tail, dir: &Dir<'_>) -> Result<PathBuf, Rejection> {
    let mut path: PathBuf = urlencoding::decode(tail.as_str())
        .map_err(|_| reject::not_found())
        .map(|path| path.split('/').collect())?;

    if path.parent().is_none() || dir.get_dir(&*path).is_some() {
        log::debug!("appending index.html to {:?}", path);
        path.push("index.html");
    }

    Ok(path)
}

#[derive(Clone, Debug, Default)]
struct Conditionals {
    if_modified_since: Option<IfModifiedSince>,
    if_unmodified_since: Option<IfUnmodifiedSince>,
    if_range: Option<IfRange>,
    range: Option<Range>,
}

enum Cond {
    WithBody(Option<Range>),
    NoBody(Response),
}

impl Conditionals {
    fn check(self, sys_time: &SystemTime, last_mod: &LastModified) -> Cond {
        if let Some(since) = self.if_unmodified_since {
            if !since.precondition_passes(*sys_time) {
                let mut res = Response::new(Body::empty());
                *res.status_mut() = StatusCode::PRECONDITION_FAILED;
                return Cond::NoBody(res);
            }
        }

        if let Some(since) = self.if_modified_since {
            if !since.is_modified(*sys_time) {
                let mut res = Response::new(Body::empty());
                *res.status_mut() = StatusCode::NOT_MODIFIED;
                return Cond::NoBody(res);
            }
        }

        if let Some(if_range) = self.if_range {
            if if_range.is_modified(None, Some(last_mod)) {
                return Cond::WithBody(None);
            }
        }

        Cond::WithBody(self.range)
    }
}

fn bytes_range(range: Range, max_len: u64) -> Option<(u64, u64)> {
    use std::ops::Bound;

    let (start, end) = range.iter().next()?;

    let start = match start {
        Bound::Unbounded => 0,
        Bound::Included(s) => s,
        Bound::Excluded(s) => s + 1,
    };

    let end = match end {
        Bound::Unbounded => max_len,
        Bound::Included(s) => s + 1,
        Bound::Excluded(s) => s,
    };

    if start < end && end <= max_len {
        Some((start, end))
    } else {
        Some((0, max_len))
    }
}

fn reply(dir: &'static Dir<'_>, path: PathBuf, conds: Conditionals) -> Result<Response, Rejection> {
    if let Some(file) = dir.get_file(&*path) {
        log::debug!("serving: {:?}", path);
        Ok(reply_conditional(file.contents(), &*path, conds))
    } else {
        log::warn!("file not found: {:?}", path);
        Err(reject::not_found())
    }
}

fn reply_conditional(buf: &'static [u8], path: &Path, conds: Conditionals) -> Response {
    static SYS_TIME: Lazy<SystemTime> = Lazy::new(|| SystemTime::now());
    static LAST_MOD: Lazy<LastModified> = Lazy::new(|| (*SYS_TIME).into());

    let len = buf.len() as u64;

    match conds.check(&SYS_TIME, &LAST_MOD) {
        Cond::WithBody(Some(range)) => match bytes_range(range, buf.len() as u64) {
            Some((0, e)) if e == len => reply_full(buf, path, *LAST_MOD),
            Some(range) => reply_range(buf, range, path, *LAST_MOD),
            None => reply_unsatisfiable(len),
        },
        Cond::WithBody(None) => reply_full(buf, path, *LAST_MOD),
        Cond::NoBody(resp) => resp,
    }
}

fn reply_full(buf: &'static [u8], path: &Path, last_mod: LastModified) -> Response {
    let mut resp = Response::new(buf.into());
    let hdrs = resp.headers_mut();

    hdrs.typed_insert(ContentLength(buf.len() as u64));
    let mime = mime_guess::from_path(path).first_or_octet_stream();
    hdrs.typed_insert(ContentType::from(mime));
    hdrs.typed_insert(AcceptRanges::bytes());
    hdrs.typed_insert(last_mod);

    resp
}

fn reply_range(
    buf: &'static [u8],
    (s, e): (u64, u64),
    path: &Path,
    last_mod: LastModified,
) -> Response {
    let len = e - s;
    let buf = &buf[s as usize..e as usize];

    let mut resp = Response::new(Body::from(buf));
    *resp.status_mut() = StatusCode::PARTIAL_CONTENT;

    let hdrs = resp.headers_mut();
    hdrs.typed_insert(ContentRange::bytes(s..e, len).unwrap());
    hdrs.typed_insert(ContentLength(len));

    let mime = mime_guess::from_path(path).first_or_octet_stream();
    hdrs.typed_insert(ContentType::from(mime));
    hdrs.typed_insert(AcceptRanges::bytes());
    hdrs.typed_insert(last_mod);

    resp
}

fn reply_unsatisfiable(len: u64) -> Response {
    let mut resp = Response::new(Body::empty());

    *resp.status_mut() = StatusCode::RANGE_NOT_SATISFIABLE;
    resp.headers_mut()
        .typed_insert(ContentRange::unsatisfied_bytes(len));

    resp
}