From d66e36b7462b2b40a8b36fe6327b33cbfa9a6190 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Wed, 1 Feb 2023 14:59:29 +1100 Subject: [PATCH] consumeAvailable should use number of reads instead of bytes Signed-off-by: Lachlan Roberts --- .../jetty/server/HttpConfiguration.java | 22 +++++++++---------- .../org/eclipse/jetty/server/HttpStream.java | 8 +++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpConfiguration.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpConfiguration.java index d358101e1ae..6797c807809 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpConfiguration.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpConfiguration.java @@ -79,7 +79,7 @@ public class HttpConfiguration implements Dumpable private boolean _relativeRedirectAllowed; private HostPort _serverAuthority; private SocketAddress _localAddress; - private long _maxUnconsumedRequestContentBytes = 1024 * 1024; + private int _maxUnconsumedRequestContentReads = 16; /** *

An interface that allows a request object to be customized @@ -718,25 +718,25 @@ public class HttpConfiguration implements Dumpable } /** - * Sets the maximum amount of bytes that will be read from the HttpStream if the content is not fully consumed by the - * application. If this is unable to consume to EOF then the connection will be made non-persistent. + * Sets the maximum amount of {@link HttpStream#read()}s that can be done by the {@link HttpStream} if the content is not + * fully consumed by the application. If this is unable to consume to EOF then the connection will be made non-persistent. * - * @param maxUnconsumedRequestContentBytes the maximum amount of unconsumed bytes that will be read from the HttpStream. + * @param maxUnconsumedRequestContentReads the maximum amount of reads for unconsumed content or -1 for unlimited. */ - public void setMaxUnconsumedRequestContentBytes(long maxUnconsumedRequestContentBytes) + public void setMaxUnconsumedRequestContentReads(int maxUnconsumedRequestContentReads) { - _maxUnconsumedRequestContentBytes = maxUnconsumedRequestContentBytes; + _maxUnconsumedRequestContentReads = maxUnconsumedRequestContentReads; } /** - * Gets the maximum amount of bytes that will be read from the HttpStream if the content is not fully consumed by the - * application. If this is unable to consume to EOF then the connection will be made non-persistent. + * Gets the maximum amount of {@link HttpStream#read()}s that can be done by the {@link HttpStream} if the content is not + * fully consumed by the application. If this is unable to consume to EOF then the connection will be made non-persistent. * - * @return the maximum amount of unconsumed bytes that will be read from the HttpStream. + * @return the maximum amount of reads for unconsumed content or -1 for unlimited. */ - public long getMaxUnconsumedRequestContentBytes() + public int getMaxUnconsumedRequestContentReads() { - return _maxUnconsumedRequestContentBytes; + return _maxUnconsumedRequestContentReads; } @Override diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpStream.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpStream.java index 6946df400d3..546c488f0de 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpStream.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/HttpStream.java @@ -108,19 +108,19 @@ public interface HttpStream extends Callback static Throwable consumeAvailable(HttpStream stream, HttpConfiguration httpConfig) { - long consumedRequestContentBytes = 0; - long maxUnconsumedRequestContentBytes = httpConfig.getMaxUnconsumedRequestContentBytes(); - while (maxUnconsumedRequestContentBytes < 0 || consumedRequestContentBytes < maxUnconsumedRequestContentBytes) + int numReads = 0; + int maxReads = httpConfig.getMaxUnconsumedRequestContentReads(); + while (maxReads < 0 || numReads < maxReads) { // We can always just read again here as EOF and Error content will be persistently returned. Chunk content = stream.read(); + numReads++; // if we cannot read to EOF then fail the stream rather than wait for unconsumed content if (content == null) return new IOException("Content not consumed"); // Always release any returned content. This is a noop for EOF and Error content. - consumedRequestContentBytes += content.remaining(); content.release(); // if the input failed, then fail the stream for same reason