diff --git a/.circleci/config.yml b/.circleci/config.yml index 0cf26d7c..c5a5236e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,11 +1,11 @@ version: 2.1 orbs: - codecov: codecov/codecov@3.2.5 + codecov: codecov/codecov@3.3.0 jobs: clippy: - docker: [image: cimg/rust:1.72.0] + docker: [image: cimg/rust:1.74.0] steps: - checkout - run: cargo --version @@ -18,7 +18,7 @@ jobs: - './target' test: - docker: [image: cimg/rust:1.72.0] + docker: [image: cimg/rust:1.74.0] steps: - checkout - run: cargo --version @@ -35,7 +35,7 @@ jobs: - './target' doc-test: - docker: [image: cimg/rust:1.72.0] + docker: [image: cimg/rust:1.74.0] steps: - checkout - run: cargo --version @@ -49,7 +49,7 @@ jobs: - './target' test-coverage: - docker: [image: cimg/rust:1.72.0] + docker: [image: cimg/rust:1.74.0] steps: - checkout - run: cargo --version @@ -68,7 +68,7 @@ jobs: - './target' feature-checks: - docker: [image: cimg/rust:1.72.0] + docker: [image: cimg/rust:1.74.0] steps: - checkout - run: cargo --version diff --git a/actix-web-lab/CHANGELOG.md b/actix-web-lab/CHANGELOG.md index a505d630..0178b759 100644 --- a/actix-web-lab/CHANGELOG.md +++ b/actix-web-lab/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Add `redirect_to_non_www` fn middleware. + ## 0.20.0 - Add `sse::Sse::from_infallible_stream()` method. diff --git a/actix-web-lab/README.md b/actix-web-lab/README.md index 34caeda4..517f88df 100644 --- a/actix-web-lab/README.md +++ b/actix-web-lab/README.md @@ -35,6 +35,7 @@ - `map_response_body`: use an async function to modify the response body [(docs)](https://docs.rs/actix-web-lab/0.20.0/actix_web_lab/middleware/fn.map_response_body.html) - `RedirectHttps`: middleware to redirect traffic to HTTPS if connection is insecure with optional HSTS [(docs)](https://docs.rs/actix-web-lab/0.20.0/actix_web_lab/middleware/struct.RedirectHttps.html) - `redirect_to_www`: function middleware to redirect traffic to `www.` if not already there [(docs)](https://docs.rs/actix-web-lab/0.20.0/actix_web_lab/middleware/fn.redirect_to_www.html) +- `redirect_to_non_www`: function middleware to redirect traffic to `www.` if not already there [(docs)](https://docs.rs/actix-web-lab/0.20.0/actix_web_lab/middleware/fn.redirect_to_non_www.html) - `ErrorHandlers`: alternative error handler middleware with simpler interface [(docs)](https://docs.rs/actix-web-lab/0.20.0/actix_web_lab/middleware/struct.ErrorHandlers.html) - `NormalizePath`: alternative path normalizing middleware with redirect option [(docs)](https://docs.rs/actix-web-lab/0.20.0/actix_web_lab/middleware/struct.NormalizePath.html) - `CatchPanic`: catch panics in wrapped handlers and middleware, returning empty 500 responses [(docs)](https://docs.rs/actix-web-lab/0.20.0/actix_web_lab/middleware/struct.CatchPanic.html) diff --git a/actix-web-lab/src/header.rs b/actix-web-lab/src/header.rs index 9df702cb..4630f12d 100644 --- a/actix-web-lab/src/header.rs +++ b/actix-web-lab/src/header.rs @@ -1,5 +1,7 @@ //! Experimental typed headers. +#[cfg(test)] +pub(crate) use self::header_test_helpers::{assert_parse_eq, assert_parse_fail}; pub use crate::{ cache_control::{CacheControl, CacheDirective}, content_length::ContentLength, @@ -51,6 +53,3 @@ mod header_test_helpers { H::parse(&req).unwrap_err(); } } - -#[cfg(test)] -pub(crate) use header_test_helpers::{assert_parse_eq, assert_parse_fail}; diff --git a/actix-web-lab/src/redirect_to_non_www.rs b/actix-web-lab/src/redirect_to_non_www.rs index 9810cddb..ce8e3ca0 100644 --- a/actix-web-lab/src/redirect_to_non_www.rs +++ b/actix-web-lab/src/redirect_to_non_www.rs @@ -7,9 +7,10 @@ use actix_web::{ use crate::middleware_from_fn::Next; -/// A function middleware to redirect traffic from `www.` to base host. +/// A function middleware to redirect traffic away from `www.` if it's present. /// /// # Examples +/// /// ``` /// # use actix_web::App; /// use actix_web_lab::middleware::{from_fn, redirect_to_non_www}; @@ -26,11 +27,10 @@ pub async fn redirect_to_non_www( let (req, pl) = req.into_parts(); let conn_info = req.connection_info(); - if conn_info.host().starts_with("www.") { + if let Some(host_no_www) = conn_info.host().strip_prefix("www.") { let scheme = conn_info.scheme(); - let host = conn_info.host()[4..].to_string(); // Skipping www. let path = req.uri().path(); - let uri = format!("{scheme}://{host}{path}"); + let uri = format!("{scheme}://{host_no_www}{path}"); let res = Redirect::to(uri).respond_to(&req); @@ -44,7 +44,7 @@ pub async fn redirect_to_non_www( } #[cfg(test)] -mod test_super { +mod tests { use actix_web::{ dev::ServiceFactory, http::{header, StatusCode}, @@ -81,7 +81,7 @@ mod test_super { let loc = res.headers().get(header::LOCATION); assert!(loc.is_some()); - assert_eq!(loc.unwrap().as_bytes().starts_with(b"http://www"), false); + assert!(!loc.unwrap().as_bytes().starts_with(b"http://www.")); let body = test::read_body(res).await; assert!(body.is_empty()); diff --git a/actix-web-lab/src/redirect_to_www.rs b/actix-web-lab/src/redirect_to_www.rs index 5da0ccd4..061dd012 100644 --- a/actix-web-lab/src/redirect_to_www.rs +++ b/actix-web-lab/src/redirect_to_www.rs @@ -10,6 +10,7 @@ use crate::middleware_from_fn::Next; /// A function middleware to redirect traffic to `www.` if not already there. /// /// # Examples +/// /// ``` /// # use actix_web::App; /// use actix_web_lab::middleware::{from_fn, redirect_to_www}; @@ -44,7 +45,7 @@ pub async fn redirect_to_www( } #[cfg(test)] -mod test_super { +mod tests { use actix_web::{ dev::ServiceFactory, http::{header, StatusCode}, @@ -79,7 +80,7 @@ mod test_super { let loc = res.headers().get(header::LOCATION); assert!(loc.is_some()); - assert!(loc.unwrap().as_bytes().starts_with(b"http://www")); + assert!(loc.unwrap().as_bytes().starts_with(b"http://www.")); let body = test::read_body(res).await; assert!(body.is_empty());