mirror of https://github.com/fafhrd91/actix-web
Add support for permanent redirects in `Files` service (#3767)
* Add support for permanent redirects in `Files` service * Rename `with_permanent_redirect_directory` to `with_permanent_redirect` in `Files` service * Tweak doc comment --------- Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
This commit is contained in:
parent
a06cdf3754
commit
6e6c2491c6
|
@ -41,6 +41,7 @@ pub struct Files {
|
||||||
index: Option<String>,
|
index: Option<String>,
|
||||||
show_index: bool,
|
show_index: bool,
|
||||||
redirect_to_slash: bool,
|
redirect_to_slash: bool,
|
||||||
|
with_permanent_redirect: bool,
|
||||||
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
default: Rc<RefCell<Option<Rc<HttpNewService>>>>,
|
||||||
renderer: Rc<DirectoryRenderer>,
|
renderer: Rc<DirectoryRenderer>,
|
||||||
mime_override: Option<Rc<MimeOverride>>,
|
mime_override: Option<Rc<MimeOverride>>,
|
||||||
|
@ -65,6 +66,7 @@ impl Clone for Files {
|
||||||
index: self.index.clone(),
|
index: self.index.clone(),
|
||||||
show_index: self.show_index,
|
show_index: self.show_index,
|
||||||
redirect_to_slash: self.redirect_to_slash,
|
redirect_to_slash: self.redirect_to_slash,
|
||||||
|
with_permanent_redirect: self.with_permanent_redirect,
|
||||||
default: self.default.clone(),
|
default: self.default.clone(),
|
||||||
renderer: self.renderer.clone(),
|
renderer: self.renderer.clone(),
|
||||||
file_flags: self.file_flags,
|
file_flags: self.file_flags,
|
||||||
|
@ -113,6 +115,7 @@ impl Files {
|
||||||
index: None,
|
index: None,
|
||||||
show_index: false,
|
show_index: false,
|
||||||
redirect_to_slash: false,
|
redirect_to_slash: false,
|
||||||
|
with_permanent_redirect: false,
|
||||||
default: Rc::new(RefCell::new(None)),
|
default: Rc::new(RefCell::new(None)),
|
||||||
renderer: Rc::new(directory_listing),
|
renderer: Rc::new(directory_listing),
|
||||||
mime_override: None,
|
mime_override: None,
|
||||||
|
@ -144,6 +147,14 @@ impl Files {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Redirect with permanent redirect status code (308).
|
||||||
|
///
|
||||||
|
/// By default redirect with temporary redirect status code (307).
|
||||||
|
pub fn with_permanent_redirect(mut self) -> Self {
|
||||||
|
self.with_permanent_redirect = true;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set custom directory renderer.
|
/// Set custom directory renderer.
|
||||||
pub fn files_listing_renderer<F>(mut self, f: F) -> Self
|
pub fn files_listing_renderer<F>(mut self, f: F) -> Self
|
||||||
where
|
where
|
||||||
|
@ -388,6 +399,7 @@ impl ServiceFactory<ServiceRequest> for Files {
|
||||||
guards: self.use_guards.clone(),
|
guards: self.use_guards.clone(),
|
||||||
hidden_files: self.hidden_files,
|
hidden_files: self.hidden_files,
|
||||||
size_threshold: self.read_mode_threshold,
|
size_threshold: self.read_mode_threshold,
|
||||||
|
with_permanent_redirect: self.with_permanent_redirect,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(ref default) = *self.default.borrow() {
|
if let Some(ref default) = *self.default.borrow() {
|
||||||
|
|
|
@ -736,7 +736,21 @@ mod tests {
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/tests").to_request();
|
let req = TestRequest::with_uri("/tests").to_request();
|
||||||
let resp = test::call_service(&srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::FOUND);
|
assert_eq!(resp.status(), StatusCode::TEMPORARY_REDIRECT);
|
||||||
|
|
||||||
|
// should redirect if index present with permanent redirect
|
||||||
|
let srv = test::init_service(
|
||||||
|
App::new().service(
|
||||||
|
Files::new("/", ".")
|
||||||
|
.index_file("test.png")
|
||||||
|
.redirect_to_slash_directory()
|
||||||
|
.with_permanent_redirect(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let req = TestRequest::with_uri("/tests").to_request();
|
||||||
|
let resp = test::call_service(&srv, req).await;
|
||||||
|
assert_eq!(resp.status(), StatusCode::PERMANENT_REDIRECT);
|
||||||
|
|
||||||
// should redirect if files listing is enabled
|
// should redirect if files listing is enabled
|
||||||
let srv = test::init_service(
|
let srv = test::init_service(
|
||||||
|
@ -749,7 +763,7 @@ mod tests {
|
||||||
.await;
|
.await;
|
||||||
let req = TestRequest::with_uri("/tests").to_request();
|
let req = TestRequest::with_uri("/tests").to_request();
|
||||||
let resp = test::call_service(&srv, req).await;
|
let resp = test::call_service(&srv, req).await;
|
||||||
assert_eq!(resp.status(), StatusCode::FOUND);
|
assert_eq!(resp.status(), StatusCode::TEMPORARY_REDIRECT);
|
||||||
|
|
||||||
// should not redirect if the path is wrong
|
// should not redirect if the path is wrong
|
||||||
let req = TestRequest::with_uri("/not_existing").to_request();
|
let req = TestRequest::with_uri("/not_existing").to_request();
|
||||||
|
|
|
@ -40,6 +40,7 @@ pub struct FilesServiceInner {
|
||||||
pub(crate) guards: Option<Rc<dyn Guard>>,
|
pub(crate) guards: Option<Rc<dyn Guard>>,
|
||||||
pub(crate) hidden_files: bool,
|
pub(crate) hidden_files: bool,
|
||||||
pub(crate) size_threshold: u64,
|
pub(crate) size_threshold: u64,
|
||||||
|
pub(crate) with_permanent_redirect: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for FilesServiceInner {
|
impl fmt::Debug for FilesServiceInner {
|
||||||
|
@ -148,11 +149,15 @@ impl Service<ServiceRequest> for FilesService {
|
||||||
{
|
{
|
||||||
let redirect_to = format!("{}/", req.path());
|
let redirect_to = format!("{}/", req.path());
|
||||||
|
|
||||||
return Ok(req.into_response(
|
let response = if this.with_permanent_redirect {
|
||||||
HttpResponse::Found()
|
HttpResponse::PermanentRedirect()
|
||||||
|
} else {
|
||||||
|
HttpResponse::TemporaryRedirect()
|
||||||
|
}
|
||||||
.insert_header((header::LOCATION, redirect_to))
|
.insert_header((header::LOCATION, redirect_to))
|
||||||
.finish(),
|
.finish();
|
||||||
));
|
|
||||||
|
return Ok(req.into_response(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
match this.index {
|
match this.index {
|
||||||
|
|
Loading…
Reference in New Issue