Skip to content

Commit

Permalink
#7914 GZIP encoder didn't properly trigger chunked enc (#7977)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkec committed Nov 10, 2023
1 parent 18223c7 commit 9ab1bab
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
5 changes: 5 additions & 0 deletions http/tests/encoding/gzip/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-static-content</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.http.tests.integration.encoding.gzip;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;

import io.helidon.http.HeaderValues;
import io.helidon.webclient.api.WebClient;
import io.helidon.webserver.WebServerConfig;
import io.helidon.webserver.http.HttpRouting;
import io.helidon.webserver.staticcontent.StaticContentService;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;
import io.helidon.webserver.testing.junit5.SetUpServer;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@ServerTest
class GzipEncodingStaticContentTest {

@SetUpRoute
static void routing(HttpRouting.Builder builder) {
builder.register("/", StaticContentService.builder("/WEB")
.welcomeFileName("index.html")
.build());
}

@Test
void compressedStaticContent(WebClient client) throws IOException, URISyntaxException {
try (var res = client.get("/")
.header(HeaderValues.createCached("Accept-Encoding", "deflate, gzip"))
.request()) {
assertThat(res.as(String.class),
is(Files.readString(Path.of(this.getClass().getResource("/WEB/index.html").toURI()))));
}
}
}
27 changes: 27 additions & 0 deletions http/tests/encoding/gzip/src/test/resources/WEB/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
Copyright (c) 2023 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>It works!</h1>

</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,14 @@ public OutputStream outputStream() {

int writeBufferSize = ctx.listenerContext().config().writeBufferSize();
outputStream = new ClosingBufferedOutputStream(bos, writeBufferSize);

OutputStream encodedOutputStream = contentEncode(outputStream);
// Headers can be augmented by encoders
bos.checkResponseHeaders();
if (outputStreamFilter == null) {
return contentEncode(outputStream);
return encodedOutputStream;
} else {
return outputStreamFilter.apply(contentEncode(outputStream));
return outputStreamFilter.apply(encodedOutputStream);
}
}

Expand Down Expand Up @@ -383,7 +387,7 @@ private static class BlockingOutputStream extends OutputStream {
private final Http1ServerRequest request;
private final boolean keepAlive;
private final Supplier<String> streamResult;
private final boolean forcedChunked;
private boolean forcedChunked;

private BufferData firstBuffer;
private boolean closed;
Expand Down Expand Up @@ -414,13 +418,16 @@ private BlockingOutputStream(ServerResponseHeaders headers,
this.responseCloseRunnable = responseCloseRunnable;
this.ctx = ctx;
this.sendListener = sendListener;
this.isChunked = !headers.contains(HeaderNames.CONTENT_LENGTH);
this.contentLength = headers.contentLength().orElse(-1);
this.request = request;
this.keepAlive = keepAlive;
this.validateHeaders = validateHeaders;
}

void checkResponseHeaders(){
this.isChunked = !headers.contains(HeaderNames.CONTENT_LENGTH);
this.forcedChunked = headers.contains(HeaderValues.TRANSFER_ENCODING_CHUNKED)
|| headers.contains(HeaderNames.TRAILER);
this.validateHeaders = validateHeaders;
}

@Override
Expand Down

0 comments on commit 9ab1bab

Please sign in to comment.