Skip to content

malj/tower-canonical-redirect

Repository files navigation

tower-canonical-redirect

Build Status Crates.io

A tower middleware to enforce canonical hosts in HTTP requests. Useful when you want to redirect website users from insecure http connections and/or www subdomains to a single canonical https domain.

The middleware uses framework-agnostic http and futures abstractions, making it compatible with other crates such as hyper, axum, tonic, warp, etc.

Features

Example

Basic usage with axum:

use tower_canonical_redirect::CanonicalRedirectLayer;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>>
{
	let mut router = axum::Router::new()
		.route("/", axum::routing::get(async || "Hello world"));

	if let Ok(origin) = std::env::var("CANONICAL_ORIGIN")
	{
		let layer = CanonicalRedirectLayer::new(origin)?;
		router = router.layer(layer);
	}

	let listener = tokio::net::TcpListener::bind("127.0.0.1:8000").await?;
	axum::serve(listener, router).await?;

	Ok(())
}

Using the builder API:

let layer = CanonicalRedirectLayer::builder("https://example.com")
	.proto_header("X-Custom-Proto")
	.host_header("X-Custom-Host")
	.temporary_origin("http://www.example.com")
	.temporary_origin("https://www.example.com")
	.build()
	.unwrap();

License

All code in this repository is free and open source software distributed under the terms of either license, at your option:

About

A Tower middleware to enforce canonical hosts in HTTP requests.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages