Skip to content

[brotli] Add brotli support backed by org.brotli.dec #783

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions browsermob-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@
<version>${project.version}</version>
</dependency>

<!-- use google brotli for brotli decoding-->
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<version>0.1.2</version>
</dependency>

<!-- Netty will use javassist to improve performance if it is present on the classpath, but it is not required -->
<dependency>
<groupId>org.javassist</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
public class ServerResponseCaptureFilter extends HttpFiltersAdapter {
private static final Logger log = LoggerFactory.getLogger(ServerResponseCaptureFilter.class);
private static final String BROTLI_COMPRESSION = "br";

/**
* Populated by serverToProxyResponse() when processing the HttpResponse object
Expand Down Expand Up @@ -133,6 +134,13 @@ protected void decompressContents() {
} catch (RuntimeException e) {
log.warn("Failed to decompress response with encoding type " + contentEncoding + " when decoding request from " + originalRequest.getUri(), e);
}
} else if(contentEncoding.equals(BROTLI_COMPRESSION)) {
try {
fullResponseContents = BrowserMobHttpUtil.decompressBrotliContents(getRawResponseContents());
decompressionSuccessful = true;
} catch (RuntimeException e) {
log.warn("Failed to decompress response with encoding type " + contentEncoding + " when decoding request from " + originalRequest.getUri(), e);
}
} else {
log.warn("Cannot decode unsupported content encoding type {}", contentEncoding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.io.BaseEncoding;
import com.google.common.net.HostAndPort;
import com.google.common.net.MediaType;
import org.brotli.dec.BrotliInputStream;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
Expand All @@ -14,6 +15,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -78,7 +80,7 @@ public static long getHeaderSize(HttpHeaders headers) {
/**
* Decompresses the gzipped byte stream.
*
* @param fullMessage gzipped byte stream to decomress
* @param fullMessage gzipped byte stream to decompress
* @return decompressed bytes
* @throws DecompressionException thrown if the fullMessage cannot be read or decompressed for any reason
*/
Expand Down Expand Up @@ -111,6 +113,42 @@ public static byte[] decompressContents(byte[] fullMessage) throws Decompression
return fullMessage;
}

/**
* Decompresses the brotli byze stream
*
* @param fullMessage brotli byte stream to decompress
* @return decompressed bytes
* @throws DecompressionException thrown if the fullMessage cannot be read or decompressed for any reason
*/
public static byte[] decompressBrotliContents(byte[] fullMessage) throws DecompressionException {
InputStream brotliReader = null;
ByteArrayOutputStream uncompressed;
try {
brotliReader = new BrotliInputStream(new ByteArrayInputStream(fullMessage));

uncompressed = new ByteArrayOutputStream(fullMessage.length);

byte[] decompressBuffer = new byte[DECOMPRESS_BUFFER_SIZE];
int bytesRead;
while ((bytesRead = brotliReader.read(decompressBuffer)) > -1) {
uncompressed.write(decompressBuffer, 0, bytesRead);
}

fullMessage = uncompressed.toByteArray();
} catch (IOException e) {
throw new DecompressionException("Unable to decompress response", e);
} finally {
try {
if (brotliReader != null) {
brotliReader.close();
}
} catch (IOException e) {
log.warn("Unable to close brotli stream", e);
}
}
return fullMessage;
}

/**
* Returns true if the content type string indicates textual content. Currently these are any Content-Types that start with one of the
* following:
Expand Down