2019-03-06 07:10:08 +01:00
|
|
|
use std::fs::{File, Metadata};
|
|
|
|
use std::io;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
|
|
|
|
2019-03-25 21:02:37 +01:00
|
|
|
use bitflags::bitflags;
|
2019-03-06 07:10:08 +01:00
|
|
|
use mime;
|
|
|
|
use mime_guess::guess_mime_type;
|
|
|
|
|
2019-03-25 21:43:02 +01:00
|
|
|
use actix_web::http::header::{
|
|
|
|
self, ContentDisposition, DispositionParam, DispositionType,
|
|
|
|
};
|
2019-03-06 07:10:08 +01:00
|
|
|
use actix_web::http::{ContentEncoding, Method, StatusCode};
|
2019-04-13 23:50:54 +02:00
|
|
|
use actix_web::middleware::BodyEncoding;
|
2019-03-25 01:00:59 +01:00
|
|
|
use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse, Responder};
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-03-06 18:27:02 +01:00
|
|
|
use crate::range::HttpRange;
|
|
|
|
use crate::ChunkedReadFile;
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-03-25 21:02:37 +01:00
|
|
|
bitflags! {
|
|
|
|
pub(crate) struct Flags: u32 {
|
|
|
|
const ETAG = 0b00000001;
|
|
|
|
const LAST_MD = 0b00000010;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Flags {
|
|
|
|
fn default() -> Self {
|
|
|
|
Flags::all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 07:10:08 +01:00
|
|
|
/// A file with an associated name.
|
|
|
|
#[derive(Debug)]
|
2019-03-25 21:02:37 +01:00
|
|
|
pub struct NamedFile {
|
2019-03-06 07:10:08 +01:00
|
|
|
path: PathBuf,
|
|
|
|
file: File,
|
|
|
|
pub(crate) content_type: mime::Mime,
|
|
|
|
pub(crate) content_disposition: header::ContentDisposition,
|
|
|
|
pub(crate) md: Metadata,
|
|
|
|
modified: Option<SystemTime>,
|
2019-04-01 03:19:18 +02:00
|
|
|
pub(crate) encoding: Option<ContentEncoding>,
|
2019-03-06 07:10:08 +01:00
|
|
|
pub(crate) status_code: StatusCode,
|
2019-03-25 21:02:37 +01:00
|
|
|
pub(crate) flags: Flags,
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NamedFile {
|
|
|
|
/// Creates an instance from a previously opened file.
|
|
|
|
///
|
|
|
|
/// The given `path` need not exist and is only used to determine the `ContentType` and
|
|
|
|
/// `ContentDisposition` headers.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2019-03-25 01:00:59 +01:00
|
|
|
/// ```rust
|
|
|
|
/// use actix_files::NamedFile;
|
2019-03-06 07:10:08 +01:00
|
|
|
/// use std::io::{self, Write};
|
|
|
|
/// use std::env;
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// fn main() -> io::Result<()> {
|
|
|
|
/// let mut file = File::create("foo.txt")?;
|
|
|
|
/// file.write_all(b"Hello, world!")?;
|
|
|
|
/// let named_file = NamedFile::from_file(file, "bar.txt")?;
|
2019-05-05 04:51:00 +02:00
|
|
|
/// # std::fs::remove_file("foo.txt");
|
2019-03-06 07:10:08 +01:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn from_file<P: AsRef<Path>>(file: File, path: P) -> io::Result<NamedFile> {
|
|
|
|
let path = path.as_ref().to_path_buf();
|
|
|
|
|
|
|
|
// Get the name of the file and use it to construct default Content-Type
|
|
|
|
// and Content-Disposition values
|
|
|
|
let (content_type, content_disposition) = {
|
|
|
|
let filename = match path.file_name() {
|
|
|
|
Some(name) => name.to_string_lossy(),
|
|
|
|
None => {
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
"Provided path has no filename",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let ct = guess_mime_type(&path);
|
2019-03-25 21:02:37 +01:00
|
|
|
let disposition_type = match ct.type_() {
|
|
|
|
mime::IMAGE | mime::TEXT | mime::VIDEO => DispositionType::Inline,
|
|
|
|
_ => DispositionType::Attachment,
|
|
|
|
};
|
2019-03-06 07:10:08 +01:00
|
|
|
let cd = ContentDisposition {
|
|
|
|
disposition: disposition_type,
|
|
|
|
parameters: vec![DispositionParam::Filename(filename.into_owned())],
|
|
|
|
};
|
|
|
|
(ct, cd)
|
|
|
|
};
|
|
|
|
|
|
|
|
let md = file.metadata()?;
|
|
|
|
let modified = md.modified().ok();
|
|
|
|
let encoding = None;
|
|
|
|
Ok(NamedFile {
|
|
|
|
path,
|
|
|
|
file,
|
|
|
|
content_type,
|
|
|
|
content_disposition,
|
|
|
|
md,
|
|
|
|
modified,
|
|
|
|
encoding,
|
|
|
|
status_code: StatusCode::OK,
|
2019-03-25 21:02:37 +01:00
|
|
|
flags: Flags::default(),
|
2019-03-06 07:10:08 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:02:37 +01:00
|
|
|
/// Attempts to open a file in read-only mode.
|
2019-03-06 07:10:08 +01:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2019-03-25 01:00:59 +01:00
|
|
|
/// ```rust
|
2019-03-25 21:02:37 +01:00
|
|
|
/// use actix_files::NamedFile;
|
2019-03-06 07:10:08 +01:00
|
|
|
///
|
2019-03-25 21:02:37 +01:00
|
|
|
/// let file = NamedFile::open("foo.txt");
|
2019-03-06 07:10:08 +01:00
|
|
|
/// ```
|
2019-03-25 21:02:37 +01:00
|
|
|
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<NamedFile> {
|
|
|
|
Self::from_file(File::open(&path)?, path)
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to the underlying `File` object.
|
|
|
|
#[inline]
|
|
|
|
pub fn file(&self) -> &File {
|
|
|
|
&self.file
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the path of this file.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2019-03-25 01:00:59 +01:00
|
|
|
/// ```rust
|
2019-03-06 07:10:08 +01:00
|
|
|
/// # use std::io;
|
2019-03-25 01:00:59 +01:00
|
|
|
/// use actix_files::NamedFile;
|
2019-03-06 07:10:08 +01:00
|
|
|
///
|
|
|
|
/// # fn path() -> io::Result<()> {
|
|
|
|
/// let file = NamedFile::open("test.txt")?;
|
|
|
|
/// assert_eq!(file.path().as_os_str(), "foo.txt");
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn path(&self) -> &Path {
|
|
|
|
self.path.as_path()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set response **Status Code**
|
|
|
|
pub fn set_status_code(mut self, status: StatusCode) -> Self {
|
|
|
|
self.status_code = status;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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, mime_type: mime::Mime) -> Self {
|
|
|
|
self.content_type = mime_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, cd: header::ContentDisposition) -> Self {
|
|
|
|
self.content_disposition = cd;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set content encoding for serving this file
|
|
|
|
#[inline]
|
|
|
|
pub fn set_content_encoding(mut self, enc: ContentEncoding) -> Self {
|
|
|
|
self.encoding = Some(enc);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:02:37 +01:00
|
|
|
#[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_MD, value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-06 07:10:08 +01:00
|
|
|
pub(crate) fn etag(&self) -> Option<header::EntityTag> {
|
|
|
|
// This etag format is similar to Apache's.
|
|
|
|
self.modified.as_ref().map(|mtime| {
|
|
|
|
let ino = {
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
self.md.ino()
|
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let dur = mtime
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("modification time must be after epoch");
|
|
|
|
header::EntityTag::strong(format!(
|
|
|
|
"{:x}:{:x}:{:x}:{:x}",
|
|
|
|
ino,
|
|
|
|
self.md.len(),
|
|
|
|
dur.as_secs(),
|
|
|
|
dur.subsec_nanos()
|
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn last_modified(&self) -> Option<header::HttpDate> {
|
|
|
|
self.modified.map(|mtime| mtime.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:02:37 +01:00
|
|
|
impl Deref for NamedFile {
|
2019-03-06 07:10:08 +01:00
|
|
|
type Target = File;
|
|
|
|
|
|
|
|
fn deref(&self) -> &File {
|
|
|
|
&self.file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:02:37 +01:00
|
|
|
impl DerefMut for NamedFile {
|
2019-03-06 07:10:08 +01:00
|
|
|
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<&header::EntityTag>, req: &HttpRequest) -> bool {
|
|
|
|
match req.get_header::<header::IfMatch>() {
|
|
|
|
None | Some(header::IfMatch::Any) => true,
|
|
|
|
Some(header::IfMatch::Items(ref items)) => {
|
|
|
|
if let Some(some_etag) = etag {
|
|
|
|
for item in items {
|
|
|
|
if item.strong_eq(some_etag) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if `req` doesn't have an `If-None-Match` header matching `req`.
|
|
|
|
fn none_match(etag: Option<&header::EntityTag>, req: &HttpRequest) -> bool {
|
|
|
|
match req.get_header::<header::IfNoneMatch>() {
|
|
|
|
Some(header::IfNoneMatch::Any) => false,
|
|
|
|
Some(header::IfNoneMatch::Items(ref items)) => {
|
|
|
|
if let Some(some_etag) = etag {
|
|
|
|
for item in items {
|
|
|
|
if item.weak_eq(some_etag) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:02:37 +01:00
|
|
|
impl Responder for NamedFile {
|
2019-03-06 07:10:08 +01:00
|
|
|
type Error = Error;
|
|
|
|
type Future = Result<HttpResponse, Error>;
|
|
|
|
|
|
|
|
fn respond_to(self, req: &HttpRequest) -> Self::Future {
|
|
|
|
if self.status_code != StatusCode::OK {
|
|
|
|
let mut resp = HttpResponse::build(self.status_code);
|
|
|
|
resp.set(header::ContentType(self.content_type.clone()))
|
|
|
|
.header(
|
|
|
|
header::CONTENT_DISPOSITION,
|
|
|
|
self.content_disposition.to_string(),
|
|
|
|
);
|
2019-04-04 23:00:56 +02:00
|
|
|
if let Some(current_encoding) = self.encoding {
|
|
|
|
resp.encoding(current_encoding);
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
let reader = ChunkedReadFile {
|
|
|
|
size: self.md.len(),
|
|
|
|
offset: 0,
|
|
|
|
file: Some(self.file),
|
|
|
|
fut: None,
|
|
|
|
counter: 0,
|
|
|
|
};
|
|
|
|
return Ok(resp.streaming(reader));
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:02:37 +01:00
|
|
|
match req.method() {
|
|
|
|
&Method::HEAD | &Method::GET => (),
|
|
|
|
_ => {
|
|
|
|
return Ok(HttpResponse::MethodNotAllowed()
|
2019-03-25 21:43:02 +01:00
|
|
|
.header(header::CONTENT_TYPE, "text/plain")
|
|
|
|
.header(header::ALLOW, "GET, HEAD")
|
|
|
|
.body("This resource only supports GET and HEAD."));
|
2019-03-25 21:02:37 +01:00
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 21:43:02 +01:00
|
|
|
let etag = if self.flags.contains(Flags::ETAG) {
|
|
|
|
self.etag()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-03-25 21:02:37 +01:00
|
|
|
let last_modified = if self.flags.contains(Flags::LAST_MD) {
|
2019-03-06 07:10:08 +01:00
|
|
|
self.last_modified()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// check preconditions
|
|
|
|
let precondition_failed = if !any_match(etag.as_ref(), req) {
|
|
|
|
true
|
|
|
|
} else if let (Some(ref m), Some(header::IfUnmodifiedSince(ref since))) =
|
|
|
|
(last_modified, req.get_header())
|
|
|
|
{
|
|
|
|
m > since
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
// check last modified
|
|
|
|
let not_modified = if !none_match(etag.as_ref(), req) {
|
|
|
|
true
|
2019-04-07 00:02:02 +02:00
|
|
|
} else if req.headers().contains_key(&header::IF_NONE_MATCH) {
|
2019-03-06 07:10:08 +01:00
|
|
|
false
|
|
|
|
} else if let (Some(ref m), Some(header::IfModifiedSince(ref since))) =
|
|
|
|
(last_modified, req.get_header())
|
|
|
|
{
|
|
|
|
m <= since
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut resp = HttpResponse::build(self.status_code);
|
|
|
|
resp.set(header::ContentType(self.content_type.clone()))
|
|
|
|
.header(
|
|
|
|
header::CONTENT_DISPOSITION,
|
|
|
|
self.content_disposition.to_string(),
|
|
|
|
);
|
2019-03-27 19:29:31 +01:00
|
|
|
// default compressing
|
|
|
|
if let Some(current_encoding) = self.encoding {
|
|
|
|
resp.encoding(current_encoding);
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
|
|
|
resp.if_some(last_modified, |lm, resp| {
|
|
|
|
resp.set(header::LastModified(lm));
|
|
|
|
})
|
|
|
|
.if_some(etag, |etag, resp| {
|
|
|
|
resp.set(header::ETag(etag));
|
|
|
|
});
|
|
|
|
|
|
|
|
resp.header(header::ACCEPT_RANGES, "bytes");
|
|
|
|
|
|
|
|
let mut length = self.md.len();
|
|
|
|
let mut offset = 0;
|
|
|
|
|
|
|
|
// check for range header
|
2019-04-07 00:02:02 +02:00
|
|
|
if let Some(ranges) = req.headers().get(&header::RANGE) {
|
2019-03-06 07:10:08 +01:00
|
|
|
if let Ok(rangesheader) = ranges.to_str() {
|
|
|
|
if let Ok(rangesvec) = HttpRange::parse(rangesheader, length) {
|
|
|
|
length = rangesvec[0].length;
|
|
|
|
offset = rangesvec[0].start;
|
2019-03-27 19:29:31 +01:00
|
|
|
resp.encoding(ContentEncoding::Identity);
|
2019-03-06 07:10:08 +01:00
|
|
|
resp.header(
|
|
|
|
header::CONTENT_RANGE,
|
|
|
|
format!(
|
|
|
|
"bytes {}-{}/{}",
|
|
|
|
offset,
|
|
|
|
offset + length - 1,
|
|
|
|
self.md.len()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
resp.header(header::CONTENT_RANGE, format!("bytes */{}", length));
|
|
|
|
return Ok(resp.status(StatusCode::RANGE_NOT_SATISFIABLE).finish());
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return Ok(resp.status(StatusCode::BAD_REQUEST).finish());
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
resp.header(header::CONTENT_LENGTH, format!("{}", length));
|
|
|
|
|
|
|
|
if precondition_failed {
|
|
|
|
return Ok(resp.status(StatusCode::PRECONDITION_FAILED).finish());
|
|
|
|
} else if not_modified {
|
|
|
|
return Ok(resp.status(StatusCode::NOT_MODIFIED).finish());
|
|
|
|
}
|
|
|
|
|
|
|
|
if *req.method() == Method::HEAD {
|
|
|
|
Ok(resp.finish())
|
|
|
|
} else {
|
|
|
|
let reader = ChunkedReadFile {
|
|
|
|
offset,
|
|
|
|
size: length,
|
|
|
|
file: Some(self.file),
|
|
|
|
fut: None,
|
|
|
|
counter: 0,
|
|
|
|
};
|
|
|
|
if offset != 0 || length != self.md.len() {
|
|
|
|
return Ok(resp.status(StatusCode::PARTIAL_CONTENT).streaming(reader));
|
|
|
|
};
|
|
|
|
Ok(resp.streaming(reader))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|