Skip to content

Commit

Permalink
Create FileBufferedResponseHandler to buffer responses into a file. (#…
Browse files Browse the repository at this point in the history
…6010)

FileBufferedResponseHandler adds an HttpOutput.Interceptor to buffer all responses into a file until the output is closed. This allows the commit to be delayed until the response is complete and thus headers and response status can be changed while writing the body.

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Apr 19, 2021
1 parent 39fe2ec commit eca8edc
Show file tree
Hide file tree
Showing 7 changed files with 995 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,9 @@ public static boolean hasNoBody(int status)
switch (status)
{
case NO_CONTENT_204:
case NOT_MODIFIED_304:
case RESET_CONTENT_205:
case PARTIAL_CONTENT_206:
case NOT_MODIFIED_304:
return true;

default:
Expand Down
20 changes: 14 additions & 6 deletions jetty-io/src/main/java/org/eclipse/jetty/io/ByteArrayEndPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class ByteArrayEndPoint extends AbstractEndPoint
static final Logger LOG = Log.getLogger(ByteArrayEndPoint.class);
static final InetAddress NOIP;
static final InetSocketAddress NOIPPORT;
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 1024;

static
{
Expand Down Expand Up @@ -80,6 +81,7 @@ public void run()
private final Locker _locker = new Locker();
private final Condition _hasOutput = _locker.newCondition();
private final Queue<ByteBuffer> _inQ = new ArrayDeque<>();
private final int _outputSize;
private ByteBuffer _out;
private boolean _growOutput;

Expand Down Expand Up @@ -129,7 +131,8 @@ public ByteArrayEndPoint(Scheduler timer, long idleTimeoutMs, ByteBuffer input,
super(timer);
if (BufferUtil.hasContent(input))
addInput(input);
_out = output == null ? BufferUtil.allocate(1024) : output;
_outputSize = (output == null) ? 1024 : output.capacity();
_out = output == null ? BufferUtil.allocate(_outputSize) : output;
setIdleTimeout(idleTimeoutMs);
onOpen();
}
Expand Down Expand Up @@ -296,7 +299,7 @@ public ByteBuffer takeOutput()
try (Locker.Lock lock = _locker.lock())
{
b = _out;
_out = BufferUtil.allocate(b.capacity());
_out = BufferUtil.allocate(_outputSize);
}
getWriteFlusher().completeWrite();
return b;
Expand All @@ -322,7 +325,7 @@ public ByteBuffer waitForOutput(long time, TimeUnit unit) throws InterruptedExce
return null;
}
b = _out;
_out = BufferUtil.allocate(b.capacity());
_out = BufferUtil.allocate(_outputSize);
}
getWriteFlusher().completeWrite();
return b;
Expand Down Expand Up @@ -436,9 +439,14 @@ public boolean flush(ByteBuffer... buffers) throws IOException
BufferUtil.compact(_out);
if (b.remaining() > BufferUtil.space(_out))
{
ByteBuffer n = BufferUtil.allocate(_out.capacity() + b.remaining() * 2);
BufferUtil.append(n, _out);
_out = n;
// Don't grow larger than MAX_BUFFER_SIZE to avoid memory issues.
if (_out.capacity() < MAX_BUFFER_SIZE)
{
long newBufferCapacity = Math.min((long)(_out.capacity() + b.remaining() * 1.5), MAX_BUFFER_SIZE);
ByteBuffer n = BufferUtil.allocate(Math.toIntExact(newBufferCapacity));
BufferUtil.append(n, _out);
_out = n;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ public void close() throws IOException
catch (Throwable t)
{
onWriteComplete(true, t);
throw t;
}
}
}
Expand Down

0 comments on commit eca8edc

Please sign in to comment.