|
1 | | -use actix_http::{header, uri::Uri, RequestHead}; |
| 1 | +use actix_http::{header, uri::Uri, RequestHead, Version}; |
2 | 2 |
|
3 | 3 | use super::{Guard, GuardContext}; |
4 | 4 |
|
@@ -66,6 +66,7 @@ fn get_host_uri(req: &RequestHead) -> Option<Uri> { |
66 | 66 | req.headers |
67 | 67 | .get(header::HOST) |
68 | 68 | .and_then(|host_value| host_value.to_str().ok()) |
| 69 | + .filter(|_| req.version < Version::HTTP_2) |
69 | 70 | .or_else(|| req.uri.host()) |
70 | 71 | .and_then(|host| host.parse().ok()) |
71 | 72 | } |
@@ -123,6 +124,38 @@ mod tests { |
123 | 124 | use super::*; |
124 | 125 | use crate::test::TestRequest; |
125 | 126 |
|
| 127 | + #[test] |
| 128 | + fn host_not_from_header_if_http2() { |
| 129 | + let req = TestRequest::default() |
| 130 | + .uri("www.rust-lang.org") |
| 131 | + .insert_header(( |
| 132 | + header::HOST, |
| 133 | + header::HeaderValue::from_static("www.example.com"), |
| 134 | + )) |
| 135 | + .to_srv_request(); |
| 136 | + |
| 137 | + let host = Host("www.example.com"); |
| 138 | + assert!(host.check(&req.guard_ctx())); |
| 139 | + |
| 140 | + let host = Host("www.rust-lang.org"); |
| 141 | + assert!(!host.check(&req.guard_ctx())); |
| 142 | + |
| 143 | + let req = TestRequest::default() |
| 144 | + .version(actix_http::Version::HTTP_2) |
| 145 | + .uri("www.rust-lang.org") |
| 146 | + .insert_header(( |
| 147 | + header::HOST, |
| 148 | + header::HeaderValue::from_static("www.example.com"), |
| 149 | + )) |
| 150 | + .to_srv_request(); |
| 151 | + |
| 152 | + let host = Host("www.example.com"); |
| 153 | + assert!(!host.check(&req.guard_ctx())); |
| 154 | + |
| 155 | + let host = Host("www.rust-lang.org"); |
| 156 | + assert!(host.check(&req.guard_ctx())); |
| 157 | + } |
| 158 | + |
126 | 159 | #[test] |
127 | 160 | fn host_from_header() { |
128 | 161 | let req = TestRequest::default() |
|
0 commit comments