Skip to content

Commit

Permalink
Fix compression error log (#158)
Browse files Browse the repository at this point in the history
* fix compression error log

fixes an issue where the outputstream is flushed after sending a compressed response

* some docs

* Update CompressionTest.java
  • Loading branch information
SentryMan authored Jan 6, 2025
1 parent af41e86 commit 08cba43
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
8 changes: 5 additions & 3 deletions avaje-jex/src/main/java/io/avaje/jex/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,12 @@ default Context headers(Map<String, String> headers) {
String protocol();

/**
* Gets basic-auth credentials from the request, or throws.
* Gets basic-auth credentials from the request.
*
* <p>Returns a wrapper object containing the Base64 decoded username and password from the
* Authorization header, or null if basic-auth is not properly configured
* @return The Base64 decoded username and password from the
* Authorization header, or null if no header is sent
*
* @throws IllegalStateException if the Authorization header is malformed
*/
BasicAuthCredentials basicAuthCredentials();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ private void initialiseChunked() throws IOException {
@Override
public void close() throws IOException {
if (stream != null) {
stream.flush();
if (!context.responseSent()) {
stream.flush();
}
stream.close();
} else {
context.write(buffer.toByteArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -43,11 +46,22 @@ static void end() {
}

@Test
void testCompression() {
HttpResponse<String> res =
pair.request().header(Constants.ACCEPT_ENCODING, "deflate, gzip;q=1.0, *;q=0.5").path("compress").GET().asString();
void testCompression() throws IOException {
var res =
pair.request()
.header(Constants.ACCEPT_ENCODING, "deflate, gzip;q=1.0, *;q=0.5")
.path("compress")
.GET()
.asInputStream();
assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.headers().firstValue(Constants.CONTENT_ENCODING)).contains("gzip");

var expected = CompressionTest.class.getResourceAsStream("/64KB.json").readAllBytes();

final var gzipInputStream = new GZIPInputStream(res.body());
var decompressed = gzipInputStream.readAllBytes();
gzipInputStream.close();
assertThat(decompressed).isEqualTo(expected);
}

@Test
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.ebean</groupId>
<version>${ebean.version}</version>
<artifactId>querybean-generator</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-jex</artifactId>
Expand Down

0 comments on commit 08cba43

Please sign in to comment.