use of java.util.zip.Inflater in project jetty.project by eclipse.
the class GzipTester method getResponseMetadata.
public ContentMetadata getResponseMetadata(Response response) throws Exception {
long size = response.getContentBytes().length;
String contentEncoding = response.get("Content-Encoding");
ByteArrayInputStream bais = null;
InputStream in = null;
DigestOutputStream digester = null;
ByteArrayOutputStream uncompressedStream = null;
try {
MessageDigest digest = MessageDigest.getInstance("SHA1");
bais = new ByteArrayInputStream(response.getContentBytes());
if (contentEncoding == null) {
LOG.debug("No response content-encoding");
in = new PassThruInputStream(bais);
} else if (contentEncoding.contains(GzipHandler.GZIP)) {
in = new GZIPInputStream(bais);
} else if (contentEncoding.contains(GzipHandler.DEFLATE)) {
in = new InflaterInputStream(bais, new Inflater(true));
} else {
assertThat("Unexpected response content-encoding", contentEncoding, isEmptyOrNullString());
}
uncompressedStream = new ByteArrayOutputStream((int) size);
digester = new DigestOutputStream(uncompressedStream, digest);
IO.copy(in, digester);
byte[] output = uncompressedStream.toByteArray();
String actualSha1Sum = Hex.asHex(digest.digest());
return new ContentMetadata(output.length, actualSha1Sum);
} finally {
IO.close(digester);
IO.close(in);
IO.close(bais);
IO.close(uncompressedStream);
}
}
use of java.util.zip.Inflater in project jetty.project by eclipse.
the class GzipTester method assertIsResponseGzipCompressed.
public HttpTester.Response assertIsResponseGzipCompressed(String method, String requestedFilename, String serverFilename, long ifmodifiedsince) throws Exception {
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
request.setMethod(method);
request.setVersion("HTTP/1.0");
request.setHeader("Host", "tester");
request.setHeader("Accept-Encoding", compressionType);
if (ifmodifiedsince > 0)
request.setHeader(HttpHeader.IF_MODIFIED_SINCE.asString(), DateGenerator.formatDate(ifmodifiedsince));
if (this.userAgent != null)
request.setHeader("User-Agent", this.userAgent);
request.setURI("/context/" + requestedFilename);
// Issue the request
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
// Assert the response headers
// Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK));
// Response headers should have either a Transfer-Encoding indicating chunked OR a Content-Length
/*
* TODO need to check for the 3rd option of EOF content. To do this properly you might need to look at both HTTP/1.1 and HTTP/1.0 requests String
* contentLength = response.get("Content-Length"); String transferEncoding = response.get("Transfer-Encoding");
*
* boolean chunked = (transferEncoding != null) && (transferEncoding.indexOf("chunk") >= 0); if(!chunked) {
* Assert.assertThat("Response.header[Content-Length]",contentLength,notNullValue()); } else {
* Assert.assertThat("Response.header[Transfer-Encoding]",transferEncoding,notNullValue()); }
*/
int qindex = compressionType.indexOf(";");
if (qindex < 0)
Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType));
else
Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType.substring(0, qindex)));
Assert.assertThat(response.get("ETag"), Matchers.startsWith("W/"));
// Assert that the decompressed contents are what we expect.
File serverFile = testdir.getPathFile(serverFilename).toFile();
String expected = IO.readToString(serverFile);
String actual = null;
ByteArrayInputStream bais = null;
InputStream in = null;
ByteArrayOutputStream out = null;
try {
bais = new ByteArrayInputStream(response.getContentBytes());
if (compressionType.startsWith(GzipHandler.GZIP)) {
in = new GZIPInputStream(bais);
} else if (compressionType.startsWith(GzipHandler.DEFLATE)) {
in = new InflaterInputStream(bais, new Inflater(true));
}
out = new ByteArrayOutputStream();
IO.copy(in, out);
actual = out.toString(encoding);
assertThat("Uncompressed contents", actual, equalTo(expected));
} finally {
IO.close(out);
IO.close(in);
IO.close(bais);
}
return response;
}
use of java.util.zip.Inflater in project jetty.project by eclipse.
the class GzipTester method assertNonStaticContentIsResponseGzipCompressed.
public HttpTester.Response assertNonStaticContentIsResponseGzipCompressed(String method, String path, String expected) throws Exception {
HttpTester.Request request = HttpTester.newRequest();
HttpTester.Response response;
request.setMethod(method);
request.setVersion("HTTP/1.0");
request.setHeader("Host", "tester");
request.setHeader("Accept-Encoding", accept);
if (this.userAgent != null)
request.setHeader("User-Agent", this.userAgent);
request.setURI("/context/" + path);
// Issue the request
response = HttpTester.parseResponse(tester.getResponses(request.generate()));
int qindex = compressionType.indexOf(";");
if (qindex < 0)
Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType));
else
Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType.substring(0, qindex)));
ByteArrayInputStream bais = null;
InputStream in = null;
ByteArrayOutputStream out = null;
String actual = null;
try {
bais = new ByteArrayInputStream(response.getContentBytes());
if (compressionType.startsWith(GzipHandler.GZIP)) {
in = new GZIPInputStream(bais);
} else if (compressionType.startsWith(GzipHandler.DEFLATE)) {
in = new InflaterInputStream(bais, new Inflater(true));
}
out = new ByteArrayOutputStream();
IO.copy(in, out);
actual = out.toString(encoding);
assertThat("Uncompressed contents", actual, equalTo(expected));
} finally {
IO.close(out);
IO.close(in);
IO.close(bais);
}
return response;
}
use of java.util.zip.Inflater in project jetty.project by eclipse.
the class IncludedGzipTest method testGzip.
@Test
public void testGzip() throws Exception {
// generated and parsed test
ByteBuffer request = BufferUtil.toBuffer("GET /context/file.txt HTTP/1.0\r\n" + "Host: tester\r\n" + "Accept-Encoding: " + compressionType + "\r\n" + "\r\n");
HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request));
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals(compressionType, response.get("Content-Encoding"));
InputStream testIn = null;
ByteArrayInputStream compressedResponseStream = new ByteArrayInputStream(response.getContentBytes());
if (compressionType.equals(GzipHandler.GZIP)) {
testIn = new GZIPInputStream(compressedResponseStream);
} else if (compressionType.equals(GzipHandler.DEFLATE)) {
testIn = new InflaterInputStream(compressedResponseStream, new Inflater(true));
}
ByteArrayOutputStream testOut = new ByteArrayOutputStream();
IO.copy(testIn, testOut);
assertEquals(__content, testOut.toString("ISO8859_1"));
}
use of java.util.zip.Inflater in project dropwizard by dropwizard.
the class BiDiGzipHandler method wrapDeflatedRequest.
private WrappedServletRequest wrapDeflatedRequest(HttpServletRequest request) throws IOException {
final Inflater inflater = buildInflater();
final InflaterInputStream input = new InflaterInputStream(request.getInputStream(), inflater, inputBufferSize) {
@Override
public void close() throws IOException {
super.close();
localInflater.set(inflater);
}
};
return new WrappedServletRequest(request, input);
}
Aggregations