Search in sources :

Example 11 with HttpServerCodec

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

the class Http2ClientTest method createH2CServer.

private ServerBootstrap createH2CServer(BiFunction<Http2ConnectionDecoder, Http2ConnectionEncoder, Http2FrameListener> handler, boolean upgrade) {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.group(new NioEventLoopGroup());
    bootstrap.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            if (upgrade) {
                HttpServerCodec sourceCodec = new HttpServerCodec();
                HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory = protocol -> {
                    if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                        return new Http2ServerUpgradeCodec(createHttpConnectionHandler(handler));
                    } else {
                        return null;
                    }
                };
                ch.pipeline().addLast(sourceCodec);
                ch.pipeline().addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
            } else {
                Http2ConnectionHandler clientHandler = createHttpConnectionHandler(handler);
                ch.pipeline().addLast("handler", clientHandler);
            }
        }
    });
    return bootstrap;
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) Http2ConnectionHandler(io.netty.handler.codec.http2.Http2ConnectionHandler) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Http2Exception(io.netty.handler.codec.http2.Http2Exception) StreamResetException(io.vertx.core.http.StreamResetException) ConnectException(java.net.ConnectException) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException)

Example 12 with HttpServerCodec

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

the class WebSocketServerHandshaker method handshake.

/**
     * Performs the opening handshake
     *
     * When call this method you <strong>MUST NOT</strong> retain the {@link FullHttpRequest} which is passed in.
     *
     * @param channel
     *            Channel
     * @param req
     *            HTTP Request
     * @param responseHeaders
     *            Extra headers to add to the handshake response or {@code null} if no extra headers should be added
     * @param promise
     *            the {@link ChannelPromise} to be notified when the opening handshake is done
     * @return future
     *            the {@link ChannelFuture} which is notified when the opening handshake is done
     */
public final ChannelFuture handshake(Channel channel, FullHttpRequest req, HttpHeaders responseHeaders, final ChannelPromise promise) {
    if (logger.isDebugEnabled()) {
        logger.debug("{} WebSocket version {} server handshake", channel, version());
    }
    FullHttpResponse response = newHandshakeResponse(req, responseHeaders);
    ChannelPipeline p = channel.pipeline();
    if (p.get(HttpObjectAggregator.class) != null) {
        p.remove(HttpObjectAggregator.class);
    }
    if (p.get(HttpContentCompressor.class) != null) {
        p.remove(HttpContentCompressor.class);
    }
    ChannelHandlerContext ctx = p.context(HttpRequestDecoder.class);
    final String encoderName;
    if (ctx == null) {
        // this means the user use a HttpServerCodec
        ctx = p.context(HttpServerCodec.class);
        if (ctx == null) {
            promise.setFailure(new IllegalStateException("No HttpDecoder and no HttpServerCodec in the pipeline"));
            return promise;
        }
        p.addBefore(ctx.name(), "wsdecoder", newWebsocketDecoder());
        p.addBefore(ctx.name(), "wsencoder", newWebSocketEncoder());
        encoderName = ctx.name();
    } else {
        p.replace(ctx.name(), "wsdecoder", newWebsocketDecoder());
        encoderName = p.context(HttpResponseEncoder.class).name();
        p.addBefore(encoderName, "wsencoder", newWebSocketEncoder());
    }
    channel.writeAndFlush(response).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ChannelPipeline p = future.channel().pipeline();
                p.remove(encoderName);
                promise.setSuccess();
            } else {
                promise.setFailure(future.cause());
            }
        }
    });
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelPipeline(io.netty.channel.ChannelPipeline) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 13 with HttpServerCodec

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

the class WebSocketServerHandshaker method handshake.

/**
     * Performs the opening handshake
     *
     * When call this method you <strong>MUST NOT</strong> retain the {@link HttpRequest} which is passed in.
     *
     * @param channel
     *            Channel
     * @param req
     *            HTTP Request
     * @param responseHeaders
     *            Extra headers to add to the handshake response or {@code null} if no extra headers should be added
     * @param promise
     *            the {@link ChannelPromise} to be notified when the opening handshake is done
     * @return future
     *            the {@link ChannelFuture} which is notified when the opening handshake is done
     */
public final ChannelFuture handshake(final Channel channel, HttpRequest req, final HttpHeaders responseHeaders, final ChannelPromise promise) {
    if (req instanceof FullHttpRequest) {
        return handshake(channel, (FullHttpRequest) req, responseHeaders, promise);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{} WebSocket version {} server handshake", channel, version());
    }
    ChannelPipeline p = channel.pipeline();
    ChannelHandlerContext ctx = p.context(HttpRequestDecoder.class);
    if (ctx == null) {
        // this means the user use a HttpServerCodec
        ctx = p.context(HttpServerCodec.class);
        if (ctx == null) {
            promise.setFailure(new IllegalStateException("No HttpDecoder and no HttpServerCodec in the pipeline"));
            return promise;
        }
    }
    // Add aggregator and ensure we feed the HttpRequest so it is aggregated. A limit o 8192 should be more then
    // enough for the websockets handshake payload.
    //
    // TODO: Make handshake work without HttpObjectAggregator at all.
    String aggregatorName = "httpAggregator";
    p.addAfter(ctx.name(), aggregatorName, new HttpObjectAggregator(8192));
    p.addAfter(aggregatorName, "handshaker", new SimpleChannelInboundHandler<FullHttpRequest>() {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
            // Remove ourself and do the actual handshake
            ctx.pipeline().remove(this);
            handshake(channel, msg, responseHeaders, promise);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            // Remove ourself and fail the handshake promise.
            ctx.pipeline().remove(this);
            promise.tryFailure(cause);
            ctx.fireExceptionCaught(cause);
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            // Fail promise if Channel was closed
            promise.tryFailure(CLOSED_CHANNEL_EXCEPTION);
            ctx.fireChannelInactive();
        }
    });
    try {
        ctx.fireChannelRead(ReferenceCountUtil.retain(req));
    } catch (Throwable cause) {
        promise.setFailure(cause);
    }
    return promise;
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPipeline(io.netty.channel.ChannelPipeline) ClosedChannelException(java.nio.channels.ClosedChannelException)

Example 14 with HttpServerCodec

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

the class HttpHelloWorldServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }
    p.addLast(new HttpServerCodec());
    p.addLast(new HttpHelloWorldServerHandler());
}
Also used : HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 15 with HttpServerCodec

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

the class WebSocketServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
    pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
    pipeline.addLast(new WebSocketFrameHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) WebSocketServerCompressionHandler(io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)19 ChannelPipeline (io.netty.channel.ChannelPipeline)18 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 HttpServerUpgradeHandler (io.netty.handler.codec.http.HttpServerUpgradeHandler)4 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)4 HttpMessage (io.netty.handler.codec.http.HttpMessage)3 WebSocketServerProtocolHandler (io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler)2 Http2ServerUpgradeCodec (io.netty.handler.codec.http2.Http2ServerUpgradeCodec)2 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 HelloWorldHttp1Handler (io.netty.example.http2.helloworld.server.HelloWorldHttp1Handler)1