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

4.x: GZIP encoder - properly trigger chunked enc #7977

Merged
merged 1 commit into from
Nov 10, 2023
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
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