Skip to content

Commit

Permalink
Skips content encoding of empty entities. Attempting to encode an emp…
Browse files Browse the repository at this point in the history
…ty entity with an encoder such as GZIP will result in a non-empty entity (a GZIP preamble) which is problematic --especially when a 204 is returned by a handler. (helidon-io#9000)
  • Loading branch information
spericas authored and barchetta committed Jul 18, 2024
1 parent aadd6c6 commit f6ec66d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
5 changes: 5 additions & 0 deletions webserver/tests/webserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@
<artifactId>helidon-logging-jul</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.http.encoding</groupId>
<artifactId>helidon-http-encoding-gzip</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024 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.webserver.tests;

import io.helidon.http.HeaderNames;
import io.helidon.http.Status;
import io.helidon.logging.common.LogConfig;
import io.helidon.webclient.http1.Http1Client;
import io.helidon.webclient.http1.Http1ClientResponse;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;

import org.junit.jupiter.api.Test;

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

@ServerTest
class ContentEncodingEmptyTest {

private final Http1Client client;

ContentEncodingEmptyTest(Http1Client client) {
this.client = client;
}

@SetUpRoute
static void routing(HttpRules rules) {
LogConfig.configureRuntime();
rules.post("/hello", (req, res) -> {
res.status(Status.NO_CONTENT_204);
res.send(); // empty entity
});
}

@Test
void gzipEncodeEmptyEntity() {
Http1ClientResponse res = client.post("hello")
.header(HeaderNames.CONTENT_TYPE, "application/json")
.header(HeaderNames.ACCEPT_ENCODING, "gzip")
.request();
assertThat(res.status().code(), is(204));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 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.
Expand Down Expand Up @@ -197,14 +197,15 @@ protected MediaContext mediaContext() {
}

/**
* Entity bytes encoded using content encoding.
* Entity bytes encoded using content encoding. Does not attempt encoding
* if entity is empty.
*
* @param configuredEntity plain bytes
* @return encoded bytes
*/
protected byte[] entityBytes(byte[] configuredEntity) {
byte[] entity = configuredEntity;
if (contentEncodingContext.contentEncodingEnabled()) {
if (contentEncodingContext.contentEncodingEnabled() && entity.length > 0) {
ContentEncoder encoder = contentEncodingContext.encoder(requestHeaders);
// we want to preserve optimization here, let's create a new byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream(entity.length);
Expand Down

0 comments on commit f6ec66d

Please sign in to comment.