Skip to content

Commit

Permalink
support Host guards when Host header is unset (actix#1129)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm authored and fafhrd91 committed Oct 14, 2019
1 parent 1ca9d87 commit ace98e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* Add `Payload::into_inner` method and make stored `def::Payload` public. (#1110)

### Changed

* Support `Host` guards when the `Host` header is unset (e.g. HTTP/2 requests) (#1129)

## [1.0.8] - 2019-09-25

### Added
Expand Down
34 changes: 30 additions & 4 deletions src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,11 @@ pub fn Host<H: AsRef<str>>(host: H) -> HostGuard {

fn get_host_uri(req: &RequestHead) -> Option<Uri> {
use core::str::FromStr;
let host_value = req.headers.get(header::HOST)?;
let host = host_value.to_str().ok()?;
let uri = Uri::from_str(host).ok()?;
Some(uri)
req.headers.get(header::HOST)
.and_then(|host_value| host_value.to_str().ok())
.or_else(|| req.uri.host())
.map(|host: &str| Uri::from_str(host).ok())
.and_then(|host_success| host_success)
}

#[doc(hidden)]
Expand Down Expand Up @@ -400,6 +401,31 @@ mod tests {
assert!(!pred.check(req.head()));
}

#[test]
fn test_host_without_header() {
let req = TestRequest::default()
.uri("www.rust-lang.org")
.to_http_request();

let pred = Host("www.rust-lang.org");
assert!(pred.check(req.head()));

let pred = Host("www.rust-lang.org").scheme("https");
assert!(pred.check(req.head()));

let pred = Host("blog.rust-lang.org");
assert!(!pred.check(req.head()));

let pred = Host("blog.rust-lang.org").scheme("https");
assert!(!pred.check(req.head()));

let pred = Host("crates.io");
assert!(!pred.check(req.head()));

let pred = Host("localhost");
assert!(!pred.check(req.head()));
}

#[test]
fn test_methods() {
let req = TestRequest::default().to_http_request();
Expand Down

0 comments on commit ace98e3

Please sign in to comment.