mirror of https://github.com/fafhrd91/actix-web
Merge branch 'route-service' of github.com:ibraheemdev/actix-web into route-service
This commit is contained in:
commit
8f24d99ceb
|
@ -10,12 +10,15 @@
|
||||||
* Update `language-tags` to `0.3`.
|
* Update `language-tags` to `0.3`.
|
||||||
* `ServiceResponse::take_body`. [#2201]
|
* `ServiceResponse::take_body`. [#2201]
|
||||||
* `ServiceResponse::map_body` closure receives and returns `B` instead of `ResponseBody<B>` types. [#2201]
|
* `ServiceResponse::map_body` closure receives and returns `B` instead of `ResponseBody<B>` types. [#2201]
|
||||||
|
* `HttpServer::{listen_rustls(), bind_rustls()}` now honor the ALPN protocols in the configuation parameter. [#2226]
|
||||||
|
* `middleware::normalize` now will not try to normalize URIs with no valid path [#2246]
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* `HttpResponse::take_body` and old `HttpResponse::into_body` method that casted body type. [#2201]
|
* `HttpResponse::take_body` and old `HttpResponse::into_body` method that casted body type. [#2201]
|
||||||
|
|
||||||
[#2200]: https://github.com/actix/actix-web/pull/2200
|
[#2200]: https://github.com/actix/actix-web/pull/2200
|
||||||
[#2201]: https://github.com/actix/actix-web/pull/2201
|
[#2201]: https://github.com/actix/actix-web/pull/2201
|
||||||
|
[#2246]: https://github.com/actix/actix-web/pull/2246
|
||||||
|
|
||||||
|
|
||||||
## 4.0.0-beta.6 - 2021-04-17
|
## 4.0.0-beta.6 - 2021-04-17
|
||||||
|
|
|
@ -58,7 +58,7 @@ rustls = ["actix-http/rustls", "actix-tls/accept", "actix-tls/rustls"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-codec = "0.4.0"
|
actix-codec = "0.4.0"
|
||||||
actix-macros = "0.2.0"
|
actix-macros = "0.2.1"
|
||||||
actix-router = "0.2.7"
|
actix-router = "0.2.7"
|
||||||
actix-rt = "2.2"
|
actix-rt = "2.2"
|
||||||
actix-server = "2.0.0-beta.3"
|
actix-server = "2.0.0-beta.3"
|
||||||
|
|
|
@ -235,6 +235,8 @@ impl NamedFile {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set content encoding for serving this file
|
/// Set content encoding for serving this file
|
||||||
|
///
|
||||||
|
/// Must be used with [`actix_web::middleware::Compress`] to take effect.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_content_encoding(mut self, enc: ContentEncoding) -> Self {
|
pub fn set_content_encoding(mut self, enc: ContentEncoding) -> Self {
|
||||||
self.encoding = Some(enc);
|
self.encoding = Some(enc);
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
* Update `language-tags` to `0.3`.
|
* Update `language-tags` to `0.3`.
|
||||||
* Reduce the level from `error` to `debug` for the log line that is emitted when a `500 Internal Server Error` is built using `HttpResponse::from_error`. [#2201]
|
* Reduce the level from `error` to `debug` for the log line that is emitted when a `500 Internal Server Error` is built using `HttpResponse::from_error`. [#2201]
|
||||||
* `ResponseBuilder::message_body` now returns a `Result`. [#2201]
|
* `ResponseBuilder::message_body` now returns a `Result`. [#2201]
|
||||||
|
* `HttpServer::{listen_rustls(), bind_rustls()}` now honor the ALPN protocols in the configuation parameter. [#2226]
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* Stop re-exporting `http` crate's `HeaderMap` types in addition to ours. [#2171]
|
* Stop re-exporting `http` crate's `HeaderMap` types in addition to ours. [#2171]
|
||||||
|
|
|
@ -91,6 +91,7 @@ serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
tls-openssl = { version = "0.10", package = "openssl" }
|
tls-openssl = { version = "0.10", package = "openssl" }
|
||||||
tls-rustls = { version = "0.19", package = "rustls" }
|
tls-rustls = { version = "0.19", package = "rustls" }
|
||||||
|
webpki = { version = "0.21.0" }
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "ws"
|
name = "ws"
|
||||||
|
|
|
@ -171,7 +171,8 @@ mod rustls {
|
||||||
Error = TlsError<io::Error, DispatchError>,
|
Error = TlsError<io::Error, DispatchError>,
|
||||||
InitError = S::InitError,
|
InitError = S::InitError,
|
||||||
> {
|
> {
|
||||||
let protos = vec!["h2".to_string().into()];
|
let mut protos = vec![b"h2".to_vec()];
|
||||||
|
protos.extend_from_slice(&config.alpn_protocols);
|
||||||
config.set_protocols(&protos);
|
config.set_protocols(&protos);
|
||||||
|
|
||||||
Acceptor::new(config)
|
Acceptor::new(config)
|
||||||
|
|
|
@ -8,40 +8,42 @@ use http::header::{HeaderName, InvalidHeaderName};
|
||||||
|
|
||||||
pub trait AsHeaderName: Sealed {}
|
pub trait AsHeaderName: Sealed {}
|
||||||
|
|
||||||
|
pub struct Seal;
|
||||||
|
|
||||||
pub trait Sealed {
|
pub trait Sealed {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName>;
|
fn try_as_name(&self, seal: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sealed for HeaderName {
|
impl Sealed for HeaderName {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
Ok(Cow::Borrowed(self))
|
Ok(Cow::Borrowed(self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsHeaderName for HeaderName {}
|
impl AsHeaderName for HeaderName {}
|
||||||
|
|
||||||
impl Sealed for &HeaderName {
|
impl Sealed for &HeaderName {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
Ok(Cow::Borrowed(*self))
|
Ok(Cow::Borrowed(*self))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsHeaderName for &HeaderName {}
|
impl AsHeaderName for &HeaderName {}
|
||||||
|
|
||||||
impl Sealed for &str {
|
impl Sealed for &str {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
HeaderName::from_str(self).map(Cow::Owned)
|
HeaderName::from_str(self).map(Cow::Owned)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsHeaderName for &str {}
|
impl AsHeaderName for &str {}
|
||||||
|
|
||||||
impl Sealed for String {
|
impl Sealed for String {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
HeaderName::from_str(self).map(Cow::Owned)
|
HeaderName::from_str(self).map(Cow::Owned)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl AsHeaderName for String {}
|
impl AsHeaderName for String {}
|
||||||
|
|
||||||
impl Sealed for &String {
|
impl Sealed for &String {
|
||||||
fn try_as_name(&self) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
fn try_as_name(&self, _: Seal) -> Result<Cow<'_, HeaderName>, InvalidHeaderName> {
|
||||||
HeaderName::from_str(self).map(Cow::Owned)
|
HeaderName::from_str(self).map(Cow::Owned)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -213,7 +213,7 @@ impl HeaderMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_value(&self, key: impl AsHeaderName) -> Option<&Value> {
|
fn get_value(&self, key: impl AsHeaderName) -> Option<&Value> {
|
||||||
match key.try_as_name().ok()? {
|
match key.try_as_name(super::as_name::Seal).ok()? {
|
||||||
Cow::Borrowed(name) => self.inner.get(name),
|
Cow::Borrowed(name) => self.inner.get(name),
|
||||||
Cow::Owned(name) => self.inner.get(&name),
|
Cow::Owned(name) => self.inner.get(&name),
|
||||||
}
|
}
|
||||||
|
@ -279,7 +279,7 @@ impl HeaderMap {
|
||||||
/// assert!(map.get("INVALID HEADER NAME").is_none());
|
/// assert!(map.get("INVALID HEADER NAME").is_none());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn get_mut(&mut self, key: impl AsHeaderName) -> Option<&mut HeaderValue> {
|
pub fn get_mut(&mut self, key: impl AsHeaderName) -> Option<&mut HeaderValue> {
|
||||||
match key.try_as_name().ok()? {
|
match key.try_as_name(super::as_name::Seal).ok()? {
|
||||||
Cow::Borrowed(name) => self.inner.get_mut(name).map(|v| v.first_mut()),
|
Cow::Borrowed(name) => self.inner.get_mut(name).map(|v| v.first_mut()),
|
||||||
Cow::Owned(name) => self.inner.get_mut(&name).map(|v| v.first_mut()),
|
Cow::Owned(name) => self.inner.get_mut(&name).map(|v| v.first_mut()),
|
||||||
}
|
}
|
||||||
|
@ -327,7 +327,7 @@ impl HeaderMap {
|
||||||
/// assert!(map.contains_key(header::ACCEPT));
|
/// assert!(map.contains_key(header::ACCEPT));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn contains_key(&self, key: impl AsHeaderName) -> bool {
|
pub fn contains_key(&self, key: impl AsHeaderName) -> bool {
|
||||||
match key.try_as_name() {
|
match key.try_as_name(super::as_name::Seal) {
|
||||||
Ok(Cow::Borrowed(name)) => self.inner.contains_key(name),
|
Ok(Cow::Borrowed(name)) => self.inner.contains_key(name),
|
||||||
Ok(Cow::Owned(name)) => self.inner.contains_key(&name),
|
Ok(Cow::Owned(name)) => self.inner.contains_key(&name),
|
||||||
Err(_) => false,
|
Err(_) => false,
|
||||||
|
@ -410,7 +410,7 @@ impl HeaderMap {
|
||||||
///
|
///
|
||||||
/// assert!(map.is_empty());
|
/// assert!(map.is_empty());
|
||||||
pub fn remove(&mut self, key: impl AsHeaderName) -> Removed {
|
pub fn remove(&mut self, key: impl AsHeaderName) -> Removed {
|
||||||
let value = match key.try_as_name() {
|
let value = match key.try_as_name(super::as_name::Seal) {
|
||||||
Ok(Cow::Borrowed(name)) => self.inner.remove(name),
|
Ok(Cow::Borrowed(name)) => self.inner.remove(name),
|
||||||
Ok(Cow::Owned(name)) => self.inner.remove(&name),
|
Ok(Cow::Owned(name)) => self.inner.remove(&name),
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
|
|
|
@ -305,7 +305,8 @@ mod rustls {
|
||||||
Error = TlsError<io::Error, DispatchError>,
|
Error = TlsError<io::Error, DispatchError>,
|
||||||
InitError = (),
|
InitError = (),
|
||||||
> {
|
> {
|
||||||
let protos = vec!["h2".to_string().into(), "http/1.1".to_string().into()];
|
let mut protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
|
||||||
|
protos.extend_from_slice(&config.alpn_protocols);
|
||||||
config.set_protocols(&protos);
|
config.set_protocols(&protos);
|
||||||
|
|
||||||
Acceptor::new(config)
|
Acceptor::new(config)
|
||||||
|
|
|
@ -20,10 +20,15 @@ use futures_core::Stream;
|
||||||
use futures_util::stream::{once, StreamExt as _};
|
use futures_util::stream::{once, StreamExt as _};
|
||||||
use rustls::{
|
use rustls::{
|
||||||
internal::pemfile::{certs, pkcs8_private_keys},
|
internal::pemfile::{certs, pkcs8_private_keys},
|
||||||
NoClientAuth, ServerConfig as RustlsServerConfig,
|
NoClientAuth, ServerConfig as RustlsServerConfig, Session,
|
||||||
};
|
};
|
||||||
|
use webpki::DNSNameRef;
|
||||||
|
|
||||||
use std::io::{self, BufReader};
|
use std::{
|
||||||
|
io::{self, BufReader, Write},
|
||||||
|
net::{SocketAddr, TcpStream as StdTcpStream},
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
async fn load_body<S>(mut stream: S) -> Result<BytesMut, PayloadError>
|
async fn load_body<S>(mut stream: S) -> Result<BytesMut, PayloadError>
|
||||||
where
|
where
|
||||||
|
@ -52,6 +57,25 @@ fn tls_config() -> RustlsServerConfig {
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_negotiated_alpn_protocol(
|
||||||
|
addr: SocketAddr,
|
||||||
|
client_alpn_protocol: &[u8],
|
||||||
|
) -> Option<Vec<u8>> {
|
||||||
|
let mut config = rustls::ClientConfig::new();
|
||||||
|
config.alpn_protocols.push(client_alpn_protocol.to_vec());
|
||||||
|
let mut sess = rustls::ClientSession::new(
|
||||||
|
&Arc::new(config),
|
||||||
|
DNSNameRef::try_from_ascii_str("localhost").unwrap(),
|
||||||
|
);
|
||||||
|
let mut sock = StdTcpStream::connect(addr).unwrap();
|
||||||
|
let mut stream = rustls::Stream::new(&mut sess, &mut sock);
|
||||||
|
// The handshake will fails because the client will not be able to verify the server
|
||||||
|
// certificate, but it doesn't matter here as we are just interested in the negotiated ALPN
|
||||||
|
// protocol
|
||||||
|
let _ = stream.flush();
|
||||||
|
sess.get_alpn_protocol().map(|proto| proto.to_vec())
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_h1() -> io::Result<()> {
|
async fn test_h1() -> io::Result<()> {
|
||||||
let srv = test_server(move || {
|
let srv = test_server(move || {
|
||||||
|
@ -460,3 +484,85 @@ async fn test_h1_service_error() {
|
||||||
let bytes = srv.load_body(response).await.unwrap();
|
let bytes = srv.load_body(response).await.unwrap();
|
||||||
assert_eq!(bytes, Bytes::from_static(b"error"));
|
assert_eq!(bytes, Bytes::from_static(b"error"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const H2_ALPN_PROTOCOL: &[u8] = b"h2";
|
||||||
|
const HTTP1_1_ALPN_PROTOCOL: &[u8] = b"http/1.1";
|
||||||
|
const CUSTOM_ALPN_PROTOCOL: &[u8] = b"custom";
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_alpn_h1() -> io::Result<()> {
|
||||||
|
let srv = test_server(move || {
|
||||||
|
let mut config = tls_config();
|
||||||
|
config.alpn_protocols.push(CUSTOM_ALPN_PROTOCOL.to_vec());
|
||||||
|
HttpService::build()
|
||||||
|
.h1(|_| ok::<_, Error>(Response::ok()))
|
||||||
|
.rustls(config)
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
get_negotiated_alpn_protocol(srv.addr(), CUSTOM_ALPN_PROTOCOL),
|
||||||
|
Some(CUSTOM_ALPN_PROTOCOL.to_vec())
|
||||||
|
);
|
||||||
|
|
||||||
|
let response = srv.sget("/").send().await.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_alpn_h2() -> io::Result<()> {
|
||||||
|
let srv = test_server(move || {
|
||||||
|
let mut config = tls_config();
|
||||||
|
config.alpn_protocols.push(CUSTOM_ALPN_PROTOCOL.to_vec());
|
||||||
|
HttpService::build()
|
||||||
|
.h2(|_| ok::<_, Error>(Response::ok()))
|
||||||
|
.rustls(config)
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
get_negotiated_alpn_protocol(srv.addr(), H2_ALPN_PROTOCOL),
|
||||||
|
Some(H2_ALPN_PROTOCOL.to_vec())
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_negotiated_alpn_protocol(srv.addr(), CUSTOM_ALPN_PROTOCOL),
|
||||||
|
Some(CUSTOM_ALPN_PROTOCOL.to_vec())
|
||||||
|
);
|
||||||
|
|
||||||
|
let response = srv.sget("/").send().await.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn test_alpn_h2_1() -> io::Result<()> {
|
||||||
|
let srv = test_server(move || {
|
||||||
|
let mut config = tls_config();
|
||||||
|
config.alpn_protocols.push(CUSTOM_ALPN_PROTOCOL.to_vec());
|
||||||
|
HttpService::build()
|
||||||
|
.finish(|_| ok::<_, Error>(Response::ok()))
|
||||||
|
.rustls(config)
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
get_negotiated_alpn_protocol(srv.addr(), H2_ALPN_PROTOCOL),
|
||||||
|
Some(H2_ALPN_PROTOCOL.to_vec())
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_negotiated_alpn_protocol(srv.addr(), HTTP1_1_ALPN_PROTOCOL),
|
||||||
|
Some(HTTP1_1_ALPN_PROTOCOL.to_vec())
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
get_negotiated_alpn_protocol(srv.addr(), CUSTOM_ALPN_PROTOCOL),
|
||||||
|
Some(CUSTOM_ALPN_PROTOCOL.to_vec())
|
||||||
|
);
|
||||||
|
|
||||||
|
let response = srv.sget("/").send().await.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
@ -171,27 +171,10 @@ method_macro! {
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_attribute]
|
||||||
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
let input = syn::parse_macro_input!(item as syn::ItemFn);
|
||||||
let mut input = syn::parse_macro_input!(item as syn::ItemFn);
|
|
||||||
let attrs = &input.attrs;
|
|
||||||
let vis = &input.vis;
|
|
||||||
let sig = &mut input.sig;
|
|
||||||
let body = &input.block;
|
|
||||||
|
|
||||||
if sig.asyncness.is_none() {
|
|
||||||
return syn::Error::new_spanned(sig.fn_token, "only async fn is supported")
|
|
||||||
.to_compile_error()
|
|
||||||
.into();
|
|
||||||
}
|
|
||||||
|
|
||||||
sig.asyncness = None;
|
|
||||||
|
|
||||||
(quote! {
|
(quote! {
|
||||||
#(#attrs)*
|
#[actix_web::rt::main(system = "::actix_web::rt::System")]
|
||||||
#vis #sig {
|
#input
|
||||||
actix_web::rt::System::new()
|
|
||||||
.block_on(async move { #body })
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
|
@ -170,6 +170,8 @@ pub mod dev {
|
||||||
fn get_encoding(&self) -> Option<ContentEncoding>;
|
fn get_encoding(&self) -> Option<ContentEncoding>;
|
||||||
|
|
||||||
/// Set content encoding
|
/// Set content encoding
|
||||||
|
///
|
||||||
|
/// Must be used with [`crate::middleware::Compress`] to take effect.
|
||||||
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self;
|
fn encoding(&mut self, encoding: ContentEncoding) -> &mut Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -137,53 +137,57 @@ where
|
||||||
|
|
||||||
let original_path = head.uri.path();
|
let original_path = head.uri.path();
|
||||||
|
|
||||||
// Either adds a string to the end (duplicates will be removed anyways) or trims all slashes from the end
|
// An empty path here means that the URI has no valid path. We skip normalization in this
|
||||||
let path = match self.trailing_slash_behavior {
|
// case, because adding a path can make the URI invalid
|
||||||
TrailingSlash::Always => original_path.to_string() + "/",
|
if !original_path.is_empty() {
|
||||||
TrailingSlash::MergeOnly => original_path.to_string(),
|
// Either adds a string to the end (duplicates will be removed anyways) or trims all
|
||||||
TrailingSlash::Trim => original_path.trim_end_matches('/').to_string(),
|
// slashes from the end
|
||||||
};
|
let path = match self.trailing_slash_behavior {
|
||||||
|
TrailingSlash::Always => format!("{}/", original_path),
|
||||||
// normalize multiple /'s to one /
|
TrailingSlash::MergeOnly => original_path.to_string(),
|
||||||
let path = self.merge_slash.replace_all(&path, "/");
|
TrailingSlash::Trim => original_path.trim_end_matches('/').to_string(),
|
||||||
|
|
||||||
// Ensure root paths are still resolvable. If resulting path is blank after previous step
|
|
||||||
// it means the path was one or more slashes. Reduce to single slash.
|
|
||||||
let path = if path.is_empty() { "/" } else { path.as_ref() };
|
|
||||||
|
|
||||||
// Check whether the path has been changed
|
|
||||||
//
|
|
||||||
// This check was previously implemented as string length comparison
|
|
||||||
//
|
|
||||||
// That approach fails when a trailing slash is added,
|
|
||||||
// and a duplicate slash is removed,
|
|
||||||
// since the length of the strings remains the same
|
|
||||||
//
|
|
||||||
// For example, the path "/v1//s" will be normalized to "/v1/s/"
|
|
||||||
// Both of the paths have the same length,
|
|
||||||
// so the change can not be deduced from the length comparison
|
|
||||||
if path != original_path {
|
|
||||||
let mut parts = head.uri.clone().into_parts();
|
|
||||||
let query = parts.path_and_query.as_ref().and_then(|pq| pq.query());
|
|
||||||
|
|
||||||
let path = if let Some(q) = query {
|
|
||||||
Bytes::from(format!("{}?{}", path, q))
|
|
||||||
} else {
|
|
||||||
Bytes::copy_from_slice(path.as_bytes())
|
|
||||||
};
|
};
|
||||||
parts.path_and_query = Some(PathAndQuery::from_maybe_shared(path).unwrap());
|
|
||||||
|
|
||||||
let uri = Uri::from_parts(parts).unwrap();
|
// normalize multiple /'s to one /
|
||||||
req.match_info_mut().get_mut().update(&uri);
|
let path = self.merge_slash.replace_all(&path, "/");
|
||||||
req.head_mut().uri = uri;
|
|
||||||
|
// Ensure root paths are still resolvable. If resulting path is blank after previous
|
||||||
|
// step it means the path was one or more slashes. Reduce to single slash.
|
||||||
|
let path = if path.is_empty() { "/" } else { path.as_ref() };
|
||||||
|
|
||||||
|
// Check whether the path has been changed
|
||||||
|
//
|
||||||
|
// This check was previously implemented as string length comparison
|
||||||
|
//
|
||||||
|
// That approach fails when a trailing slash is added,
|
||||||
|
// and a duplicate slash is removed,
|
||||||
|
// since the length of the strings remains the same
|
||||||
|
//
|
||||||
|
// For example, the path "/v1//s" will be normalized to "/v1/s/"
|
||||||
|
// Both of the paths have the same length,
|
||||||
|
// so the change can not be deduced from the length comparison
|
||||||
|
if path != original_path {
|
||||||
|
let mut parts = head.uri.clone().into_parts();
|
||||||
|
let query = parts.path_and_query.as_ref().and_then(|pq| pq.query());
|
||||||
|
|
||||||
|
let path = match query {
|
||||||
|
Some(q) => Bytes::from(format!("{}?{}", path, q)),
|
||||||
|
None => Bytes::copy_from_slice(path.as_bytes()),
|
||||||
|
};
|
||||||
|
parts.path_and_query = Some(PathAndQuery::from_maybe_shared(path).unwrap());
|
||||||
|
|
||||||
|
let uri = Uri::from_parts(parts).unwrap();
|
||||||
|
req.match_info_mut().get_mut().update(&uri);
|
||||||
|
req.head_mut().uri = uri;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.service.call(req)
|
self.service.call(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use actix_http::StatusCode;
|
||||||
use actix_service::IntoService;
|
use actix_service::IntoService;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -387,6 +391,22 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_rt::test]
|
||||||
|
async fn no_path() {
|
||||||
|
let app = init_service(
|
||||||
|
App::new()
|
||||||
|
.wrap(NormalizePath::default())
|
||||||
|
.service(web::resource("/").to(HttpResponse::Ok)),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// This URI will be interpreted as an authority form, i.e. there is no path nor scheme
|
||||||
|
// (https://datatracker.ietf.org/doc/html/rfc7230#section-5.3.3)
|
||||||
|
let req = TestRequest::with_uri("eh").to_request();
|
||||||
|
let res = call_service(&app, req).await;
|
||||||
|
assert_eq!(res.status(), StatusCode::NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_in_place_normalization() {
|
async fn test_in_place_normalization() {
|
||||||
let srv = |req: ServiceRequest| {
|
let srv = |req: ServiceRequest| {
|
||||||
|
|
|
@ -368,7 +368,7 @@ where
|
||||||
#[cfg(feature = "rustls")]
|
#[cfg(feature = "rustls")]
|
||||||
/// Use listener for accepting incoming tls connection requests
|
/// Use listener for accepting incoming tls connection requests
|
||||||
///
|
///
|
||||||
/// This method sets alpn protocols to "h2" and "http/1.1"
|
/// This method prepends alpn protocols "h2" and "http/1.1" to configured ones
|
||||||
pub fn listen_rustls(
|
pub fn listen_rustls(
|
||||||
self,
|
self,
|
||||||
lst: net::TcpListener,
|
lst: net::TcpListener,
|
||||||
|
@ -482,7 +482,7 @@ where
|
||||||
#[cfg(feature = "rustls")]
|
#[cfg(feature = "rustls")]
|
||||||
/// Start listening for incoming tls connections.
|
/// Start listening for incoming tls connections.
|
||||||
///
|
///
|
||||||
/// This method sets alpn protocols to "h2" and "http/1.1"
|
/// This method prepends alpn protocols "h2" and "http/1.1" to configured ones
|
||||||
pub fn bind_rustls<A: net::ToSocketAddrs>(
|
pub fn bind_rustls<A: net::ToSocketAddrs>(
|
||||||
mut self,
|
mut self,
|
||||||
addr: A,
|
addr: A,
|
||||||
|
|
Loading…
Reference in New Issue