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
18 changes: 18 additions & 0 deletions .autover/changes/8c1d9773-a398-4953-97d4-9747e9606611.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Projects": [
{
"Name": "Amazon.Lambda.AspNetCoreServer",
"Type": "Patch",
"ChangelogMessages": [
"Null-safe marshalling for Path, HttpMethod, path parameters, and headers to avoid NullReferenceException with incomplete API Gateway events"
]
},
{
"Name": "Amazon.Lambda.TestTool",
"Type": "Patch",
"ChangelogMessages": [
"Populate RequestContext (and Path/HttpMethod defaults) when the API Gateway emulator builds REST APIGatewayProxyRequest events"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ protected override Amazon.Lambda.Core.ResponseStreaming.HttpResponseStreamPrelud
/// <param name="lambdaContext"></param>
protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
{
if (apiGatewayRequest == null)
{
_logger.LogError("MarshallRequest: apiGatewayRequest is null");
throw new ArgumentNullException(nameof(apiGatewayRequest));
}

_logger.LogDebug("MarshallRequest: Path={Path}, HttpMethod={HttpMethod}, Resource={Resource}, RequestContext={HasContext}",
apiGatewayRequest.Path ?? "(null)",
apiGatewayRequest.HttpMethod ?? "(null)",
apiGatewayRequest.Resource ?? "(null)",
apiGatewayRequest.RequestContext != null);

{
var authFeatures = (IHttpAuthenticationFeature)features;

Expand Down Expand Up @@ -201,7 +213,8 @@ protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxy
var rawQueryString = Utilities.CreateQueryStringParameters(
apiGatewayRequest.QueryStringParameters, apiGatewayRequest.MultiValueQueryStringParameters, true);

requestFeatures.RawTarget = apiGatewayRequest.Path + rawQueryString;
var pathForTarget = apiGatewayRequest.Path ?? "/";
requestFeatures.RawTarget = pathForTarget + rawQueryString;
requestFeatures.QueryString = rawQueryString;
requestFeatures.Path = path;

Expand Down Expand Up @@ -365,24 +378,24 @@ protected virtual string ParseHttpPath(APIGatewayProxyRequest apiGatewayRequest)
if (apiGatewayRequest.PathParameters != null && apiGatewayRequest.PathParameters.TryGetValue("proxy", out var proxy) &&
!string.IsNullOrEmpty(apiGatewayRequest.Resource))
{
var proxyPath = proxy;
var proxyPath = proxy ?? "";
path = apiGatewayRequest.Resource.Replace("{proxy+}", proxyPath);

// Adds all the rest of non greedy parameters in apiGateway.Resource to the path
foreach (var pathParameter in apiGatewayRequest.PathParameters.Where(pp => pp.Key != "proxy"))
{
path = path.Replace($"{{{pathParameter.Key}}}", pathParameter.Value);
path = path.Replace($"{{{pathParameter.Key}}}", pathParameter.Value ?? "");
}
}

if (string.IsNullOrEmpty(path))
{
path = apiGatewayRequest.Path;
path = apiGatewayRequest.Path ?? "/";
}

if (!path.StartsWith("/"))
if (string.IsNullOrEmpty(path) || !path.StartsWith("/"))
{
path = "/" + path;
path = "/" + (path ?? "");
}
return path;
}
Expand All @@ -394,7 +407,7 @@ protected virtual string ParseHttpPath(APIGatewayProxyRequest apiGatewayRequest)
/// </summary>
protected virtual string ParseHttpMethod(APIGatewayProxyRequest apiGatewayRequest)
{
return apiGatewayRequest.HttpMethod;
return apiGatewayRequest.HttpMethod ?? "GET";
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,21 @@ internal static void SetHeadersCollection(IHeaderDictionary headers, IDictionary
{
foreach (var kvp in multiValues)
{
headers[kvp.Key] = new StringValues(kvp.Value.ToArray());
var values = kvp.Value?.Where(v => v != null).ToArray() ?? Array.Empty<string>();
if (values.Length > 0)
{
headers[kvp.Key] = new StringValues(values);
}
}
}
else if (singleValues?.Count > 0)
{
foreach (var kvp in singleValues)
{
headers[kvp.Key] = new StringValues(kvp.Value);
if (kvp.Value != null)
{
headers[kvp.Key] = new StringValues(kvp.Value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,34 @@ public static async Task<APIGatewayProxyRequest> ToApiGatewayRequest(
pathParameters = encodedPathParameters;
}

var pathValue = path ?? "/";
var resourceValue = apiGatewayRouteConfig.Path ?? "";
var httpMethodValue = request.Method ?? "GET";
var userAgent = request.Headers.UserAgent.ToString();

var proxyRequest = new APIGatewayProxyRequest
{
Resource = apiGatewayRouteConfig.Path,
Path = path,
HttpMethod = request.Method,
Resource = resourceValue,
Path = pathValue,
HttpMethod = httpMethodValue,
Body = body,
IsBase64Encoded = false
IsBase64Encoded = false,
RequestContext = new APIGatewayProxyRequest.ProxyRequestContext
{
Path = pathValue,
AccountId = "123456789012",
ResourceId = "test-invoke-resource-id",
Stage = "test-invoke-stage",
RequestId = HttpRequestUtility.GenerateRequestId(),
Identity = new APIGatewayProxyRequest.RequestIdentity
{
SourceIp = "127.0.0.1",
UserAgent = userAgent
},
ResourcePath = resourceValue,
HttpMethod = httpMethodValue,
ApiId = "test-invoke-api-id"
}
};

if (headers.Any())
Expand Down