Code Monkey home page Code Monkey logo

actix-web-middleware-redirect-scheme's Introduction

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

actix-web-middleware-redirect-scheme's People

Contributors

juzi5201314 avatar perdumonocle avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.