Skip to content
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

#220 Fix case-sensitivity of HTTP headers in models #364

Open
wants to merge 2 commits into
base: events-v4-serialization-v2
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions aws-lambda-java-events/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.18.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public static class RequestContext implements Serializable {
private String path;
private Map<String, String> queryStringParameters;
private Map<String, List<String>> multiValueQueryStringParameters;
private Map<String, String> headers;
private Map<String, List<String>> multiValueHeaders;
private HttpHeaders<String> headers;
private HttpHeaders<List<String>> multiValueHeaders;
private String body;
private boolean isBase64Encoded;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ApplicationLoadBalancerResponseEvent implements Serializable {
private int statusCode;
private String statusDescription;
private boolean isBase64Encoded;
private Map<String, String> headers;
private Map<String, List<String>> multiValueHeaders;
private HttpHeaders<String> headers;
private HttpHeaders<List<String>> multiValueHeaders;
private String body;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.amazonaws.services.lambda.runtime.events;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
* Class that represents Http Headers.
* <br>
* Http Headers are case-insensitive.
* Thus, requesting a header "host" will yield the same result as "Host" or "HOST"
*/
public class HttpHeaders<T> implements Map<String, T> {

// Headers are case insensitive (https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2)
private final Map<String, T> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

@Override
public int size() {
return map.size();
}

@Override
public boolean isEmpty() {
return map.isEmpty();
}

@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}

@Override
public T get(Object key) {
return map.get(key);
}

@Override
public T put(String key, T value) {
return map.put(key, value);
}

@Override
public T remove(Object key) {
return map.remove(key);
}

@Override
public void putAll(Map<? extends String, ? extends T> m) {
map.putAll(m);
}

@Override
public void clear() {
map.clear();
}

@Override
public Set<String> keySet() {
return map.keySet();
}

@Override
public Collection<T> values() {
return map.values();
}

@Override
public Set<Entry<String, T>> entrySet() {
return map.entrySet();
}

public static <T> HttpHeaders<T> mergeOrReplace(Map<String, T> from) {
if (from == null) return null;
if (from instanceof HttpHeaders) return (HttpHeaders<T>) from;

HttpHeaders<T> out = new HttpHeaders<>();
if (from.isEmpty()) return out;

out.putAll(from);
return out;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.HttpHeaders;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -39,7 +40,7 @@ public class APIGatewayCustomAuthorizerEvent {
private String resource;
private String path;
private String httpMethod;
private Map<String, String> headers;
private HttpHeaders<String> headers;
private Map<String, String> queryStringParameters;
private Map<String, String> pathParameters;
private Map<String, String> stageVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.HttpHeaders;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -37,8 +38,8 @@ public class APIGatewayProxyRequestEvent implements Serializable {
private String resource;
private String path;
private String httpMethod;
private Map<String, String> headers;
private Map<String, List<String>> multiValueHeaders;
private HttpHeaders<String> headers;
private HttpHeaders<List<String>> multiValueHeaders;
private Map<String, String> queryStringParameters;
private Map<String, List<String>> multiValueQueryStringParameters;
private Map<String, String> pathParameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.HttpHeaders;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -34,7 +35,7 @@ public class APIGatewayProxyResponseEvent implements Serializable {
private static final long serialVersionUID = 2263167344670024172L;

private Integer statusCode;
private Map<String, String> headers;
private HttpHeaders<String> headers;
private String body;
private Boolean isBase64Encoded;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.HttpHeaders;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class APIGatewayV2CustomAuthorizerEvent {
private String rawPath;
private String rawQueryString;
private List<String> cookies;
private Map<String, String> headers;
private HttpHeaders<String> headers;
private Map<String, String> queryStringParameters;
private RequestContext requestContext;
private Map<String, String> pathParameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.HttpHeaders;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -37,14 +38,18 @@ public class APIGatewayV2HTTPEvent {
private String rawPath;
private String rawQueryString;
private List<String> cookies;
private Map<String, String> headers;
private HttpHeaders<String> headers;
private Map<String, String> queryStringParameters;
private Map<String, String> pathParameters;
private Map<String, String> stageVariables;
private String body;
private boolean isBase64Encoded;
private RequestContext requestContext;

public void setHeaders(Map<String, String> headers) {
this.headers = HttpHeaders.mergeOrReplace(headers);
}

@AllArgsConstructor
@Builder(setterPrefix = "with")
@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.HttpHeaders;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -28,9 +29,28 @@
public class APIGatewayV2HTTPResponse {

private int statusCode;
private Map<String, String> headers;
private Map<String, List<String>> multiValueHeaders;
private HttpHeaders<String> headers;
private HttpHeaders<List<String>> multiValueHeaders;
private List<String> cookies;
private String body;
private boolean isBase64Encoded;

public static APIGatewayV2HTTPResponseBuilder builder() {
return new APIGatewayV2HTTPResponseBuilder();
}

public static class APIGatewayV2HTTPResponseBuilder {
private HttpHeaders<String> headers;
private HttpHeaders<List<String>> multiValueHeaders;

public APIGatewayV2HTTPResponseBuilder withHeaders(Map<String, String> headers) {
this.headers = HttpHeaders.mergeOrReplace(headers);
return this;
}

public APIGatewayV2HTTPResponseBuilder withMultiValueHeaders(Map<String, List<String>> multiValueHeaders) {
this.multiValueHeaders = HttpHeaders.mergeOrReplace(multiValueHeaders);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.amazonaws.services.lambda.runtime.events.apigateway;

import com.amazonaws.services.lambda.runtime.events.HttpHeaders;
import lombok.Data;
import lombok.Builder;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -91,8 +92,8 @@ public static class RequestContext implements Serializable {
private String resource;
private String path;
private String httpMethod;
private Map<String, String> headers;
private Map<String, List<String>> multiValueHeaders;
private HttpHeaders<String> headers;
private HttpHeaders<List<String>> multiValueHeaders;
private Map<String, String> queryStringParameters;
private Map<String, List<String>> multiValueQueryStringParameters;
private Map<String, String> pathParameters;
Expand Down
Loading