Skip to content

Commit c5caf84

Browse files
authored
Move raw path into HttpPreRequest (#113231)
Currently, the raw path is only available from the RestRequest. This makes the logic to determine if a handler supports streaming more challenging to evaluate. This commit moves the raw path into pre request to allow easier streaming support logic.
1 parent b9855b8 commit c5caf84

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpRequest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.netty.handler.codec.http.HttpHeaderNames;
1818
import io.netty.handler.codec.http.HttpHeaders;
1919
import io.netty.handler.codec.http.HttpMethod;
20+
import io.netty.handler.codec.http.QueryStringDecoder;
2021
import io.netty.handler.codec.http.cookie.Cookie;
2122
import io.netty.handler.codec.http.cookie.ServerCookieDecoder;
2223
import io.netty.handler.codec.http.cookie.ServerCookieEncoder;
@@ -48,6 +49,7 @@ public class Netty4HttpRequest implements HttpRequest {
4849
private final Exception inboundException;
4950
private final boolean pooled;
5051
private final int sequence;
52+
private final QueryStringDecoder queryStringDecoder;
5153

5254
Netty4HttpRequest(int sequence, io.netty.handler.codec.http.HttpRequest request, Netty4HttpRequestBodyStream contentStream) {
5355
this(
@@ -94,6 +96,7 @@ private Netty4HttpRequest(
9496
this.pooled = pooled;
9597
this.released = released;
9698
this.inboundException = inboundException;
99+
this.queryStringDecoder = new QueryStringDecoder(request.uri());
97100
}
98101

99102
@Override
@@ -106,6 +109,11 @@ public String uri() {
106109
return request.uri();
107110
}
108111

112+
@Override
113+
public String rawPath() {
114+
return queryStringDecoder.rawPath();
115+
}
116+
109117
@Override
110118
public HttpBody body() {
111119
assert released.get() == false;

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpServerTransport.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,7 @@ protected HttpMessage createMessage(String[] initialLine) throws Exception {
374374
// combines the HTTP message pieces into a single full HTTP request (with headers and body)
375375
final HttpObjectAggregator aggregator = new Netty4HttpAggregator(
376376
handlingSettings.maxContentLength(),
377-
httpPreRequest -> enabled.get() == false
378-
|| (httpPreRequest.uri().contains("_bulk") == false
379-
|| httpPreRequest.uri().contains("_bulk_update")
380-
|| httpPreRequest.uri().contains("/_xpack/monitoring/_bulk"))
377+
httpPreRequest -> enabled.get() == false || (httpPreRequest.rawPath().endsWith("/_bulk") == false)
381378
);
382379
aggregator.setMaxCumulationBufferComponents(transport.maxCompositeBufferComponents);
383380
ch.pipeline()

qa/smoke-test-http/src/javaRestTest/java/org/elasticsearch/http/IncrementalBulkRestIT.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE, supportsDedicatedMasters = false, numDataNodes = 2, numClientNodes = 0)
3030
public class IncrementalBulkRestIT extends HttpSmokeTestCase {
3131

32+
public void testBulkUriMatchingDoesNotMatchBulkCapabilitiesApi() throws IOException {
33+
Request request = new Request("GET", "/_capabilities?method=GET&path=%2F_bulk&capabilities=failure_store_status&pretty");
34+
Response response = getRestClient().performRequest(request);
35+
assertEquals(200, response.getStatusLine().getStatusCode());
36+
}
37+
3238
public void testBulkMissingBody() throws IOException {
3339
Request request = new Request(randomBoolean() ? "POST" : "PUT", "/_bulk");
3440
request.setJsonEntity("");

server/src/main/java/org/elasticsearch/http/HttpPreRequest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ public interface HttpPreRequest {
3333
*/
3434
String uri();
3535

36+
/**
37+
* The uri without the query string.
38+
*/
39+
default String rawPath() {
40+
String uri = uri();
41+
final int index = uri.indexOf('?');
42+
if (index >= 0) {
43+
return uri.substring(0, index);
44+
} else {
45+
final int index2 = uri.indexOf('#');
46+
if (index2 >= 0) {
47+
return uri.substring(0, index2);
48+
} else {
49+
return uri;
50+
}
51+
}
52+
}
53+
3654
/**
3755
* Get all of the headers and values associated with the HTTP headers.
3856
* Modifications of this map are not supported.

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ public boolean isContentConsumed() {
105105
protected RestRequest(
106106
XContentParserConfiguration parserConfig,
107107
Map<String, String> params,
108-
String path,
108+
String rawPath,
109109
Map<String, List<String>> headers,
110110
HttpRequest httpRequest,
111111
HttpChannel httpChannel
112112
) {
113-
this(parserConfig, params, path, headers, httpRequest, httpChannel, requestIdGenerator.incrementAndGet());
113+
this(parserConfig, params, rawPath, headers, httpRequest, httpChannel, requestIdGenerator.incrementAndGet());
114114
}
115115

116116
@SuppressWarnings("this-escape")
117117
private RestRequest(
118118
XContentParserConfiguration parserConfig,
119119
Map<String, String> params,
120-
String path,
120+
String rawPath,
121121
Map<String, List<String>> headers,
122122
HttpRequest httpRequest,
123123
HttpChannel httpChannel,
@@ -149,7 +149,7 @@ private RestRequest(
149149
: parserConfig.withRestApiVersion(effectiveApiVersion);
150150
this.httpChannel = httpChannel;
151151
this.params = params;
152-
this.rawPath = path;
152+
this.rawPath = rawPath;
153153
this.headers = Collections.unmodifiableMap(headers);
154154
this.requestId = requestId;
155155
}
@@ -204,11 +204,10 @@ void ensureSafeBuffers() {
204204
*/
205205
public static RestRequest request(XContentParserConfiguration parserConfig, HttpRequest httpRequest, HttpChannel httpChannel) {
206206
Map<String, String> params = params(httpRequest.uri());
207-
String path = path(httpRequest.uri());
208207
return new RestRequest(
209208
parserConfig,
210209
params,
211-
path,
210+
httpRequest.rawPath(),
212211
httpRequest.getHeaders(),
213212
httpRequest,
214213
httpChannel,
@@ -229,15 +228,6 @@ private static Map<String, String> params(final String uri) {
229228
return params;
230229
}
231230

232-
private static String path(final String uri) {
233-
final int index = uri.indexOf('?');
234-
if (index >= 0) {
235-
return uri.substring(0, index);
236-
} else {
237-
return uri;
238-
}
239-
}
240-
241231
/**
242232
* Creates a new REST request. The path is not decoded so this constructor will not throw a
243233
* {@link BadParameterException}.

0 commit comments

Comments
 (0)