From 2cbb0e19e0a6c5cdfccd7080baa106b864def40b Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 10 Oct 2022 16:22:26 +0200 Subject: [PATCH] Forward port of PR #7778 to `jetty-12.0.x`. Signed-off-by: Simone Bordet --- .../eclipse/jetty/ee10/servlet/HttpInput.java | 38 +++++++++++++++++-- .../eclipse/jetty/ee9/nested/HttpInput.java | 38 +++++++++++++++++-- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/HttpInput.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/HttpInput.java index 430743d58d3..ca6149852cb 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/HttpInput.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/HttpInput.java @@ -138,14 +138,34 @@ public class HttpInput extends ServletInputStream implements Runnable } } - private int get(Content.Chunk content, byte[] bytes, int offset, int length) + private int get(Content.Chunk chunk, byte[] bytes, int offset, int length) { - length = Math.min(content.remaining(), length); - content.getByteBuffer().get(bytes, offset, length); + length = Math.min(chunk.remaining(), length); + chunk.getByteBuffer().get(bytes, offset, length); _contentConsumed.add(length); return length; } + private int get(Content.Chunk chunk, ByteBuffer des) + { + var capacity = des.remaining(); + var src = chunk.getByteBuffer(); + if (src.remaining() > capacity) + { + int limit = src.limit(); + src.limit(src.position() + capacity); + des.put(src); + src.limit(limit); + } + else + { + des.put(src); + } + var consumed = capacity - des.remaining(); + _contentConsumed.add(consumed); + return consumed; + } + public long getContentConsumed() { return _contentConsumed.sum(); @@ -257,6 +277,16 @@ public class HttpInput extends ServletInputStream implements Runnable @Override public int read(byte[] b, int off, int len) throws IOException + { + return read(null, b, off, len); + } + + public int read(ByteBuffer buffer) throws IOException + { + return read(buffer, null, -1, -1); + } + + private int read(ByteBuffer buffer, byte[] b, int off, int len) throws IOException { try (AutoLock lock = _contentProducer.lock()) { @@ -272,7 +302,7 @@ public class HttpInput extends ServletInputStream implements Runnable throw new IllegalStateException("read on unready input"); if (!chunk.isTerminal()) { - int read = get(chunk, b, off, len); + int read = buffer == null ? get(chunk, b, off, len) : get(chunk, buffer); if (LOG.isDebugEnabled()) LOG.debug("read produced {} byte(s) {}", read, this); if (!chunk.hasRemaining()) diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpInput.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpInput.java index 5df0903da39..c78a26bd63e 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpInput.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/HttpInput.java @@ -131,14 +131,34 @@ public class HttpInput extends ServletInputStream implements Runnable } } - private int get(Content.Chunk content, byte[] bytes, int offset, int length) + private int get(Content.Chunk chunk, byte[] bytes, int offset, int length) { - length = Math.min(content.remaining(), length); - content.getByteBuffer().get(bytes, offset, length); + length = Math.min(chunk.remaining(), length); + chunk.getByteBuffer().get(bytes, offset, length); _contentConsumed.add(length); return length; } + private int get(Content.Chunk chunk, ByteBuffer des) + { + var capacity = des.remaining(); + var src = chunk.getByteBuffer(); + if (src.remaining() > capacity) + { + int limit = src.limit(); + src.limit(src.position() + capacity); + des.put(src); + src.limit(limit); + } + else + { + des.put(src); + } + var consumed = capacity - des.remaining(); + _contentConsumed.add(consumed); + return consumed; + } + public long getContentConsumed() { return _contentConsumed.sum(); @@ -262,6 +282,16 @@ public class HttpInput extends ServletInputStream implements Runnable @Override public int read(byte[] b, int off, int len) throws IOException + { + return read(null, b, off, len); + } + + public int read(ByteBuffer buffer) throws IOException + { + return read(buffer, null, -1, -1); + } + + private int read(ByteBuffer buffer, byte[] b, int off, int len) throws IOException { try (AutoLock lock = _contentProducer.lock()) { @@ -298,7 +328,7 @@ public class HttpInput extends ServletInputStream implements Runnable } else { - int read = get(content, b, off, len); + int read = buffer == null ? get(content, b, off, len) : get(content, buffer); if (LOG.isDebugEnabled()) LOG.debug("read produced {} byte(s) {}", read, this); if (!content.hasRemaining())