diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/EncodingOptions.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/EncodingOptions.cs new file mode 100644 index 000000000..f3250061f --- /dev/null +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/EncodingOptions.cs @@ -0,0 +1,20 @@ +namespace Amazon.Lambda.AspNetCoreServer.Hosting +{ + /// + /// Options for configuring AWS Lambda content type transformation + /// + public class EncodingOptions : IEncodingOptions + { + /// + /// Defines a mapping from registered content types to the response encoding format + /// which dictates what transformations should be applied before returning response content + /// + public Dictionary? ResponseContentEncodingForContentType { get; set ; } + + /// + /// Defines a mapping from registered content encodings to the response encoding format + /// which dictates what transformations should be applied before returning response content + /// + public Dictionary? ResponseContentEncodingForContentEncoding { get; set; } + } +} diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/HostingOptions.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/HostingOptions.cs index b9e58e7bc..69acaec0a 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/HostingOptions.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/HostingOptions.cs @@ -12,4 +12,10 @@ public class HostingOptions /// back to JSON to return to Lambda. /// public ILambdaSerializer Serializer { get; set; } + + /// + /// The encoding response options sent from the Lambda to the API Gateway. + /// Used to adjust what content types are sent to the API Gatway as binary data + /// + public IEncodingOptions EncodingOptions { get; set; } } \ No newline at end of file diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/IEncodingOptions.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/IEncodingOptions.cs new file mode 100644 index 000000000..240eea21c --- /dev/null +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/IEncodingOptions.cs @@ -0,0 +1,8 @@ +namespace Amazon.Lambda.AspNetCoreServer.Hosting +{ + public interface IEncodingOptions + { + public Dictionary? ResponseContentEncodingForContentType { get; set; } + public Dictionary? ResponseContentEncodingForContentEncoding { get; set; } + } +} \ No newline at end of file diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Internal/LambdaRuntimeSupportServer.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Internal/LambdaRuntimeSupportServer.cs index 05493e244..33528f873 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Internal/LambdaRuntimeSupportServer.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/Internal/LambdaRuntimeSupportServer.cs @@ -1,9 +1,7 @@ using Amazon.Lambda.AspNetCoreServer.Internal; using Amazon.Lambda.Core; using Amazon.Lambda.RuntimeSupport; -using Amazon.Lambda.Serialization.SystemTextJson; using Microsoft.AspNetCore.Hosting.Server; -using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.Extensions.DependencyInjection; namespace Amazon.Lambda.AspNetCoreServer.Hosting.Internal @@ -18,6 +16,7 @@ public abstract class LambdaRuntimeSupportServer : LambdaServer { IServiceProvider _serviceProvider; internal ILambdaSerializer Serializer; + internal IEncodingOptions EncodingOptions; /// /// Creates an instance on the LambdaRuntimeSupportServer @@ -27,6 +26,7 @@ public LambdaRuntimeSupportServer(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; Serializer = serviceProvider.GetRequiredService(); + EncodingOptions = serviceProvider.GetRequiredService(); } /// @@ -51,6 +51,34 @@ public override Task StartAsync(IHttpApplication application /// /// protected abstract HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider); + + /// + /// Registers encoding options for the given + /// + /// + /// + /// + protected void RegisterEncodingOptions(AbstractAspNetCoreFunction aspNetCoreFunction) + { + if (EncodingOptions != null) + { + if (EncodingOptions.ResponseContentEncodingForContentType != null) + { + foreach (var responseContentEncodingForContentType in EncodingOptions.ResponseContentEncodingForContentType) + { + aspNetCoreFunction.RegisterResponseContentEncodingForContentType(responseContentEncodingForContentType.Key, responseContentEncodingForContentType.Value); + } + } + + if (EncodingOptions.ResponseContentEncodingForContentEncoding != null) + { + foreach (var responseContentEncodingForContentEncoding in EncodingOptions.ResponseContentEncodingForContentEncoding) + { + aspNetCoreFunction.RegisterResponseContentEncodingForContentEncoding(responseContentEncodingForContentEncoding.Key, responseContentEncodingForContentEncoding.Value); + } + } + } + } } /// @@ -74,7 +102,9 @@ public APIGatewayHttpApiV2LambdaRuntimeSupportServer(IServiceProvider servicePro /// protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider) { - var handler = new APIGatewayHttpApiV2MinimalApi(serviceProvider).FunctionHandlerAsync; + var apiGatewayHttpApiV2MinimalApi = new APIGatewayHttpApiV2MinimalApi(serviceProvider); + RegisterEncodingOptions(apiGatewayHttpApiV2MinimalApi); + var handler = apiGatewayHttpApiV2MinimalApi.FunctionHandlerAsync; return HandlerWrapper.GetHandlerWrapper(handler, this.Serializer); } @@ -115,7 +145,9 @@ public APIGatewayRestApiLambdaRuntimeSupportServer(IServiceProvider serviceProvi /// protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider) { - var handler = new APIGatewayRestApiMinimalApi(serviceProvider).FunctionHandlerAsync; + var apiGatewayRestApiMinimalApi = new APIGatewayRestApiMinimalApi(serviceProvider); + RegisterEncodingOptions(apiGatewayRestApiMinimalApi); + var handler = apiGatewayRestApiMinimalApi.FunctionHandlerAsync; return HandlerWrapper.GetHandlerWrapper(handler, this.Serializer); } @@ -156,7 +188,9 @@ public ApplicationLoadBalancerLambdaRuntimeSupportServer(IServiceProvider servic /// protected override HandlerWrapper CreateHandlerWrapper(IServiceProvider serviceProvider) { - var handler = new ApplicationLoadBalancerMinimalApi(serviceProvider).FunctionHandlerAsync; + var applicationLoadBalancerMinimalApi = new ApplicationLoadBalancerMinimalApi(serviceProvider); + RegisterEncodingOptions(applicationLoadBalancerMinimalApi); + var handler = applicationLoadBalancerMinimalApi.FunctionHandlerAsync; return HandlerWrapper.GetHandlerWrapper(handler, this.Serializer); } diff --git a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs index c68ccc81d..16b036346 100644 --- a/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs +++ b/Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/ServiceCollectionExtensions.cs @@ -1,6 +1,6 @@ using Amazon.Lambda.AspNetCoreServer.Hosting; -using Amazon.Lambda.AspNetCoreServer.Internal; using Amazon.Lambda.AspNetCoreServer.Hosting.Internal; +using Amazon.Lambda.AspNetCoreServer.Internal; using Amazon.Lambda.Core; using Amazon.Lambda.Serialization.SystemTextJson; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -66,6 +66,7 @@ public static IServiceCollection AddAWSLambdaHosting(this IServiceCollection ser configure.Invoke(hostingOptions); services.TryAddSingleton(hostingOptions.Serializer ?? new DefaultLambdaJsonSerializer()); + services.TryAddSingleton(hostingOptions.EncodingOptions); var serverType = eventSource switch {