use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.
the class GZIPContentDecoderTest method testSmallBlockWithGZIPTrailerChunked.
@Test
public void testSmallBlockWithGZIPTrailerChunked() throws Exception {
String data = "0";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream output = new GZIPOutputStream(baos);
output.write(data.getBytes(StandardCharsets.UTF_8));
output.close();
byte[] bytes = baos.toByteArray();
// The trailer is 4+4 bytes, chunk the last 3 bytes
byte[] bytes1 = new byte[bytes.length - 3];
System.arraycopy(bytes, 0, bytes1, 0, bytes1.length);
byte[] bytes2 = new byte[bytes.length - bytes1.length];
System.arraycopy(bytes, bytes1.length, bytes2, 0, bytes2.length);
GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048);
ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1));
assertEquals(0, decoded.capacity());
decoder.release(decoded);
decoded = decoder.decode(ByteBuffer.wrap(bytes2));
assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString());
decoder.release(decoded);
}
use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.
the class GZIPContentDecoderTest method testBigBlockOneByteAtATime.
@Test
public void testBigBlockOneByteAtATime() throws Exception {
String data = "0123456789ABCDEF";
for (int i = 0; i < 10; ++i) data += data;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream output = new GZIPOutputStream(baos);
output.write(data.getBytes(StandardCharsets.UTF_8));
output.close();
byte[] bytes = baos.toByteArray();
String result = "";
GZIPContentDecoder decoder = new GZIPContentDecoder(64);
ByteBuffer buffer = ByteBuffer.wrap(bytes);
while (buffer.hasRemaining()) {
ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(new byte[] { buffer.get() }));
if (decoded.hasRemaining())
result += StandardCharsets.UTF_8.decode(decoded).toString();
decoder.release(decoded);
}
assertEquals(data, result);
assertTrue(decoder.isFinished());
}
use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.
the class AsyncMiddleManServletTest method testLargeChunkedBufferedDownstreamTransformation.
private void testLargeChunkedBufferedDownstreamTransformation(final boolean gzipped) throws Exception {
// Tests the race between a incomplete write performed from ProxyResponseListener.onSuccess()
// and ProxyResponseListener.onComplete() being called before the write has completed.
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OutputStream output = response.getOutputStream();
if (gzipped) {
output = new GZIPOutputStream(output);
response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
}
Random random = new Random();
byte[] chunk = new byte[1024 * 1024];
for (int i = 0; i < 16; ++i) {
random.nextBytes(chunk);
output.write(chunk);
output.flush();
}
}
});
startProxy(new AsyncMiddleManServlet() {
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
ContentTransformer transformer = new BufferingContentTransformer();
if (gzipped)
transformer = new GZIPContentTransformer(transformer);
return transformer;
}
});
startClient();
final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", serverConnector.getLocalPort()).onResponseContent(new Response.ContentListener() {
@Override
public void onContent(Response response, ByteBuffer content) {
// Slow down the reader so that the
// write from the proxy gets congested.
sleep(1);
}
}).send(new Response.CompleteListener() {
@Override
public void onComplete(Result result) {
Assert.assertTrue(result.isSucceeded());
Assert.assertEquals(200, result.getResponse().getStatus());
latch.countDown();
}
});
Assert.assertTrue(latch.await(15, TimeUnit.SECONDS));
}
use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.
the class ProxyServletTest method testGZIPContentIsProxied.
@Test
public void testGZIPContentIsProxied() throws Exception {
final byte[] content = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
startServer(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
resp.addHeader("Content-Encoding", "gzip");
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(resp.getOutputStream());
gzipOutputStream.write(content);
gzipOutputStream.close();
}
});
startProxy();
startClient();
ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
Assert.assertArrayEquals(content, response.getContent());
}
use of java.util.zip.GZIPOutputStream in project vert.x by eclipse.
the class Http2ClientTest method testResponseCompression.
private void testResponseCompression(boolean enabled) throws Exception {
byte[] expected = TestUtils.randomAlphaString(1000).getBytes();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream in = new GZIPOutputStream(baos);
in.write(expected);
in.close();
byte[] compressed = baos.toByteArray();
server.close();
server = vertx.createHttpServer(serverOptions);
server.requestHandler(req -> {
assertEquals(enabled ? "deflate, gzip" : null, req.getHeader(HttpHeaderNames.ACCEPT_ENCODING));
req.response().putHeader(HttpHeaderNames.CONTENT_ENCODING.toLowerCase(), "gzip").end(Buffer.buffer(compressed));
});
startServer();
client.close();
client = vertx.createHttpClient(clientOptions.setTryUseCompression(enabled));
client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
String encoding = resp.getHeader(HttpHeaderNames.CONTENT_ENCODING);
assertEquals(enabled ? null : "gzip", encoding);
resp.bodyHandler(buff -> {
assertEquals(Buffer.buffer(enabled ? expected : compressed), buff);
testComplete();
});
}).end();
await();
}
Aggregations