Merge branch 'master' into h1-test

This commit is contained in:
Rob Ede 2020-12-23 00:19:27 +00:00 committed by GitHub
commit 6de9b942e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 30 deletions

View File

@ -1,6 +1,9 @@
# Changes
## Unreleased - 2020-xx-xx
### Changed
* Bumped `rand` to `0.8`
### Fixed
* added the actual parsing error to `test::read_body_json` [#1812]

View File

@ -112,7 +112,7 @@ tinyvec = { version = "1", features = ["alloc"] }
[dev-dependencies]
actix = "0.10.0"
actix-http = { version = "2.1.0", features = ["actors"] }
rand = "0.7"
rand = "0.8"
env_logger = "0.8"
serde_derive = "1.0"
brotli2 = "0.3.2"

View File

@ -1,7 +1,8 @@
# Changes
## Unreleased - 2020-xx-xx
### Changed
* Bumped `rand` to `0.8`
## 2.2.0 - 2020-11-25
### Added

View File

@ -72,7 +72,7 @@ log = "0.4"
mime = "0.3"
percent-encoding = "2.1"
pin-project = "1.0.0"
rand = "0.7"
rand = "0.8"
regex = "1.3"
serde = "1.0"
serde_json = "1.0"

View File

@ -1,6 +1,8 @@
# Changes
## Unreleased - 2020-xx-xx
### Changed
* Bumped `rand` to `0.8`
## 2.0.3 - 2020-11-29

View File

@ -50,7 +50,7 @@ futures-core = { version = "0.3.5", default-features = false }
log =" 0.4"
mime = "0.3"
percent-encoding = "2.1"
rand = "0.7"
rand = "0.8"
serde = "1.0"
serde_json = "1.0"
serde_urlencoded = "0.7"

View File

@ -480,6 +480,7 @@ async fn test_client_gzip_encoding_large_random() {
let data = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(100_000)
.map(char::from)
.collect::<String>();
let srv = test::start(|| {
@ -529,6 +530,7 @@ async fn test_client_brotli_encoding_large_random() {
let data = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(70_000)
.map(char::from)
.collect::<String>();
let srv = test::start(|| {

View File

@ -1,10 +1,11 @@
//! `Middleware` to normalize request's URI
//! For middleware documentation, see [`NormalizePath`].
use std::task::{Context, Poll};
use actix_http::http::{PathAndQuery, Uri};
use actix_service::{Service, Transform};
use bytes::Bytes;
use futures_util::future::{ok, Ready};
use futures_util::future::{ready, Ready};
use regex::Regex;
use crate::service::{ServiceRequest, ServiceResponse};
@ -17,10 +18,12 @@ pub enum TrailingSlash {
/// Always add a trailing slash to the end of the path.
/// This will require all routes to end in a trailing slash for them to be accessible.
Always,
/// Only merge any present multiple trailing slashes.
///
/// Note: This option provides the best compatibility with the v2 version of this middlware.
/// Note: This option provides the best compatibility with the v2 version of this middleware.
MergeOnly,
/// Trim trailing slashes from the end of the path.
Trim,
}
@ -32,28 +35,53 @@ impl Default for TrailingSlash {
}
#[derive(Default, Clone, Copy)]
/// `Middleware` to normalize request's URI in place
/// Middleware to normalize a request's path so that routes can be matched less strictly.
///
/// Performs following:
///
/// - Merges multiple slashes into one.
/// # Normalization Steps
/// - Merges multiple consecutive slashes into one. (For example, `/path//one` always
/// becomes `/path/one`.)
/// - Appends a trailing slash if one is not present, removes one if present, or keeps trailing
/// slashes as-is, depending on the supplied `TrailingSlash` variant.
/// slashes as-is, depending on which [`TrailingSlash`] variant is supplied
/// to [`new`](NormalizePath::new()).
///
/// # Default Behavior
/// The default constructor chooses to strip trailing slashes from the end
/// ([`TrailingSlash::Trim`]), the effect is that route definitions should be defined without
/// trailing slashes or else they will be inaccessible.
///
/// # Example
/// ```rust
/// use actix_web::{web, http, middleware, App, HttpResponse};
/// use actix_web::{web, middleware, App};
///
/// # fn main() {
/// # #[actix_rt::test]
/// # async fn normalize() {
/// let app = App::new()
/// .wrap(middleware::NormalizePath::default())
/// .service(
/// web::resource("/test")
/// .route(web::get().to(|| HttpResponse::Ok()))
/// .route(web::method(http::Method::HEAD).to(|| HttpResponse::MethodNotAllowed()))
/// );
/// .route("/test", web::get().to(|| async { "test" }))
/// .route("/unmatchable/", web::get().to(|| async { "unmatchable" }));
///
/// use actix_web::http::StatusCode;
/// use actix_web::test::{call_service, init_service, TestRequest};
///
/// let mut app = init_service(app).await;
///
/// let req = TestRequest::with_uri("/test").to_request();
/// let res = call_service(&mut app, req).await;
/// assert_eq!(res.status(), StatusCode::OK);
///
/// let req = TestRequest::with_uri("/test/").to_request();
/// let res = call_service(&mut app, req).await;
/// assert_eq!(res.status(), StatusCode::OK);
///
/// let req = TestRequest::with_uri("/unmatchable").to_request();
/// let res = call_service(&mut app, req).await;
/// assert_eq!(res.status(), StatusCode::NOT_FOUND);
///
/// let req = TestRequest::with_uri("/unmatchable/").to_request();
/// let res = call_service(&mut app, req).await;
/// assert_eq!(res.status(), StatusCode::NOT_FOUND);
/// # }
/// ```
pub struct NormalizePath(TrailingSlash);
impl NormalizePath {
@ -76,11 +104,11 @@ where
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ok(NormalizePathNormalization {
ready(Ok(NormalizePathNormalization {
service,
merge_slash: Regex::new("//+").unwrap(),
trailing_slash_behavior: self.0,
})
}))
}
}
@ -160,9 +188,11 @@ mod tests {
use actix_service::IntoService;
use super::*;
use crate::dev::ServiceRequest;
use crate::test::{call_service, init_service, TestRequest};
use crate::{web, App, HttpResponse};
use crate::{
dev::ServiceRequest,
test::{call_service, init_service, TestRequest},
web, App, HttpResponse,
};
#[actix_rt::test]
async fn test_wrap() {
@ -244,7 +274,7 @@ mod tests {
}
#[actix_rt::test]
async fn keep_trailing_slash_unchange() {
async fn keep_trailing_slash_unchanged() {
let mut app = init_service(
App::new()
.wrap(NormalizePath(TrailingSlash::MergeOnly))
@ -279,7 +309,7 @@ mod tests {
async fn test_in_place_normalization() {
let srv = |req: ServiceRequest| {
assert_eq!("/v1/something/", req.path());
ok(req.into_response(HttpResponse::Ok().finish()))
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
};
let mut normalize = NormalizePath::default()
@ -310,7 +340,7 @@ mod tests {
let srv = |req: ServiceRequest| {
assert_eq!(URI, req.path());
ok(req.into_response(HttpResponse::Ok().finish()))
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
};
let mut normalize = NormalizePath::default()
@ -324,12 +354,12 @@ mod tests {
}
#[actix_rt::test]
async fn should_normalize_notrail() {
async fn should_normalize_no_trail() {
const URI: &str = "/v1/something";
let srv = |req: ServiceRequest| {
assert_eq!(URI.to_string() + "/", req.path());
ok(req.into_response(HttpResponse::Ok().finish()))
ready(Ok(req.into_response(HttpResponse::Ok().finish())))
};
let mut normalize = NormalizePath::default()

View File

@ -248,6 +248,7 @@ async fn test_body_gzip_large_random() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(70_000)
.map(char::from)
.collect::<String>();
let srv_data = data.clone();
@ -529,6 +530,7 @@ async fn test_reading_gzip_encoding_large_random() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(60_000)
.map(char::from)
.collect::<String>();
let srv = test::start_with(test::config().h1(), || {
@ -614,6 +616,7 @@ async fn test_reading_deflate_encoding_large_random() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(160_000)
.map(char::from)
.collect::<String>();
let srv = test::start_with(test::config().h1(), || {
@ -672,6 +675,7 @@ async fn test_brotli_encoding_large() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(320_000)
.map(char::from)
.collect::<String>();
let srv = test::start_with(test::config().h1(), || {
@ -753,6 +757,7 @@ async fn test_reading_deflate_encoding_large_random_rustls() {
let data = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(160_000)
.map(char::from)
.collect::<String>();
// load ssl keys