Search in sources :

Example 1 with HttpRequestDecoder

use of io.netty.handler.codec.http.HttpRequestDecoder in project vert.x by eclipse.

the class HttpServerImpl method configureHttp1.

private void configureHttp1(ChannelPipeline pipeline) {
    if (logEnabled) {
        pipeline.addLast("logging", new LoggingHandler());
    }
    if (USE_FLASH_POLICY_HANDLER) {
        pipeline.addLast("flashpolicy", new FlashPolicyHandler());
    }
    pipeline.addLast("httpDecoder", new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(), options.getMaxChunkSize(), false));
    pipeline.addLast("httpEncoder", new VertxHttpResponseEncoder());
    if (options.isDecompressionSupported()) {
        pipeline.addLast("inflater", new HttpContentDecompressor(true));
    }
    if (options.isCompressionSupported()) {
        pipeline.addLast("deflater", new HttpChunkContentCompressor(options.getCompressionLevel()));
    }
    if (sslHelper.isSSL() || options.isCompressionSupported()) {
        // only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used.
        // For large file / sendfile support
        pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    }
    if (options.getIdleTimeout() > 0) {
        pipeline.addLast("idle", new IdleStateHandler(0, 0, options.getIdleTimeout()));
    }
    pipeline.addLast("handler", new ServerHandler(pipeline.channel()));
}
Also used : FlashPolicyHandler(io.vertx.core.http.impl.cgbystrom.FlashPolicyHandler) LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Example 2 with HttpRequestDecoder

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

the class WebSocketServerHandshaker00Test 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", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII));
    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.ORIGIN, "http://example.com");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1  46546xW%0l 1 5");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1  .P00");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");
    if (subProtocol) {
        new WebSocketServerHandshaker00("ws://example.com/chat", "chat", Integer.MAX_VALUE).handshake(ch, req);
    } else {
        new WebSocketServerHandshaker00("ws://example.com/chat", null, Integer.MAX_VALUE).handshake(ch, req);
    }
    EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
    ch2.writeInbound(ch.readOutbound());
    HttpResponse res = ch2.readInbound();
    Assert.assertEquals("ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION));
    if (subProtocol) {
        Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    } else {
        Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    }
    LastHttpContent content = ch2.readInbound();
    Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));
    content.release();
    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) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 3 with HttpRequestDecoder

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

the class WebSocketServerProtocolHandlerTest method testHandleTextFrame.

@Test
public void testHandleTextFrame() {
    CustomTextFrameHandler customTextFrameHandler = new CustomTextFrameHandler();
    EmbeddedChannel ch = createChannel(customTextFrameHandler);
    writeUpgradeRequest(ch);
    if (ch.pipeline().context(HttpRequestDecoder.class) != null) {
        // Removing the HttpRequestDecoder because we are writing a TextWebSocketFrame and thus
        // decoding is not neccessary.
        ch.pipeline().remove(HttpRequestDecoder.class);
    }
    ch.writeInbound(new TextWebSocketFrame("payload"));
    assertEquals("processed: payload", customTextFrameHandler.getContent());
}
Also used : HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 4 with HttpRequestDecoder

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

the class PortUnificationServerHandler method switchToHttp.

private void switchToHttp(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.pipeline();
    p.addLast("decoder", new HttpRequestDecoder());
    p.addLast("encoder", new HttpResponseEncoder());
    p.addLast("deflater", new HttpContentCompressor());
    p.addLast("handler", new HttpSnoopServerHandler());
    p.remove(this);
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) HttpSnoopServerHandler(io.netty.example.http.snoop.HttpSnoopServerHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 5 with HttpRequestDecoder

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

the class HttpCorsServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build();
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpResponseEncoder());
    pipeline.addLast(new HttpRequestDecoder());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new CorsHandler(corsConfig));
    pipeline.addLast(new OkResponseHandler());
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) CorsConfig(io.netty.handler.codec.http.cors.CorsConfig) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) CorsHandler(io.netty.handler.codec.http.cors.CorsHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)23 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)19 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)12 ChannelPipeline (io.netty.channel.ChannelPipeline)11 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)6 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)5 SocketChannel (io.netty.channel.socket.SocketChannel)4 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)4 LoggingHandler (io.netty.handler.logging.LoggingHandler)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)3 HttpResponse (io.netty.handler.codec.http.HttpResponse)3 HttpResponseDecoder (io.netty.handler.codec.http.HttpResponseDecoder)3 RequestStateCleanerHandler (com.nike.riposte.server.handler.RequestStateCleanerHandler)2 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 ChannelHandler (io.netty.channel.ChannelHandler)2