@@ -76,21 +76,24 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
76
76
77
77
let builder = http:: Request :: builder ( )
78
78
. 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
79
let host = ag
85
80
. headers
86
81
. get ( http:: header:: HOST )
87
82
. and_then ( |s| s. to_str ( ) . ok ( ) )
88
- . or ( ag. request_context . domain_name . as_deref ( ) )
89
- . unwrap_or_default ( ) ;
90
-
83
+ . or ( ag. request_context . domain_name . as_deref ( ) ) ;
91
84
let path = apigw_path_with_stage ( & ag. request_context . stage , & raw_path) ;
92
- let mut url = format ! ( "{}://{}{}" , scheme, host, path) ;
93
85
86
+ let mut url = match host {
87
+ None => path,
88
+ Some ( host) => {
89
+ let scheme = ag
90
+ . headers
91
+ . get ( x_forwarded_proto ( ) )
92
+ . and_then ( |s| s. to_str ( ) . ok ( ) )
93
+ . unwrap_or ( "https" ) ;
94
+ format ! ( "{}://{}{}" , scheme, host, path)
95
+ }
96
+ } ;
94
97
if let Some ( query) = ag. raw_query_string {
95
98
url. push ( '?' ) ;
96
99
url. push_str ( & query) ;
@@ -199,18 +202,20 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {
199
202
200
203
let builder = http:: Request :: builder ( )
201
204
. 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 ( ) ;
205
+ let host = alb. headers . get ( http:: header:: HOST ) . and_then ( |s| s. to_str ( ) . ok ( ) ) ;
206
+
207
+ let mut url = match host {
208
+ None => raw_path. clone ( ) ,
209
+ Some ( host) => {
210
+ let scheme = alb
211
+ . headers
212
+ . get ( x_forwarded_proto ( ) )
213
+ . and_then ( |s| s. to_str ( ) . ok ( ) )
214
+ . unwrap_or ( "https" ) ;
215
+ format ! ( "{}://{}{}" , scheme, host, & raw_path)
216
+ }
217
+ } ;
212
218
213
- let mut url = format ! ( "{}://{}{}" , scheme, host, & raw_path) ;
214
219
if !alb. multi_value_query_string_parameters . is_empty ( ) {
215
220
url. push ( '?' ) ;
216
221
url. push_str ( & alb. multi_value_query_string_parameters . to_query_string ( ) ) ;
@@ -625,4 +630,20 @@ mod tests {
625
630
assert_eq ! ( req. method( ) , "GET" ) ;
626
631
assert_eq ! ( req. uri( ) , "/test/test/hello?name=me" ) ;
627
632
}
633
+
634
+ #[ test]
635
+ fn deserialize_alb_no_host ( ) {
636
+ // generated from ALB health checks
637
+ let input = include_str ! ( "../tests/data/alb_no_host.json" ) ;
638
+ let result = from_str ( input) ;
639
+ assert ! (
640
+ result. is_ok( ) ,
641
+ "event was not parsed as expected {:?} given {}" ,
642
+ result,
643
+ input
644
+ ) ;
645
+ let req = result. expect ( "failed to parse request" ) ;
646
+ assert_eq ! ( req. method( ) , "GET" ) ;
647
+ assert_eq ! ( req. uri( ) , "/v1/health/" ) ;
648
+ }
628
649
}
0 commit comments