Search in sources :

Example 41 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project netty by netty.

the class HttpSnoopServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }
    p.addLast(new HttpRequestDecoder());
    // Uncomment the following line if you don't want to handle HttpChunks.
    // p.addLast(new HttpObjectAggregator(1048576));
    p.addLast(new HttpResponseEncoder());
    // Remove the following line if you don't want automatic content compression.
    // p.addLast(new HttpContentCompressor());
    p.addLast(new HttpSnoopServerHandler());
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 42 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project netty by netty.

the class HttpUploadServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpRequestDecoder());
    pipeline.addLast(new HttpResponseEncoder());
    // Remove the following line if you don't want automatic content compression.
    pipeline.addLast(new HttpContentCompressor());
    pipeline.addLast(new HttpUploadServerHandler());
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 43 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project netty by netty.

the class WebSocketServerHandshaker13Test method testPerformOpeningHandshake0.

private static void testPerformOpeningHandshake0(boolean subProtocol) {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(42), new HttpResponseEncoder(), new HttpRequestDecoder());
    if (subProtocol) {
        testUpgrade0(ch, new WebSocketServerHandshaker13("ws://example.com/chat", "chat", false, Integer.MAX_VALUE, false));
    } else {
        testUpgrade0(ch, new WebSocketServerHandshaker13("ws://example.com/chat", null, false, Integer.MAX_VALUE, false));
    }
    assertFalse(ch.finish());
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Example 44 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project netty by netty.

the class WebSocketServerHandshaker08Test method testPerformOpeningHandshake0.

private static void testPerformOpeningHandshake0(boolean subProtocol) {
    EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
    FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat");
    req.headers().set(HttpHeaderNames.HOST, "server.example.com");
    req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
    req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ==");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, "http://example.com");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, "8");
    if (subProtocol) {
        new WebSocketServerHandshaker08("ws://example.com/chat", "chat", false, Integer.MAX_VALUE, false).handshake(ch, req);
    } else {
        new WebSocketServerHandshaker08("ws://example.com/chat", null, false, Integer.MAX_VALUE, false).handshake(ch, req);
    }
    ByteBuf resBuf = ch.readOutbound();
    EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
    ch2.writeInbound(resBuf);
    HttpResponse res = ch2.readInbound();
    assertEquals("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT));
    if (subProtocol) {
        assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    } else {
        assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    }
    ReferenceCountUtil.release(res);
    req.release();
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) ByteBuf(io.netty.buffer.ByteBuf)

Example 45 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project netty by netty.

the class WebSocketServerProtocolHandlerTest method testCreateUTF8Validator.

@Test
public void testCreateUTF8Validator() {
    WebSocketServerProtocolConfig config = WebSocketServerProtocolConfig.newBuilder().websocketPath("/test").withUTF8Validator(true).build();
    EmbeddedChannel ch = new EmbeddedChannel(new WebSocketServerProtocolHandler(config), new HttpRequestDecoder(), new HttpResponseEncoder(), new MockOutboundHandler());
    writeUpgradeRequest(ch);
    FullHttpResponse response = responses.remove();
    assertEquals(SWITCHING_PROTOCOLS, response.status());
    response.release();
    assertNotNull(ch.pipeline().get(Utf8FrameValidator.class));
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)52 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)45 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)28 ChannelPipeline (io.netty.channel.ChannelPipeline)25 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)12 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)11 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)10 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)10 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)10 SocketChannel (io.netty.channel.socket.SocketChannel)9 Channel (io.netty.channel.Channel)8 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)8 Test (org.junit.jupiter.api.Test)8 EventLoopGroup (io.netty.channel.EventLoopGroup)7 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)7 SslHandler (io.netty.handler.ssl.SslHandler)7 InetSocketAddress (java.net.InetSocketAddress)7 Unpooled (io.netty.buffer.Unpooled)6 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)6