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
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ private String getContentTypeWithCharset(String headerValue, ContainerConfig con
return headerValue;
}

if (!config.isAppendDefaultContentCharset()) {
return headerValue;
}

if (headerValue.contains("charset=")) {
return headerValue;
}
Expand All @@ -92,7 +96,6 @@ private String getContentTypeWithCharset(String headerValue, ContainerConfig con
if (!headerValue.trim().endsWith(";")) {
newValue += "; ";
}

newValue += "charset=" + config.getDefaultContentCharset();
return newValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static ContainerConfig defaultConfig() {
configuration.setQueryStringCaseSensitive(false);
configuration.addBinaryContentTypes("application/octet-stream", "image/jpeg", "image/png", "image/gif");
configuration.setDefaultContentCharset(DEFAULT_CONTENT_CHARSET);
configuration.setAppendDefaultContentCharset(true);
configuration.setInitializationTimeout(MAX_INIT_TIMEOUT_MS);
configuration.setDisableExceptionMapper(false);

Expand All @@ -55,6 +56,7 @@ public static ContainerConfig defaultConfig() {
private boolean stripBasePath;
private String uriEncoding;
private String defaultContentCharset;
private boolean appendDefaultContentCharset = true;
private boolean consolidateSetCookieHeaders;
private boolean useStageAsServletContext;
private List<String> validFilePaths;
Expand Down Expand Up @@ -290,6 +292,25 @@ public void setDefaultContentCharset(String defaultContentCharset) {
this.defaultContentCharset = defaultContentCharset;
}

/**
* Returns whether the default charset should be appended to the <code>Content-Type</code> header.
* This is enabled by default.
*
* @return <code>true</code> if the default charset should be appended, <code>false</code> otherwise.
*/
public boolean isAppendDefaultContentCharset() {
return appendDefaultContentCharset;
}

/**
* Sets whether the default charset should be appended to the <code>Content-Type</code> header.
*
* @param appendDefaultContentCharset whether to append the default charset.
*/
public void setAppendDefaultContentCharset(boolean appendDefaultContentCharset) {
this.appendDefaultContentCharset = appendDefaultContentCharset;
}

/**
* Returns the maximum amount of time (in milliseconds) set for the initialization time. See documentation on the
* {@link #setInitializationTimeout(int)} for additional details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,49 @@ void readRequest_contentCharset_setsDefaultCharsetWhenNotSpecified() {
}
}

@Test
void readRequest_contentCharset_doesNotAppendDefaultCharsetWhenDisabled() {
String requestContentType = "application/json";

AwsProxyRequest request = new AwsProxyRequestBuilder(ENCODED_REQUEST_PATH, "GET")
.header(HttpHeaders.CONTENT_TYPE, requestContentType)
.build();

ContainerConfig config = ContainerConfig.defaultConfig();
config.setAppendDefaultContentCharset(false);

try {
HttpServletRequest servletRequest = reader.readRequest(request, null, null, config);
assertNotNull(servletRequest);
assertEquals(requestContentType, servletRequest.getHeader(HttpHeaders.CONTENT_TYPE));
} catch (InvalidRequestEventException e) {
e.printStackTrace();
fail("Could not read request");
}
}

@Test
void readRequest_contentCharset_preservesRequestCharsetWhenAppendingDisabled() {
String requestContentType = "application/json; charset=UTF-8";

AwsProxyRequest request = new AwsProxyRequestBuilder(ENCODED_REQUEST_PATH, "GET")
.header(HttpHeaders.CONTENT_TYPE, requestContentType)
.build();

ContainerConfig config = ContainerConfig.defaultConfig();
config.setAppendDefaultContentCharset(false);

try {
HttpServletRequest servletRequest = reader.readRequest(request, null, null, config);

assertEquals(requestContentType, servletRequest.getHeader(HttpHeaders.CONTENT_TYPE));
assertEquals("UTF-8", servletRequest.getCharacterEncoding());
} catch (InvalidRequestEventException e) {
e.printStackTrace();
fail("Could not read request");
}
}

@Test
void readRequest_contentCharset_appendsCharsetToComplextContentType() {
String contentType = "multipart/form-data; boundary=something";
Expand Down