Skip to content

Commit

Permalink
Merge branch 'Adds-non-greedy-parameter-values-into-the-path-when-{pr…
Browse files Browse the repository at this point in the history
…oxy+}' of https://github.com/christostatitzikidis/aws-lambda-dotnet into christostatitzikidis-Adds-non-greedy-parameter-values-into-the-path-when-{proxy+}
  • Loading branch information
normj committed Feb 12, 2023
2 parents a00fdca + 4acde9e commit b0951c1
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,21 @@ protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxy
requestFeatures.Method = apiGatewayRequest.HttpMethod;

string path = null;

// Replaces {proxy+} in path, if exists
if (apiGatewayRequest.PathParameters != null && apiGatewayRequest.PathParameters.TryGetValue("proxy", out var proxy) &&
!string.IsNullOrEmpty(apiGatewayRequest.Resource))
{
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);
}
}

if (string.IsNullOrEmpty(path))
{
path = apiGatewayRequest.Path;
Expand Down Expand Up @@ -253,7 +261,7 @@ protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxy
}

{
var tlsConnectionFeature = (ITlsConnectionFeature) features;
var tlsConnectionFeature = (ITlsConnectionFeature)features;
var clientCertPem = apiGatewayRequest?.RequestContext?.Identity?.ClientCert?.ClientCertPem;
if (clientCertPem != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
</ItemGroup>

<ItemGroup>
<None Remove="additional-path-parameters-in-non-proxy-path.json" />
<None Remove="additional-path-parameters-in-proxy-path.json" />
<None Remove="authtest-access-request.json" />
<None Remove="missing-resource-request.json" />
<None Remove="values-get-all-httpapi-request.json" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,26 @@ public async Task TestEncodeSlashInResourcePath()
Assert.Equal("{\"first\":\"a\",\"second\":\"b\"}", response.Body);
}

[Fact]
public async Task TestAdditionalPathParametersInProxyPath()
{
var response = await this.InvokeAPIGatewayRequest("additional-path-parameters-in-proxy-path.json");
Assert.Equal(200, response.StatusCode);

var root = JsonSerializer.Deserialize<JsonObject>(response.Body);
Assert.Equal("/path/bar/api", root?["Path"]?.ToString());
}

[Fact]
public async Task TestAdditionalPathParametersInNonProxyPath()
{
var response = await this.InvokeAPIGatewayRequest("additional-path-parameters-in-non-proxy-path.json");
Assert.Equal(200, response.StatusCode);

var root = JsonSerializer.Deserialize<JsonObject>(response.Body);
Assert.Equal("/path/bar/api", root?["Path"]?.ToString());
}

[Fact]
public async Task TestSpaceInResourcePathAndQueryString()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"resource": "/path/{foo}/api",
"path": "path/bar/api",
"httpMethod": "GET",
"headers": {
"Accept": "text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "m1138alqlk.execute-api.us-west-2.amazonaws.com",
"upgrade-insecure-requests": "1",
"User-Agent": "Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/64.0.3282.140 Safari\/537.36",
"Via": "2.0 ce270f4a88edde7438864bc44406e83a.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "_3bfLvO5mG_QItkPQ80NiKlmrwbYanuQBlSy8QyZAvGRQKDr0B2dIg==",
"X-Amzn-Trace-Id": "Root=1-5a98f3fa-80b360c0b7a112d97f0679ca",
"X-Forwarded-For": "50.35.77.7, 54.182.214.94",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"queryStringParameters": null,
"pathParameters": {
"foo": "bar"
},
"stageVariables": null,
"requestContext": {
"requestTime": "02\/Mar\/2018:06:49:30 +0000",
"path": "/path/bar/api",
"accountId": "626492997873",
"protocol": "HTTP\/1.1",
"resourceId": "fm247q",
"stage": "Prod",
"requestTimeEpoch": 1519973370547,
"requestId": "dbb330b2-1de5-11e8-b1a9-3f92103b3613",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"sourceIp": "50.35.77.7",
"accessKey": null,
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36",
"user": null
},
"resourcePath": "/path/{foo}/api",
"httpMethod": "GET",
"apiId": "m1138alqlk"
},
"body": null,
"isBase64Encoded": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"resource": "/path/{foo}/{proxy+}",
"path": "path/bar/api",
"httpMethod": "GET",
"headers": {
"Accept": "text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "m1138alqlk.execute-api.us-west-2.amazonaws.com",
"upgrade-insecure-requests": "1",
"User-Agent": "Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/64.0.3282.140 Safari\/537.36",
"Via": "2.0 ce270f4a88edde7438864bc44406e83a.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "_3bfLvO5mG_QItkPQ80NiKlmrwbYanuQBlSy8QyZAvGRQKDr0B2dIg==",
"X-Amzn-Trace-Id": "Root=1-5a98f3fa-80b360c0b7a112d97f0679ca",
"X-Forwarded-For": "50.35.77.7, 54.182.214.94",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"queryStringParameters": null,
"pathParameters": {
"proxy": "api",
"foo": "bar"
},
"stageVariables": null,
"requestContext": {
"requestTime": "02\/Mar\/2018:06:49:30 +0000",
"path": "/path/bar/api",
"accountId": "626492997873",
"protocol": "HTTP\/1.1",
"resourceId": "fm247q",
"stage": "Prod",
"requestTimeEpoch": 1519973370547,
"requestId": "dbb330b2-1de5-11e8-b1a9-3f92103b3613",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"sourceIp": "50.35.77.7",
"accessKey": null,
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36",
"user": null
},
"resourcePath": "/path/{foo}/{proxy+}",
"httpMethod": "GET",
"apiId": "m1138alqlk"
},
"body": null,
"isBase64Encoded": false
}

0 comments on commit b0951c1

Please sign in to comment.