Skip to content

Commit

Permalink
refactor: use strip prefix in redirect_to_non_www
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Nov 18, 2023
1 parent b5e2f06 commit 781de4e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
12 changes: 6 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions actix-web-lab/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add `redirect_to_non_www` fn middleware.

## 0.20.0

- Add `sse::Sse::from_infallible_stream()` method.
Expand Down
1 change: 1 addition & 0 deletions actix-web-lab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions actix-web-lab/src/header.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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};
12 changes: 6 additions & 6 deletions actix-web-lab/src/redirect_to_non_www.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);

Expand All @@ -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},
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 3 additions & 2 deletions actix-web-lab/src/redirect_to_www.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 781de4e

Please sign in to comment.