Crate axum_server_dual_protocol

Source
Expand description

§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(|extensions: Extensions| async move {
		match 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.

Re-exports§

pub use axum_server;
pub use bytes;
pub use http;
pub use http_body_util;
pub use tokio;
pub use tokio_rustls;
pub use tokio_util;
pub use tower_service;

Structs§

DualProtocolAcceptor
Simultaneous HTTP and HTTPS Acceptor.
DualProtocolAcceptorFuture
Future type for DualProtocolAcceptor.
DualProtocolService
Service wrapping user-supplied app to apply global Layers according to configuration.
DualProtocolServiceFuture
Future type for DualProtocolService.
UpgradeHttp
Service upgrading HTTP requests to HTTPS by using a 301 “Moved Permanently” status code.
UpgradeHttpFuture
Future type for UpgradeHttp.
UpgradeHttpLayer
Layer upgrading HTTP requests to HTTPS.

Enums§

Protocol
The protocol used by this connection. See Request::extensions().

Traits§

ServerExt
Supplies configuration methods for Server with DualProtocolAcceptor.

Functions§

bind_dual_protocol
Create a Server that will bind to the provided address, accepting both HTTP and HTTPS on the same port.
from_tcp_dual_protocol
Create a Server from an existing TcpListener, accepting both HTTP and HTTPS on the same port.