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
44 changes: 38 additions & 6 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ fn into_proxy_request(ag: ApiGatewayProxyRequest) -> http::Request<Body> {

// merge headers into multi_value_headers and make
// multi-value_headers our cannoncial source of request headers
let mut headers = ag.multi_value_headers;
headers.extend(ag.headers);
let headers = merge_headers(ag.multi_value_headers, ag.headers);

let base64 = ag.is_base64_encoded;
let mut req = builder
Expand Down Expand Up @@ -266,8 +265,7 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {

// merge headers into multi_value_headers and make
// multi-value_headers our cannoncial source of request headers
let mut headers = alb.multi_value_headers;
headers.extend(alb.headers);
let headers = merge_headers(alb.multi_value_headers, alb.headers);

let base64 = alb.is_base64_encoded;

Expand Down Expand Up @@ -330,8 +328,7 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<

// merge headers into multi_value_headers and make
// multi-value_headers our canonical source of request headers
let mut headers = ag.multi_value_headers;
headers.extend(ag.headers);
let headers = merge_headers(ag.multi_value_headers, ag.headers);

let base64 = ag.is_base64_encoded;
let mut req = builder
Expand Down Expand Up @@ -529,6 +526,23 @@ pub fn from_str(s: &str) -> Result<crate::Request, JsonError> {
serde_json::from_str(s).map(LambdaRequest::into)
}

/// Combine the two header maps that REST API Gateway, ALB and WebSocket events carry.
///
/// A repeated header appears in both: `headers` holds only its last value and
/// `multi_value_headers` holds all of them. `HeaderMap::extend` replaces existing
/// keys, so extending the multi-value map with the single-value one drops every
/// value but the last. Keep the multi-value entries and take a single-value entry
/// only for a name the multi-value map lacks.
fn merge_headers(multi_value_headers: HeaderMap, headers: HeaderMap) -> HeaderMap {
let mut merged = multi_value_headers;
for (name, value) in &headers {
if !merged.contains_key(name) {
merged.append(name.clone(), value.clone());
}
}
merged
}

fn x_forwarded_proto() -> HeaderName {
HeaderName::from_static("x-forwarded-proto")
}
Expand Down Expand Up @@ -775,6 +789,24 @@ mod tests {
assert!(parts.uri.to_string().contains("multiValueName=you&multiValueName=me"));
}

#[test]
fn keeps_every_value_of_a_header_repeated_in_apigw_multi_value_headers() {
// API Gateway REST sends a repeated header in both maps: `headers` holds
// only the last value, `multiValueHeaders` holds all of them.
let input = include_str!("../tests/data/apigw_multi_value_proxy_request_repeated_header.json");
let req = from_str(input).expect("failed to parse request");
let values: Vec<_> = req.headers().get_all("x-repeated").iter().collect();
assert_eq!(values, vec!["first", "second"]);
}

#[test]
fn keeps_every_value_of_a_header_repeated_in_alb_multi_value_headers() {
let input = include_str!("../tests/data/alb_multi_value_request_repeated_header.json");
let req = from_str(input).expect("failed to parse request");
let values: Vec<_> = req.headers().get_all("x-repeated").iter().collect();
assert_eq!(values, vec!["first", "second"]);
}

#[test]
fn deserializes_alb_multi_value_request_events() {
// from docs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"requestContext": {
"elb": {
"targetGroupArn": "arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/6d0ecf831eec9f09"
}
},
"httpMethod": "GET",
"path": "/",
"queryStringParameters": {
"myKey": "val2",
"myOtherKey": "val3"
},
"multiValueQueryStringParameters": {
"myKey": [
"val1",
"val2"
],
"myOtherKey": [
"val3",
"val4"
]
},
"headers": {
"accept": "text/html,application/xhtml+xml",
"accept-language": "en-US,en;q=0.8",
"content-type": "text/plain",
"cookie": "name1=value1",
"host": "lambda-846800462-us-east-2.elb.amazonaws.com",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)",
"x-amzn-trace-id": "Root=1-5bdb40ca-556d8b0c50dc66f0511bf520",
"x-forwarded-for": "72.21.198.66",
"x-forwarded-port": "443",
"x-forwarded-proto": "https",
"x-repeated": "second"
},
"multiValueHeaders": {
"accept": [
"text/html,application/xhtml+xml"
],
"accept-language": [
"en-US,en;q=0.8"
],
"content-type": [
"text/plain"
],
"cookie": [
"name1=value1",
"name2=value2"
],
"host": [
"lambda-846800462-us-east-2.elb.amazonaws.com"
],
"user-agent": [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)"
],
"x-amzn-trace-id": [
"Root=1-5bdb40ca-556d8b0c50dc66f0511bf520"
],
"x-forwarded-for": [
"72.21.198.66"
],
"x-forwarded-port": [
"443"
],
"x-forwarded-proto": [
"https"
],
"x-repeated": [
"first",
"second"
]
},
"isBase64Encoded": false,
"body": "request_body"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"resource": "/{proxy+}",
"path": "/hello/world",
"httpMethod": "POST",
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"cache-control": "no-cache",
"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",
"Content-Type": "application/json",
"headerName": "headerValue",
"Host": "gy415nuibc.execute-api.us-east-1.amazonaws.com",
"Postman-Token": "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f",
"User-Agent": "PostmanRuntime/2.4.5",
"Via": "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==",
"X-Forwarded-For": "54.240.196.186, 54.182.214.83",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
"X-Repeated": "second"
},
"multiValueHeaders": {
"Accept": [
"*/*"
],
"Accept-Encoding": [
"gzip, deflate"
],
"cache-control": [
"no-cache"
],
"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"
],
"Content-Type": [
"application/json"
],
"headerName": [
"headerValue"
],
"Host": [
"gy415nuibc.execute-api.us-east-1.amazonaws.com"
],
"Postman-Token": [
"9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f"
],
"User-Agent": [
"PostmanRuntime/2.4.5"
],
"Via": [
"1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)"
],
"X-Amz-Cf-Id": [
"pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A=="
],
"X-Forwarded-For": [
"54.240.196.186, 54.182.214.83"
],
"X-Forwarded-Port": [
"443"
],
"X-Forwarded-Proto": [
"https"
],
"X-Repeated": [
"first",
"second"
]
},
"queryStringParameters": {
"name": "me",
"multiValueName": "me"
},
"multiValueQueryStringParameters": {
"name": [
"me"
],
"multiValueName": [
"you",
"me"
]
},
"pathParameters": {
"proxy": "hello/world"
},
"stageVariables": {
"stageVariableName": "stageVariableValue"
},
"requestContext": {
"accountId": "12345678912",
"resourceId": "roq9wj",
"stage": "testStage",
"requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33",
"requestTimeEpoch": 1583798639428,
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"apiKey": null,
"sourceIp": "192.168.196.186",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "PostmanRuntime/2.4.5",
"user": null
},
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"apiId": "gy415nuibc"
},
"body": "{\r\n\t\"a\": 1\r\n}",
"isBase64Encoded": false
}