Search in sources :

Example 36 with GZIPOutputStream

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);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 37 with GZIPOutputStream

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());
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 38 with GZIPOutputStream

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));
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Random(java.util.Random) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 39 with GZIPOutputStream

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 40 with GZIPOutputStream

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();
}
Also used : Arrays(java.util.Arrays) JksOptions(io.vertx.core.net.JksOptions) BiFunction(java.util.function.BiFunction) Http2ConnectionEncoder(io.netty.handler.codec.http2.Http2ConnectionEncoder) AsciiString(io.netty.util.AsciiString) Cert(io.vertx.test.core.tls.Cert) Http2ConnectionDecoder(io.netty.handler.codec.http2.Http2ConnectionDecoder) HttpVersion(io.vertx.core.http.HttpVersion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Http2Exception(io.netty.handler.codec.http2.Http2Exception) ApplicationProtocolNegotiationHandler(io.netty.handler.ssl.ApplicationProtocolNegotiationHandler) AbstractHttp2ConnectionHandlerBuilder(io.netty.handler.codec.http2.AbstractHttp2ConnectionHandlerBuilder) StreamResetException(io.vertx.core.http.StreamResetException) ChannelInitializer(io.netty.channel.ChannelInitializer) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Set(java.util.Set) ChannelPipeline(io.netty.channel.ChannelPipeline) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) Http2FrameListener(io.netty.handler.codec.http2.Http2FrameListener) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) SslHandler(io.netty.handler.ssl.SslHandler) Http2Headers(io.netty.handler.codec.http2.Http2Headers) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Http2Error(io.netty.handler.codec.http2.Http2Error) GZIPOutputStream(java.util.zip.GZIPOutputStream) NetSocket(io.vertx.core.net.NetSocket) Trust(io.vertx.test.core.tls.Trust) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) io.vertx.core(io.vertx.core) Http2EventAdapter(io.netty.handler.codec.http2.Http2EventAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientRequest(io.vertx.core.http.HttpClientRequest) ByteBuf(io.netty.buffer.ByteBuf) ConnectException(java.net.ConnectException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) SocketAddress(io.vertx.core.net.SocketAddress) EventLoopGroup(io.netty.channel.EventLoopGroup) VertxInternal(io.vertx.core.impl.VertxInternal) ApplicationProtocolNames(io.netty.handler.ssl.ApplicationProtocolNames) Test(org.junit.Test) SSLHelper(io.vertx.core.net.impl.SSLHelper) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) Http2Settings(io.netty.handler.codec.http2.Http2Settings) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) HttpMethod(io.vertx.core.http.HttpMethod) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Collections(java.util.Collections) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) Http2CodecUtil(io.netty.handler.codec.http2.Http2CodecUtil) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AsciiString(io.netty.util.AsciiString)

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)322 ByteArrayOutputStream (java.io.ByteArrayOutputStream)135 FileOutputStream (java.io.FileOutputStream)96 IOException (java.io.IOException)93 OutputStream (java.io.OutputStream)66 File (java.io.File)62 Test (org.junit.Test)48 BufferedOutputStream (java.io.BufferedOutputStream)30 FileInputStream (java.io.FileInputStream)28 OutputStreamWriter (java.io.OutputStreamWriter)27 GZIPInputStream (java.util.zip.GZIPInputStream)25 InputStream (java.io.InputStream)24 ByteBuffer (java.nio.ByteBuffer)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 BufferedWriter (java.io.BufferedWriter)18 DropBoxManager (android.os.DropBoxManager)15 BufferedReader (java.io.BufferedReader)11 DataOutputStream (java.io.DataOutputStream)11 FileNotFoundException (java.io.FileNotFoundException)11 InputStreamReader (java.io.InputStreamReader)11