@@ -76,21 +76,24 @@ 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" ) ;
8479 let host = ag
8580 . headers
8681 . get ( http:: header:: HOST )
8782 . 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 ( ) ) ;
9184 let path = apigw_path_with_stage ( & ag. request_context . stage , & raw_path) ;
92- let mut url = format ! ( "{}://{}{}" , scheme, host, path) ;
9385
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+ } ;
9497 if let Some ( query) = ag. raw_query_string {
9598 url. push ( '?' ) ;
9699 url. push_str ( & query) ;
@@ -199,18 +202,20 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {
199202
200203 let builder = http:: Request :: builder ( )
201204 . 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+ } ;
212218
213- let mut url = format ! ( "{}://{}{}" , scheme, host, & raw_path) ;
214219 if !alb. multi_value_query_string_parameters . is_empty ( ) {
215220 url. push ( '?' ) ;
216221 url. push_str ( & alb. multi_value_query_string_parameters . to_query_string ( ) ) ;
@@ -625,4 +630,20 @@ mod tests {
625630 assert_eq ! ( req. method( ) , "GET" ) ;
626631 assert_eq ! ( req. uri( ) , "/test/test/hello?name=me" ) ;
627632 }
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+ }
628649}
0 commit comments