Search in sources :

Example 91 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project netty by netty.

the class DataCompressionHttp2Test method gzipEncodingSingleEmptyMessage.

@Test
public void gzipEncodingSingleEmptyMessage() throws Exception {
    final String text = "";
    final ByteBuf data = Unpooled.copiedBuffer(text.getBytes());
    bootstrapEnv(data.readableBytes());
    try {
        final Http2Headers headers = new DefaultHttp2Headers().method(POST).path(PATH).set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientEncoder.writeHeaders(ctxClient(), 3, headers, 0, false, newPromiseClient());
                clientEncoder.writeData(ctxClient(), 3, data.retain(), 0, true, newPromiseClient());
                clientHandler.flush(ctxClient());
            }
        });
        awaitServer();
        assertEquals(text, serverOut.toString(CharsetUtil.UTF_8.name()));
    } finally {
        data.release();
    }
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 92 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project netty by netty.

the class DataCompressionHttp2Test method brotliEncodingSingleEmptyMessage.

@Test
public void brotliEncodingSingleEmptyMessage() throws Exception {
    final String text = "";
    final ByteBuf data = Unpooled.copiedBuffer(text.getBytes());
    bootstrapEnv(data.readableBytes());
    try {
        final Http2Headers headers = new DefaultHttp2Headers().method(POST).path(PATH).set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.BR);
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientEncoder.writeHeaders(ctxClient(), 3, headers, 0, false, newPromiseClient());
                clientEncoder.writeData(ctxClient(), 3, data.retain(), 0, true, newPromiseClient());
                clientHandler.flush(ctxClient());
            }
        });
        awaitServer();
        assertEquals(text, serverOut.toString(CharsetUtil.UTF_8.name()));
    } finally {
        data.release();
    }
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 93 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project netty by netty.

the class DataCompressionHttp2Test method gzipEncodingSingleMessage.

@Test
public void gzipEncodingSingleMessage() throws Exception {
    final String text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccc";
    final ByteBuf data = Unpooled.copiedBuffer(text.getBytes());
    bootstrapEnv(data.readableBytes());
    try {
        final Http2Headers headers = new DefaultHttp2Headers().method(POST).path(PATH).set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientEncoder.writeHeaders(ctxClient(), 3, headers, 0, false, newPromiseClient());
                clientEncoder.writeData(ctxClient(), 3, data.retain(), 0, true, newPromiseClient());
                clientHandler.flush(ctxClient());
            }
        });
        awaitServer();
        assertEquals(text, serverOut.toString(CharsetUtil.UTF_8.name()));
    } finally {
        data.release();
    }
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 94 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project netty by netty.

the class DataCompressionHttp2Test method gzipEncodingMultipleMessages.

@Test
public void gzipEncodingMultipleMessages() throws Exception {
    final String text1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccc";
    final String text2 = "dddddddddddddddddddeeeeeeeeeeeeeeeeeeeffffffffffffffffffff";
    final ByteBuf data1 = Unpooled.copiedBuffer(text1.getBytes());
    final ByteBuf data2 = Unpooled.copiedBuffer(text2.getBytes());
    bootstrapEnv(data1.readableBytes() + data2.readableBytes());
    try {
        final Http2Headers headers = new DefaultHttp2Headers().method(POST).path(PATH).set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
        runInChannel(clientChannel, new Http2Runnable() {

            @Override
            public void run() throws Http2Exception {
                clientEncoder.writeHeaders(ctxClient(), 3, headers, 0, false, newPromiseClient());
                clientEncoder.writeData(ctxClient(), 3, data1.retain(), 0, false, newPromiseClient());
                clientEncoder.writeData(ctxClient(), 3, data2.retain(), 0, true, newPromiseClient());
                clientHandler.flush(ctxClient());
            }
        });
        awaitServer();
        assertEquals(text1 + text2, serverOut.toString(CharsetUtil.UTF_8.name()));
    } finally {
        data1.release();
        data2.release();
    }
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 95 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project netty by netty.

the class Http2StaticFileServerHandler method sendListing.

private void sendListing(ChannelHandlerContext ctx, File dir, String dirPath) {
    StringBuilder buf = new StringBuilder().append("<!DOCTYPE html>\r\n").append("<html><head><meta charset='utf-8' /><title>").append("Listing of: ").append(dirPath).append("</title></head><body>\r\n").append("<h3>Listing of: ").append(dirPath).append("</h3>\r\n").append("<ul>").append("<li><a href=\"../\">..</a></li>\r\n");
    File[] files = dir.listFiles();
    if (files != null) {
        for (File f : files) {
            if (f.isHidden() || !f.canRead()) {
                continue;
            }
            String name = f.getName();
            if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
                continue;
            }
            buf.append("<li><a href=\"").append(name).append("\">").append(name).append("</a></li>\r\n");
        }
    }
    buf.append("</ul></body></html>\r\n");
    ByteBuf buffer = ctx.alloc().buffer(buf.length());
    buffer.writeCharSequence(buf.toString(), CharsetUtil.UTF_8);
    Http2Headers headers = new DefaultHttp2Headers();
    headers.status(OK.toString());
    headers.add(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
    ctx.write(new DefaultHttp2HeadersFrame(headers).stream(stream));
    ctx.writeAndFlush(new DefaultHttp2DataFrame(buffer, true).stream(stream));
}
Also used : DefaultHttp2HeadersFrame(io.netty.handler.codec.http2.DefaultHttp2HeadersFrame) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) ByteBuf(io.netty.buffer.ByteBuf) RandomAccessFile(java.io.RandomAccessFile) ChunkedFile(io.netty.handler.stream.ChunkedFile) File(java.io.File)

Aggregations

DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)72 Http2Headers (io.netty.handler.codec.http2.Http2Headers)58 AsciiString (io.netty.util.AsciiString)56 ByteBuf (io.netty.buffer.ByteBuf)54 Test (org.junit.Test)49 Test (org.junit.jupiter.api.Test)32 ChannelFuture (io.netty.channel.ChannelFuture)27 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelPromise (io.netty.channel.ChannelPromise)17 Channel (io.netty.channel.Channel)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 List (java.util.List)15 ChannelInitializer (io.netty.channel.ChannelInitializer)14 ChannelPipeline (io.netty.channel.ChannelPipeline)14 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)14 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)14 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)14 ApplicationProtocolNames (io.netty.handler.ssl.ApplicationProtocolNames)14 ApplicationProtocolNegotiationHandler (io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)14