mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-07 00:00:15 +02:00
Deprecate Host and Scheme extractors
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#![allow(deprecated)]
|
||||
|
||||
use super::rejection::{FailedToResolveHost, HostRejection};
|
||||
use axum_core::{
|
||||
extract::{FromRequestParts, OptionalFromRequestParts},
|
||||
@@ -25,6 +27,7 @@ const X_FORWARDED_HOST_HEADER_KEY: &str = "X-Forwarded-Host";
|
||||
///
|
||||
/// Note that user agents can set `X-Forwarded-Host` and `Host` headers to arbitrary values so make
|
||||
/// sure to validate them to avoid security issues.
|
||||
#[deprecated = "will be removed in the next version; see https://github.com/tokio-rs/axum/issues/3442"]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Host(pub String);
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ mod scheme;
|
||||
#[cfg(feature = "optional-path")]
|
||||
pub use self::optional_path::OptionalPath;
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub use self::host::Host;
|
||||
|
||||
#[cfg(feature = "cached")]
|
||||
@@ -62,6 +63,7 @@ pub use self::query::{OptionalQueryRejection, Query, QueryRejection};
|
||||
#[cfg(feature = "multipart")]
|
||||
pub use self::multipart::Multipart;
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[cfg(feature = "scheme")]
|
||||
#[doc(no_inline)]
|
||||
pub use self::scheme::{Scheme, SchemeMissing};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Extractor that parses the scheme of a request.
|
||||
//! See [`Scheme`] for more details.
|
||||
#![allow(deprecated)]
|
||||
|
||||
use axum_core::{__define_rejection as define_rejection, extract::FromRequestParts};
|
||||
use http::{
|
||||
@@ -17,6 +18,7 @@ const X_FORWARDED_PROTO_HEADER_KEY: &str = "X-Forwarded-Proto";
|
||||
///
|
||||
/// Note that user agents can set the `X-Forwarded-Proto` header to arbitrary values so make
|
||||
/// sure to validate them to avoid security issues.
|
||||
#[deprecated = "will be removed in the next version; see https://github.com/tokio-rs/axum/issues/3442"]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Scheme(pub String);
|
||||
|
||||
|
||||
@@ -6,12 +6,11 @@
|
||||
|
||||
use axum::{
|
||||
handler::HandlerWithoutStateExt,
|
||||
http::{uri::Authority, StatusCode, Uri},
|
||||
http::{StatusCode, Uri},
|
||||
response::Redirect,
|
||||
routing::get,
|
||||
BoxError, Router,
|
||||
};
|
||||
use axum_extra::extract::Host;
|
||||
use axum_server::tls_rustls::RustlsConfig;
|
||||
use std::{future::Future, net::SocketAddr, path::PathBuf, time::Duration};
|
||||
use tokio::signal;
|
||||
@@ -106,33 +105,21 @@ async fn redirect_http_to_https<F>(ports: Ports, signal: F)
|
||||
where
|
||||
F: Future<Output = ()> + Send + 'static,
|
||||
{
|
||||
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");
|
||||
|
||||
@@ -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