|
6 | 6 | using System.Text; |
7 | 7 | using System.Text.Json; |
8 | 8 | using Amazon.Lambda.APIGatewayEvents; |
| 9 | +using Amazon.Lambda.ApplicationLoadBalancerEvents; |
9 | 10 | using Amazon.Lambda.RuntimeSupport; |
10 | 11 | using Amazon.Lambda.RuntimeSupport.Helpers; |
| 12 | +using Microsoft.AspNetCore.WebUtilities; |
11 | 13 | using Microsoft.Extensions.DependencyInjection; |
12 | 14 |
|
13 | 15 | namespace Amazon.Lambda.AspNetCoreServer.Hosting.Internal; |
@@ -141,20 +143,66 @@ public LambdaSnapstartInitializerHttpMessageHandler(LambdaEventSource lambdaEven |
141 | 143 | _lambdaEventSource = lambdaEventSource; |
142 | 144 | } |
143 | 145 |
|
144 | | - protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 146 | + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
145 | 147 | { |
146 | | - // Copy request to correct request, ie APIGatewayProxyRequest |
| 148 | + if (null == request?.RequestUri) |
| 149 | + return new HttpResponseMessage(HttpStatusCode.OK); |
147 | 150 |
|
148 | | - // TODO - IMPLEMENT |
149 | | - var translatedRequest = new APIGatewayProxyRequest |
| 151 | + var duckRequest = new |
150 | 152 | { |
151 | | - Path = request.RequestUri.MakeRelativeUri(BaseUri).ToString(), |
152 | | - HttpMethod = request.Method.ToString() |
| 153 | + Body = await ReadContent(request), |
| 154 | + Headers = request.Headers |
| 155 | + .ToDictionary( |
| 156 | + kvp => kvp.Key, |
| 157 | + kvp => kvp.Value.FirstOrDefault(), |
| 158 | + StringComparer.OrdinalIgnoreCase), |
| 159 | + HttpMethod = request.Method.ToString(), |
| 160 | + Path = request.RequestUri?.MakeRelativeUri(BaseUri).ToString() ?? string.Empty, |
| 161 | + RawQuery = request.RequestUri?.Query, |
| 162 | + Query = QueryHelpers.ParseNullableQuery(request.RequestUri?.Query) |
| 163 | + }; |
| 164 | + |
| 165 | + object translatedRequest = _lambdaEventSource switch |
| 166 | + { |
| 167 | + LambdaEventSource.ApplicationLoadBalancer => new ApplicationLoadBalancerRequest |
| 168 | + { |
| 169 | + Body = duckRequest.Body, |
| 170 | + Headers = duckRequest.Headers, |
| 171 | + Path = duckRequest.Path, |
| 172 | + HttpMethod = duckRequest.HttpMethod, |
| 173 | + QueryStringParameters = duckRequest.Query?.ToDictionary(k => k.Key, v => v.Value.ToString()) |
| 174 | + }, |
| 175 | + LambdaEventSource.HttpApi => new APIGatewayProxyRequest |
| 176 | + { |
| 177 | + Body = duckRequest.Body, |
| 178 | + Headers = duckRequest.Headers, |
| 179 | + Path = duckRequest.Path, |
| 180 | + HttpMethod = duckRequest.HttpMethod, |
| 181 | + QueryStringParameters = duckRequest.Query?.ToDictionary(k => k.Key, v => v.Value.ToString()) |
| 182 | + }, |
| 183 | + LambdaEventSource.RestApi => new APIGatewayHttpApiV2ProxyRequest |
| 184 | + { |
| 185 | + Body = duckRequest.Body, |
| 186 | + Headers = duckRequest.Headers, |
| 187 | + RawPath = duckRequest.Path, |
| 188 | + QueryStringParameters = duckRequest.Query?.ToDictionary(k => k.Key, v => v.Value.ToString()), |
| 189 | + RawQueryString = duckRequest.RawQuery |
| 190 | + }, |
| 191 | + _ => throw new NotImplementedException( |
| 192 | + $"Unknown {nameof(LambdaEventSource)}: {Enum.GetName(_lambdaEventSource)}") |
153 | 193 | }; |
154 | 194 |
|
155 | 195 | CapturedHttpRequests.Add(translatedRequest); |
156 | 196 |
|
157 | | - return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)); |
| 197 | + return new HttpResponseMessage(HttpStatusCode.OK); |
| 198 | + } |
| 199 | + |
| 200 | + private async Task<string> ReadContent(HttpRequestMessage r) |
| 201 | + { |
| 202 | + if (r.Content == null) |
| 203 | + return string.Empty; |
| 204 | + |
| 205 | + return await r.Content.ReadAsStringAsync(); |
158 | 206 | } |
159 | 207 | } |
160 | 208 | } |
0 commit comments