Search in sources :

Example 26 with HttpObjectAggregator

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

the class RtspDecoderTest method testReceiveAnnounce.

/**
     * There was a problem when an ANNOUNCE request was issued by the server,
     * i.e. entered through the response decoder. First the decoder failed to
     * parse the ANNOUNCE request, then it stopped receiving any more
     * responses. This test verifies that the issue is solved.
     */
@Test
public void testReceiveAnnounce() {
    byte[] data1 = ("ANNOUNCE rtsp://172.20.184.218:554/d3abaaa7-65f2-" + "42b4-8d6b-379f492fcf0f RTSP/1.0\r\n" + "CSeq: 2\r\n" + "Session: 2777476816092819869\r\n" + "x-notice: 5402 \"Session Terminated by Server\" " + "event-date=20150514T075303Z\r\n" + "Range: npt=0\r\n\r\n").getBytes();
    byte[] data2 = ("RTSP/1.0 200 OK\r\n" + "Server: Orbit2x\r\n" + "CSeq: 172\r\n" + "Session: 2547019973447939919\r\n" + "\r\n").getBytes();
    EmbeddedChannel ch = new EmbeddedChannel(new RtspDecoder(), new HttpObjectAggregator(1048576));
    ch.writeInbound(Unpooled.wrappedBuffer(data1), Unpooled.wrappedBuffer(data2));
    HttpObject res1 = ch.readInbound();
    System.out.println(res1);
    assertNotNull(res1);
    assertTrue(res1 instanceof FullHttpRequest);
    ((FullHttpRequest) res1).release();
    HttpObject res2 = ch.readInbound();
    System.out.println(res2);
    assertNotNull(res2);
    assertTrue(res2 instanceof FullHttpResponse);
    ((FullHttpResponse) res2).release();
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.Test)

Example 27 with HttpObjectAggregator

use of io.netty.handler.codec.http.HttpObjectAggregator 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();
    Assert.assertEquals("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT));
    if (subProtocol) {
        Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    } else {
        Assert.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 28 with HttpObjectAggregator

use of io.netty.handler.codec.http.HttpObjectAggregator in project camel by apache.

the class HttpClientInitializerFactory method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    // create a new pipeline
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = configureClientSSLOnDemand();
    if (sslHandler != null) {
        //TODO must close on SSL exception
        //sslHandler.setCloseOnSSLException(true);
        LOG.debug("Client SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        pipeline.addLast("ssl", sslHandler);
    }
    pipeline.addLast("http", new HttpClientCodec());
    List<ChannelHandler> encoders = producer.getConfiguration().getEncoders();
    for (int x = 0; x < encoders.size(); x++) {
        ChannelHandler encoder = encoders.get(x);
        if (encoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            encoder = ((ChannelHandlerFactory) encoder).newChannelHandler();
        }
        pipeline.addLast("encoder-" + x, encoder);
    }
    List<ChannelHandler> decoders = producer.getConfiguration().getDecoders();
    for (int x = 0; x < decoders.size(); x++) {
        ChannelHandler decoder = decoders.get(x);
        if (decoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            decoder = ((ChannelHandlerFactory) decoder).newChannelHandler();
        }
        pipeline.addLast("decoder-" + x, decoder);
    }
    pipeline.addLast("aggregator", new HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
    if (producer.getConfiguration().getRequestTimeout() > 0) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Using request timeout {} millis", producer.getConfiguration().getRequestTimeout());
        }
        ChannelHandler timeout = new ReadTimeoutHandler(producer.getConfiguration().getRequestTimeout(), TimeUnit.MILLISECONDS);
        pipeline.addLast("timeout", timeout);
    }
    // handler to route Camel messages
    pipeline.addLast("handler", new HttpClientChannelHandler(producer));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpClientChannelHandler(org.apache.camel.component.netty4.http.handlers.HttpClientChannelHandler) ChannelHandlerFactory(org.apache.camel.component.netty4.ChannelHandlerFactory) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) HttpClientChannelHandler(org.apache.camel.component.netty4.http.handlers.HttpClientChannelHandler) ChannelHandler(io.netty.channel.ChannelHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 29 with HttpObjectAggregator

use of io.netty.handler.codec.http.HttpObjectAggregator in project camel by apache.

the class HttpServerInitializerFactory method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    // create a new pipeline
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = configureServerSSLOnDemand();
    if (sslHandler != null) {
        //TODO must close on SSL exception
        // sslHandler.setCloseOnSSLException(true);
        LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        pipeline.addLast("ssl", sslHandler);
    }
    pipeline.addLast("decoder", new HttpRequestDecoder(4096, configuration.getMaxHeaderSize(), 8192));
    List<ChannelHandler> decoders = consumer.getConfiguration().getDecoders();
    for (int x = 0; x < decoders.size(); x++) {
        ChannelHandler decoder = decoders.get(x);
        if (decoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            decoder = ((ChannelHandlerFactory) decoder).newChannelHandler();
        }
        pipeline.addLast("decoder-" + x, decoder);
    }
    pipeline.addLast("encoder", new HttpResponseEncoder());
    List<ChannelHandler> encoders = consumer.getConfiguration().getEncoders();
    for (int x = 0; x < encoders.size(); x++) {
        ChannelHandler encoder = encoders.get(x);
        if (encoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            encoder = ((ChannelHandlerFactory) encoder).newChannelHandler();
        }
        pipeline.addLast("encoder-" + x, encoder);
    }
    pipeline.addLast("aggregator", new HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
    if (supportCompressed()) {
        pipeline.addLast("deflater", new HttpContentCompressor());
    }
    int port = consumer.getConfiguration().getPort();
    ChannelHandler handler = consumer.getEndpoint().getComponent().getMultiplexChannelHandler(port).getChannelHandler();
    if (consumer.getConfiguration().isUsingExecutorService()) {
        EventExecutorGroup applicationExecutor = consumer.getEndpoint().getComponent().getExecutorService();
        pipeline.addLast(applicationExecutor, "handler", handler);
    } else {
        pipeline.addLast("handler", handler);
    }
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChannelHandlerFactory(org.apache.camel.component.netty4.ChannelHandlerFactory) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 30 with HttpObjectAggregator

use of io.netty.handler.codec.http.HttpObjectAggregator in project netty-socketio by mrniko.

the class SocketIOChannelInitializer method addSocketioHandlers.

/**
     * Adds the socketio channel handlers
     *
     * @param pipeline
     */
protected void addSocketioHandlers(ChannelPipeline pipeline) {
    pipeline.addLast(HTTP_REQUEST_DECODER, new HttpRequestDecoder());
    pipeline.addLast(HTTP_AGGREGATOR, new HttpObjectAggregator(configuration.getMaxHttpContentLength()) {

        @Override
        protected Object newContinueResponse(HttpMessage start, int maxContentLength, ChannelPipeline pipeline) {
            return null;
        }
    });
    pipeline.addLast(HTTP_ENCODER, new HttpResponseEncoder());
    if (configuration.isHttpCompression()) {
        pipeline.addLast(HTTP_COMPRESSION, new HttpContentCompressor());
    }
    pipeline.addLast(PACKET_HANDLER, packetHandler);
    pipeline.addLast(AUTHORIZE_HANDLER, authorizeHandler);
    pipeline.addLast(XHR_POLLING_TRANSPORT, xhrPollingTransport);
    // TODO use single instance when https://github.com/netty/netty/issues/4755 will be resolved
    if (configuration.isWebsocketCompression()) {
        pipeline.addLast(WEB_SOCKET_TRANSPORT_COMPRESSION, new WebSocketServerCompressionHandler());
    }
    pipeline.addLast(WEB_SOCKET_TRANSPORT, webSocketTransport);
    pipeline.addLast(SOCKETIO_ENCODER, encoderHandler);
    pipeline.addLast(WRONG_URL_HANDLER, wrongUrlHandler);
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) WebSocketServerCompressionHandler(io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler) HttpMessage(io.netty.handler.codec.http.HttpMessage) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)35 ChannelPipeline (io.netty.channel.ChannelPipeline)24 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)12 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)12 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)11 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)10 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)9 SocketChannel (io.netty.channel.socket.SocketChannel)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)6 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)6 Channel (io.netty.channel.Channel)5 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)5 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)5 Bootstrap (io.netty.bootstrap.Bootstrap)4 ByteBuf (io.netty.buffer.ByteBuf)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)4 HttpResponseDecoder (io.netty.handler.codec.http.HttpResponseDecoder)4