From 9f105ef81b6de7d1fb2240869d62c3f8cf3cdb47 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sun, 30 Jul 2023 08:17:50 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20redirections=20in=20HTTP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/prelude.rs | 2 +- pagetop/src/response.rs | 4 +- pagetop/src/response/redirect.rs | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 pagetop/src/response/redirect.rs diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index abc19629..4509536c 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -38,6 +38,6 @@ pub use crate::service; pub use crate::service::HttpMessage; pub use crate::response::fatal_error::*; -pub use crate::response::{page::*, ResponseError}; +pub use crate::response::{page::*, redirect::*, ResponseError}; pub use crate::app::Application; diff --git a/pagetop/src/response.rs b/pagetop/src/response.rs index 985d918d..7878d767 100644 --- a/pagetop/src/response.rs +++ b/pagetop/src/response.rs @@ -1,7 +1,9 @@ -//! Tipos de respuestas a peticiones web. +//! Types of web request responses. pub use actix_web::ResponseError; pub mod page; +pub mod redirect; + pub mod fatal_error; diff --git a/pagetop/src/response/redirect.rs b/pagetop/src/response/redirect.rs new file mode 100644 index 00000000..eb2678a8 --- /dev/null +++ b/pagetop/src/response/redirect.rs @@ -0,0 +1,76 @@ +//! Perform redirections in HTTP. +//! +//! **URL redirection**, also known as *URL forwarding*, is a technique to give more than one URL +//! address to a web resource. HTTP has a response called ***HTTP redirect*** for this operation +//! (see [Redirections in HTTP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections)). +//! +//! There are several types of redirects, sorted into three categories: +//! +//! * **Permanent redirections**. These redirections are meant to last forever. They imply that +//! the original URL should no longer be used, and replaced with the new one. Search engine +//! robots, RSS readers, and other crawlers will update the original URL for the resource. +//! +//! * **Temporary redirections**. Sometimes the requested resource can't be accessed from its +//! canonical location, but it can be accessed from another place. In this case, a temporary +//! redirect can be used. Search engine robots and other crawlers don't memorize the new, +//! temporary URL. Temporary redirections are also used when creating, updating, or deleting +//! resources, to show temporary progress pages. +//! +//! * **Special redirections**. + +use crate::service::HttpResponse; + +pub struct Redirect; + +impl Redirect { + /// Permanent redirection. Status Code **301**. GET methods unchanged. Others may or may not be + /// changed to GET. Typical for reorganization of a website. + pub fn moved(redirect_to_url: &str) -> HttpResponse { + HttpResponse::MovedPermanently() + .append_header(("Location", redirect_to_url)) + .finish() + } + + /// Permanent redirection. Status Code **308**. Method and body not changed. Typical for + /// reorganization of a website, with non-GET links/operations. + pub fn permanent(redirect_to_url: &str) -> HttpResponse { + HttpResponse::PermanentRedirect() + .append_header(("Location", redirect_to_url)) + .finish() + } + + /// Temporary redirection. Status Code **302**. GET methods unchanged. Others may or may not be + /// changed to GET. Used when the web page is temporarily unavailable for unforeseen reasons. + pub fn found(redirect_to_url: &str) -> HttpResponse { + HttpResponse::Found() + .append_header(("Location", redirect_to_url)) + .finish() + } + + /// Temporary redirection. Status Code **303**. GET methods unchanged. Others changed to GET + /// (body lost). Used to redirect after a PUT or a POST, so that refreshing the result page + /// doesn't re-trigger the operation. + pub fn see_other(redirect_to_url: &str) -> HttpResponse { + HttpResponse::SeeOther() + .append_header(("Location", redirect_to_url)) + .finish() + } + + /// Temporary redirection. Status Code **307**. Method and body not changed. The web page is + /// temporarily unavailable for unforeseen reasons. Better than [`found()`](found) when non-GET + /// operations are available on the site. + pub fn temporary(redirect_to_url: &str) -> HttpResponse { + HttpResponse::TemporaryRedirect() + .append_header(("Location", redirect_to_url)) + .finish() + } + + /// Special redirection. Status Code **304**. Redirects a page to the locally cached copy (that + /// was stale). Sent for revalidated conditional requests. Indicates that the cached response is + /// still fresh and can be used. + pub fn not_modified(redirect_to_url: &str) -> HttpResponse { + HttpResponse::NotModified() + .append_header(("Location", redirect_to_url)) + .finish() + } +}