Skip to content

Fix APIGW path with stage #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2023
Merged
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
44 changes: 38 additions & 6 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,21 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<

#[cfg(any(feature = "apigw_rest", feature = "apigw_http", feature = "apigw_websockets"))]
fn apigw_path_with_stage(stage: &Option<String>, path: &str) -> String {
match stage {
None => path.into(),
Some(stage) if stage == "$default" => path.into(),
Some(stage) => format!("/{stage}{path}"),
if env::var("AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH").is_ok() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be documented somewhere? Although looking through the docs I couldn't find an obvious home.

Otherwise LGTM!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's exactly why I didn't write any docs about it 😅

In theory, nobody should use this option, but who knows. The only case I could find for something like this is if your stage has a name, for example Prod, and you decide that the real path for your api also starts with /Prod. I've never seen something like this, but you never know.

return path.into();
}

let stage = match stage {
None => return path.into(),
Some(stage) if stage == "$default" => return path.into(),
Some(stage) => stage,
};

let prefix = format!("/{stage}/");
if path.starts_with(&prefix) {
path.into()
} else {
format!("/{stage}{path}")
}
}

Expand Down Expand Up @@ -531,7 +542,7 @@ mod tests {
assert_eq!(req.method(), "GET");
assert_eq!(
req.uri(),
"https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/test/hello?name=me"
"https://wt6mne2s9k.execute-api.us-west-2.amazonaws.com/test/hello?name=me"
);

// Ensure this is an APIGW request
Expand Down Expand Up @@ -733,7 +744,7 @@ mod tests {
);
let req = result.expect("failed to parse request");
assert_eq!(req.method(), "GET");
assert_eq!(req.uri(), "/test/test/hello?name=me");
assert_eq!(req.uri(), "/test/hello?name=me");
}

#[test]
Expand Down Expand Up @@ -768,4 +779,25 @@ mod tests {
let url = build_request_uri("/path with spaces/and multiple segments", &HeaderMap::new(), None, None);
assert_eq!("/path%20with%20spaces/and%20multiple%20segments", url);
}

#[test]
fn deserializes_apigw_http_request_with_stage_in_path() {
let input = include_str!("../tests/data/apigw_v2_proxy_request_with_stage_in_path.json");
let result = from_str(input);
assert!(
result.is_ok(),
"event was not parsed as expected {result:?} given {input}"
);
let req = result.expect("failed to parse request");
assert_eq!("/Prod/my/path", req.uri().path());
assert_eq!("/Prod/my/path", req.raw_http_path());
}

#[test]
fn test_apigw_path_with_stage() {
assert_eq!("/path", apigw_path_with_stage(&None, "/path"));
assert_eq!("/path", apigw_path_with_stage(&Some("$default".into()), "/path"));
assert_eq!("/Prod/path", apigw_path_with_stage(&Some("Prod".into()), "/Prod/path"));
assert_eq!("/Prod/path", apigw_path_with_stage(&Some("Prod".into()), "/path"));
}
}
2 changes: 1 addition & 1 deletion lambda-http/tests/data/apigw_no_host.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/test/hello",
"path": "/hello",
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, lzma, sdch, br",
Expand Down
2 changes: 1 addition & 1 deletion lambda-http/tests/data/apigw_proxy_request.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"path": "/test/hello",
"path": "/hello",
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, lzma, sdch, br",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"version": "2.0",
"routeKey": "Prod",
"rawPath": "/Prod/my/path",
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
"cookies": [
"cookie1=value1",
"cookie2=value2"
],
"headers": {
"Header1": "value1",
"Header2": "value2"
},
"queryStringParameters": {
"parameter1": "value1,value2",
"parameter2": "value"
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"authorizer": {
"jwt": {
"claims": {
"claim1": "value1",
"claim2": "value2"
},
"scopes": [
"scope1",
"scope2"
]
}
},
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/Prod/my/path",
"protocol": "HTTP/1.1",
"sourceIp": "IP",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "Prod",
"stage": "Prod",
"time": "12/Mar/2020:19:03:58 +0000",
"timeEpoch": 1583348638390
},
"body": "Hello from Lambda",
"pathParameters": {
"parameter1": "value1"
},
"isBase64Encoded": false,
"stageVariables": {
"stageVariable1": "value1",
"stageVariable2": "value2"
}
}