Skip to content

Commit ffe1ca4

Browse files
committed
Respect accept header on no handler (#30383)
Today when processing a request for a URL path for which we can not find a handler we send back a plain-text response. Yet, we have the accept header in our hand and can respect the accepted media type of the request. This commit addresses this.
1 parent faccc59 commit ffe1ca4

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

docs/CHANGELOG.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ Engine::
225225

226226
Ingest::
227227
* Don't allow referencing the pattern bank name in the pattern bank {pull}29295[#29295] (issue: {issue}29257[#29257])
228+
Respect accept header on requests with no handler ({pull}30383[#30383])
229+
Rollup::
230+
* Validate timezone in range queries to ensure they match the selected job when
231+
searching ({pull}30338[#30338])
228232

229233
[float]
230234
=== Regressions
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.http;
21+
22+
import org.apache.http.message.BasicHeader;
23+
import org.apache.http.util.EntityUtils;
24+
import org.elasticsearch.client.Response;
25+
import org.elasticsearch.client.ResponseException;
26+
27+
import java.io.IOException;
28+
29+
import static org.hamcrest.Matchers.containsString;
30+
import static org.hamcrest.Matchers.equalTo;
31+
import static org.hamcrest.Matchers.is;
32+
33+
public class NoHandlerIT extends HttpSmokeTestCase {
34+
35+
public void testNoHandlerRespectsAcceptHeader() throws IOException {
36+
runTestNoHandlerRespectsAcceptHeader(
37+
"application/json",
38+
"application/json; charset=UTF-8",
39+
"\"error\":\"no handler found for uri [/foo/bar/baz/qux/quux] and method [GET]\"");
40+
runTestNoHandlerRespectsAcceptHeader(
41+
"application/yaml",
42+
"application/yaml",
43+
"error: \"no handler found for uri [/foo/bar/baz/qux/quux] and method [GET]\"");
44+
}
45+
46+
private void runTestNoHandlerRespectsAcceptHeader(
47+
final String accept, final String contentType, final String expect) throws IOException {
48+
final ResponseException e =
49+
expectThrows(
50+
ResponseException.class,
51+
() -> getRestClient().performRequest("GET", "/foo/bar/baz/qux/quux", new BasicHeader("Accept", accept)));
52+
53+
final Response response = e.getResponse();
54+
assertThat(response.getHeader("Content-Type"), equalTo(contentType));
55+
assertThat(EntityUtils.toString(e.getResponse().getEntity()), containsString(expect));
56+
assertThat(response.getStatusLine().getStatusCode(), is(400));
57+
}
58+
59+
}

server/src/main/java/org/elasticsearch/rest/RestController.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,15 @@ private void handleOptionsRequest(RestRequest request, RestChannel channel, Set<
401401
* Handle a requests with no candidate handlers (return a 400 Bad Request
402402
* error).
403403
*/
404-
private void handleBadRequest(RestRequest request, RestChannel channel) {
405-
channel.sendResponse(new BytesRestResponse(BAD_REQUEST,
406-
"No handler found for uri [" + request.uri() + "] and method [" + request.method() + "]"));
404+
private void handleBadRequest(RestRequest request, RestChannel channel) throws IOException {
405+
try (XContentBuilder builder = channel.newErrorBuilder()) {
406+
builder.startObject();
407+
{
408+
builder.field("error", "no handler found for uri [" + request.uri() + "] and method [" + request.method() + "]");
409+
}
410+
builder.endObject();
411+
channel.sendResponse(new BytesRestResponse(BAD_REQUEST, builder));
412+
}
407413
}
408414

409415
/**

x-pack/qa/ml-disabled/src/test/java/org/elasticsearch/xpack/ml/integration/MlPluginDisabledIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ public void testActionsFail() throws Exception {
5151
ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest("put",
5252
MachineLearning.BASE_PATH + "anomaly_detectors/foo", Collections.emptyMap(),
5353
new StringEntity(Strings.toString(xContentBuilder), ContentType.APPLICATION_JSON)));
54-
assertThat(exception.getMessage(), containsString("No handler found for uri [/_xpack/ml/anomaly_detectors/foo] and method [PUT]"));
54+
assertThat(exception.getMessage(), containsString("no handler found for uri [/_xpack/ml/anomaly_detectors/foo] and method [PUT]"));
5555
}
5656
}

0 commit comments

Comments
 (0)