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

Add ability to parse Content-Type from content type contains charset #27301

Closed
wants to merge 3 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
20 changes: 19 additions & 1 deletion core/src/main/java/org/elasticsearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ private boolean hasContentTypeOrCanAutoDetect(final RestRequest restRequest, fin
}
}
} else if (restHandler != null && restHandler.supportsContentStream() && restRequest.header("Content-Type") != null) {
final String lowercaseMediaType = restRequest.header("Content-Type").toLowerCase(Locale.ROOT);
final String lowercaseMediaType = parseMediaType(restRequest.header("Content-Type"));

// we also support newline delimited JSON: http://specs.okfnlabs.org/ndjson/
if (lowercaseMediaType.equals("application/x-ndjson")) {
restRequest.setXContentType(XContentType.JSON);
Expand All @@ -308,6 +309,23 @@ private boolean hasContentTypeOrCanAutoDetect(final RestRequest restRequest, fin
return true;
}

private String parseMediaType(String rawContentType) {
final String contentType = rawContentType.toLowerCase(Locale.ROOT);
final int firstSemiColonIndex = contentType.indexOf(';');
if (firstSemiColonIndex == -1) {
return contentType;
}
final String mediaType = contentType.substring(0, firstSemiColonIndex).trim();
final String charsetCandidate = contentType.substring(firstSemiColonIndex + 1);

final String[] keyValue = charsetCandidate.split("=", 2);
if (keyValue.length != 2 || keyValue[0].trim().equalsIgnoreCase("charset") == false
|| keyValue[1].trim().equalsIgnoreCase("utf-8") == false) {
deprecationLogger.deprecated("Content-Type [" + rawContentType + "] contains unrecognized [" + charsetCandidate + "]");
}
return mediaType;
}

private boolean autoDetectXContentType(RestRequest restRequest) {
deprecationLogger.deprecated("Content type detection for rest requests is deprecated. Specify the content type using " +
"the [Content-Type] header.");
Expand Down
44 changes: 44 additions & 0 deletions core/src/test/java/org/elasticsearch/rest/RestControllerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,50 @@ public boolean supportsContentStream() {
assertTrue(channel.getSendResponseCalled());
}

public void testContentTypeNotOnlyMediaType() {
restController.registerHandler(RestRequest.Method.GET, "/foo", new RestHandler() {
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
}

@Override
public boolean supportsContentStream() {
return true;
}
});

String goodMimeType = randomFrom("application/x-ndjson",
"application/x-ndjson; charset=UTF-8",
"application/x-ndjson; charset=utf-8",
"application/x-ndjson;charset=UTF-8");
String content = randomAlphaOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
.withContent(new BytesArray(content), null).withPath("/foo")
.withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList(goodMimeType))).build();
AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.OK);

assertFalse(channel.getSendResponseCalled());
restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
assertTrue(channel.getSendResponseCalled());

String badMimeType = randomFrom("application/x-ndjson; charset=utf-8; unknown",
"application/x-ndjson; charset=unknown",
"application/x-ndjson;charset=UTF-16",
"application/x-ndjson; unknown");
content = randomAlphaOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
.withContent(new BytesArray(content), null).withPath("/foo")
.withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList(badMimeType))).build();
channel = new AssertingChannel(fakeRestRequest, true, RestStatus.OK);

assertFalse(channel.getSendResponseCalled());
restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
assertTrue(channel.getSendResponseCalled());
assertWarnings("Content-Type [" + badMimeType + "] contains unrecognized ["
+ badMimeType.substring(badMimeType.indexOf(";") + 1).toLowerCase(Locale.ROOT) + "]");
}

public void testDispatchWithContentStreamAutoDetect() {
FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
.withContent(new BytesArray("{}"), null).withPath("/foo").build();
Expand Down