From 7520a107bee67099338813728147d2aee25ed240 Mon Sep 17 00:00:00 2001 From: Jay Modi Date: Thu, 2 Feb 2017 14:07:13 -0500 Subject: Optionally require a valid content type for all rest requests with content (#22691) This change adds a strict mode for xcontent parsing on the rest layer. The strict mode will be off by default for 5.x and in a separate commit will be enabled by default for 6.0. The strict mode, which can be enabled by setting `http.content_type.required: true` in 5.x, will require that all incoming rest requests have a valid and supported content type header before the request is dispatched. In the non-strict mode, the Content-Type header will be inspected and if it is not present or not valid, we will continue with auto detection of content like we have done previously. The content type header is parsed to the matching XContentType value with the only exception being for plain text requests. This value is then passed on with the content bytes so that we can reduce the number of places where we need to auto-detect the content type. As part of this, many transport requests and builders were updated to provide methods that accepted the XContentType along with the bytes and the methods that would rely on auto-detection have been deprecated. In the non-strict mode, deprecation warnings are issued whenever a request with body doesn't provide the Content-Type header. See #19388 --- .../java/org/elasticsearch/rest/action/document/RestBulkAction.java | 2 +- .../org/elasticsearch/rest/action/document/RestGetSourceAction.java | 2 +- .../java/org/elasticsearch/rest/action/document/RestIndexAction.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'core/src/main/java/org/elasticsearch/rest/action/document') diff --git a/core/src/main/java/org/elasticsearch/rest/action/document/RestBulkAction.java b/core/src/main/java/org/elasticsearch/rest/action/document/RestBulkAction.java index af0faac553..8fdf2792a4 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/document/RestBulkAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/document/RestBulkAction.java @@ -93,7 +93,7 @@ public class RestBulkAction extends BaseRestHandler { bulkRequest.timeout(request.paramAsTime("timeout", BulkShardRequest.DEFAULT_TIMEOUT)); bulkRequest.setRefreshPolicy(request.param("refresh")); bulkRequest.add(request.content(), defaultIndex, defaultType, defaultRouting, defaultFields, - defaultFetchSourceContext, defaultPipeline, null, allowExplicitIndex); + defaultFetchSourceContext, defaultPipeline, null, allowExplicitIndex, request.getXContentType()); return channel -> client.bulk(bulkRequest, new RestBuilderListener(channel) { @Override diff --git a/core/src/main/java/org/elasticsearch/rest/action/document/RestGetSourceAction.java b/core/src/main/java/org/elasticsearch/rest/action/document/RestGetSourceAction.java index 728e1ff599..83d424ed74 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/document/RestGetSourceAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/document/RestGetSourceAction.java @@ -66,7 +66,7 @@ public class RestGetSourceAction extends BaseRestHandler { client.get(getRequest, new RestResponseListener(channel) { @Override public RestResponse buildResponse(GetResponse response) throws Exception { - XContentBuilder builder = channel.newBuilder(response.getSourceInternal(), false); + XContentBuilder builder = channel.newBuilder(request.getXContentType(), false); if (response.isSourceEmpty()) { // check if doc source (or doc itself) is missing return new BytesRestResponse(NOT_FOUND, builder); } else { diff --git a/core/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java b/core/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java index 042b0d57b5..ddaf226875 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java @@ -65,7 +65,7 @@ public class RestIndexAction extends BaseRestHandler { indexRequest.routing(request.param("routing")); indexRequest.parent(request.param("parent")); // order is important, set it after routing, so it will set the routing indexRequest.setPipeline(request.param("pipeline")); - indexRequest.source(request.content()); + indexRequest.source(request.content(), request.getXContentType()); indexRequest.timeout(request.paramAsTime("timeout", IndexRequest.DEFAULT_TIMEOUT)); indexRequest.setRefreshPolicy(request.param("refresh")); indexRequest.version(RestActions.parseVersion(request)); -- cgit v1.2.3