Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .autover/changes/12b85f20-8478-422b-a662-2f4aff4b6509.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.AspNetCoreServer.Hosting",
"Type": "Minor",
"ChangelogMessages": [
"Exposing the extensions points from Amazon.Lambda.AspNetCoreServer onto the HostingOptions. For example customizing the serialization by adding a callback for PostMarshallResponseFeature."
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="Amazon.Lambda.AspNetCoreServer.Hosting.Tests,PublicKey=0024000004800000940000000602000000240000525341310004000001000100db5f59f098d27276c7833875a6263a3cc74ab17ba9a9df0b52aedbe7252745db7274d5271fd79c1f08f668ecfa8eaab5626fa76adc811d3c8fc55859b0d09d3bc0a84eecd0ba891f2b8a2fc55141cdcc37c2053d53491e650a479967c3622762977900eddbf1252ed08a2413f00a28f3a0752a81203f03ccb7f684db373518b4" />
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\Amazon.Lambda.Core\Amazon.Lambda.Core.csproj" />
Expand Down
220 changes: 218 additions & 2 deletions Libraries/src/Amazon.Lambda.AspNetCoreServer.Hosting/HostingOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Amazon.Lambda.Core;
using Amazon.Lambda.Core;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Features.Authentication;

namespace Amazon.Lambda.AspNetCoreServer.Hosting;

Expand All @@ -12,4 +14,218 @@ public class HostingOptions
/// back to JSON to return to Lambda.
/// </summary>
public ILambdaSerializer Serializer { get; set; }
}

/// <summary>
/// The default response content encoding to use when no explicit content type or content encoding mapping is registered.
/// Defaults to ResponseContentEncoding.Default (UTF-8 text).
/// </summary>
public ResponseContentEncoding DefaultResponseContentEncoding { get; set; } = ResponseContentEncoding.Default;

/// <summary>
/// Controls whether unhandled exception details are included in responses.
/// Defaults to false for security.
/// </summary>
public bool IncludeUnhandledExceptionDetailInResponse { get; set; } = false;

/// <summary>
/// Callback invoked after request marshalling to customize the HTTP request feature.
/// Receives the IHttpRequestFeature, Lambda request object, and ILambdaContext.
/// The Lambda request object will need to be cast to the appropriate type based on the event source.
/// <para>
/// <list type="table">
/// <listheader>
/// <term>API Type</term>
/// <description>Event Type</description>
/// </listheader>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.HttpApi">HttpApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest">APIGatewayHttpApiV2ProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.RestApi">RestApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest">APIGatewayProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.ApplicationLoadBalancer">ApplicationLoadBalancer</see></term>
/// <description><see cref="Amazon.Lambda.ApplicationLoadBalancerEvents.ApplicationLoadBalancerRequest">ApplicationLoadBalancerRequest</see></description>
/// </item>
/// </list>
/// </para>
/// </summary>
public Action<IHttpRequestFeature, object, ILambdaContext>? PostMarshallRequestFeature { get; set; }

/// <summary>
/// Callback invoked after response marshalling to customize the HTTP response feature.
/// Receives the IHttpResponseFeature, Lambda response object, and ILambdaContext.
/// The Lambda response object will need to be cast to the appropriate type based on the event source.
/// <para>
/// <list type="table">
/// <listheader>
/// <term>API Type</term>
/// <description>Event Type</description>
/// </listheader>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.HttpApi">HttpApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyResponse">APIGatewayHttpApiV2ProxyResponse</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.RestApi">RestApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse">APIGatewayProxyResponse</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.ApplicationLoadBalancer">ApplicationLoadBalancer</see></term>
/// <description><see cref="Amazon.Lambda.ApplicationLoadBalancerEvents.ApplicationLoadBalancerResponse">ApplicationLoadBalancerResponse</see></description>
/// </item>
/// </list>
/// </para>
/// </summary>
public Action<IHttpResponseFeature, object, ILambdaContext>? PostMarshallResponseFeature { get; set; }

/// <summary>
/// Callback invoked after connection marshalling to customize the HTTP connection feature.
/// Receives the IHttpConnectionFeature, Lambda request object, and ILambdaContext.
/// The Lambda request object will need to be cast to the appropriate type based on the event source.
/// <para>
/// <list type="table">
/// <listheader>
/// <term>API Type</term>
/// <description>Event Type</description>
/// </listheader>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.HttpApi">HttpApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest">APIGatewayHttpApiV2ProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.RestApi">RestApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest">APIGatewayProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.ApplicationLoadBalancer">ApplicationLoadBalancer</see></term>
/// <description><see cref="Amazon.Lambda.ApplicationLoadBalancerEvents.ApplicationLoadBalancerRequest">ApplicationLoadBalancerRequest</see></description>
/// </item>
/// </list>
/// </para>
/// </summary>
public Action<IHttpConnectionFeature, object, ILambdaContext>? PostMarshallConnectionFeature { get; set; }

/// <summary>
/// Callback invoked after authentication marshalling to customize the HTTP authentication feature.
/// Receives the IHttpAuthenticationFeature, Lambda request object, and ILambdaContext.
/// The Lambda request object will need to be cast to the appropriate type based on the event source.
/// <para>
/// <list type="table">
/// <listheader>
/// <term>API Type</term>
/// <description>Event Type</description>
/// </listheader>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.HttpApi">HttpApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest">APIGatewayHttpApiV2ProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.RestApi">RestApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest">APIGatewayProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.ApplicationLoadBalancer">ApplicationLoadBalancer</see></term>
/// <description><see cref="Amazon.Lambda.ApplicationLoadBalancerEvents.ApplicationLoadBalancerRequest">ApplicationLoadBalancerRequest</see></description>
/// </item>
/// </list>
/// </para>
/// </summary>
public Action<IHttpAuthenticationFeature, object, ILambdaContext>? PostMarshallHttpAuthenticationFeature { get; set; }

/// <summary>
/// Callback invoked after TLS connection marshalling to customize the TLS connection feature.
/// Receives the ITlsConnectionFeature, Lambda request object, and ILambdaContext.
/// The Lambda request object will need to be cast to the appropriate type based on the event source.
/// <para>
/// <list type="table">
/// <listheader>
/// <term>API Type</term>
/// <description>Event Type</description>
/// </listheader>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.HttpApi">HttpApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest">APIGatewayHttpApiV2ProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.RestApi">RestApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest">APIGatewayProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.ApplicationLoadBalancer">ApplicationLoadBalancer</see></term>
/// <description><see cref="Amazon.Lambda.ApplicationLoadBalancerEvents.ApplicationLoadBalancerRequest">ApplicationLoadBalancerRequest</see></description>
/// </item>
/// </list>
/// </para>
/// </summary>
public Action<ITlsConnectionFeature, object, ILambdaContext>? PostMarshallTlsConnectionFeature { get; set; }

/// <summary>
/// Callback invoked after items marshalling to customize the items feature.
/// Receives the IItemsFeature, Lambda request object, and ILambdaContext.
/// The Lambda request object will need to be cast to the appropriate type based on the event source.
/// <para>
/// <list type="table">
/// <listheader>
/// <term>API Type</term>
/// <description>Event Type</description>
/// </listheader>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.HttpApi">HttpApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest">APIGatewayHttpApiV2ProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.RestApi">RestApi</see></term>
/// <description><see cref="Amazon.Lambda.APIGatewayEvents.APIGatewayProxyRequest">APIGatewayProxyRequest</see></description>
/// </item>
/// <item>
/// <term><see cref="Microsoft.Extensions.DependencyInjection.LambdaEventSource.ApplicationLoadBalancer">ApplicationLoadBalancer</see></term>
/// <description><see cref="Amazon.Lambda.ApplicationLoadBalancerEvents.ApplicationLoadBalancerRequest">ApplicationLoadBalancerRequest</see></description>
/// </item>
/// </list>
/// </para>
/// </summary>
public Action<IItemsFeature, object, ILambdaContext>? PostMarshallItemsFeature { get; set; }

/// <summary>
/// Internal storage for content type to response content encoding mappings.
/// </summary>
internal Dictionary<string, ResponseContentEncoding> ContentTypeEncodings { get; } = new();

/// <summary>
/// Internal storage for content encoding to response content encoding mappings.
/// </summary>
internal Dictionary<string, ResponseContentEncoding> ContentEncodingEncodings { get; } = new();

/// <summary>
/// Registers a response content encoding for a specific content type.
/// </summary>
/// <param name="contentType">The content type (e.g., "application/json", "image/png")</param>
/// <param name="encoding">The response content encoding to use for this content type</param>
public void RegisterResponseContentEncodingForContentType(string contentType, ResponseContentEncoding encoding)
{
if (string.IsNullOrEmpty(contentType))
{
return;
}

ContentTypeEncodings[contentType] = encoding;
}

/// <summary>
/// Registers a response content encoding for a specific content encoding.
/// </summary>
/// <param name="contentEncoding">The content encoding (e.g., "gzip", "deflate", "br")</param>
/// <param name="encoding">The response content encoding to use for this content encoding</param>
public void RegisterResponseContentEncodingForContentEncoding(string contentEncoding, ResponseContentEncoding encoding)
{
if (string.IsNullOrEmpty(contentEncoding))
{
return;
}

ContentEncodingEncodings[contentEncoding] = encoding;
}
}
Loading
Loading