#https-server #axum-server #https #server-port #server #tls-server #client-server

axum-server-dual-protocol

Host a HTTP and HTTPS server on the same port with axum-server

9 releases (breaking)

0.7.0 Jul 31, 2024
0.6.0 Dec 22, 2023
0.5.2 Jun 16, 2023
0.4.0 May 4, 2023
0.1.0 Jul 28, 2022

#71 in HTTP server

Download history 421/week @ 2024-07-18 445/week @ 2024-07-25 421/week @ 2024-08-01 338/week @ 2024-08-08 561/week @ 2024-08-15 579/week @ 2024-08-22 417/week @ 2024-08-29 361/week @ 2024-09-05 426/week @ 2024-09-12 249/week @ 2024-09-19 233/week @ 2024-09-26 224/week @ 2024-10-03 272/week @ 2024-10-10 505/week @ 2024-10-17 638/week @ 2024-10-24 453/week @ 2024-10-31

1,901 downloads per month
Used in 5 crates (4 directly)

MIT/Apache

40KB
448 lines

axum-server-dual-protocol

Crates.io Version Live Build Status Docs.rs Documentation Main Documentation

Description

Provides utilities to host a axum-server server that accepts the HTTP and HTTPS protocol on the same port. See bind_dual_protocol().

A common use case for this is if a HTTPS server is hosted on a non-traditional port, having no corresponding HTTP port. This can be an issue for clients who try to connect over HTTP and get a connection reset error. See ServerExt::set_upgrade().

Usage

The simplest way to start is to use bind_dual_protocol():

let app = Router::new().route(
	"/",
	routing::get(|request: Request<Body>| async move {
		match request.extensions().get::<Protocol>().unwrap() {
			Protocol::Tls => "Hello, secure World!",
			Protocol::Plain => "Hello, insecure World!",
		}
	}),
);

// User-supplied certificate and private key.
let config = RustlsConfig::from_der(certificate, private_key).await?;

axum_server_dual_protocol::bind_dual_protocol(address, config)
	.serve(app.into_make_service())
	.await?;

We now have a server accepting both HTTP and HTTPS requests! Now we can automatically upgrade incoming HTTP requests to HTTPS using ServerExt::set_upgrade() like this:

use axum_server_dual_protocol::ServerExt;

axum_server_dual_protocol::bind_dual_protocol(address, config)
	.set_upgrade(true)
	.serve(app.into_make_service())
	.await?;

Alternatively UpgradeHttpLayer can be used:

let app = Router::new()
	.route("/", routing::get(|| async { "Hello, world!" }))
	.layer(UpgradeHttpLayer);

Features

default

By default the aws-lc-rs CryptoProvider is enabled.

Conditional Configurations

docsrs

This requires Rust nightly and enhances the documentation. It must only be used with RUSTDOCFLAGS, not with RUSTFLAGS.

MSRV

As this library heavily relies on axum-server, axum, tower and hyper the MSRV depends on theirs. At the point of time this was written the highest MSRV was axum with 1.66.

Changelog

See the CHANGELOG file for details.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~15–25MB
~448K SLoC