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

Recognize & handle brotli compression of the response body #742

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions browsermob-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,17 @@
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.meteogroup.jbrotli</groupId>
<artifactId>jbrotli-servlet</artifactId>
<version>0.5.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>bintray-nitram509-jbrotli</id>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better not rely on a private bintray repo, why is there no official build?

<name>bintray</name>
<url>http://dl.bintray.com/nitram509/jbrotli</url>
</repository>
</repositories>
</project>
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 @@ -9,12 +9,15 @@
import io.netty.handler.codec.http.HttpResponse;
import net.lightbody.bmp.exception.DecompressionException;
import net.lightbody.bmp.exception.UnsupportedCharsetException;
import org.meteogroup.jbrotli.io.BrotliInputStream;
import org.meteogroup.jbrotli.libloader.BrotliLibraryLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -111,6 +114,47 @@ public static byte[] decompressContents(byte[] fullMessage) throws Decompression
return fullMessage;
}

/**
* Decompresses the brotli byte 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 {
BrotliLibraryLoader.loadBrotli();

InputStream brotliInputReader = null;
ByteArrayOutputStream uncompressed = null;

try {
brotliInputReader = new BrotliInputStream(new ByteArrayInputStream(fullMessage));

uncompressed = new ByteArrayOutputStream();

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

uncompressed.flush();
fullMessage = uncompressed.toByteArray();
} catch (IOException e) {
throw new DecompressionException("Unable to decompress response", e);
} finally {
try {
if (brotliInputReader != null) {
brotliInputReader.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