Skip to content

Commit

Permalink
Handle unsupported media type properly for streaming requests (#15531)
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta authored Aug 31, 2024
1 parent 2224d48 commit 03d9a24
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,16 @@ public void testInvalidHeaderValue() throws IOException {
assertThat(map.get("type"), equalTo("content_type_header_exception"));
assertThat(map.get("reason"), equalTo("java.lang.IllegalArgumentException: invalid Content-Type header []"));
}

public void testUnsupportedContentType() throws IOException {
final Request request = new Request("POST", "/_bulk/stream");
final RequestOptions.Builder options = request.getOptions().toBuilder();
request.setOptions(options);
final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
final Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(406));
final ObjectPath objectPath = ObjectPath.createFromResponse(response);
final String error = objectPath.evaluate("error");
assertThat(error, equalTo("Content-Type header [] is not supported"));
}
}
8 changes: 7 additions & 1 deletion server/src/main/java/org/opensearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th

private void dispatchRequest(RestRequest request, RestChannel channel, RestHandler handler) throws Exception {
final int contentLength = request.content().length();
final MediaType mediaType = request.getMediaType();
if (contentLength > 0) {
final MediaType mediaType = request.getMediaType();
if (mediaType == null) {
sendContentTypeErrorMessage(request.getAllHeaderValues("Content-Type"), channel);
return;
Expand All @@ -343,6 +343,7 @@ private void dispatchRequest(RestRequest request, RestChannel channel, RestHandl
return;
}
}

RestChannel responseChannel = channel;
try {
if (handler.canTripCircuitBreaker()) {
Expand All @@ -364,6 +365,11 @@ private void dispatchRequest(RestRequest request, RestChannel channel, RestHandl
+ "]"
);
}

if (mediaType == null) {
sendContentTypeErrorMessage(request.getAllHeaderValues("Content-Type"), responseChannel);
return;
}
} else {
// if we could reserve bytes for the request we need to send the response also over this channel
responseChannel = new ResourceHandlingHttpChannel(channel, circuitBreakerService, contentLength);
Expand Down

0 comments on commit 03d9a24

Please sign in to comment.