Skip to content

Allow registration of binary ContentTypes #191

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

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.amazonaws.serverless.exceptions.InvalidResponseObjectException;
import com.amazonaws.serverless.proxy.ResponseWriter;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.internal.testutils.Timer;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.services.lambda.runtime.Context;
Expand All @@ -39,7 +40,7 @@ public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse,
if (containerResponse.getAwsResponseBodyString() != null) {
String responseString;

if (isValidUtf8(containerResponse.getAwsResponseBodyBytes())) {
if (!isBinary(containerResponse.getContentType()) && isValidUtf8(containerResponse.getAwsResponseBodyBytes())) {
responseString = containerResponse.getAwsResponseBodyString();
} else {
responseString = Base64.getMimeEncoder().encodeToString(containerResponse.getAwsResponseBodyBytes());
Expand All @@ -55,4 +56,17 @@ public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse,
Timer.stop("SERVLET_RESPONSE_WRITE");
return awsProxyResponse;
}

private boolean isBinary(String contentType) {
if(contentType != null) {
int semidx = contentType.indexOf(';');
if(semidx >= 0) {
return LambdaContainerHandler.getContainerConfig().isBinaryContentType(contentType.substring(0, semidx));
}
else {
return LambdaContainerHandler.getContainerConfig().isBinaryContentType(contentType);
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;


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

return configuration;
}
Expand All @@ -38,6 +40,7 @@ public static ContainerConfig defaultConfig() {
private List<String> validFilePaths;
private List<String> customDomainNames;
private boolean queryStringCaseSensitive;
private final HashSet<String> binaryContentTypes = new HashSet<>();

public ContainerConfig() {
validFilePaths = new ArrayList<>();
Expand Down Expand Up @@ -219,4 +222,25 @@ public boolean isQueryStringCaseSensitive() {
public void setQueryStringCaseSensitive(boolean queryStringCaseSensitive) {
this.queryStringCaseSensitive = queryStringCaseSensitive;
}

/**
* Configure specified content type(s) as binary
* @param contentTypes list of exact content types that will be considered as binary
*/
public void addBinaryContentTypes(String... contentTypes) {
if(contentTypes != null) {
for(String type: contentTypes) {
binaryContentTypes.add(type);
}
}
}

/**
* Determine if specified content type has been configured as binary
* @param contentType content type to query
* @return
*/
public boolean isBinaryContentType(String contentType) {
return contentType != null && binaryContentTypes.contains(contentType.trim());
}
}