-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add http stream content size handler #120246
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eaf5d32
add http stream content size handler
mhl-b 89cd5e5
fix leak
mhl-b 1cdfe46
feedback
mhl-b 06037be
Merge remote-tracking branch 'upstream/main' into http-content-size-h…
mhl-b 26fd11a
update test
mhl-b c560536
merge main
mhl-b 6080e50
add mixed test
mhl-b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,7 +54,6 @@ | |
| import org.elasticsearch.common.util.CollectionUtils; | ||
| import org.elasticsearch.features.NodeFeature; | ||
| import org.elasticsearch.http.HttpBodyTracer; | ||
| import org.elasticsearch.http.HttpHandlingSettings; | ||
| import org.elasticsearch.http.HttpServerTransport; | ||
| import org.elasticsearch.http.HttpTransportSettings; | ||
| import org.elasticsearch.plugins.ActionPlugin; | ||
|
|
@@ -93,10 +92,15 @@ | |
| @ESIntegTestCase.ClusterScope(numDataNodes = 1) | ||
| public class Netty4IncrementalRequestHandlingIT extends ESNetty4IntegTestCase { | ||
|
|
||
| private static final int maxContentLength = 50 * 1024 * 1024; | ||
|
|
||
| @Override | ||
| protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { | ||
| Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal, otherSettings)); | ||
| builder.put(HttpTransportSettings.SETTING_HTTP_MAX_CONTENT_LENGTH.getKey(), new ByteSizeValue(50, ByteSizeUnit.MB)); | ||
| builder.put( | ||
| HttpTransportSettings.SETTING_HTTP_MAX_CONTENT_LENGTH.getKey(), | ||
| new ByteSizeValue(maxContentLength, ByteSizeUnit.BYTES) | ||
| ); | ||
| return builder.build(); | ||
| } | ||
|
|
||
|
|
@@ -135,7 +139,7 @@ public void testReceiveAllChunks() throws Exception { | |
| var opaqueId = opaqueId(reqNo); | ||
|
|
||
| // this dataset will be compared with one on server side | ||
| var dataSize = randomIntBetween(1024, maxContentLength()); | ||
| var dataSize = randomIntBetween(1024, maxContentLength); | ||
| var sendData = Unpooled.wrappedBuffer(randomByteArrayOfLength(dataSize)); | ||
| sendData.retain(); | ||
| ctx.clientChannel.writeAndFlush(fullHttpRequest(opaqueId, sendData)); | ||
|
|
@@ -243,7 +247,7 @@ public void testServerExceptionMidStream() throws Exception { | |
| public void testClientBackpressure() throws Exception { | ||
| try (var ctx = setupClientCtx()) { | ||
| var opaqueId = opaqueId(0); | ||
| var payloadSize = maxContentLength(); | ||
| var payloadSize = maxContentLength; | ||
| var totalParts = 10; | ||
| var partSize = payloadSize / totalParts; | ||
| ctx.clientChannel.writeAndFlush(httpRequest(opaqueId, payloadSize)); | ||
|
|
@@ -285,7 +289,7 @@ public void test100Continue() throws Exception { | |
| try (var ctx = setupClientCtx()) { | ||
| for (int reqNo = 0; reqNo < randomIntBetween(2, 10); reqNo++) { | ||
| var id = opaqueId(reqNo); | ||
| var acceptableContentLength = randomIntBetween(0, maxContentLength()); | ||
| var acceptableContentLength = randomIntBetween(0, maxContentLength); | ||
|
|
||
| // send request header and await 100-continue | ||
| var req = httpRequest(id, acceptableContentLength); | ||
|
|
@@ -317,7 +321,7 @@ public void test413TooLargeOnExpect100Continue() throws Exception { | |
| try (var ctx = setupClientCtx()) { | ||
| for (int reqNo = 0; reqNo < randomIntBetween(2, 10); reqNo++) { | ||
| var id = opaqueId(reqNo); | ||
| var oversized = maxContentLength() + 1; | ||
| var oversized = maxContentLength + 1; | ||
|
|
||
| // send request header and await 413 too large | ||
| var req = httpRequest(id, oversized); | ||
|
|
@@ -333,32 +337,28 @@ public void test413TooLargeOnExpect100Continue() throws Exception { | |
| } | ||
| } | ||
|
|
||
| // ensures that oversized chunked encoded request has no limits at http layer | ||
| // rest handler is responsible for oversized requests | ||
| public void testOversizedChunkedEncodingNoLimits() throws Exception { | ||
| // ensures that oversized chunked encoded request has maxContentLength limit and returns 413 | ||
| public void testOversizedChunkedEncoding() throws Exception { | ||
|
Comment on lines
-336
to
+341
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With stream introduction we dropped limits for chunked content. Bringing them back. |
||
| try (var ctx = setupClientCtx()) { | ||
| for (var reqNo = 0; reqNo < randomIntBetween(2, 10); reqNo++) { | ||
| var id = opaqueId(reqNo); | ||
| var contentSize = maxContentLength() + 1; | ||
| var content = randomByteArrayOfLength(contentSize); | ||
| var is = new ByteBufInputStream(Unpooled.wrappedBuffer(content)); | ||
| var chunkedIs = new ChunkedStream(is); | ||
| var httpChunkedIs = new HttpChunkedInput(chunkedIs, LastHttpContent.EMPTY_LAST_CONTENT); | ||
| var req = httpRequest(id, 0); | ||
| HttpUtil.setTransferEncodingChunked(req, true); | ||
|
|
||
| ctx.clientChannel.pipeline().addLast(new ChunkedWriteHandler()); | ||
| ctx.clientChannel.writeAndFlush(req); | ||
| ctx.clientChannel.writeAndFlush(httpChunkedIs); | ||
| var handler = ctx.awaitRestChannelAccepted(id); | ||
| var consumed = handler.readAllBytes(); | ||
| assertEquals(contentSize, consumed); | ||
| handler.sendResponse(new RestResponse(RestStatus.OK, "")); | ||
|
|
||
| var resp = (FullHttpResponse) safePoll(ctx.clientRespQueue); | ||
| assertEquals(HttpResponseStatus.OK, resp.status()); | ||
| resp.release(); | ||
| } | ||
| var id = opaqueId(0); | ||
| var contentSize = maxContentLength + 1; | ||
| var content = randomByteArrayOfLength(contentSize); | ||
| var is = new ByteBufInputStream(Unpooled.wrappedBuffer(content)); | ||
| var chunkedIs = new ChunkedStream(is); | ||
| var httpChunkedIs = new HttpChunkedInput(chunkedIs, LastHttpContent.EMPTY_LAST_CONTENT); | ||
| var req = httpRequest(id, 0); | ||
| HttpUtil.setTransferEncodingChunked(req, true); | ||
|
|
||
| ctx.clientChannel.pipeline().addLast(new ChunkedWriteHandler()); | ||
| ctx.clientChannel.writeAndFlush(req); | ||
| ctx.clientChannel.writeAndFlush(httpChunkedIs); | ||
| var handler = ctx.awaitRestChannelAccepted(id); | ||
| var consumed = handler.readAllBytes(); | ||
| assertTrue(consumed <= maxContentLength); | ||
|
|
||
| var resp = (FullHttpResponse) safePoll(ctx.clientRespQueue); | ||
| assertEquals(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, resp.status()); | ||
| resp.release(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -369,7 +369,7 @@ public void testBadRequestReleaseQueuedChunks() throws Exception { | |
| try (var ctx = setupClientCtx()) { | ||
| for (var reqNo = 0; reqNo < randomIntBetween(2, 10); reqNo++) { | ||
| var id = opaqueId(reqNo); | ||
| var contentSize = randomIntBetween(0, maxContentLength()); | ||
| var contentSize = randomIntBetween(0, maxContentLength); | ||
| var req = httpRequest(id, contentSize); | ||
| var content = randomContent(contentSize, true); | ||
|
|
||
|
|
@@ -405,7 +405,7 @@ public void testHttpClientStats() throws Exception { | |
|
|
||
| for (var reqNo = 0; reqNo < randomIntBetween(2, 10); reqNo++) { | ||
| var id = opaqueId(reqNo); | ||
| var contentSize = randomIntBetween(0, maxContentLength()); | ||
| var contentSize = randomIntBetween(0, maxContentLength); | ||
| totalBytesSent += contentSize; | ||
| ctx.clientChannel.writeAndFlush(httpRequest(id, contentSize)); | ||
| ctx.clientChannel.writeAndFlush(randomContent(contentSize, true)); | ||
|
|
@@ -485,10 +485,6 @@ private void assertHttpBodyLogging(Function<Ctx, Runnable> test) throws Exceptio | |
| } | ||
| } | ||
|
|
||
| private int maxContentLength() { | ||
| return HttpHandlingSettings.fromSettings(internalCluster().getInstance(Settings.class)).maxContentLength(); | ||
| } | ||
|
|
||
| private String opaqueId(int reqNo) { | ||
| return getTestName() + "-" + reqNo; | ||
| } | ||
|
|
@@ -658,14 +654,22 @@ void sendResponse(RestResponse response) { | |
| int readBytes(int bytes) { | ||
| var consumed = 0; | ||
| if (recvLast == false) { | ||
| while (consumed < bytes) { | ||
| stream.next(); | ||
| var recvChunk = safePoll(recvChunks); | ||
| consumed += recvChunk.chunk.length(); | ||
| recvChunk.chunk.close(); | ||
| if (recvChunk.isLast) { | ||
| recvLast = true; | ||
| break; | ||
| stream.next(); | ||
| while (consumed < bytes && streamClosed == false) { | ||
| try { | ||
| var recvChunk = recvChunks.poll(10, TimeUnit.MILLISECONDS); | ||
| if (recvChunk != null) { | ||
| consumed += recvChunk.chunk.length(); | ||
| recvChunk.chunk.close(); | ||
| if (recvChunk.isLast) { | ||
| recvLast = true; | ||
| break; | ||
| } | ||
| stream.next(); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new AssertionError(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: suggest
SHOUTY_SNAKE_CASEfor a constant, plus avoiding* 1024 * 1024.