1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

support Host guards when Host header is unset (#1129)

This commit is contained in:
Anton Lazarev 2019-10-14 16:05:54 -07:00 committed by Nikolay Kim
parent 1ca9d87f0a
commit ace98e3a1e
2 changed files with 34 additions and 4 deletions

View File

@ -6,6 +6,10 @@
* Add `Payload::into_inner` method and make stored `def::Payload` public. (#1110) * 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 ## [1.0.8] - 2019-09-25
### Added ### Added

View File

@ -276,10 +276,11 @@ pub fn Host<H: AsRef<str>>(host: H) -> HostGuard {
fn get_host_uri(req: &RequestHead) -> Option<Uri> { fn get_host_uri(req: &RequestHead) -> Option<Uri> {
use core::str::FromStr; use core::str::FromStr;
let host_value = req.headers.get(header::HOST)?; req.headers.get(header::HOST)
let host = host_value.to_str().ok()?; .and_then(|host_value| host_value.to_str().ok())
let uri = Uri::from_str(host).ok()?; .or_else(|| req.uri.host())
Some(uri) .map(|host: &str| Uri::from_str(host).ok())
.and_then(|host_success| host_success)
} }
#[doc(hidden)] #[doc(hidden)]
@ -400,6 +401,31 @@ mod tests {
assert!(!pred.check(req.head())); 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] #[test]
fn test_methods() { fn test_methods() {
let req = TestRequest::default().to_http_request(); let req = TestRequest::default().to_http_request();