mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-23 00:00:15 +02:00
Deprecate Host and Scheme extractors
This commit is contained in:
@@ -13,7 +13,6 @@ use axum::{
|
||||
routing::get,
|
||||
BoxError, Router,
|
||||
};
|
||||
use axum_extra::extract::Host;
|
||||
use axum_server::tls_rustls::RustlsConfig;
|
||||
use std::{net::SocketAddr, path::PathBuf};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
@@ -72,33 +71,21 @@ async fn handler() -> &'static str {
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn redirect_http_to_https(ports: Ports) {
|
||||
fn make_https(host: &str, uri: Uri, https_port: u16) -> Result<Uri, BoxError> {
|
||||
fn make_https(uri: Uri, https_port: u16) -> Result<Uri, BoxError> {
|
||||
let mut parts = uri.into_parts();
|
||||
|
||||
parts.scheme = Some(axum::http::uri::Scheme::HTTPS);
|
||||
parts.authority = Some(format!("localhost:{https_port}").parse()?);
|
||||
|
||||
if parts.path_and_query.is_none() {
|
||||
parts.path_and_query = Some("/".parse().unwrap());
|
||||
}
|
||||
|
||||
let authority: Authority = host.parse()?;
|
||||
let bare_host = match authority.port() {
|
||||
Some(port_struct) => authority
|
||||
.as_str()
|
||||
.strip_suffix(port_struct.as_str())
|
||||
.unwrap()
|
||||
.strip_suffix(':')
|
||||
.unwrap(), // if authority.port() is Some(port) then we can be sure authority ends with :{port}
|
||||
None => authority.as_str(),
|
||||
};
|
||||
|
||||
parts.authority = Some(format!("{bare_host}:{https_port}").parse()?);
|
||||
|
||||
Ok(Uri::from_parts(parts)?)
|
||||
}
|
||||
|
||||
let redirect = move |Host(host): Host, uri: Uri| async move {
|
||||
match make_https(&host, uri, ports.https) {
|
||||
let redirect = move |uri: Uri| async move {
|
||||
match make_https(uri, ports.https) {
|
||||
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
||||
|
||||
Reference in New Issue
Block a user