mirror of https://github.com/fafhrd91/actix-web
Serve index file directly instead of redirecting
This commit is contained in:
parent
a534fdd125
commit
21159209da
102
src/fs.rs
102
src/fs.rs
|
@ -744,7 +744,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());
|
||||||
|
@ -769,17 +769,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);
|
||||||
|
@ -1436,38 +1430,50 @@ 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\""
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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\""
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1481,25 +1487,17 @@ 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]
|
||||||
|
@ -1513,25 +1511,17 @@ 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()
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(loc, "/test/Cargo.toml");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue