mirror of
https://github.com/actix/actix-extras.git
synced 2025-01-23 15:24:36 +01:00
Serve static file directly instead of redirecting (#676)
This commit is contained in:
parent
9968afe4a6
commit
346d85a884
126
src/fs.rs
126
src/fs.rs
@ -790,7 +790,7 @@ impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
|
|||||||
|
|
||||||
/// Set index file
|
/// Set index file
|
||||||
///
|
///
|
||||||
/// Redirects to specific index file for directory "/" instead of
|
/// Shows specific index file for directory "/" instead of
|
||||||
/// showing files listing.
|
/// showing files listing.
|
||||||
pub fn index_file<T: Into<String>>(mut self, index: T) -> StaticFiles<S, C> {
|
pub fn index_file<T: Into<String>>(mut self, index: T) -> StaticFiles<S, C> {
|
||||||
self.index = Some(index.into());
|
self.index = Some(index.into());
|
||||||
@ -815,17 +815,11 @@ impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
|
|||||||
|
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
if let Some(ref redir_index) = self.index {
|
if let Some(ref redir_index) = self.index {
|
||||||
// TODO: Don't redirect, just return the index content.
|
let path = path.join(redir_index);
|
||||||
// TODO: It'd be nice if there were a good usable URL manipulation
|
|
||||||
// library
|
NamedFile::open_with_config(path, C::default())?
|
||||||
let mut new_path: String = req.path().to_owned();
|
.set_cpu_pool(self.cpu_pool.clone())
|
||||||
if !new_path.ends_with('/') {
|
.respond_to(&req)?
|
||||||
new_path.push('/');
|
|
||||||
}
|
|
||||||
new_path.push_str(redir_index);
|
|
||||||
HttpResponse::Found()
|
|
||||||
.header(header::LOCATION, new_path.as_str())
|
|
||||||
.finish()
|
|
||||||
.respond_to(&req)
|
.respond_to(&req)
|
||||||
} else if self.show_index {
|
} else if self.show_index {
|
||||||
let dir = Directory::new(self.directory.clone(), path);
|
let dir = Directory::new(self.directory.clone(), path);
|
||||||
@ -1482,43 +1476,66 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_redirect_to_index() {
|
fn test_serve_index() {
|
||||||
let st = StaticFiles::new(".").unwrap().index_file("index.html");
|
let st = StaticFiles::new(".").unwrap().index_file("test.binary");
|
||||||
let req = TestRequest::default().uri("/tests").finish();
|
let req = TestRequest::default().uri("/tests").finish();
|
||||||
|
|
||||||
let resp = st.handle(&req).respond_to(&req).unwrap();
|
let resp = st.handle(&req).respond_to(&req).unwrap();
|
||||||
let resp = resp.as_msg();
|
let resp = resp.as_msg();
|
||||||
assert_eq!(resp.status(), StatusCode::FOUND);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::LOCATION).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).expect("content type"),
|
||||||
"/tests/index.html"
|
"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 req = TestRequest::default().uri("/tests/").finish();
|
||||||
let resp = st.handle(&req).respond_to(&req).unwrap();
|
let resp = st.handle(&req).respond_to(&req).unwrap();
|
||||||
let resp = resp.as_msg();
|
let resp = resp.as_msg();
|
||||||
assert_eq!(resp.status(), StatusCode::FOUND);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::LOCATION).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
"/tests/index.html"
|
"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);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_redirect_to_index_nested() {
|
fn test_serve_index_nested() {
|
||||||
let st = StaticFiles::new(".").unwrap().index_file("mod.rs");
|
let st = StaticFiles::new(".").unwrap().index_file("mod.rs");
|
||||||
let req = TestRequest::default().uri("/src/client").finish();
|
let req = TestRequest::default().uri("/src/client").finish();
|
||||||
let resp = st.handle(&req).respond_to(&req).unwrap();
|
let resp = st.handle(&req).respond_to(&req).unwrap();
|
||||||
let resp = resp.as_msg();
|
let resp = resp.as_msg();
|
||||||
assert_eq!(resp.status(), StatusCode::FOUND);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
resp.headers().get(header::LOCATION).unwrap(),
|
resp.headers().get(header::CONTENT_TYPE).unwrap(),
|
||||||
"/src/client/mod.rs"
|
"text/x-rust"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
resp.headers().get(header::CONTENT_DISPOSITION).unwrap(),
|
||||||
|
"inline; filename=\"mod.rs\""
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn integration_redirect_to_index_with_prefix() {
|
fn integration_serve_index_with_prefix() {
|
||||||
let mut srv = test::TestServer::with_factory(|| {
|
let mut srv = test::TestServer::with_factory(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.prefix("public")
|
.prefix("public")
|
||||||
@ -1527,29 +1544,21 @@ mod tests {
|
|||||||
|
|
||||||
let request = srv.get().uri(srv.url("/public")).finish().unwrap();
|
let request = srv.get().uri(srv.url("/public")).finish().unwrap();
|
||||||
let response = srv.execute(request.send()).unwrap();
|
let response = srv.execute(request.send()).unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::FOUND);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
let loc = response
|
let bytes = srv.execute(response.body()).unwrap();
|
||||||
.headers()
|
let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
||||||
.get(header::LOCATION)
|
assert_eq!(bytes, data);
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(loc, "/public/Cargo.toml");
|
|
||||||
|
|
||||||
let request = srv.get().uri(srv.url("/public/")).finish().unwrap();
|
let request = srv.get().uri(srv.url("/public/")).finish().unwrap();
|
||||||
let response = srv.execute(request.send()).unwrap();
|
let response = srv.execute(request.send()).unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::FOUND);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
let loc = response
|
let bytes = srv.execute(response.body()).unwrap();
|
||||||
.headers()
|
let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
||||||
.get(header::LOCATION)
|
assert_eq!(bytes, data);
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(loc, "/public/Cargo.toml");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn integration_redirect_to_index() {
|
fn integration_serve_index() {
|
||||||
let mut srv = test::TestServer::with_factory(|| {
|
let mut srv = test::TestServer::with_factory(|| {
|
||||||
App::new().handler(
|
App::new().handler(
|
||||||
"test",
|
"test",
|
||||||
@ -1559,25 +1568,26 @@ mod tests {
|
|||||||
|
|
||||||
let request = srv.get().uri(srv.url("/test")).finish().unwrap();
|
let request = srv.get().uri(srv.url("/test")).finish().unwrap();
|
||||||
let response = srv.execute(request.send()).unwrap();
|
let response = srv.execute(request.send()).unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::FOUND);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
let loc = response
|
let bytes = srv.execute(response.body()).unwrap();
|
||||||
.headers()
|
let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
||||||
.get(header::LOCATION)
|
assert_eq!(bytes, data);
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(loc, "/test/Cargo.toml");
|
|
||||||
|
|
||||||
let request = srv.get().uri(srv.url("/test/")).finish().unwrap();
|
let request = srv.get().uri(srv.url("/test/")).finish().unwrap();
|
||||||
let response = srv.execute(request.send()).unwrap();
|
let response = srv.execute(request.send()).unwrap();
|
||||||
assert_eq!(response.status(), StatusCode::FOUND);
|
assert_eq!(response.status(), StatusCode::OK);
|
||||||
let loc = response
|
let bytes = srv.execute(response.body()).unwrap();
|
||||||
.headers()
|
let data = Bytes::from(fs::read("Cargo.toml").unwrap());
|
||||||
.get(header::LOCATION)
|
assert_eq!(bytes, data);
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
// nonexistent index file
|
||||||
.unwrap();
|
let request = srv.get().uri(srv.url("/test/unknown")).finish().unwrap();
|
||||||
assert_eq!(loc, "/test/Cargo.toml");
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user