2018-04-10 06:57:40 +02:00
|
|
|
//! Static files support
|
2017-10-16 10:19:23 +02:00
|
|
|
use std::fmt::Write;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::fs::{DirEntry, File, Metadata};
|
|
|
|
use std::io::{Read, Seek};
|
2017-12-04 01:57:25 +01:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2018-04-13 00:50:20 +02:00
|
|
|
use std::sync::Mutex;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
2018-04-14 04:46:14 +02:00
|
|
|
use std::{cmp, env, io};
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2018-03-06 09:43:25 +01:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
use bytes::{BufMut, Bytes, BytesMut};
|
|
|
|
use futures::{Async, Future, Poll, Stream};
|
|
|
|
use futures_cpupool::{CpuFuture, CpuPool};
|
2018-04-29 14:02:50 +02:00
|
|
|
use mime;
|
2018-04-29 18:09:08 +02:00
|
|
|
use mime_guess::{get_mime_type, guess_mime_type};
|
2018-01-13 20:33:42 +01:00
|
|
|
|
2018-03-08 02:40:13 +01:00
|
|
|
use error::Error;
|
2018-05-04 01:22:08 +02:00
|
|
|
use handler::{AsyncResult, Handler, Responder, RouteHandler, WrapHandler};
|
2018-04-14 01:02:01 +02:00
|
|
|
use header;
|
2018-03-31 08:07:33 +02:00
|
|
|
use http::{Method, StatusCode};
|
2018-03-06 09:43:25 +01:00
|
|
|
use httpmessage::HttpMessage;
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-24 08:25:32 +02:00
|
|
|
use httpresponse::HttpResponse;
|
2018-04-14 01:02:01 +02:00
|
|
|
use param::FromParam;
|
2017-12-04 01:57:25 +01:00
|
|
|
|
2018-04-14 04:46:14 +02:00
|
|
|
/// Env variable for default cpu pool size for `StaticFiles`
|
|
|
|
const ENV_CPU_POOL_VAR: &str = "ACTIX_FS_POOL";
|
|
|
|
|
2017-12-04 01:57:25 +01:00
|
|
|
/// A file with an associated name; responds with the Content-Type based on the
|
|
|
|
/// file extension.
|
|
|
|
#[derive(Debug)]
|
2018-03-06 09:43:25 +01:00
|
|
|
pub struct NamedFile {
|
|
|
|
path: PathBuf,
|
|
|
|
file: File,
|
|
|
|
md: Metadata,
|
|
|
|
modified: Option<SystemTime>,
|
2018-03-08 02:40:13 +01:00
|
|
|
cpu_pool: Option<CpuPool>,
|
2018-03-16 00:55:22 +01:00
|
|
|
only_get: bool,
|
2018-04-08 19:53:58 +02:00
|
|
|
status_code: StatusCode,
|
2018-03-06 09:43:25 +01:00
|
|
|
}
|
2017-12-04 01:57:25 +01:00
|
|
|
|
|
|
|
impl NamedFile {
|
|
|
|
/// Attempts to open a file in read-only mode.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use actix_web::fs::NamedFile;
|
|
|
|
///
|
|
|
|
/// let file = NamedFile::open("foo.txt");
|
|
|
|
/// ```
|
|
|
|
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<NamedFile> {
|
|
|
|
let file = File::open(path.as_ref())?;
|
2018-03-06 09:43:25 +01:00
|
|
|
let md = file.metadata()?;
|
|
|
|
let path = path.as_ref().to_path_buf();
|
|
|
|
let modified = md.modified().ok();
|
2018-03-08 02:40:13 +01:00
|
|
|
let cpu_pool = None;
|
2018-04-14 01:02:01 +02:00
|
|
|
Ok(NamedFile {
|
|
|
|
path,
|
|
|
|
file,
|
|
|
|
md,
|
|
|
|
modified,
|
|
|
|
cpu_pool,
|
|
|
|
only_get: false,
|
|
|
|
status_code: StatusCode::OK,
|
|
|
|
})
|
2018-03-16 00:55:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allow only GET and HEAD methods
|
|
|
|
#[inline]
|
|
|
|
pub fn only_get(mut self) -> Self {
|
|
|
|
self.only_get = true;
|
|
|
|
self
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns reference to the underlying `File` object.
|
|
|
|
#[inline]
|
|
|
|
pub fn file(&self) -> &File {
|
2018-03-06 09:43:25 +01:00
|
|
|
&self.file
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve the path of this file.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use std::io;
|
|
|
|
/// use actix_web::fs::NamedFile;
|
|
|
|
///
|
|
|
|
/// # 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 {
|
2018-03-06 09:43:25 +01:00
|
|
|
self.path.as_path()
|
|
|
|
}
|
|
|
|
|
2018-03-08 02:42:57 +01:00
|
|
|
/// Set `CpuPool` to use
|
2018-03-08 02:40:13 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn set_cpu_pool(mut self, cpu_pool: CpuPool) -> Self {
|
|
|
|
self.cpu_pool = Some(cpu_pool);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:53:58 +02:00
|
|
|
/// Set response **Status Code**
|
|
|
|
pub fn set_status_code(mut self, status: StatusCode) -> Self {
|
|
|
|
self.status_code = status;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-03-06 09:43:25 +01:00
|
|
|
fn etag(&self) -> Option<header::EntityTag> {
|
|
|
|
// This etag format is similar to Apache's.
|
|
|
|
self.modified.as_ref().map(|mtime| {
|
|
|
|
let ino = {
|
|
|
|
#[cfg(unix)]
|
2018-04-14 01:02:01 +02:00
|
|
|
{
|
|
|
|
self.md.ino()
|
|
|
|
}
|
2018-03-06 09:43:25 +01:00
|
|
|
#[cfg(not(unix))]
|
2018-04-14 01:02:01 +02:00
|
|
|
{
|
|
|
|
0
|
|
|
|
}
|
2018-03-06 09:43:25 +01:00
|
|
|
};
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
let dur = mtime
|
|
|
|
.duration_since(UNIX_EPOCH)
|
2018-03-06 09:43:25 +01:00
|
|
|
.expect("modification time must be after epoch");
|
2018-04-14 01:02:01 +02:00
|
|
|
header::EntityTag::strong(format!(
|
|
|
|
"{:x}:{:x}:{:x}:{:x}",
|
|
|
|
ino,
|
|
|
|
self.md.len(),
|
|
|
|
dur.as_secs(),
|
|
|
|
dur.subsec_nanos()
|
|
|
|
))
|
2018-03-06 09:43:25 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn last_modified(&self) -> Option<header::HttpDate> {
|
|
|
|
self.modified.map(|mtime| mtime.into())
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for NamedFile {
|
|
|
|
type Target = File;
|
|
|
|
|
|
|
|
fn deref(&self) -> &File {
|
2018-03-06 09:43:25 +01:00
|
|
|
&self.file
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for NamedFile {
|
|
|
|
fn deref_mut(&mut self) -> &mut File {
|
2018-03-06 09:43:25 +01:00
|
|
|
&mut self.file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if `req` has no `If-Match` header or one which matches `etag`.
|
2018-05-04 20:44:22 +02:00
|
|
|
fn any_match<S>(etag: Option<&header::EntityTag>, req: &HttpRequest<S>) -> bool {
|
2018-03-06 09:43:25 +01:00
|
|
|
match req.get_header::<header::IfMatch>() {
|
2018-03-08 02:40:13 +01:00
|
|
|
None | Some(header::IfMatch::Any) => true,
|
|
|
|
Some(header::IfMatch::Items(ref items)) => {
|
2018-03-06 09:43:25 +01:00
|
|
|
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`.
|
2018-05-04 20:44:22 +02:00
|
|
|
fn none_match<S>(etag: Option<&header::EntityTag>, req: &HttpRequest<S>) -> bool {
|
2018-03-06 09:43:25 +01:00
|
|
|
match req.get_header::<header::IfNoneMatch>() {
|
2018-03-08 02:40:13 +01:00
|
|
|
Some(header::IfNoneMatch::Any) => false,
|
|
|
|
Some(header::IfNoneMatch::Items(ref items)) => {
|
2018-03-06 09:43:25 +01:00
|
|
|
if let Some(some_etag) = etag {
|
|
|
|
for item in items {
|
|
|
|
if item.weak_eq(some_etag) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2018-03-08 02:40:13 +01:00
|
|
|
None => true,
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
impl Responder for NamedFile {
|
2017-12-04 01:57:25 +01:00
|
|
|
type Item = HttpResponse;
|
|
|
|
type Error = io::Error;
|
|
|
|
|
2018-05-04 20:44:22 +02:00
|
|
|
fn respond_to<S>(self, req: &HttpRequest<S>) -> Result<HttpResponse, io::Error> {
|
2018-04-10 19:57:53 +02:00
|
|
|
if self.status_code != StatusCode::OK {
|
|
|
|
let mut resp = HttpResponse::build(self.status_code);
|
|
|
|
resp.if_some(self.path().extension(), |ext, resp| {
|
2018-04-29 18:09:08 +02:00
|
|
|
resp.set(header::ContentType(get_mime_type(
|
|
|
|
&ext.to_string_lossy(),
|
|
|
|
)));
|
2018-04-27 09:49:55 +02:00
|
|
|
}).if_some(self.path().file_name(), |file_name, resp| {
|
2018-04-29 18:09:08 +02:00
|
|
|
let mime_type = guess_mime_type(self.path());
|
|
|
|
let inline_or_attachment = match mime_type.type_() {
|
|
|
|
mime::IMAGE | mime::TEXT => "inline",
|
|
|
|
_ => "attachment",
|
|
|
|
};
|
|
|
|
resp.header(
|
|
|
|
"Content-Disposition",
|
|
|
|
format!(
|
|
|
|
"{inline_or_attachment}; filename={filename}",
|
|
|
|
inline_or_attachment = inline_or_attachment,
|
|
|
|
filename = file_name.to_string_lossy()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2018-04-10 19:57:53 +02:00
|
|
|
let reader = ChunkedReadFile {
|
|
|
|
size: self.md.len(),
|
|
|
|
offset: 0,
|
2018-04-29 18:09:08 +02:00
|
|
|
cpu_pool: self.cpu_pool
|
|
|
|
.unwrap_or_else(|| req.cpu_pool().clone()),
|
2018-04-10 19:57:53 +02:00
|
|
|
file: Some(self.file),
|
|
|
|
fut: None,
|
|
|
|
};
|
2018-04-14 01:02:01 +02:00
|
|
|
return Ok(resp.streaming(reader));
|
2018-04-10 19:57:53 +02:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
if self.only_get && *req.method() != Method::GET && *req.method() != Method::HEAD
|
|
|
|
{
|
2018-03-31 08:07:33 +02:00
|
|
|
return Ok(HttpResponse::MethodNotAllowed()
|
2018-04-14 01:02:01 +02:00
|
|
|
.header(header::CONTENT_TYPE, "text/plain")
|
|
|
|
.header(header::ALLOW, "GET, HEAD")
|
|
|
|
.body("This resource only supports GET and HEAD."));
|
2018-03-06 09:43:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let etag = self.etag();
|
|
|
|
let last_modified = self.last_modified();
|
|
|
|
|
|
|
|
// check preconditions
|
2018-05-04 20:44:22 +02:00
|
|
|
let precondition_failed = if !any_match(etag.as_ref(), req) {
|
2018-03-06 09:43:25 +01:00
|
|
|
true
|
2018-03-08 02:40:13 +01:00
|
|
|
} else if let (Some(ref m), Some(header::IfUnmodifiedSince(ref since))) =
|
2018-03-06 09:43:25 +01:00
|
|
|
(last_modified, req.get_header())
|
|
|
|
{
|
|
|
|
m > since
|
2018-03-05 23:04:30 +01:00
|
|
|
} else {
|
2018-03-06 09:43:25 +01:00
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
// check last modified
|
2018-05-04 20:44:22 +02:00
|
|
|
let not_modified = if !none_match(etag.as_ref(), req) {
|
2018-03-06 09:43:25 +01:00
|
|
|
true
|
2018-03-08 02:40:13 +01:00
|
|
|
} else if let (Some(ref m), Some(header::IfModifiedSince(ref since))) =
|
2018-03-06 09:43:25 +01:00
|
|
|
(last_modified, req.get_header())
|
|
|
|
{
|
|
|
|
m <= since
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2018-04-08 19:53:58 +02:00
|
|
|
let mut resp = HttpResponse::build(self.status_code);
|
2018-03-06 09:43:25 +01:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
resp.if_some(self.path().extension(), |ext, resp| {
|
2018-04-29 18:09:08 +02:00
|
|
|
resp.set(header::ContentType(get_mime_type(
|
|
|
|
&ext.to_string_lossy(),
|
|
|
|
)));
|
2018-04-27 09:49:55 +02:00
|
|
|
}).if_some(self.path().file_name(), |file_name, resp| {
|
2018-04-29 18:09:08 +02:00
|
|
|
let mime_type = guess_mime_type(self.path());
|
|
|
|
let inline_or_attachment = match mime_type.type_() {
|
|
|
|
mime::IMAGE | mime::TEXT => "inline",
|
|
|
|
_ => "attachment",
|
|
|
|
};
|
|
|
|
resp.header(
|
|
|
|
"Content-Disposition",
|
|
|
|
format!(
|
|
|
|
"{inline_or_attachment}; filename={filename}",
|
|
|
|
inline_or_attachment = inline_or_attachment,
|
|
|
|
filename = file_name.to_string_lossy()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.if_some(last_modified, |lm, resp| {
|
2018-04-14 01:02:01 +02:00
|
|
|
resp.set(header::LastModified(lm));
|
2018-04-29 18:09:08 +02:00
|
|
|
})
|
|
|
|
.if_some(etag, |etag, resp| {
|
|
|
|
resp.set(header::ETag(etag));
|
|
|
|
});
|
2018-03-06 09:43:25 +01:00
|
|
|
|
|
|
|
if precondition_failed {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Ok(resp.status(StatusCode::PRECONDITION_FAILED).finish());
|
2018-03-06 09:43:25 +01:00
|
|
|
} else if not_modified {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Ok(resp.status(StatusCode::NOT_MODIFIED).finish());
|
2018-03-06 09:43:25 +01:00
|
|
|
}
|
|
|
|
|
2018-03-16 00:55:22 +01:00
|
|
|
if *req.method() == Method::HEAD {
|
2018-03-31 08:07:33 +02:00
|
|
|
Ok(resp.finish())
|
2018-03-16 00:55:22 +01:00
|
|
|
} else {
|
2018-03-08 02:40:13 +01:00
|
|
|
let reader = ChunkedReadFile {
|
|
|
|
size: self.md.len(),
|
|
|
|
offset: 0,
|
2018-04-29 18:09:08 +02:00
|
|
|
cpu_pool: self.cpu_pool
|
|
|
|
.unwrap_or_else(|| req.cpu_pool().clone()),
|
2018-03-08 02:40:13 +01:00
|
|
|
file: Some(self.file),
|
|
|
|
fut: None,
|
|
|
|
};
|
2018-03-31 08:07:33 +02:00
|
|
|
Ok(resp.streaming(reader))
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 02:40:13 +01:00
|
|
|
/// A helper created from a `std::fs::File` which reads the file
|
|
|
|
/// chunk-by-chunk on a `CpuPool`.
|
|
|
|
pub struct ChunkedReadFile {
|
|
|
|
size: u64,
|
|
|
|
offset: u64,
|
|
|
|
cpu_pool: CpuPool,
|
|
|
|
file: Option<File>,
|
|
|
|
fut: Option<CpuFuture<(File, Bytes), io::Error>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for ChunkedReadFile {
|
|
|
|
type Item = Bytes;
|
2018-04-14 01:02:01 +02:00
|
|
|
type Error = Error;
|
2018-03-08 02:40:13 +01:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Bytes>, Error> {
|
|
|
|
if self.fut.is_some() {
|
|
|
|
return match self.fut.as_mut().unwrap().poll()? {
|
|
|
|
Async::Ready((file, bytes)) => {
|
|
|
|
self.fut.take();
|
|
|
|
self.file = Some(file);
|
|
|
|
self.offset += bytes.len() as u64;
|
|
|
|
Ok(Async::Ready(Some(bytes)))
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2018-03-08 02:40:13 +01:00
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let size = self.size;
|
|
|
|
let offset = self.offset;
|
|
|
|
|
|
|
|
if size == offset {
|
|
|
|
Ok(Async::Ready(None))
|
|
|
|
} else {
|
|
|
|
let mut file = self.file.take().expect("Use after completion");
|
|
|
|
self.fut = Some(self.cpu_pool.spawn_fn(move || {
|
|
|
|
let max_bytes = cmp::min(size.saturating_sub(offset), 65_536) as usize;
|
|
|
|
let mut buf = BytesMut::with_capacity(max_bytes);
|
|
|
|
file.seek(io::SeekFrom::Start(offset))?;
|
2018-04-14 01:02:01 +02:00
|
|
|
let nbytes = file.read(unsafe { buf.bytes_mut() })?;
|
2018-03-08 02:40:13 +01:00
|
|
|
if nbytes == 0 {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(io::ErrorKind::UnexpectedEof.into());
|
2018-03-08 02:40:13 +01:00
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
unsafe { buf.advance_mut(nbytes) };
|
2018-03-08 02:40:13 +01:00
|
|
|
Ok((file, buf.freeze()))
|
|
|
|
}));
|
|
|
|
self.poll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 00:53:07 +02:00
|
|
|
type DirectoryRenderer<S> =
|
|
|
|
Fn(&Directory, &HttpRequest<S>) -> Result<HttpResponse, io::Error>;
|
|
|
|
|
2017-12-04 01:57:25 +01:00
|
|
|
/// A directory; responds with the generated directory listing.
|
|
|
|
#[derive(Debug)]
|
2018-04-14 01:02:01 +02:00
|
|
|
pub struct Directory {
|
2018-05-03 00:53:07 +02:00
|
|
|
pub base: PathBuf,
|
|
|
|
pub path: PathBuf,
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Directory {
|
|
|
|
pub fn new(base: PathBuf, path: PathBuf) -> Directory {
|
2018-04-29 18:09:08 +02:00
|
|
|
Directory { base, path }
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 00:53:07 +02:00
|
|
|
pub fn is_visible(&self, entry: &io::Result<DirEntry>) -> bool {
|
2017-12-04 01:57:25 +01:00
|
|
|
if let Ok(ref entry) = *entry {
|
|
|
|
if let Some(name) = entry.file_name().to_str() {
|
|
|
|
if name.starts_with('.') {
|
2018-04-14 01:02:01 +02:00
|
|
|
return false;
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Ok(ref md) = entry.metadata() {
|
|
|
|
let ft = md.file_type();
|
2018-04-14 01:02:01 +02:00
|
|
|
return ft.is_dir() || ft.is_file() || ft.is_symlink();
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 00:53:07 +02:00
|
|
|
fn directory_listing<S>(
|
|
|
|
dir: &Directory, req: &HttpRequest<S>,
|
|
|
|
) -> Result<HttpResponse, io::Error> {
|
|
|
|
let index_of = format!("Index of {}", req.path());
|
|
|
|
let mut body = String::new();
|
|
|
|
let base = Path::new(req.path());
|
|
|
|
|
|
|
|
for entry in dir.path.read_dir()? {
|
|
|
|
if dir.is_visible(&entry) {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
let p = match entry.path().strip_prefix(&dir.path) {
|
|
|
|
Ok(p) => base.join(p),
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
// show file url as relative to static path
|
|
|
|
let file_url = format!("{}", p.to_string_lossy());
|
|
|
|
|
|
|
|
// if file is a directory, add '/' to the end of the name
|
|
|
|
if let Ok(metadata) = entry.metadata() {
|
|
|
|
if metadata.is_dir() {
|
|
|
|
let _ = write!(
|
|
|
|
body,
|
|
|
|
"<li><a href=\"{}\">{}/</a></li>",
|
|
|
|
file_url,
|
|
|
|
entry.file_name().to_string_lossy()
|
|
|
|
);
|
2017-12-04 01:57:25 +01:00
|
|
|
} else {
|
2018-05-03 00:53:07 +02:00
|
|
|
let _ = write!(
|
|
|
|
body,
|
|
|
|
"<li><a href=\"{}\">{}</a></li>",
|
|
|
|
file_url,
|
|
|
|
entry.file_name().to_string_lossy()
|
|
|
|
);
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
2018-05-03 00:53:07 +02:00
|
|
|
} else {
|
|
|
|
continue;
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-03 00:53:07 +02:00
|
|
|
|
|
|
|
let html = format!(
|
|
|
|
"<html>\
|
|
|
|
<head><title>{}</title></head>\
|
|
|
|
<body><h1>{}</h1>\
|
|
|
|
<ul>\
|
|
|
|
{}\
|
|
|
|
</ul></body>\n</html>",
|
|
|
|
index_of, index_of, body
|
|
|
|
);
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type("text/html; charset=utf-8")
|
|
|
|
.body(html))
|
2017-12-04 01:57:25 +01:00
|
|
|
}
|
|
|
|
|
2017-10-16 10:19:23 +02:00
|
|
|
/// Static files handling
|
|
|
|
///
|
2018-03-31 09:16:55 +02:00
|
|
|
/// `StaticFile` handler must be registered with `App::handler()` method,
|
2018-01-03 00:23:31 +01:00
|
|
|
/// because `StaticFile` handler requires access sub-path information.
|
2017-10-16 10:19:23 +02:00
|
|
|
///
|
|
|
|
/// ```rust
|
2017-12-06 20:00:39 +01:00
|
|
|
/// # extern crate actix_web;
|
2018-03-31 09:16:55 +02:00
|
|
|
/// use actix_web::{fs, App};
|
2017-10-16 10:19:23 +02:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
/// let app = App::new()
|
2018-04-07 04:34:55 +02:00
|
|
|
/// .handler("/static", fs::StaticFiles::new("."))
|
2017-10-16 10:19:23 +02:00
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-11 13:05:13 +01:00
|
|
|
pub struct StaticFiles<S> {
|
2017-10-16 10:19:23 +02:00
|
|
|
directory: PathBuf,
|
|
|
|
accessible: bool,
|
2018-01-29 12:23:45 +01:00
|
|
|
index: Option<String>,
|
2017-12-04 01:58:31 +01:00
|
|
|
show_index: bool,
|
2018-03-08 02:40:13 +01:00
|
|
|
cpu_pool: CpuPool,
|
2018-03-11 13:05:13 +01:00
|
|
|
default: Box<RouteHandler<S>>,
|
2018-05-03 00:53:07 +02:00
|
|
|
renderer: Box<DirectoryRenderer<S>>,
|
2017-11-25 19:52:43 +01:00
|
|
|
_chunk_size: usize,
|
|
|
|
_follow_symlinks: bool,
|
2017-10-16 10:19:23 +02:00
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
lazy_static! {
|
2018-04-14 04:46:14 +02:00
|
|
|
static ref DEFAULT_CPUPOOL: Mutex<CpuPool> = {
|
|
|
|
let default = match env::var(ENV_CPU_POOL_VAR) {
|
|
|
|
Ok(val) => {
|
|
|
|
if let Ok(val) = val.parse() {
|
|
|
|
val
|
|
|
|
} else {
|
|
|
|
error!("Can not parse ACTIX_FS_POOL value");
|
|
|
|
20
|
|
|
|
}
|
2018-04-17 21:55:13 +02:00
|
|
|
}
|
2018-04-14 04:46:14 +02:00
|
|
|
Err(_) => 20,
|
|
|
|
};
|
|
|
|
Mutex::new(CpuPool::new(default))
|
|
|
|
};
|
2018-04-13 00:50:20 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 13:05:13 +01:00
|
|
|
impl<S: 'static> StaticFiles<S> {
|
2018-04-07 04:34:55 +02:00
|
|
|
/// Create new `StaticFiles` instance for specified base directory.
|
2018-04-14 04:46:14 +02:00
|
|
|
///
|
|
|
|
/// `StaticFile` uses `CpuPool` for blocking filesystem operations.
|
|
|
|
/// By default pool with 20 threads is used.
|
|
|
|
/// Pool size can be changed by setting ACTIX_FS_POOL environment variable.
|
2018-04-07 04:34:55 +02:00
|
|
|
pub fn new<T: Into<PathBuf>>(dir: T) -> StaticFiles<S> {
|
2018-04-14 04:46:14 +02:00
|
|
|
// use default CpuPool
|
|
|
|
let pool = { DEFAULT_CPUPOOL.lock().unwrap().clone() };
|
|
|
|
|
|
|
|
StaticFiles::with_pool(dir, pool)
|
|
|
|
}
|
|
|
|
|
2018-04-17 21:55:13 +02:00
|
|
|
/// Create new `StaticFiles` instance for specified base directory and
|
|
|
|
/// `CpuPool`.
|
2018-04-14 04:46:14 +02:00
|
|
|
pub fn with_pool<T: Into<PathBuf>>(dir: T, pool: CpuPool) -> StaticFiles<S> {
|
2017-10-16 10:19:23 +02:00
|
|
|
let dir = dir.into();
|
|
|
|
|
2017-10-22 07:59:09 +02:00
|
|
|
let (dir, access) = match dir.canonicalize() {
|
|
|
|
Ok(dir) => {
|
|
|
|
if dir.is_dir() {
|
|
|
|
(dir, true)
|
|
|
|
} else {
|
|
|
|
warn!("Is not directory `{:?}`", dir);
|
|
|
|
(dir, false)
|
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2017-10-22 07:59:09 +02:00
|
|
|
Err(err) => {
|
|
|
|
warn!("Static files directory `{:?}` error: {}", dir, err);
|
2017-10-16 10:19:23 +02:00
|
|
|
(dir, false)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
StaticFiles {
|
|
|
|
directory: dir,
|
|
|
|
accessible: access,
|
2018-01-29 12:23:45 +01:00
|
|
|
index: None,
|
2018-04-07 04:34:55 +02:00
|
|
|
show_index: false,
|
2018-04-13 00:50:20 +02:00
|
|
|
cpu_pool: pool,
|
2018-04-14 01:02:01 +02:00
|
|
|
default: Box::new(WrapHandler::new(|_| {
|
|
|
|
HttpResponse::new(StatusCode::NOT_FOUND)
|
|
|
|
})),
|
2018-05-03 00:53:07 +02:00
|
|
|
renderer: Box::new(directory_listing),
|
2017-11-25 19:52:43 +01:00
|
|
|
_chunk_size: 0,
|
|
|
|
_follow_symlinks: false,
|
2017-10-16 10:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 04:34:55 +02:00
|
|
|
/// Show files listing for directories.
|
|
|
|
///
|
|
|
|
/// By default show files listing is disabled.
|
|
|
|
pub fn show_files_listing(mut self) -> Self {
|
|
|
|
self.show_index = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-05-03 00:53:07 +02:00
|
|
|
/// Set custom directory renderer
|
|
|
|
pub fn files_listing_renderer<F>(mut self, f: F) -> Self
|
|
|
|
where
|
|
|
|
for<'r, 's> F: Fn(&'r Directory, &'s HttpRequest<S>)
|
|
|
|
-> Result<HttpResponse, io::Error>
|
|
|
|
+ 'static,
|
|
|
|
{
|
|
|
|
self.renderer = Box::new(f);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-01-29 12:23:45 +01:00
|
|
|
/// Set index file
|
|
|
|
///
|
|
|
|
/// Redirects to specific index file for directory "/" instead of
|
|
|
|
/// showing files listing.
|
2018-03-11 13:05:13 +01:00
|
|
|
pub fn index_file<T: Into<String>>(mut self, index: T) -> StaticFiles<S> {
|
2018-01-29 12:23:45 +01:00
|
|
|
self.index = Some(index.into());
|
|
|
|
self
|
|
|
|
}
|
2018-03-11 13:05:13 +01:00
|
|
|
|
2018-03-29 19:44:26 +02:00
|
|
|
/// Sets default handler which is used when no matched file could be found.
|
2018-03-11 13:05:13 +01:00
|
|
|
pub fn default_handler<H: Handler<S>>(mut self, handler: H) -> StaticFiles<S> {
|
|
|
|
self.default = Box::new(WrapHandler::new(handler));
|
|
|
|
self
|
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 13:05:13 +01:00
|
|
|
impl<S: 'static> Handler<S> for StaticFiles<S> {
|
2018-05-04 01:22:08 +02:00
|
|
|
type Result = Result<AsyncResult<HttpResponse>, Error>;
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2017-12-26 18:00:45 +01:00
|
|
|
fn handle(&mut self, req: HttpRequest<S>) -> Self::Result {
|
2017-10-16 10:19:23 +02:00
|
|
|
if !self.accessible {
|
2018-03-16 20:12:55 +01:00
|
|
|
Ok(self.default.handle(req))
|
2017-10-16 10:19:23 +02:00
|
|
|
} else {
|
2018-04-29 18:09:08 +02:00
|
|
|
let relpath = match req.match_info()
|
|
|
|
.get("tail")
|
2018-04-30 05:34:59 +02:00
|
|
|
.map(|tail| PathBuf::from_param(tail.trim_left_matches('/')))
|
2018-04-29 18:09:08 +02:00
|
|
|
{
|
|
|
|
Some(Ok(path)) => path,
|
|
|
|
_ => return Ok(self.default.handle(req)),
|
|
|
|
};
|
2017-12-08 21:29:28 +01:00
|
|
|
|
2017-10-16 10:19:23 +02:00
|
|
|
// full filepath
|
2017-12-04 01:57:25 +01:00
|
|
|
let path = self.directory.join(&relpath).canonicalize()?;
|
2017-10-16 10:19:23 +02:00
|
|
|
|
2017-12-04 01:57:25 +01:00
|
|
|
if path.is_dir() {
|
2018-01-29 12:23:45 +01:00
|
|
|
if let Some(ref redir_index) = self.index {
|
2018-02-07 22:31:09 +01:00
|
|
|
// TODO: Don't redirect, just return the index content.
|
2018-04-14 01:02:01 +02:00
|
|
|
// TODO: It'd be nice if there were a good usable URL manipulation
|
|
|
|
// library
|
2018-02-07 22:31:09 +01:00
|
|
|
let mut new_path: String = req.path().to_owned();
|
|
|
|
for el in relpath.iter() {
|
|
|
|
new_path.push_str(&el.to_string_lossy());
|
|
|
|
new_path.push('/');
|
|
|
|
}
|
2018-04-10 19:13:52 +02:00
|
|
|
if !new_path.ends_with('/') {
|
|
|
|
new_path.push('/');
|
|
|
|
}
|
2018-02-07 22:31:09 +01:00
|
|
|
new_path.push_str(redir_index);
|
2018-03-31 08:07:33 +02:00
|
|
|
HttpResponse::Found()
|
2018-03-31 02:31:18 +02:00
|
|
|
.header(header::LOCATION, new_path.as_str())
|
2018-03-31 08:07:33 +02:00
|
|
|
.finish()
|
2018-05-04 20:44:22 +02:00
|
|
|
.respond_to(&req)
|
2018-01-29 12:23:45 +01:00
|
|
|
} else if self.show_index {
|
2018-05-03 00:53:07 +02:00
|
|
|
let dir = Directory::new(self.directory.clone(), path);
|
|
|
|
Ok((*self.renderer)(&dir, &req)?.into())
|
2017-12-04 01:58:31 +01:00
|
|
|
} else {
|
2018-03-11 13:05:13 +01:00
|
|
|
Ok(self.default.handle(req))
|
2017-12-04 01:58:31 +01:00
|
|
|
}
|
2017-10-16 10:19:23 +02:00
|
|
|
} else {
|
2018-04-14 01:02:01 +02:00
|
|
|
NamedFile::open(path)?
|
|
|
|
.set_cpu_pool(self.cpu_pool.clone())
|
2018-05-04 20:44:22 +02:00
|
|
|
.respond_to(&req)?
|
|
|
|
.respond_to(&req)
|
2017-10-16 10:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-04 03:15:09 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-04-10 19:13:52 +02:00
|
|
|
use application::App;
|
2018-03-06 09:43:25 +01:00
|
|
|
use http::{header, Method, StatusCode};
|
2018-04-14 01:02:01 +02:00
|
|
|
use test::{self, TestRequest};
|
2017-12-04 03:15:09 +01:00
|
|
|
|
|
|
|
#[test]
|
2018-04-29 14:02:50 +02:00
|
|
|
fn test_named_file_text() {
|
2017-12-04 03:15:09 +01:00
|
|
|
assert!(NamedFile::open("test--").is_err());
|
2018-04-29 18:09:08 +02:00
|
|
|
let mut file = NamedFile::open("Cargo.toml")
|
|
|
|
.unwrap()
|
|
|
|
.set_cpu_pool(CpuPool::new(1));
|
2018-04-14 01:02:01 +02:00
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2017-12-04 03:15:09 +01:00
|
|
|
|
2017-12-14 18:43:42 +01:00
|
|
|
let resp = file.respond_to(HttpRequest::default()).unwrap();
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
2018-04-29 18:09:08 +02:00
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"text/x-toml"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers()
|
|
|
|
.get(header::CONTENT_DISPOSITION)
|
|
|
|
.unwrap(),
|
2018-04-29 14:02:50 +02:00
|
|
|
"inline; filename=Cargo.toml"
|
2018-04-27 09:49:55 +02:00
|
|
|
);
|
2018-04-29 14:02:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_named_file_image() {
|
2018-04-29 18:09:08 +02:00
|
|
|
let mut file = NamedFile::open("tests/test.png")
|
|
|
|
.unwrap()
|
|
|
|
.set_cpu_pool(CpuPool::new(1));
|
2018-04-29 14:02:50 +02:00
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
|
|
|
|
|
|
|
let resp = file.respond_to(HttpRequest::default()).unwrap();
|
2018-04-27 09:49:55 +02:00
|
|
|
assert_eq!(
|
2018-04-29 18:09:08 +02:00
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"image/png"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers()
|
|
|
|
.get(header::CONTENT_DISPOSITION)
|
|
|
|
.unwrap(),
|
2018-04-29 14:02:50 +02:00
|
|
|
"inline; filename=test.png"
|
2018-04-27 09:49:55 +02:00
|
|
|
);
|
2017-12-04 03:15:09 +01:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:53:58 +02:00
|
|
|
#[test]
|
2018-04-29 14:02:50 +02:00
|
|
|
fn test_named_file_binary() {
|
2018-04-29 18:09:08 +02:00
|
|
|
let mut file = NamedFile::open("tests/test.binary")
|
|
|
|
.unwrap()
|
|
|
|
.set_cpu_pool(CpuPool::new(1));
|
2018-04-29 14:02:50 +02:00
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
|
|
|
|
|
|
|
let resp = file.respond_to(HttpRequest::default()).unwrap();
|
|
|
|
assert_eq!(
|
2018-04-29 18:09:08 +02:00
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"application/octet-stream"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers()
|
|
|
|
.get(header::CONTENT_DISPOSITION)
|
|
|
|
.unwrap(),
|
2018-04-29 14:02:50 +02:00
|
|
|
"attachment; filename=test.binary"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_named_file_status_code_text() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut file = NamedFile::open("Cargo.toml")
|
|
|
|
.unwrap()
|
2018-04-08 19:53:58 +02:00
|
|
|
.set_status_code(StatusCode::NOT_FOUND)
|
|
|
|
.set_cpu_pool(CpuPool::new(1));
|
2018-04-14 01:02:01 +02:00
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2018-04-08 19:53:58 +02:00
|
|
|
|
|
|
|
let resp = file.respond_to(HttpRequest::default()).unwrap();
|
2018-04-27 09:49:55 +02:00
|
|
|
assert_eq!(
|
2018-04-29 18:09:08 +02:00
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"text/x-toml"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers()
|
|
|
|
.get(header::CONTENT_DISPOSITION)
|
|
|
|
.unwrap(),
|
2018-04-29 14:02:50 +02:00
|
|
|
"inline; filename=Cargo.toml"
|
2018-04-27 09:49:55 +02:00
|
|
|
);
|
2018-04-08 19:53:58 +02:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2018-03-06 09:43:25 +01:00
|
|
|
#[test]
|
|
|
|
fn test_named_file_not_allowed() {
|
|
|
|
let req = TestRequest::default().method(Method::POST).finish();
|
|
|
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
|
2018-03-16 00:55:22 +01:00
|
|
|
let resp = file.only_get().respond_to(req).unwrap();
|
2018-03-06 09:43:25 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
|
|
|
}
|
|
|
|
|
2018-03-16 00:55:22 +01:00
|
|
|
#[test]
|
|
|
|
fn test_named_file_any_method() {
|
|
|
|
let req = TestRequest::default().method(Method::POST).finish();
|
|
|
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
let resp = file.respond_to(req).unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
2017-12-04 03:15:09 +01:00
|
|
|
#[test]
|
|
|
|
fn test_static_files() {
|
2018-04-07 04:34:55 +02:00
|
|
|
let mut st = StaticFiles::new(".").show_files_listing();
|
2017-12-04 03:15:09 +01:00
|
|
|
st.accessible = false;
|
2018-04-14 01:02:01 +02:00
|
|
|
let resp = st.handle(HttpRequest::default())
|
|
|
|
.respond_to(HttpRequest::default())
|
|
|
|
.unwrap();
|
2018-05-02 02:19:15 +02:00
|
|
|
let resp = resp.as_msg();
|
2018-03-11 13:05:13 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2017-12-04 03:15:09 +01:00
|
|
|
|
|
|
|
st.accessible = true;
|
|
|
|
st.show_index = false;
|
2018-04-14 01:02:01 +02:00
|
|
|
let resp = st.handle(HttpRequest::default())
|
|
|
|
.respond_to(HttpRequest::default())
|
|
|
|
.unwrap();
|
2018-05-02 02:19:15 +02:00
|
|
|
let resp = resp.as_msg();
|
2018-03-11 13:05:13 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2017-12-04 03:15:09 +01:00
|
|
|
|
2017-12-08 21:29:28 +01:00
|
|
|
let mut req = HttpRequest::default();
|
|
|
|
req.match_info_mut().add("tail", "");
|
|
|
|
|
2017-12-04 03:15:09 +01:00
|
|
|
st.show_index = true;
|
2018-04-29 18:09:08 +02:00
|
|
|
let resp = st.handle(req)
|
|
|
|
.respond_to(HttpRequest::default())
|
|
|
|
.unwrap();
|
2018-05-02 02:19:15 +02:00
|
|
|
let resp = resp.as_msg();
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"text/html; charset=utf-8"
|
|
|
|
);
|
2017-12-04 03:15:09 +01:00
|
|
|
assert!(resp.body().is_binary());
|
|
|
|
assert!(format!("{:?}", resp.body()).contains("README.md"));
|
|
|
|
}
|
2018-01-29 12:23:45 +01:00
|
|
|
|
|
|
|
#[test]
|
2018-02-07 22:31:09 +01:00
|
|
|
fn test_redirect_to_index() {
|
2018-04-07 04:34:55 +02:00
|
|
|
let mut st = StaticFiles::new(".").index_file("index.html");
|
2018-01-29 12:23:45 +01:00
|
|
|
let mut req = HttpRequest::default();
|
2018-04-14 02:00:18 +02:00
|
|
|
req.match_info_mut().add("tail", "tests");
|
2018-01-29 12:23:45 +01:00
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
let resp = st.handle(req)
|
|
|
|
.respond_to(HttpRequest::default())
|
|
|
|
.unwrap();
|
2018-05-02 02:19:15 +02:00
|
|
|
let resp = resp.as_msg();
|
2018-01-29 12:23:45 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::FOUND);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::LOCATION).unwrap(),
|
|
|
|
"/tests/index.html"
|
|
|
|
);
|
2018-02-07 22:31:09 +01:00
|
|
|
|
|
|
|
let mut req = HttpRequest::default();
|
2018-04-14 02:00:18 +02:00
|
|
|
req.match_info_mut().add("tail", "tests/");
|
2018-02-07 22:31:09 +01:00
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
let resp = st.handle(req)
|
|
|
|
.respond_to(HttpRequest::default())
|
|
|
|
.unwrap();
|
2018-05-02 02:19:15 +02:00
|
|
|
let resp = resp.as_msg();
|
2018-02-07 22:31:09 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::FOUND);
|
2018-04-29 18:09:08 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::LOCATION).unwrap(),
|
|
|
|
"/tests/index.html"
|
|
|
|
);
|
2018-02-07 22:31:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_redirect_to_index_nested() {
|
2018-04-07 04:34:55 +02:00
|
|
|
let mut st = StaticFiles::new(".").index_file("Cargo.toml");
|
2018-02-07 22:31:09 +01:00
|
|
|
let mut req = HttpRequest::default();
|
2018-04-13 06:28:17 +02:00
|
|
|
req.match_info_mut().add("tail", "tools/wsload");
|
2018-02-07 22:31:09 +01:00
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
let resp = st.handle(req)
|
|
|
|
.respond_to(HttpRequest::default())
|
|
|
|
.unwrap();
|
2018-05-02 02:19:15 +02:00
|
|
|
let resp = resp.as_msg();
|
2018-02-07 22:31:09 +01:00
|
|
|
assert_eq!(resp.status(), StatusCode::FOUND);
|
2018-04-14 01:02:01 +02:00
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::LOCATION).unwrap(),
|
|
|
|
"/tools/wsload/Cargo.toml"
|
|
|
|
);
|
2018-01-29 12:23:45 +01:00
|
|
|
}
|
2018-04-10 19:13:52 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn integration_redirect_to_index_with_prefix() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::with_factory(|| {
|
|
|
|
App::new()
|
2018-04-10 19:13:52 +02:00
|
|
|
.prefix("public")
|
2018-04-14 01:02:01 +02:00
|
|
|
.handler("/", StaticFiles::new(".").index_file("Cargo.toml"))
|
|
|
|
});
|
2018-04-10 19:13:52 +02:00
|
|
|
|
|
|
|
let request = srv.get().uri(srv.url("/public")).finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::FOUND);
|
2018-04-29 18:09:08 +02:00
|
|
|
let loc = response
|
|
|
|
.headers()
|
|
|
|
.get(header::LOCATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
2018-04-10 19:13:52 +02:00
|
|
|
assert_eq!(loc, "/public/Cargo.toml");
|
|
|
|
|
|
|
|
let request = srv.get().uri(srv.url("/public/")).finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::FOUND);
|
2018-04-29 18:09:08 +02:00
|
|
|
let loc = response
|
|
|
|
.headers()
|
|
|
|
.get(header::LOCATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
2018-04-10 19:13:52 +02:00
|
|
|
assert_eq!(loc, "/public/Cargo.toml");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn integration_redirect_to_index() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::with_factory(|| {
|
|
|
|
App::new().handler("test", StaticFiles::new(".").index_file("Cargo.toml"))
|
|
|
|
});
|
2018-04-10 19:13:52 +02:00
|
|
|
|
|
|
|
let request = srv.get().uri(srv.url("/test")).finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::FOUND);
|
2018-04-29 18:09:08 +02:00
|
|
|
let loc = response
|
|
|
|
.headers()
|
|
|
|
.get(header::LOCATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
2018-04-10 19:13:52 +02:00
|
|
|
assert_eq!(loc, "/test/Cargo.toml");
|
|
|
|
|
|
|
|
let request = srv.get().uri(srv.url("/test/")).finish().unwrap();
|
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::FOUND);
|
2018-04-29 18:09:08 +02:00
|
|
|
let loc = response
|
|
|
|
.headers()
|
|
|
|
.get(header::LOCATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
2018-04-10 19:13:52 +02:00
|
|
|
assert_eq!(loc, "/test/Cargo.toml");
|
|
|
|
}
|
2018-04-13 19:13:12 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn integration_percent_encoded() {
|
2018-04-14 01:02:01 +02:00
|
|
|
let mut srv = test::TestServer::with_factory(|| {
|
|
|
|
App::new().handler("test", StaticFiles::new(".").index_file("Cargo.toml"))
|
|
|
|
});
|
|
|
|
|
2018-04-29 18:09:08 +02:00
|
|
|
let request = srv.get()
|
|
|
|
.uri(srv.url("/test/%43argo.toml"))
|
|
|
|
.finish()
|
|
|
|
.unwrap();
|
2018-04-13 19:13:12 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
}
|
2017-12-04 03:15:09 +01:00
|
|
|
}
|