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

pub mod builder;
pub mod scheme;
pub mod service;

pub use crate::builder::RedirectSchemeBuilder;
pub use crate::scheme::RedirectScheme;