Skip to content

Commit edd82d3

Browse files
committed
Ignore hosts from ALB health checks when they are not present
ALB health checks don't include a host value. Signed-off-by: David Calavera <[email protected]>
1 parent c737920 commit edd82d3

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

lambda-http/src/request.rs

+42-25
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,20 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
7676

7777
let builder = http::Request::builder()
7878
.uri({
79-
let scheme = ag
80-
.headers
81-
.get(x_forwarded_proto())
82-
.and_then(|s| s.to_str().ok())
83-
.unwrap_or("https");
84-
let host = ag
85-
.headers
86-
.get(http::header::HOST)
87-
.and_then(|s| s.to_str().ok())
88-
.or(ag.request_context.domain_name.as_deref())
89-
.unwrap_or_default();
90-
79+
let host = ag.headers.get(http::header::HOST).and_then(|s| s.to_str().ok());
9180
let path = apigw_path_with_stage(&ag.request_context.stage, &raw_path);
92-
let mut url = format!("{}://{}{}", scheme, host, path);
9381

82+
let mut url = match host {
83+
None => path,
84+
Some(host) => {
85+
let scheme = ag
86+
.headers
87+
.get(x_forwarded_proto())
88+
.and_then(|s| s.to_str().ok())
89+
.unwrap_or("https");
90+
format!("{}://{}{}", scheme, host, path)
91+
}
92+
};
9493
if let Some(query) = ag.raw_query_string {
9594
url.push('?');
9695
url.push_str(&query);
@@ -199,18 +198,20 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {
199198

200199
let builder = http::Request::builder()
201200
.uri({
202-
let scheme = alb
203-
.headers
204-
.get(x_forwarded_proto())
205-
.and_then(|s| s.to_str().ok())
206-
.unwrap_or("https");
207-
let host = alb
208-
.headers
209-
.get(http::header::HOST)
210-
.and_then(|s| s.to_str().ok())
211-
.unwrap_or_default();
212-
213-
let mut url = format!("{}://{}{}", scheme, host, &raw_path);
201+
let host = alb.headers.get(http::header::HOST).and_then(|s| s.to_str().ok());
202+
203+
let mut url = match host {
204+
None => raw_path.clone(),
205+
Some(host) => {
206+
let scheme = alb
207+
.headers
208+
.get(x_forwarded_proto())
209+
.and_then(|s| s.to_str().ok())
210+
.unwrap_or("https");
211+
format!("{}://{}{}", scheme, host, &raw_path)
212+
}
213+
};
214+
214215
if !alb.multi_value_query_string_parameters.is_empty() {
215216
url.push('?');
216217
url.push_str(&alb.multi_value_query_string_parameters.to_query_string());
@@ -625,4 +626,20 @@ mod tests {
625626
assert_eq!(req.method(), "GET");
626627
assert_eq!(req.uri(), "/test/test/hello?name=me");
627628
}
629+
630+
#[test]
631+
fn deserialize_alb_no_host() {
632+
// generated from ALB health checks
633+
let input = include_str!("../tests/data/alb_no_host.json");
634+
let result = from_str(input);
635+
assert!(
636+
result.is_ok(),
637+
"event was not parsed as expected {:?} given {}",
638+
result,
639+
input
640+
);
641+
let req = result.expect("failed to parse request");
642+
assert_eq!(req.method(), "GET");
643+
assert_eq!(req.uri(), "/v1/health/");
644+
}
628645
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"body": "",
3+
"headers": {
4+
"user-agent": "ELB-HealthChecker/2.0"
5+
},
6+
"httpMethod": "GET",
7+
8+
"isBase64Encoded": false,
9+
10+
"path": "/v1/health/",
11+
"queryStringParameters": {},
12+
"requestContext": {
13+
"elb": {
14+
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:111111111:targetgroup/cdn-ingestor-tg-usea1-dev/3fe2aca58c0da101"
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)