diff --git a/actix-files/src/files.rs b/actix-files/src/files.rs index 83f488970..3491f59e2 100644 --- a/actix-files/src/files.rs +++ b/actix-files/src/files.rs @@ -41,6 +41,7 @@ pub struct Files { index: Option, show_index: bool, redirect_to_slash: bool, + with_permanent_redirect: bool, default: Rc>>>, renderer: Rc, mime_override: Option>, @@ -65,6 +66,7 @@ impl Clone for Files { index: self.index.clone(), show_index: self.show_index, redirect_to_slash: self.redirect_to_slash, + with_permanent_redirect: self.with_permanent_redirect, default: self.default.clone(), renderer: self.renderer.clone(), file_flags: self.file_flags, @@ -113,6 +115,7 @@ impl Files { index: None, show_index: false, redirect_to_slash: false, + with_permanent_redirect: false, default: Rc::new(RefCell::new(None)), renderer: Rc::new(directory_listing), mime_override: None, @@ -144,6 +147,14 @@ impl Files { 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. pub fn files_listing_renderer(mut self, f: F) -> Self where @@ -388,6 +399,7 @@ impl ServiceFactory for Files { guards: self.use_guards.clone(), hidden_files: self.hidden_files, size_threshold: self.read_mode_threshold, + with_permanent_redirect: self.with_permanent_redirect, }; if let Some(ref default) = *self.default.borrow() { diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index 551a14fa4..07c6bbca5 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -736,7 +736,21 @@ mod tests { .await; let req = TestRequest::with_uri("/tests").to_request(); 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 let srv = test::init_service( @@ -749,7 +763,7 @@ mod tests { .await; let req = TestRequest::with_uri("/tests").to_request(); 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 let req = TestRequest::with_uri("/not_existing").to_request(); diff --git a/actix-files/src/service.rs b/actix-files/src/service.rs index 527ab7f4b..f63ba46c6 100644 --- a/actix-files/src/service.rs +++ b/actix-files/src/service.rs @@ -40,6 +40,7 @@ pub struct FilesServiceInner { pub(crate) guards: Option>, pub(crate) hidden_files: bool, pub(crate) size_threshold: u64, + pub(crate) with_permanent_redirect: bool, } impl fmt::Debug for FilesServiceInner { @@ -148,11 +149,15 @@ impl Service for FilesService { { let redirect_to = format!("{}/", req.path()); - return Ok(req.into_response( - HttpResponse::Found() - .insert_header((header::LOCATION, redirect_to)) - .finish(), - )); + let response = if this.with_permanent_redirect { + HttpResponse::PermanentRedirect() + } else { + HttpResponse::TemporaryRedirect() + } + .insert_header((header::LOCATION, redirect_to)) + .finish(); + + return Ok(req.into_response(response)); } match this.index {