Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ public boolean handle(Request request, Response response, Callback callback) thr
if (inflatable && tryInflate || etagMatches)
{
// Wrap the request to update the fields and do any inflation
request = new GzipRequest(request, inflatable && tryInflate ? getInflateBufferSize() : -1);
GzipRequest gzipRequest = new GzipRequest(request, _inflaterPool, inflatable && tryInflate ? getInflateBufferSize() : -1);
request = gzipRequest;
callback = Callback.from(callback, gzipRequest::destroy);
}

if (tryDeflate && _vary != null)
Expand All @@ -611,9 +613,10 @@ public boolean handle(Request request, Response response, Callback callback) thr
if (next.handle(request, response, callback))
return true;

// If the request was not accepted, destroy any gzipRequest wrapper
// If the request was not handled, destroy GzipRequest.
if (request instanceof GzipRequest gzipRequest)
gzipRequest.destroy();

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ public class GzipRequest extends Request.Wrapper
{
private static final HttpField X_CE_GZIP = new PreEncodedHttpField("X-Content-Encoding", "gzip");

// TODO: use InflaterPool from somewhere.
private static final InflaterPool __inflaterPool = new InflaterPool(-1, true);

private final HttpFields _fields;
private Decoder _decoder;
private GzipTransformer _gzipTransformer;

public GzipRequest(Request request, int inflateBufferSize)
public GzipRequest(Request request, InflaterPool inflaterPool, int inflateBufferSize)
{
super(request);
_fields = updateRequestFields(request, inflateBufferSize > 0);

if (inflateBufferSize > 0)
{
Components components = getComponents();
_decoder = new Decoder(__inflaterPool, components.getByteBufferPool(), inflateBufferSize);
_decoder = new Decoder(inflaterPool, components.getByteBufferPool(), inflateBufferSize);
_gzipTransformer = new GzipTransformer(getWrapped(), _decoder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,18 @@ public GzipResponseAndCallback(GzipHandler handler, Request request, Response re
@Override
public void succeeded()
{
try
{
// We need to write nothing here to intercept the committing of the
// response and possibly change headers in case write is never called.
if (_last)
_callback.succeeded();
else
write(true, null, _callback);
}
finally
{
if (getRequest() instanceof GzipRequest gzipRequest)
gzipRequest.destroy();
}
// We need to write nothing here to intercept the committing of the
// response and possibly change headers in case write is never called.
if (_last)
_callback.succeeded();
else
write(true, null, _callback);
}

@Override
public void failed(Throwable x)
{
try
{
_callback.failed(x);
}
finally
{
if (getRequest() instanceof GzipRequest gzipRequest)
gzipRequest.destroy();
}
_callback.failed(x);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.Inflater;
Expand Down Expand Up @@ -71,8 +72,12 @@
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.Fields;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.Pool;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.compression.CompressionPool;
import org.eclipse.jetty.util.compression.DeflaterPool;
import org.eclipse.jetty.util.compression.InflaterPool;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -86,6 +91,7 @@
import org.junit.jupiter.params.provider.ValueSource;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -944,6 +950,10 @@ public void testGzippedRequestPost() throws Exception

assertThat(response.getStatus(), is(200));
assertThat(response.getContent(), is(data));

InflaterPool inflaterPool = _gzipHandler.getBean(InflaterPool.class);
Pool<CompressionPool<Inflater>.Entry> pool = inflaterPool.getPool();
await().atMost(5, TimeUnit.SECONDS).until(pool::getInUseCount, is(0));
}

@Test
Expand Down Expand Up @@ -1307,6 +1317,10 @@ public void testLargeResponse() throws Exception
{
assertEquals(CONTENT, new String(Arrays.copyOfRange(bytes, i * CONTENT_BYTES.length, (i + 1) * CONTENT_BYTES.length), StandardCharsets.UTF_8), "chunk " + i);
}

DeflaterPool deflaterPool = _gzipHandler.getBean(DeflaterPool.class);
Pool<CompressionPool<Deflater>.Entry> pool = deflaterPool.getPool();
await().atMost(5, TimeUnit.SECONDS).until(pool::getInUseCount, is(0));
}

/**
Expand Down
Loading