Skip to content

Commit c8bebd3

Browse files
committed
Merge #191 and moved initialization of HashSet to constructor
2 parents 3cd1ec3 + 2c62ca7 commit c8bebd3

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletResponseWriter.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.amazonaws.serverless.exceptions.InvalidResponseObjectException;
1717
import com.amazonaws.serverless.proxy.ResponseWriter;
18+
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
1819
import com.amazonaws.serverless.proxy.internal.testutils.Timer;
1920
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
2021
import com.amazonaws.services.lambda.runtime.Context;
@@ -39,7 +40,7 @@ public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse,
3940
if (containerResponse.getAwsResponseBodyString() != null) {
4041
String responseString;
4142

42-
if (isValidUtf8(containerResponse.getAwsResponseBodyBytes())) {
43+
if (!isBinary(containerResponse.getContentType()) && isValidUtf8(containerResponse.getAwsResponseBodyBytes())) {
4344
responseString = containerResponse.getAwsResponseBodyString();
4445
} else {
4546
responseString = Base64.getMimeEncoder().encodeToString(containerResponse.getAwsResponseBodyBytes());
@@ -55,4 +56,17 @@ public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse,
5556
Timer.stop("SERVLET_RESPONSE_WRITE");
5657
return awsProxyResponse;
5758
}
59+
60+
private boolean isBinary(String contentType) {
61+
if(contentType != null) {
62+
int semidx = contentType.indexOf(';');
63+
if(semidx >= 0) {
64+
return LambdaContainerHandler.getContainerConfig().isBinaryContentType(contentType.substring(0, semidx));
65+
}
66+
else {
67+
return LambdaContainerHandler.getContainerConfig().isBinaryContentType(contentType);
68+
}
69+
}
70+
return false;
71+
}
5872
}

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/model/ContainerConfig.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest;
55

66
import java.util.ArrayList;
7+
import java.util.HashSet;
78
import java.util.List;
89

910

@@ -22,6 +23,7 @@ public static ContainerConfig defaultConfig() {
2223
configuration.setUseStageAsServletContext(false);
2324
configuration.setValidFilePaths(DEFAULT_FILE_PATHS);
2425
configuration.setQueryStringCaseSensitive(false);
26+
configuration.addBinaryContentTypes("application/octet-stream", "image/jpeg", "image/png", "image/gif");
2527

2628
return configuration;
2729
}
@@ -38,10 +40,12 @@ public static ContainerConfig defaultConfig() {
3840
private List<String> validFilePaths;
3941
private List<String> customDomainNames;
4042
private boolean queryStringCaseSensitive;
43+
private final HashSet<String> binaryContentTypes;
4144

4245
public ContainerConfig() {
4346
validFilePaths = new ArrayList<>();
4447
customDomainNames = new ArrayList<>();
48+
binaryContentTypes = new HashSet<>();
4549
}
4650

4751

@@ -223,4 +227,25 @@ public boolean isQueryStringCaseSensitive() {
223227
public void setQueryStringCaseSensitive(boolean queryStringCaseSensitive) {
224228
this.queryStringCaseSensitive = queryStringCaseSensitive;
225229
}
230+
231+
/**
232+
* Configure specified content type(s) as binary
233+
* @param contentTypes list of exact content types that will be considered as binary
234+
*/
235+
public void addBinaryContentTypes(String... contentTypes) {
236+
if(contentTypes != null) {
237+
for(String type: contentTypes) {
238+
binaryContentTypes.add(type);
239+
}
240+
}
241+
}
242+
243+
/**
244+
* Determine if specified content type has been configured as binary
245+
* @param contentType content type to query
246+
* @return
247+
*/
248+
public boolean isBinaryContentType(String contentType) {
249+
return contentType != null && binaryContentTypes.contains(contentType.trim());
250+
}
226251
}

0 commit comments

Comments
 (0)