#actix-web #middleware #redirect #https #ssl

actix-web-middleware-redirect-scheme

A middleware for actix-web which forwards all http requests to https and vice versa. Based on actix-web-middleware-redirect-https.

14 stable releases

3.0.0 Oct 29, 2020
2.3.3 May 18, 2020
2.3.1 Mar 16, 2020
1.1.1 Mar 6, 2020

#163 in HTTP server

Download history 9/week @ 2022-03-01 8/week @ 2022-03-08 65/week @ 2022-03-15 58/week @ 2022-03-22 58/week @ 2022-03-29 60/week @ 2022-04-05 40/week @ 2022-04-12 58/week @ 2022-04-19 101/week @ 2022-04-26 144/week @ 2022-05-03 243/week @ 2022-05-10 134/week @ 2022-05-17 97/week @ 2022-05-24 215/week @ 2022-05-31 219/week @ 2022-06-07 74/week @ 2022-06-14

625 downloads per month

MIT license

19KB
169 lines

actix-web-middleware-redirect-scheme

Build Status Latest Version Docs

A middleware for actix-web which forwards all http requests to https and vice versa. Based on actix-web-middleware-redirect-https.

Usage HTTP -> HTTPS

# Cargo.toml
[dependencies]
actix-web-middleware-redirect-scheme = "3.0"
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:

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":

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("Temporary HTTPS!")));

This is equivalent:

RedirectSchemeBuilder::new().temporary()

and

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:

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

# Cargo.toml
[dependencies]
actix-web-middleware-redirect-scheme = "3.0"
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:

RedirectSchemeBuilder::new().https_to_http()

and

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:

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!")));

Dependencies

~22MB
~500K SLoC