2020-10-06 23:08:33 +02:00
|
|
|
//! Static file serving for Actix Web.
|
2020-09-21 00:18:25 +02:00
|
|
|
//!
|
|
|
|
//! Provides a non-blocking service for serving static files from disk.
|
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//! ```rust
|
|
|
|
//! use actix_web::App;
|
|
|
|
//! use actix_files::Files;
|
|
|
|
//!
|
|
|
|
//! let app = App::new()
|
2020-10-06 22:56:28 +02:00
|
|
|
//! .service(Files::new("/static", ".").prefer_utf8(true));
|
2020-09-21 00:18:25 +02:00
|
|
|
//! ```
|
2020-09-10 15:46:35 +02:00
|
|
|
|
|
|
|
#![deny(rust_2018_idioms)]
|
2020-09-21 00:18:25 +02:00
|
|
|
#![warn(missing_docs, missing_debug_implementations)]
|
|
|
|
|
|
|
|
use actix_service::boxed::{BoxService, BoxServiceFactory};
|
|
|
|
use actix_web::{
|
|
|
|
dev::{ServiceRequest, ServiceResponse},
|
2021-01-09 17:04:19 +01:00
|
|
|
error::Error,
|
2020-09-21 00:18:25 +02:00
|
|
|
http::header::DispositionType,
|
2019-04-01 03:19:18 +02:00
|
|
|
};
|
2019-08-11 22:43:29 +02:00
|
|
|
use mime_guess::from_ext;
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2020-09-21 00:18:25 +02:00
|
|
|
mod chunked;
|
|
|
|
mod directory;
|
2020-10-06 22:56:28 +02:00
|
|
|
mod encoding;
|
2019-03-06 07:10:08 +01:00
|
|
|
mod error;
|
2020-09-21 00:18:25 +02:00
|
|
|
mod files;
|
2019-03-06 07:10:08 +01:00
|
|
|
mod named;
|
2020-09-21 00:18:25 +02:00
|
|
|
mod path_buf;
|
2019-03-06 18:27:02 +01:00
|
|
|
mod range;
|
2020-09-21 00:18:25 +02:00
|
|
|
mod service;
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2020-09-21 00:18:25 +02:00
|
|
|
pub use crate::chunked::ChunkedReadFile;
|
|
|
|
pub use crate::directory::Directory;
|
|
|
|
pub use crate::files::Files;
|
2019-03-06 07:10:08 +01:00
|
|
|
pub use crate::named::NamedFile;
|
2019-03-06 18:27:02 +01:00
|
|
|
pub use crate::range::HttpRange;
|
2020-09-21 00:18:25 +02:00
|
|
|
pub use crate::service::FilesService;
|
|
|
|
|
|
|
|
use self::directory::{directory_listing, DirectoryRenderer};
|
|
|
|
use self::error::FilesError;
|
|
|
|
use self::path_buf::PathBufWrap;
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
|
|
|
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
2019-03-06 07:10:08 +01:00
|
|
|
|
|
|
|
/// Return the MIME type associated with a filename extension (case-insensitive).
|
|
|
|
/// If `ext` is empty or no associated type for the extension was found, returns
|
|
|
|
/// the type `application/octet-stream`.
|
|
|
|
#[inline]
|
|
|
|
pub fn file_extension_to_mime(ext: &str) -> mime::Mime {
|
2019-08-11 22:43:29 +02:00
|
|
|
from_ext(ext).first_or_octet_stream()
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2020-09-10 15:46:35 +02:00
|
|
|
type MimeOverride = dyn Fn(&mime::Name<'_>) -> DispositionType;
|
2019-03-25 21:02:37 +01:00
|
|
|
|
2019-03-06 07:10:08 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-09-21 00:18:25 +02:00
|
|
|
use std::{
|
|
|
|
fs::{self, File},
|
|
|
|
ops::Add,
|
|
|
|
time::{Duration, SystemTime},
|
|
|
|
};
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2020-09-21 00:18:25 +02:00
|
|
|
use actix_service::ServiceFactory;
|
|
|
|
use actix_web::{
|
|
|
|
guard,
|
|
|
|
http::{
|
|
|
|
header::{self, ContentDisposition, DispositionParam, DispositionType},
|
|
|
|
Method, StatusCode,
|
|
|
|
},
|
|
|
|
middleware::Compress,
|
|
|
|
test::{self, TestRequest},
|
|
|
|
web, App, HttpResponse, Responder,
|
2019-04-01 19:26:09 +02:00
|
|
|
};
|
2020-09-21 00:18:25 +02:00
|
|
|
use futures_util::future::ok;
|
|
|
|
|
|
|
|
use super::*;
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_file_extension_to_mime() {
|
2020-10-06 22:56:28 +02:00
|
|
|
let m = file_extension_to_mime("");
|
|
|
|
assert_eq!(m, mime::APPLICATION_OCTET_STREAM);
|
|
|
|
|
2019-03-06 07:10:08 +01:00
|
|
|
let m = file_extension_to_mime("jpg");
|
|
|
|
assert_eq!(m, mime::IMAGE_JPEG);
|
|
|
|
|
|
|
|
let m = file_extension_to_mime("invalid extension!!");
|
|
|
|
assert_eq!(m, mime::APPLICATION_OCTET_STREAM);
|
|
|
|
|
|
|
|
let m = file_extension_to_mime("");
|
|
|
|
assert_eq!(m, mime::APPLICATION_OCTET_STREAM);
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_if_modified_since_without_if_none_match() {
|
|
|
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
let since =
|
|
|
|
header::HttpDate::from(SystemTime::now().add(Duration::from_secs(60)));
|
|
|
|
|
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(header::IF_MODIFIED_SINCE, since)
|
|
|
|
.to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_MODIFIED);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 19:18:23 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_if_modified_since_without_if_none_match_same() {
|
|
|
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
let since = file.last_modified().unwrap();
|
|
|
|
|
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(header::IF_MODIFIED_SINCE, since)
|
|
|
|
.to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_MODIFIED);
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_if_modified_since_with_if_none_match() {
|
|
|
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
let since =
|
|
|
|
header::HttpDate::from(SystemTime::now().add(Duration::from_secs(60)));
|
|
|
|
|
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(header::IF_NONE_MATCH, "miss_etag")
|
|
|
|
.header(header::IF_MODIFIED_SINCE, since)
|
|
|
|
.to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_ne!(resp.status(), StatusCode::NOT_MODIFIED);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 19:18:23 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_if_unmodified_since() {
|
|
|
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
let since = file.last_modified().unwrap();
|
|
|
|
|
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(header::IF_UNMODIFIED_SINCE, since)
|
|
|
|
.to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_if_unmodified_since_failed() {
|
|
|
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
let since = header::HttpDate::from(SystemTime::UNIX_EPOCH);
|
|
|
|
|
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(header::IF_UNMODIFIED_SINCE, since)
|
|
|
|
.to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_text() {
|
|
|
|
assert!(NamedFile::open("test--").is_err());
|
|
|
|
let mut file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"text/x-toml"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
"inline; filename=\"Cargo.toml\""
|
|
|
|
);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_content_disposition() {
|
|
|
|
assert!(NamedFile::open("test--").is_err());
|
|
|
|
let mut file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2019-07-20 07:43:49 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
"inline; filename=\"Cargo.toml\""
|
|
|
|
);
|
2019-07-20 07:43:49 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let file = NamedFile::open("Cargo.toml")
|
|
|
|
.unwrap()
|
|
|
|
.disable_content_disposition();
|
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert!(resp.headers().get(header::CONTENT_DISPOSITION).is_none());
|
2019-07-20 07:43:49 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_non_ascii_file_name() {
|
|
|
|
let mut file =
|
|
|
|
NamedFile::from_file(File::open("Cargo.toml").unwrap(), "貨物.toml")
|
|
|
|
.unwrap();
|
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2019-11-06 15:08:37 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"text/x-toml"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-11-06 15:08:37 +01:00
|
|
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
"inline; filename=\"貨物.toml\"; filename*=UTF-8''%E8%B2%A8%E7%89%A9.toml"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_set_content_type() {
|
|
|
|
let mut file = NamedFile::open("Cargo.toml")
|
|
|
|
.unwrap()
|
|
|
|
.set_content_type(mime::TEXT_XML);
|
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"text/xml"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
"inline; filename=\"Cargo.toml\""
|
|
|
|
);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_image() {
|
|
|
|
let mut file = NamedFile::open("tests/test.png").unwrap();
|
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"image/png"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
"inline; filename=\"test.png\""
|
|
|
|
);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_image_attachment() {
|
|
|
|
let cd = ContentDisposition {
|
|
|
|
disposition: DispositionType::Attachment,
|
|
|
|
parameters: vec![DispositionParam::Filename(String::from("test.png"))],
|
|
|
|
};
|
|
|
|
let mut file = NamedFile::open("tests/test.png")
|
|
|
|
.unwrap()
|
|
|
|
.set_content_disposition(cd);
|
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"image/png"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
"attachment; filename=\"test.png\""
|
|
|
|
);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_binary() {
|
|
|
|
let mut file = NamedFile::open("tests/test.binary").unwrap();
|
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"application/octet-stream"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
"attachment; filename=\"test.binary\""
|
|
|
|
);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_status_code_text() {
|
|
|
|
let mut file = NamedFile::open("Cargo.toml")
|
|
|
|
.unwrap()
|
|
|
|
.set_status_code(StatusCode::NOT_FOUND);
|
|
|
|
{
|
|
|
|
file.file();
|
|
|
|
let _f: &File = &file;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let _f: &mut File = &mut file;
|
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::default().to_http_request();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"text/x-toml"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
"inline; filename=\"Cargo.toml\""
|
|
|
|
);
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_mime_override() {
|
2020-09-10 15:46:35 +02:00
|
|
|
fn all_attachment(_: &mime::Name<'_>) -> DispositionType {
|
2019-11-26 06:25:50 +01:00
|
|
|
DispositionType::Attachment
|
|
|
|
}
|
2019-03-25 21:02:37 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let mut srv = test::init_service(
|
|
|
|
App::new().service(
|
|
|
|
Files::new("/", ".")
|
|
|
|
.mime_override(all_attachment)
|
|
|
|
.index_file("Cargo.toml"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let request = TestRequest::get().uri("/").to_request();
|
|
|
|
let response = test::call_service(&mut srv, request).await;
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let content_disposition = response
|
|
|
|
.headers()
|
|
|
|
.get(header::CONTENT_DISPOSITION)
|
|
|
|
.expect("To have CONTENT_DISPOSITION");
|
|
|
|
let content_disposition = content_disposition
|
|
|
|
.to_str()
|
|
|
|
.expect("Convert CONTENT_DISPOSITION to str");
|
|
|
|
assert_eq!(content_disposition, "attachment; filename=\"Cargo.toml\"");
|
2019-03-25 21:02:37 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_ranges_status_code() {
|
|
|
|
let mut srv = test::init_service(
|
|
|
|
App::new().service(Files::new("/test", ".").index_file("Cargo.toml")),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// Valid range header
|
|
|
|
let request = TestRequest::get()
|
|
|
|
.uri("/t%65st/Cargo.toml")
|
|
|
|
.header(header::RANGE, "bytes=10-20")
|
|
|
|
.to_request();
|
|
|
|
let response = test::call_service(&mut srv, request).await;
|
|
|
|
assert_eq!(response.status(), StatusCode::PARTIAL_CONTENT);
|
|
|
|
|
|
|
|
// Invalid range header
|
|
|
|
let request = TestRequest::get()
|
|
|
|
.uri("/t%65st/Cargo.toml")
|
|
|
|
.header(header::RANGE, "bytes=1-0")
|
|
|
|
.to_request();
|
|
|
|
let response = test::call_service(&mut srv, request).await;
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::RANGE_NOT_SATISFIABLE);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_content_range_headers() {
|
2020-07-22 01:28:33 +02:00
|
|
|
let srv = test::start(|| App::new().service(Files::new("/", ".")));
|
2019-11-26 06:25:50 +01:00
|
|
|
|
|
|
|
// Valid range header
|
2020-05-17 23:54:42 +02:00
|
|
|
let response = srv
|
|
|
|
.get("/tests/test.binary")
|
2019-11-26 06:25:50 +01:00
|
|
|
.header(header::RANGE, "bytes=10-20")
|
2020-05-17 23:54:42 +02:00
|
|
|
.send()
|
|
|
|
.await
|
2019-11-26 06:25:50 +01:00
|
|
|
.unwrap();
|
2020-05-17 23:54:42 +02:00
|
|
|
let content_range = response.headers().get(header::CONTENT_RANGE).unwrap();
|
|
|
|
assert_eq!(content_range.to_str().unwrap(), "bytes 10-20/100");
|
2019-11-26 06:25:50 +01:00
|
|
|
|
|
|
|
// Invalid range header
|
2020-05-17 23:54:42 +02:00
|
|
|
let response = srv
|
|
|
|
.get("/tests/test.binary")
|
2019-11-26 06:25:50 +01:00
|
|
|
.header(header::RANGE, "bytes=10-5")
|
2020-05-17 23:54:42 +02:00
|
|
|
.send()
|
|
|
|
.await
|
2019-11-26 06:25:50 +01:00
|
|
|
.unwrap();
|
2020-05-17 23:54:42 +02:00
|
|
|
let content_range = response.headers().get(header::CONTENT_RANGE).unwrap();
|
|
|
|
assert_eq!(content_range.to_str().unwrap(), "bytes */100");
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_content_length_headers() {
|
2020-07-22 01:28:33 +02:00
|
|
|
let srv = test::start(|| App::new().service(Files::new("/", ".")));
|
2019-11-26 06:25:50 +01:00
|
|
|
|
|
|
|
// Valid range header
|
2020-05-17 23:54:42 +02:00
|
|
|
let response = srv
|
|
|
|
.get("/tests/test.binary")
|
2019-11-26 06:25:50 +01:00
|
|
|
.header(header::RANGE, "bytes=10-20")
|
2020-05-17 23:54:42 +02:00
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let content_length = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
|
|
|
assert_eq!(content_length.to_str().unwrap(), "11");
|
|
|
|
|
|
|
|
// Valid range header, starting from 0
|
|
|
|
let response = srv
|
|
|
|
.get("/tests/test.binary")
|
|
|
|
.header(header::RANGE, "bytes=0-20")
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let content_length = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
|
|
|
assert_eq!(content_length.to_str().unwrap(), "21");
|
2019-11-26 06:25:50 +01:00
|
|
|
|
|
|
|
// Without range header
|
2020-05-17 23:54:42 +02:00
|
|
|
let mut response = srv.get("/tests/test.binary").send().await.unwrap();
|
|
|
|
let content_length = response.headers().get(header::CONTENT_LENGTH).unwrap();
|
|
|
|
assert_eq!(content_length.to_str().unwrap(), "100");
|
2019-11-26 06:25:50 +01:00
|
|
|
|
2020-05-17 23:54:42 +02:00
|
|
|
// Should be no transfer-encoding
|
|
|
|
let transfer_encoding = response.headers().get(header::TRANSFER_ENCODING);
|
|
|
|
assert!(transfer_encoding.is_none());
|
2019-11-26 06:25:50 +01:00
|
|
|
|
2020-05-17 23:54:42 +02:00
|
|
|
// Check file contents
|
|
|
|
let bytes = response.body().await.unwrap();
|
2020-09-21 00:18:25 +02:00
|
|
|
let data = web::Bytes::from(fs::read("tests/test.binary").unwrap());
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(bytes, data);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_head_content_length_headers() {
|
2020-07-22 01:28:33 +02:00
|
|
|
let srv = test::start(|| App::new().service(Files::new("/", ".")));
|
2020-05-17 23:54:42 +02:00
|
|
|
|
2020-07-22 01:28:33 +02:00
|
|
|
let response = srv.head("/tests/test.binary").send().await.unwrap();
|
2019-11-26 06:25:50 +01:00
|
|
|
|
2020-05-17 23:54:42 +02:00
|
|
|
let content_length = response
|
|
|
|
.headers()
|
|
|
|
.get(header::CONTENT_LENGTH)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(content_length, "100");
|
2019-06-13 11:27:21 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_static_files_with_spaces() {
|
|
|
|
let mut srv = test::init_service(
|
|
|
|
App::new().service(Files::new("/", ".").index_file("Cargo.toml")),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let request = TestRequest::get()
|
|
|
|
.uri("/tests/test%20space.binary")
|
|
|
|
.to_request();
|
|
|
|
let response = test::call_service(&mut srv, request).await;
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
|
|
|
|
let bytes = test::read_body(response).await;
|
2020-09-21 00:18:25 +02:00
|
|
|
let data = web::Bytes::from(fs::read("tests/test space.binary").unwrap());
|
2019-11-26 06:25:50 +01:00
|
|
|
assert_eq!(bytes, data);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_files_not_allowed() {
|
|
|
|
let mut srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
|
|
|
|
|
|
|
let req = TestRequest::default()
|
|
|
|
.uri("/Cargo.toml")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
|
|
|
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
|
|
|
|
|
|
|
let mut srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
|
|
|
let req = TestRequest::default()
|
|
|
|
.method(Method::PUT)
|
|
|
|
.uri("/Cargo.toml")
|
|
|
|
.to_request();
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_files_guards() {
|
|
|
|
let mut srv = test::init_service(
|
|
|
|
App::new().service(Files::new("/", ".").use_guards(guard::Post())),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let req = TestRequest::default()
|
|
|
|
.uri("/Cargo.toml")
|
|
|
|
.method(Method::POST)
|
|
|
|
.to_request();
|
|
|
|
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-10-08 06:09:40 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_content_encoding() {
|
|
|
|
let mut srv = test::init_service(App::new().wrap(Compress::default()).service(
|
2020-07-22 01:28:33 +02:00
|
|
|
web::resource("/").to(|| async {
|
|
|
|
NamedFile::open("Cargo.toml")
|
|
|
|
.unwrap()
|
|
|
|
.set_content_encoding(header::ContentEncoding::Identity)
|
2019-11-26 06:25:50 +01:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let request = TestRequest::get()
|
|
|
|
.uri("/")
|
|
|
|
.header(header::ACCEPT_ENCODING, "gzip")
|
|
|
|
.to_request();
|
|
|
|
let res = test::call_service(&mut srv, request).await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
assert!(!res.headers().contains_key(header::CONTENT_ENCODING));
|
2019-04-04 23:00:56 +02:00
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_content_encoding_gzip() {
|
|
|
|
let mut srv = test::init_service(App::new().wrap(Compress::default()).service(
|
2020-07-22 01:28:33 +02:00
|
|
|
web::resource("/").to(|| async {
|
|
|
|
NamedFile::open("Cargo.toml")
|
|
|
|
.unwrap()
|
|
|
|
.set_content_encoding(header::ContentEncoding::Gzip)
|
2019-11-26 06:25:50 +01:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let request = TestRequest::get()
|
|
|
|
.uri("/")
|
|
|
|
.header(header::ACCEPT_ENCODING, "gzip")
|
|
|
|
.to_request();
|
|
|
|
let res = test::call_service(&mut srv, request).await;
|
|
|
|
assert_eq!(res.status(), StatusCode::OK);
|
|
|
|
assert_eq!(
|
|
|
|
res.headers()
|
|
|
|
.get(header::CONTENT_ENCODING)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
"gzip"
|
|
|
|
);
|
2019-04-01 03:19:18 +02:00
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_named_file_allowed_method() {
|
|
|
|
let req = TestRequest::default().method(Method::GET).to_http_request();
|
|
|
|
let file = NamedFile::open("Cargo.toml").unwrap();
|
|
|
|
let resp = file.respond_to(&req).await.unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_static_files() {
|
|
|
|
let mut srv = test::init_service(
|
|
|
|
App::new().service(Files::new("/", ".").show_files_listing()),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let req = TestRequest::with_uri("/missing").to_request();
|
|
|
|
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let mut srv = test::init_service(App::new().service(Files::new("/", "."))).await;
|
|
|
|
|
|
|
|
let req = TestRequest::default().to_request();
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
let mut srv = test::init_service(
|
|
|
|
App::new().service(Files::new("/", ".").show_files_listing()),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let req = TestRequest::with_uri("/tests").to_request();
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(
|
|
|
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
"text/html; charset=utf-8"
|
|
|
|
);
|
2019-11-21 06:31:31 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
let bytes = test::read_body(resp).await;
|
|
|
|
assert!(format!("{:?}", bytes).contains("/tests/test.png"));
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_redirect_to_slash_directory() {
|
|
|
|
// should not redirect if no index
|
|
|
|
let mut srv = test::init_service(
|
|
|
|
App::new().service(Files::new("/", ".").redirect_to_slash_directory()),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let req = TestRequest::with_uri("/tests").to_request();
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
// should redirect if index present
|
|
|
|
let mut srv = test::init_service(
|
|
|
|
App::new().service(
|
|
|
|
Files::new("/", ".")
|
|
|
|
.index_file("test.png")
|
|
|
|
.redirect_to_slash_directory(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
let req = TestRequest::with_uri("/tests").to_request();
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::FOUND);
|
|
|
|
|
|
|
|
// should not redirect if the path is wrong
|
|
|
|
let req = TestRequest::with_uri("/not_existing").to_request();
|
|
|
|
let resp = test::call_service(&mut srv, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
2019-10-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_static_files_bad_directory() {
|
2019-04-13 23:50:54 +02:00
|
|
|
let _st: Files = Files::new("/", "missing");
|
|
|
|
let _st: Files = Files::new("/", "Cargo.toml");
|
2019-03-06 07:10:08 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_default_handler_file_missing() {
|
|
|
|
let mut st = Files::new("/", ".")
|
|
|
|
.default_handler(|req: ServiceRequest| {
|
|
|
|
ok(req.into_response(HttpResponse::Ok().body("default content")))
|
|
|
|
})
|
2019-12-02 16:37:13 +01:00
|
|
|
.new_service(())
|
2019-11-26 06:25:50 +01:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let req = TestRequest::with_uri("/missing").to_srv_request();
|
|
|
|
|
|
|
|
let resp = test::call_service(&mut st, req).await;
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
let bytes = test::read_body(resp).await;
|
2020-09-21 00:18:25 +02:00
|
|
|
assert_eq!(bytes, web::Bytes::from_static(b"default content"));
|
2019-04-01 03:19:18 +02:00
|
|
|
}
|
2019-03-06 07:10:08 +01:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
// #[actix_rt::test]
|
|
|
|
// async fn test_serve_index() {
|
2019-03-07 08:39:08 +01:00
|
|
|
// let st = Files::new(".").index_file("test.binary");
|
2019-03-06 07:10:08 +01:00
|
|
|
// let req = TestRequest::default().uri("/tests").finish();
|
|
|
|
|
|
|
|
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
|
|
|
// let resp = resp.as_msg();
|
|
|
|
// assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
// assert_eq!(
|
|
|
|
// resp.headers()
|
|
|
|
// .get(header::CONTENT_TYPE)
|
|
|
|
// .expect("content type"),
|
|
|
|
// "application/octet-stream"
|
|
|
|
// );
|
|
|
|
// assert_eq!(
|
|
|
|
// resp.headers()
|
|
|
|
// .get(header::CONTENT_DISPOSITION)
|
|
|
|
// .expect("content disposition"),
|
|
|
|
// "attachment; filename=\"test.binary\""
|
|
|
|
// );
|
|
|
|
|
|
|
|
// let req = TestRequest::default().uri("/tests/").finish();
|
|
|
|
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
|
|
|
// let resp = resp.as_msg();
|
|
|
|
// assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
// assert_eq!(
|
|
|
|
// resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
// "application/octet-stream"
|
|
|
|
// );
|
|
|
|
// assert_eq!(
|
|
|
|
// resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
// "attachment; filename=\"test.binary\""
|
|
|
|
// );
|
|
|
|
|
|
|
|
// // nonexistent index file
|
|
|
|
// let req = TestRequest::default().uri("/tests/unknown").finish();
|
|
|
|
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
|
|
|
// let resp = resp.as_msg();
|
|
|
|
// assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
// let req = TestRequest::default().uri("/tests/unknown/").finish();
|
|
|
|
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
|
|
|
// let resp = resp.as_msg();
|
|
|
|
// assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
|
|
// }
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
// #[actix_rt::test]
|
|
|
|
// async fn test_serve_index_nested() {
|
2019-03-07 08:39:08 +01:00
|
|
|
// let st = Files::new(".").index_file("mod.rs");
|
2019-03-06 07:10:08 +01:00
|
|
|
// let req = TestRequest::default().uri("/src/client").finish();
|
|
|
|
// let resp = st.handle(&req).respond_to(&req).unwrap();
|
|
|
|
// let resp = resp.as_msg();
|
|
|
|
// assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
// assert_eq!(
|
|
|
|
// resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
|
|
|
// "text/x-rust"
|
|
|
|
// );
|
|
|
|
// assert_eq!(
|
|
|
|
// resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
|
|
|
// "inline; filename=\"mod.rs\""
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
// #[actix_rt::test]
|
2019-03-06 07:10:08 +01:00
|
|
|
// fn integration_serve_index() {
|
|
|
|
// let mut srv = test::TestServer::with_factory(|| {
|
|
|
|
// App::new().handler(
|
|
|
|
// "test",
|
2019-03-07 08:39:08 +01:00
|
|
|
// Files::new(".").index_file("Cargo.toml"),
|
2019-03-06 07:10:08 +01:00
|
|
|
// )
|
|
|
|
// });
|
|
|
|
|
|
|
|
// let request = srv.get().uri(srv.url("/test")).finish().unwrap();
|
|
|
|
// let response = srv.execute(request.send()).unwrap();
|
|
|
|
// assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
// let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
// let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
|
|
|
// assert_eq!(bytes, data);
|
|
|
|
|
|
|
|
// let request = srv.get().uri(srv.url("/test/")).finish().unwrap();
|
|
|
|
// let response = srv.execute(request.send()).unwrap();
|
|
|
|
// assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
// let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
// let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
|
|
|
// assert_eq!(bytes, data);
|
|
|
|
|
|
|
|
// // nonexistent index file
|
|
|
|
// let request = srv.get().uri(srv.url("/test/unknown")).finish().unwrap();
|
|
|
|
// let response = srv.execute(request.send()).unwrap();
|
|
|
|
// assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
|
|
|
|
|
|
|
// let request = srv.get().uri(srv.url("/test/unknown/")).finish().unwrap();
|
|
|
|
// let response = srv.execute(request.send()).unwrap();
|
|
|
|
// assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
|
|
|
// }
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
// #[actix_rt::test]
|
2019-03-06 07:10:08 +01:00
|
|
|
// fn integration_percent_encoded() {
|
|
|
|
// let mut srv = test::TestServer::with_factory(|| {
|
|
|
|
// App::new().handler(
|
|
|
|
// "test",
|
2019-03-07 08:39:08 +01:00
|
|
|
// Files::new(".").index_file("Cargo.toml"),
|
2019-03-06 07:10:08 +01:00
|
|
|
// )
|
|
|
|
// });
|
|
|
|
|
|
|
|
// let request = srv
|
|
|
|
// .get()
|
|
|
|
// .uri(srv.url("/test/%43argo.toml"))
|
|
|
|
// .finish()
|
|
|
|
// .unwrap();
|
|
|
|
// let response = srv.execute(request.send()).unwrap();
|
|
|
|
// assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
// }
|
|
|
|
}
|