mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-04 22:14:44 +01:00
Update TLS examples to use better HTTP->HTTPS redirect
This commit is contained in:
parent
fcb45b8d32
commit
9242b9d196
2 changed files with 30 additions and 10 deletions
|
@ -7,7 +7,7 @@
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Host,
|
extract::Host,
|
||||||
handler::HandlerWithoutStateExt,
|
handler::HandlerWithoutStateExt,
|
||||||
http::{StatusCode, Uri},
|
http::{uri::Authority, StatusCode, Uri},
|
||||||
response::Redirect,
|
response::Redirect,
|
||||||
routing::get,
|
routing::get,
|
||||||
BoxError, Router,
|
BoxError, Router,
|
||||||
|
@ -106,7 +106,7 @@ async fn redirect_http_to_https<F>(ports: Ports, signal: F)
|
||||||
where
|
where
|
||||||
F: Future<Output = ()> + Send + 'static,
|
F: Future<Output = ()> + Send + 'static,
|
||||||
{
|
{
|
||||||
fn make_https(host: String, uri: Uri, ports: Ports) -> Result<Uri, BoxError> {
|
fn make_https(host: &str, uri: Uri, https_port: u16) -> Result<Uri, BoxError> {
|
||||||
let mut parts = uri.into_parts();
|
let mut parts = uri.into_parts();
|
||||||
|
|
||||||
parts.scheme = Some(axum::http::uri::Scheme::HTTPS);
|
parts.scheme = Some(axum::http::uri::Scheme::HTTPS);
|
||||||
|
@ -115,14 +115,24 @@ where
|
||||||
parts.path_and_query = Some("/".parse().unwrap());
|
parts.path_and_query = Some("/".parse().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let https_host = host.replace(&ports.http.to_string(), &ports.https.to_string());
|
let authority: Authority = host.parse()?;
|
||||||
parts.authority = Some(https_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)?)
|
Ok(Uri::from_parts(parts)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
let redirect = move |Host(host): Host, uri: Uri| async move {
|
let redirect = move |Host(host): Host, uri: Uri| async move {
|
||||||
match make_https(host, uri, ports) {
|
match make_https(&host, uri, ports.https) {
|
||||||
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
|
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Host,
|
extract::Host,
|
||||||
handler::HandlerWithoutStateExt,
|
handler::HandlerWithoutStateExt,
|
||||||
http::{StatusCode, Uri},
|
http::{uri::Authority, StatusCode, Uri},
|
||||||
response::Redirect,
|
response::Redirect,
|
||||||
routing::get,
|
routing::get,
|
||||||
BoxError, Router,
|
BoxError, Router,
|
||||||
|
@ -72,7 +72,7 @@ async fn handler() -> &'static str {
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
async fn redirect_http_to_https(ports: Ports) {
|
async fn redirect_http_to_https(ports: Ports) {
|
||||||
fn make_https(host: String, uri: Uri, ports: Ports) -> Result<Uri, BoxError> {
|
fn make_https(host: &str, uri: Uri, https_port: u16) -> Result<Uri, BoxError> {
|
||||||
let mut parts = uri.into_parts();
|
let mut parts = uri.into_parts();
|
||||||
|
|
||||||
parts.scheme = Some(axum::http::uri::Scheme::HTTPS);
|
parts.scheme = Some(axum::http::uri::Scheme::HTTPS);
|
||||||
|
@ -81,14 +81,24 @@ async fn redirect_http_to_https(ports: Ports) {
|
||||||
parts.path_and_query = Some("/".parse().unwrap());
|
parts.path_and_query = Some("/".parse().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let https_host = host.replace(&ports.http.to_string(), &ports.https.to_string());
|
let authority: Authority = host.parse()?;
|
||||||
parts.authority = Some(https_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)?)
|
Ok(Uri::from_parts(parts)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
let redirect = move |Host(host): Host, uri: Uri| async move {
|
let redirect = move |Host(host): Host, uri: Uri| async move {
|
||||||
match make_https(host, uri, ports) {
|
match make_https(&host, uri, ports.https) {
|
||||||
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
|
Ok(uri) => Ok(Redirect::permanent(&uri.to_string())),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
tracing::warn!(%error, "failed to convert URI to HTTPS");
|
||||||
|
|
Loading…
Reference in a new issue