Skip to content

Commit 447d9fb

Browse files
authored
fix: return correct header keys for each integration (#877)
Each integration handle header keys differently, this patch tries to address these differences so that we have proper headers in responses. **ALB Integration** https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers-response The names of the fields used for headers differ depending on whether you enable multi-value headers for the target group. You must use multiValueHeaders if you have enabled multi-value headers and headers otherwise. **APIGW v1 Integration** https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format If you specify values for both headers and multiValueHeaders, API Gateway merges them into a single list. If the same key-value pair is specified in both, only the values from multiValueHeaders will appear in the merged list. **APIGW v2 Integration** https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html Format 2.0 doesn't have multiValueHeaders or multiValueQueryStringParameters fields. Duplicate headers are combined with commas and included in the headers field. Duplicate query strings are combined with commas and included in the queryStringParameters field. **`awslabs/aws-lambda-go-api-proxy` source code** - https://github.com/awslabs/aws-lambda-go-api-proxy/blob/3f6c8160ae0c22b0bd05b2e3a9122736f035c74b/core/response.go#L117 - https://github.com/awslabs/aws-lambda-go-api-proxy/blob/3f6c8160ae0c22b0bd05b2e3a9122736f035c74b/core/responseALB.go#L108 - https://github.com/awslabs/aws-lambda-go-api-proxy/blob/3f6c8160ae0c22b0bd05b2e3a9122736f035c74b/core/responsev2.go#L117
1 parent 343159e commit 447d9fb

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

lambda-http/src/response.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl LambdaResponse {
6969
body,
7070
is_base64_encoded,
7171
status_code: status_code as i64,
72-
// explicitly empty, as API gateway does not properly merge headers and
73-
// multi-value-headers, resulting in duplicate headers
72+
// Explicitly empty, as API gateway v1 will merge "headers" and
73+
// "multi_value_headers" fields together resulting in duplicate response headers.
7474
headers: HeaderMap::new(),
7575
multi_value_headers: headers,
7676
}),
@@ -93,21 +93,20 @@ impl LambdaResponse {
9393
is_base64_encoded,
9494
status_code: status_code as i64,
9595
cookies,
96-
// explicitly empty, as API gateway does not properly merge headers and
97-
// multi-value-headers, resulting in duplicate headers
98-
headers: HeaderMap::new(),
99-
multi_value_headers: headers,
96+
// API gateway v2 doesn't have "multi_value_headers" field. Duplicate headers
97+
// are combined with commas and included in the headers field.
98+
headers,
99+
multi_value_headers: HeaderMap::new(),
100100
})
101101
}
102102
#[cfg(feature = "alb")]
103103
RequestOrigin::Alb => LambdaResponse::Alb(AlbTargetGroupResponse {
104104
body,
105105
status_code: status_code as i64,
106106
is_base64_encoded,
107-
// ALB responses are used for ALB integrations as well as
108-
// Lambda Function URLs. The former uses the `multi_value_headers` field,
109-
// while the later uses the `headers` field. We need to return
110-
// both fields to ensure both integrations work correctly.
107+
// ALB responses are used for ALB integration, which can be configured to use
108+
// either "headers" or "multi_value_headers" field. We need to return both fields
109+
// to ensure both configuration work correctly.
111110
headers: headers.clone(),
112111
multi_value_headers: headers,
113112
status_description: Some(format!(
@@ -121,8 +120,8 @@ impl LambdaResponse {
121120
body,
122121
is_base64_encoded,
123122
status_code: status_code as i64,
124-
// explicitly empty, as API gateway does not properly merge headers and
125-
// multi-value-headers, resulting in duplicate headers
123+
// Explicitly empty, as API gateway v1 will merge "headers" and
124+
// "multi_value_headers" fields together resulting in duplicate response headers.
126125
headers: HeaderMap::new(),
127126
multi_value_headers: headers,
128127
}),
@@ -475,7 +474,7 @@ mod tests {
475474
let json = serde_json::to_string(&response).expect("failed to serialize to json");
476475
assert_eq!(
477476
json,
478-
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-encoding":["gzip"]},"body":"MDAwMDAw","isBase64Encoded":true,"cookies":[]}"#
477+
r#"{"statusCode":200,"headers":{"content-encoding":"gzip"},"multiValueHeaders":{},"body":"MDAwMDAw","isBase64Encoded":true,"cookies":[]}"#
479478
)
480479
}
481480

@@ -493,7 +492,7 @@ mod tests {
493492
let json = serde_json::to_string(&response).expect("failed to serialize to json");
494493
assert_eq!(
495494
json,
496-
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-type":["application/json"]},"body":"000000","isBase64Encoded":false,"cookies":[]}"#
495+
r#"{"statusCode":200,"headers":{"content-type":"application/json"},"multiValueHeaders":{},"body":"000000","isBase64Encoded":false,"cookies":[]}"#
497496
)
498497
}
499498

@@ -511,7 +510,7 @@ mod tests {
511510
let json = serde_json::to_string(&response).expect("failed to serialize to json");
512511
assert_eq!(
513512
json,
514-
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-type":["application/json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
513+
r#"{"statusCode":200,"headers":{"content-type":"application/json; charset=utf-16"},"multiValueHeaders":{},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
515514
)
516515
}
517516

@@ -529,7 +528,7 @@ mod tests {
529528
let json = serde_json::to_string(&response).expect("failed to serialize to json");
530529
assert_eq!(
531530
json,
532-
r#"{"statusCode":200,"headers":{},"multiValueHeaders":{"content-type":["application/graphql-response+json; charset=utf-16"]},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
531+
r#"{"statusCode":200,"headers":{"content-type":"application/graphql-response+json; charset=utf-16"},"multiValueHeaders":{},"body":"〰〰〰","isBase64Encoded":false,"cookies":[]}"#
533532
)
534533
}
535534

0 commit comments

Comments
 (0)