actix_web_middleware_redirect_scheme/lib.rs
1//! # actix-web-middleware-redirect-scheme
2//!
3//! Provides a middleware for `actix-web` to redirect all `http` requests to `https` and vice versa. Based on actix-web-middleware-redirect-https.
4//!
5//! ## Examples
6//!
7//! ```
8//! use actix_web::{App, web, HttpResponse};
9//! use actix_web_middleware_redirect_scheme::RedirectSchemeBuilder;
10//!
11//! App::new()
12//! .wrap(RedirectSchemeBuilder::new().https_to_http().temporary().build())
13//! .route("/", web::get().to(|| HttpResponse::Ok()
14//! .content_type("text/plain")
15//! .body("Temporary to HTTP!")));
16//! ```
17//!
18//! ### Usage HTTP -> HTTPS
19//!
20//! ```toml
21//! # Cargo.toml
22//! [dependencies]
23//! actix-web-middleware-redirect-scheme = "2.3"
24//! ```
25//!
26//! ```rust
27//! use actix_web::{App, web, HttpResponse};
28//! use actix_web_middleware_redirect_scheme::RedirectSchemeBuilder;
29//!
30//! App::new()
31//! .wrap(RedirectSchemeBuilder::new().build())
32//! .route("/", web::get().to(|| HttpResponse::Ok()
33//! .content_type("text/plain")
34//! .body("Always HTTPS!")));
35//! ```
36//!
37//! You can switch on/off of redirections according to your settings:
38//!
39//! ```rust
40//! use actix_web::{App, web, HttpResponse};
41//! use actix_web_middleware_redirect_scheme::RedirectSchemeBuilder;
42//!
43//! let mut use_redir = true;
44//! // ...
45//!
46//! App::new()
47//! .wrap(RedirectSchemeBuilder::new().enable(use_redir).build())
48//! .route("/", web::get().to(|| HttpResponse::Ok()
49//! .content_type("text/plain")
50//! .body("Maybe HTTPS")));
51//! ```
52//!
53//! By default, the middleware uses answer code "301 Moved Permanently", but you can use "307 Temporary Redirect":
54//!
55//! ```rust
56//! use actix_web::{App, web, HttpResponse};
57//! use actix_web_middleware_redirect_scheme::RedirectSchemeBuilder;
58//!
59//! App::new()
60//! .wrap(RedirectSchemeBuilder::new().temporary().build())
61//! .route("/", web::get().to(|| HttpResponse::Ok()
62//! .content_type("text/plain")
63//! .body("Always HTTPS!")));
64//! ```
65//!
66//! This is equivalent:
67//!
68//! ```ignore
69//! RedirectSchemeBuilder::new().temporary()
70//! ```
71//! and
72//! ```ignore
73//! RedirectSchemeBuilder::new().permanent(false)
74//! ```
75//!
76//! By default, the middleware simply replaces the `scheme` of the URL with `https://`, but you may need to it to change other parts of the URL.
77//! For example, in development if you are not using the default ports (80 and 443) then you will need to specify their replacement, as below:
78//!
79//! ```rust
80//! use actix_web::{App, web, HttpResponse};
81//! use actix_web_middleware_redirect_scheme::RedirectSchemeBuilder;
82//!
83//! App::new()
84//! .wrap(RedirectSchemeBuilder::new().replacements(&[(":8080", ":8443")]).build())
85//! .route("/", web::get().to(|| HttpResponse::Ok()
86//! .content_type("text/plain")
87//! .body("Always HTTPS on non-default ports!")));
88//! ```
89//!
90//! ### Usage HTTPS -> HTTP
91//!
92//! ```toml
93//! # Cargo.toml
94//! [dependencies]
95//! actix-web-middleware-redirect-scheme = "2.3"
96//! ```
97//!
98//! ```rust
99//! use actix_web::{App, web, HttpResponse};
100//! use actix_web_middleware_redirect_scheme::RedirectSchemeBuilder;
101//!
102//! App::new()
103//! .wrap(RedirectSchemeBuilder::new().https_to_http().build())
104//! .route("/", web::get().to(|| HttpResponse::Ok()
105//! .content_type("text/plain")
106//! .body("Always HTTP!")));
107//! ```
108//!
109//! This is equivalent:
110//!
111//! ```ignore
112//! RedirectSchemeBuilder::new().https_to_http()
113//! ```
114//! and
115//! ```ignore
116//! RedirectSchemeBuilder::new().http_to_https(false)
117//! ```
118//!
119//! By default, the middleware simply replaces the `scheme` of the URL with `http://`, but you may need to it to change other parts of the URL.
120//! For example, in development if you are not using the default ports (80 and 443) then you will need to specify their replacement, as below:
121//!
122//! ```rust
123//! use actix_web::{App, web, HttpResponse};
124//! use actix_web_middleware_redirect_scheme::RedirectSchemeBuilder;
125//!
126//! App::new()
127//! .wrap(RedirectSchemeBuilder::new().https_to_http().replacements(&[(":8443", ":8080")]).build())
128//! .route("/", web::get().to(|| HttpResponse::Ok()
129//! .content_type("text/plain")
130//! .body("Always HTTP on non-default ports!")));
131//! ```
132
133pub mod builder;
134pub mod scheme;
135pub mod service;
136
137pub use crate::builder::RedirectSchemeBuilder;
138pub use crate::scheme::RedirectScheme;