Skip to content
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

Jetty 12 - Fix GzipHandler handling of chunked and Content-Length #8927

Merged
merged 20 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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 @@ -383,6 +383,8 @@ private void sendContent(MetaData.Request request, ByteBuffer content, boolean l
boolean hasContent = BufferUtil.hasContent(content) && !isHeadRequest;
if (hasContent || (last && !isTunnel(request, _responseMetaData)))
{
if (!hasContent)
content = BufferUtil.EMPTY_BUFFER;
if (last)
{
HttpFields trailers = retrieveTrailers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http2.api.Session;
import org.eclipse.jetty.http2.api.Stream;
import org.eclipse.jetty.http2.frames.DataFrame;
Expand All @@ -29,14 +30,15 @@
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.util.Callback;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class ContentLengthTest extends AbstractTest
{
Expand Down Expand Up @@ -116,37 +118,33 @@ public void onReset(Stream stream, ResetFrame frame, Callback callback)
assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
}

// TODO
@ParameterizedTest
@ValueSource(strings = {"GET", "HEAD", "POST", "PUT"})
@Disabled("enable when GzipHandler is implemented")
public void testGzippedContentLengthAddedByServer(String method) throws Exception
{
fail();
byte[] data = new byte[4096];

// byte[] data = new byte[4096];
//
// GzipHandler gzipHandler = new GzipHandler();
// gzipHandler.addIncludedMethods(method);
// gzipHandler.setMinGzipSize(data.length / 2);
// gzipHandler.setHandler(new EmptyServerHandler()
// {
// @Override
// protected void service(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
// {
// response.setContentLength(data.length);
// response.write(true, callback, ByteBuffer.wrap(data));
// }
// });
//
// start(gzipHandler);
//
// ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
// .method(method)
// .send();
//
// HttpFields responseHeaders = response.getHeaders();
// long contentLength = responseHeaders.getLongField(HttpHeader.CONTENT_LENGTH.asString());
// assertTrue(0 < contentLength && contentLength < data.length);
GzipHandler gzipHandler = new GzipHandler();
gzipHandler.addIncludedMethods(method);
gzipHandler.setMinGzipSize(data.length / 2);
gzipHandler.setHandler(new Handler.Processor()
{
@Override
public void process(Request request, Response response, Callback callback)
{
// Write a single buffer, with a Content-Length
response.getHeaders().putLongField(HttpHeader.CONTENT_LENGTH, data.length);
response.write(true, ByteBuffer.wrap(data), callback);
}
});

start(gzipHandler);

ContentResponse response = httpClient.newRequest("localhost", connector.getLocalPort())
.method(method)
.send();

if (!HttpMethod.HEAD.is(method))
assertThat(response.getContent().length, is(data.length));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ private void sendContent(MetaData.Request request, ByteBuffer content, boolean l
boolean hasContent = BufferUtil.hasContent(content) && !isHeadRequest;
if (hasContent || (lastContent && !isTunnel(request, responseMetaData)))
{
if (!hasContent)
content = BufferUtil.EMPTY_BUFFER;
if (lastContent)
{
HttpFields trailers = retrieveTrailers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ public void setInflateBufferSize(int size)
@Override
public Request.Processor handle(Request request) throws Exception
{
if (getHandler() == null)
return null;

final String path = Request.getPathInContext(request);

if (LOG.isDebugEnabled())
Expand Down Expand Up @@ -615,8 +618,7 @@ else if (COMMA_GZIP.matcher(field.getValue()).matches())
return wrappedRequest.wrapProcessor(super.handle(wrappedRequest));
}

// If not a supported URI- no Vary because no matter what client, this URI is always excluded
// Use pathInfo because this is be
// If not a supported URI - no Vary because no matter what client, this URI is always excluded
if (!isPathGzipable(path))
{
LOG.debug("{} excluded by path {}", this, request);
Expand Down
Loading