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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::cmp;
use std::fs::{File, Metadata};
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(unix)]
use std::os::unix::fs::MetadataExt;

use async_trait::async_trait;
use bitflags::bitflags;
use headers::*;
use mime_guess::from_path;

use super::FileChunk;
use crate::http::header;
use crate::http::header::{CONTENT_DISPOSITION, CONTENT_ENCODING};
use crate::http::range::HttpRange;
use crate::http::{Request, Response, StatusCode};
use crate::Depot;
use crate::Writer;

bitflags! {
    pub(crate) struct Flags: u8 {
        const ETAG = 0b0000_0001;
        const LAST_MODIFIED = 0b0000_0010;
        const CONTENT_DISPOSITION = 0b0000_0100;
    }
}

impl Default for Flags {
    fn default() -> Self {
        Flags::all()
    }
}

/// A file with an associated name.
#[derive(Debug)]
pub struct NamedFile {
    path: PathBuf,
    file: File,
    modified: Option<SystemTime>,
    pub buffer_size: u64,
    pub(crate) metadata: Metadata,
    pub(crate) flags: Flags,
    pub(crate) status_code: StatusCode,
    pub(crate) content_type: mime::Mime,
    pub(crate) content_disposition: HeaderValue,
    pub(crate) content_encoding: Option<HeaderValue>,
}

pub struct NamedFileBuilder {
    path: PathBuf,
    file: Option<File>,
    attached_filename: Option<String>,
    disposition_type: Option<String>,
    content_type: Option<mime::Mime>,
    content_encoding: Option<String>,
    content_disposition: Option<String>,
    buffer_size: Option<u64>,
}
impl NamedFileBuilder {
    pub fn with_attached_filename<T: Into<String>>(mut self, attached_filename: T) -> NamedFileBuilder {
        self.attached_filename = Some(attached_filename.into());
        self
    }
    pub fn with_disposition_type<T: Into<String>>(mut self, disposition_type: T) -> NamedFileBuilder {
        self.disposition_type = Some(disposition_type.into());
        self
    }
    pub fn with_content_type<T: Into<mime::Mime>>(mut self, content_type: T) -> NamedFileBuilder {
        self.content_type = Some(content_type.into());
        self
    }
    pub fn with_content_encoding<T: Into<String>>(mut self, content_encoding: T) -> NamedFileBuilder {
        self.content_encoding = Some(content_encoding.into());
        self
    }
    pub fn with_buffer_size(mut self, buffer_size: u64) -> NamedFileBuilder {
        self.buffer_size = Some(buffer_size);
        self
    }
    pub fn build(self) -> crate::Result<NamedFile> {
        let NamedFileBuilder {
            path,
            file,
            content_type,
            content_encoding,
            content_disposition,
            buffer_size,
            disposition_type,
            attached_filename,
            ..
        } = self;

        let file = match file {
            Some(file) => file,
            None => File::open(&path).map_err(crate::Error::new)?,
        };
        let content_type = content_type.unwrap_or_else(|| {
            let ct = from_path(&path).first_or_octet_stream();
            if ct.type_() == mime::TEXT && ct.get_param(mime::CHARSET).is_none() {
                //TODO: auto detect charset
                format!("{}; charset=utf-8", ct).parse::<mime::Mime>().unwrap_or(ct)
            } else {
                ct
            }
        });
        let content_disposition = content_disposition.unwrap_or_else(|| {
            disposition_type.unwrap_or_else(|| {
                let disposition_type = if attached_filename.is_some() {
                    "attachment"
                } else {
                    match content_type.type_() {
                        mime::IMAGE | mime::TEXT | mime::VIDEO => "inline",
                        _ => "attachment",
                    }
                };
                if disposition_type == "attachment" {
                    let filename = match attached_filename {
                        Some(filename) => filename,
                        None => path
                            .file_name()
                            .map(|filename| filename.to_string_lossy().to_string())
                            .unwrap_or_else(|| "file".into()),
                    };
                    format!("attachment; filename={}", filename)
                } else {
                    disposition_type.into()
                }
            })
        });
        let content_disposition = content_disposition.parse::<HeaderValue>().map_err(crate::Error::new)?;
        let metadata = file.metadata().map_err(crate::Error::new)?;
        let modified = metadata.modified().ok();
        let content_encoding = match content_encoding {
            Some(content_encoding) => Some(content_encoding.parse::<HeaderValue>().map_err(crate::Error::new)?),
            None => None,
        };

        Ok(NamedFile {
            path,
            file,
            content_type,
            content_disposition,
            metadata,
            modified,
            content_encoding,
            buffer_size: buffer_size.unwrap_or(65_536),
            status_code: StatusCode::OK,
            flags: Flags::default(),
        })
    }
}

impl NamedFile {
    pub fn builder(path: PathBuf) -> NamedFileBuilder {
        NamedFileBuilder {
            path,
            file: None,
            attached_filename: None,
            disposition_type: None,
            content_type: None,
            content_encoding: None,
            content_disposition: None,
            buffer_size: None,
        }
    }

    /// Attempts to open a file in read-only mode.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use salvo_core::fs::NamedFile;
    /// let file = NamedFile::open("foo.txt".into());
    /// ```
    pub fn open(path: PathBuf) -> crate::Result<NamedFile> {
        Self::builder(path).build()
    }

    /// Returns reference to the underlying `File` object.
    #[inline]
    pub fn file(&self) -> &File {
        &self.file
    }

    /// Retrieve the path of this file.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use std::io;
    /// # use salvo_core::fs::NamedFile;
    /// # fn path() {
    ///     let file = NamedFile::open("test.txt".into()).unwrap();
    ///     assert_eq!(file.path().as_os_str(), "foo.txt");
    /// # }
    /// ```
    #[inline]
    pub fn path(&self) -> &Path {
        self.path.as_path()
    }

    /// Set the MIME Content-Type for serving this file. By default
    /// the Content-Type is inferred from the filename extension.
    #[inline]
    pub fn set_content_type(mut self, content_type: mime::Mime) -> Self {
        self.content_type = content_type;
        self
    }

    /// Set the Content-Disposition for serving this file. This allows
    /// changing the inline/attachment disposition as well as the filename
    /// sent to the peer. By default the disposition is `inline` for text,
    /// image, and video content types, and `attachment` otherwise, and
    /// the filename is taken from the path provided in the `open` method
    /// after converting it to UTF-8 using.
    /// [to_string_lossy](https://doc.rust-lang.org/std/ffi/struct.OsStr.html#method.to_string_lossy).
    #[inline]
    pub fn set_content_disposition(mut self, content_disposition: HeaderValue) -> Self {
        self.content_disposition = content_disposition;
        self.flags.insert(Flags::CONTENT_DISPOSITION);
        self
    }

    /// Disable `Content-Disposition` header.
    ///
    /// By default Content-Disposition` header is enabled.
    #[inline]
    pub fn disable_content_disposition(mut self) -> Self {
        self.flags.remove(Flags::CONTENT_DISPOSITION);
        self
    }

    /// Set content encoding for serving this file
    #[inline]
    pub fn set_content_encoding(mut self, content_encoding: HeaderValue) -> Self {
        self.content_encoding = Some(content_encoding);
        self
    }

    #[inline]
    ///Specifies whether to use ETag or not.
    ///
    ///Default is true.
    pub fn use_etag(mut self, value: bool) -> Self {
        self.flags.set(Flags::ETAG, value);
        self
    }

    #[inline]
    ///Specifies whether to use Last-Modified or not.
    ///
    ///Default is true.
    pub fn use_last_modified(mut self, value: bool) -> Self {
        self.flags.set(Flags::LAST_MODIFIED, value);
        self
    }
    pub(crate) fn etag(&self) -> Option<ETag> {
        // This etag format is similar to Apache's.
        self.modified.as_ref().and_then(|mtime| {
            let ino = {
                #[cfg(unix)]
                {
                    self.metadata.ino()
                }
                #[cfg(not(unix))]
                {
                    0
                }
            };

            let dur = mtime.duration_since(UNIX_EPOCH).expect("modification time must be after epoch");
            let etag_str = format!("\"{:x}-{:x}-{:x}-{:x}\"", ino, self.metadata.len(), dur.as_secs(), dur.subsec_nanos());
            match etag_str.parse::<ETag>() {
                Ok(etag) => Some(etag),
                Err(e) => {
                    tracing::error!(error = ?e, etag = %etag_str, "set file's etag failed");
                    None
                }
            }
        })
    }

    pub(crate) fn last_modified(&self) -> Option<SystemTime> {
        self.modified
    }
}

#[async_trait]
impl Writer for NamedFile {
    async fn write(mut self, req: &mut Request, _depot: &mut Depot, res: &mut Response) {
        let etag = if self.flags.contains(Flags::ETAG) { self.etag() } else { None };
        let last_modified = if self.flags.contains(Flags::LAST_MODIFIED) {
            self.last_modified()
        } else {
            None
        };

        // check preconditions
        let precondition_failed = if !any_match(etag.as_ref(), req) {
            true
        } else if let (Some(ref last_modified), Some(since)) = (last_modified, req.headers().typed_get::<IfUnmodifiedSince>()) {
            !since.precondition_passes(*last_modified)
        } else {
            false
        };

        // check last modified
        let not_modified = if !none_match(etag.as_ref(), req) {
            true
        } else if req.headers().contains_key(header::IF_NONE_MATCH) {
            false
        } else if let (Some(ref last_modified), Some(since)) = (last_modified, req.headers().typed_get::<IfModifiedSince>()) {
            !since.is_modified(*last_modified)
        } else {
            false
        };

        res.headers_mut().insert(CONTENT_DISPOSITION, self.content_disposition.clone());
        res.headers_mut().typed_insert(ContentType::from(self.content_type.clone()));
        if let Some(lm) = last_modified {
            res.headers_mut().typed_insert(LastModified::from(lm));
        }
        if let Some(etag) = self.etag() {
            res.headers_mut().typed_insert(etag);
        }
        res.headers_mut().typed_insert(AcceptRanges::bytes());

        let mut length = self.metadata.len();
        if let Some(content_encoding) = &self.content_encoding {
            res.headers_mut().insert(CONTENT_ENCODING, content_encoding.clone());
        }
        let mut offset = 0;

        // check for range header
        // let mut range = None;
        if let Some(ranges) = req.headers().get(header::RANGE) {
            if let Ok(rangesheader) = ranges.to_str() {
                if let Ok(rangesvec) = HttpRange::parse(rangesheader, length) {
                    length = rangesvec[0].length;
                    offset = rangesvec[0].start;
                } else {
                    res.headers_mut().typed_insert(ContentRange::unsatisfied_bytes(length));
                    res.set_status_code(StatusCode::RANGE_NOT_SATISFIABLE);
                    return;
                };
            } else {
                res.set_status_code(StatusCode::BAD_REQUEST);
                return;
            };
        }

        if precondition_failed {
            res.set_status_code(StatusCode::PRECONDITION_FAILED);
            return;
        } else if not_modified {
            res.set_status_code(StatusCode::NOT_MODIFIED);
            return;
        }

        if offset != 0 || length != self.metadata.len() {
            res.set_status_code(StatusCode::PARTIAL_CONTENT);
            match ContentRange::bytes(offset..offset + length - 1, self.metadata.len()) {
                Ok(content_range) => {
                    res.headers_mut().typed_insert(content_range);
                }
                Err(e) => {
                    tracing::error!(error = ?e, "set file's content ranage failed");
                }
            }
            let reader = FileChunk {
                offset,
                chunk_size: cmp::min(length, self.metadata.len()),
                read_size: 0,
                file: self.file,
                buffer_size: self.buffer_size,
            };
            res.headers_mut().typed_insert(ContentLength(reader.chunk_size));
            res.streaming(reader)
        } else {
            res.set_status_code(StatusCode::OK);
            let reader = FileChunk {
                offset,
                file: self.file,
                chunk_size: length,
                read_size: 0,
                buffer_size: self.buffer_size,
            };
            res.headers_mut().typed_insert(ContentLength(length - offset));
            res.streaming(reader)
        }
    }
}

impl Deref for NamedFile {
    type Target = File;

    fn deref(&self) -> &File {
        &self.file
    }
}

impl DerefMut for NamedFile {
    fn deref_mut(&mut self) -> &mut File {
        &mut self.file
    }
}

/// Returns true if `req` has no `If-Match` header or one which matches `etag`.
fn any_match(etag: Option<&ETag>, req: &Request) -> bool {
    match req.headers().typed_get::<IfMatch>() {
        None => true,
        Some(if_match) => {
            if if_match == IfMatch::any() {
                true
            } else if let Some(etag) = etag {
                if_match.precondition_passes(etag)
            } else {
                false
            }
        }
    }
}

/// Returns true if `req` doesn't have an `If-None-Match` header matching `req`.
fn none_match(etag: Option<&ETag>, req: &Request) -> bool {
    match req.headers().typed_get::<IfMatch>() {
        None => true,
        Some(if_match) => {
            if if_match == IfMatch::any() {
                false
            } else if let Some(etag) = etag {
                !if_match.precondition_passes(etag)
            } else {
                true
            }
        }
    }
}