mirror of
https://github.com/actix/actix-web.git
synced 2024-12-21 07:36:43 +00:00
support Host guards when Host header is unset (#1129)
This commit is contained in:
parent
1ca9d87f0a
commit
ace98e3a1e
2 changed files with 34 additions and 4 deletions
|
@ -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
|
||||
|
|
34
src/guard.rs
34
src/guard.rs
|
@ -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)]
|
||||
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue