1+ #if NET8_0_OR_GREATER
12using System ;
23using System . Collections . Generic ;
34using System . Linq ;
89using System . Threading . Tasks ;
910using Amazon . Lambda . APIGatewayEvents ;
1011using Amazon . Lambda . ApplicationLoadBalancerEvents ;
12+ using Microsoft . AspNetCore . Identity . Data ;
1113using Microsoft . AspNetCore . WebUtilities ;
14+ using Microsoft . Extensions . Primitives ;
1215
13- #if NET8_0_OR_GREATER
1416namespace Amazon . Lambda . AspNetCoreServer . Internal
1517{
1618 /// <summary>
@@ -26,8 +28,18 @@ namespace Amazon.Lambda.AspNetCoreServer.Internal
2628 public class HttpRequestMessageSerializer
2729 {
2830 private static readonly Uri _baseUri = new Uri ( "http://localhost" ) ;
31+
32+ internal class DuckRequest
33+ {
34+ public string Body { get ; set ; }
35+ public Dictionary < string , string > Headers { get ; set ; } = new ( ) ;
36+ public string HttpMethod { get ; set ; }
37+ public string Path { get ; set ; }
38+ public string RawQuery { get ; set ; }
39+ public Dictionary < string , StringValues > Query { get ; set ; } = new ( ) ;
40+ }
2941
30- public static async Task < string > SerializeToJson < TRequest > ( HttpRequestMessage request )
42+ public static async Task < TRequest > ConvertToLambdaRequest < TRequest > ( HttpRequestMessage request )
3143 {
3244 if ( null == request . RequestUri )
3345 {
@@ -42,7 +54,7 @@ public static async Task<string> SerializeToJson<TRequest>(HttpRequestMessage re
4254 // make request absolut (relative to localhost) otherwise parsing the query will not work
4355 request . RequestUri = new Uri ( _baseUri , request . RequestUri ) ;
4456
45- var duckRequest = new
57+ var duckRequest = new DuckRequest
4658 {
4759 Body = await ReadContent ( request ) ,
4860 Headers = request . Headers
@@ -56,52 +68,75 @@ public static async Task<string> SerializeToJson<TRequest>(HttpRequestMessage re
5668 Query = QueryHelpers . ParseNullableQuery ( request . RequestUri ? . Query )
5769 } ;
5870
71+ if ( typeof ( TRequest ) == typeof ( ApplicationLoadBalancerRequest ) )
72+ {
73+ return ( TRequest ) ( object ) new ApplicationLoadBalancerRequest
74+ {
75+ Body = duckRequest . Body ,
76+ Headers = duckRequest . Headers ,
77+ Path = duckRequest . Path ,
78+ HttpMethod = duckRequest . HttpMethod ,
79+ QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) )
80+ } ;
81+ }
82+
83+ if ( typeof ( TRequest ) == typeof ( APIGatewayHttpApiV2ProxyRequest ) )
84+ {
85+ return ( TRequest ) ( object ) new APIGatewayHttpApiV2ProxyRequest
86+ {
87+ Body = duckRequest . Body ,
88+ Headers = duckRequest . Headers ,
89+ RawPath = duckRequest . Path ,
90+ RequestContext = new APIGatewayHttpApiV2ProxyRequest . ProxyRequestContext
91+ {
92+ Http = new APIGatewayHttpApiV2ProxyRequest . HttpDescription
93+ {
94+ Method = duckRequest . HttpMethod ,
95+ Path = duckRequest . Path
96+ }
97+ } ,
98+ QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) ) ,
99+ RawQueryString = duckRequest . RawQuery
100+ } ;
101+ }
102+
103+ if ( typeof ( TRequest ) == typeof ( APIGatewayProxyRequest ) )
104+ {
105+ return ( TRequest ) ( object ) new APIGatewayProxyRequest
106+ {
107+ Body = duckRequest . Body ,
108+ Headers = duckRequest . Headers ,
109+ Path = duckRequest . Path ,
110+ HttpMethod = duckRequest . HttpMethod ,
111+ RequestContext = new APIGatewayProxyRequest . ProxyRequestContext
112+ {
113+ HttpMethod = duckRequest . HttpMethod
114+ } ,
115+ QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) )
116+ } ;
117+ }
118+
119+ throw new NotImplementedException (
120+ $ "Unknown request type: { typeof ( TRequest ) . FullName } ") ;
121+ }
122+
123+ public static async Task < string > SerializeToJson < TRequest > ( HttpRequestMessage request )
124+ {
125+ var lambdaRequest = await ConvertToLambdaRequest < TRequest > ( request ) ;
126+
59127 string translatedRequestJson = typeof ( TRequest ) switch
60128 {
61129 var t when t == typeof ( ApplicationLoadBalancerRequest ) =>
62130 JsonSerializer . Serialize (
63- new ApplicationLoadBalancerRequest
64- {
65- Body = duckRequest . Body ,
66- Headers = duckRequest . Headers ,
67- Path = duckRequest . Path ,
68- HttpMethod = duckRequest . HttpMethod ,
69- QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) )
70- } ,
131+ lambdaRequest ,
71132 LambdaRequestTypeClasses . Default . ApplicationLoadBalancerRequest ) ,
72133 var t when t == typeof ( APIGatewayHttpApiV2ProxyRequest ) =>
73134 JsonSerializer . Serialize (
74- new APIGatewayHttpApiV2ProxyRequest
75- {
76- Body = duckRequest . Body ,
77- Headers = duckRequest . Headers ,
78- RawPath = duckRequest . Path ,
79- RequestContext = new APIGatewayHttpApiV2ProxyRequest . ProxyRequestContext
80- {
81- Http = new APIGatewayHttpApiV2ProxyRequest . HttpDescription
82- {
83- Method = duckRequest . HttpMethod ,
84- Path = duckRequest . Path
85- }
86- } ,
87- QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) ) ,
88- RawQueryString = duckRequest . RawQuery
89- } ,
135+ lambdaRequest ,
90136 LambdaRequestTypeClasses . Default . APIGatewayHttpApiV2ProxyRequest ) ,
91137 var t when t == typeof ( APIGatewayProxyRequest ) =>
92138 JsonSerializer . Serialize (
93- new APIGatewayProxyRequest
94- {
95- Body = duckRequest . Body ,
96- Headers = duckRequest . Headers ,
97- Path = duckRequest . Path ,
98- HttpMethod = duckRequest . HttpMethod ,
99- RequestContext = new APIGatewayProxyRequest . ProxyRequestContext
100- {
101- HttpMethod = duckRequest . HttpMethod
102- } ,
103- QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) )
104- } ,
139+ lambdaRequest ,
105140 LambdaRequestTypeClasses . Default . APIGatewayProxyRequest ) ,
106141 _ => throw new NotImplementedException (
107142 $ "Unknown request type: { typeof ( TRequest ) . FullName } ")
@@ -110,37 +145,18 @@ public static async Task<string> SerializeToJson<TRequest>(HttpRequestMessage re
110145 return translatedRequestJson ;
111146 }
112147
113- /// <summary>
114- /// Specialized Deserializer that uses the AOT Compatible
115- /// <see cref="LambdaRequestTypeClasses"/> to deserialize common
116- /// Request types.
117- /// </summary>
118- public static TRequest Deserialize < TRequest > ( string json )
119- {
120- return typeof ( TRequest ) switch
121- {
122- var t when t == typeof ( ApplicationLoadBalancerRequest ) =>
123- JsonSerializer . Deserialize < TRequest > ( json , LambdaRequestTypeClasses . Default . ApplicationLoadBalancerRequest ) ,
124- var t when t == typeof ( APIGatewayHttpApiV2ProxyRequest ) =>
125- JsonSerializer . Deserialize < TRequest > ( json , LambdaRequestTypeClasses . Default . APIGatewayHttpApiV2ProxyRequest ) ,
126- var t when t == typeof ( APIGatewayProxyRequest ) =>
127- JsonSerializer . Deserialize < TRequest > ( json , LambdaRequestTypeClasses . Default . APIGatewayProxyRequest ) ,
128- _ => throw new NotImplementedException (
129- $ "Unknown request type: { typeof ( TRequest ) . FullName } ")
130- } ;
131- }
132-
133148 private static async Task < string > ReadContent ( HttpRequestMessage r )
134149 {
135150 if ( r . Content == null )
136151 return string . Empty ;
137152
138153 return await r . Content . ReadAsStringAsync ( ) ;
139154 }
155+ }
140156
141- [ JsonSourceGenerationOptions ( WriteIndented = true ) ]
142- [ JsonSerializable ( typeof ( ApplicationLoadBalancerRequest ) ) ]
143- [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest ) ) ]
157+ [ JsonSourceGenerationOptions ]
158+ [ JsonSerializable ( typeof ( ApplicationLoadBalancerRequest ) ) ]
159+ [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest ) ) ]
144160 [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest . ProxyRequestContext ) ) ]
145161 [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest . ProxyRequestAuthentication ) ) ]
146162 [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest . ProxyRequestClientCert ) ) ]
@@ -150,14 +166,13 @@ private static async Task<string> ReadContent(HttpRequestMessage r)
150166 [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest . AuthorizerDescription . IAMDescription ) ) ]
151167 [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest . AuthorizerDescription . CognitoIdentityDescription ) ) ]
152168 [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest . AuthorizerDescription . JwtDescription ) ) ]
153- [ JsonSerializable ( typeof ( APIGatewayProxyRequest ) ) ]
154- [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ClientCertValidity ) ) ]
155- [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ProxyRequestClientCert ) ) ]
156- [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ProxyRequestContext ) ) ]
157- [ JsonSerializable ( typeof ( APIGatewayProxyRequest . RequestIdentity ) ) ]
158- internal partial class LambdaRequestTypeClasses : JsonSerializerContext
159- {
160- }
169+ [ JsonSerializable ( typeof ( APIGatewayProxyRequest ) ) ]
170+ [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ClientCertValidity ) ) ]
171+ [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ProxyRequestClientCert ) ) ]
172+ [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ProxyRequestContext ) ) ]
173+ [ JsonSerializable ( typeof ( APIGatewayProxyRequest . RequestIdentity ) ) ]
174+ internal partial class LambdaRequestTypeClasses : JsonSerializerContext
175+ {
161176 }
162177}
163178#endif
0 commit comments