Search in sources :

Example 36 with HttpResponseEncoder

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

the class InfluxServer method start.

public void start() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(2);
    EventLoopGroup processorGroup = new NioEventLoopGroup(4);
    ServerBootstrap bs = new ServerBootstrap();
    channel = bs.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_RCVBUF, 10485760).option(ChannelOption.SO_SNDBUF, 10485760).handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpRequestDecoder());
            p.addLast(new HttpResponseEncoder());
            p.addLast(processorGroup, new HTTPDataPointDecoder(storageEngine, writeCounter));
        }
    }).bind("localhost", 9928).sync().channel();
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 37 with HttpResponseEncoder

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

the class Netty4HttpServer method open.

@Override
public boolean open() {
    if (isAvailable()) {
        return true;
    }
    if (channel != null) {
        channel.close();
    }
    if (bossGroup == null) {
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();
    }
    boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
    // TODO max connection protect
    int maxServerConnection = url.getIntParameter(URLParamType.maxServerConnection.getName(), URLParamType.maxServerConnection.getIntValue());
    int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), 500);
    int minWorkerThread = 0, maxWorkerThread = 0;
    if (shareChannel) {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER);
    } else {
        minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER);
        maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER);
    }
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    final NettyHttpRequestHandler handler = new NettyHttpRequestHandler(this, messageHandler, new ThreadPoolExecutor(minWorkerThread, maxWorkerThread, 15, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(workerQueueSize)));
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
            ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(maxContentLength));
            ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
            ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
            ch.pipeline().addLast("serverHandler", handler);
        }
    }).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, false);
    ChannelFuture f;
    try {
        f = b.bind(url.getPort()).sync();
        channel = f.channel();
    } catch (InterruptedException e) {
        LoggerUtil.error("init http server fail.", e);
        return false;
    }
    state = ChannelState.ALIVE;
    StatsUtil.registryStatisticCallback(this);
    LoggerUtil.info("Netty4HttpServer ServerChannel finish Open: url=" + url);
    return true;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) TransportException(com.weibo.api.motan.transport.TransportException) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 38 with HttpResponseEncoder

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

the class SocketIOChannelInitializer method addSocketioHandlers.

/**
 * Adds the socketio channel handlers
 *
 * @param pipeline - channel 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)

Example 39 with HttpResponseEncoder

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

the class HttpProxyHandlerTest method testExceptionDuringConnect.

@Test
public void testExceptionDuringConnect() throws Exception {
    EventLoopGroup group = null;
    Channel serverChannel = null;
    Channel clientChannel = null;
    try {
        group = new DefaultEventLoopGroup(1);
        final LocalAddress addr = new LocalAddress("a");
        final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
        ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class).group(group).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addFirst(new HttpResponseEncoder());
                DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY);
                response.headers().add("name", "value");
                response.headers().add(HttpHeaderNames.CONTENT_LENGTH, "0");
                ch.writeAndFlush(response);
            }
        }).bind(addr);
        serverChannel = sf.sync().channel();
        ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).group(group).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addFirst(new HttpProxyHandler(addr));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        exception.set(cause);
                    }
                });
            }
        }).connect(new InetSocketAddress("localhost", 1234));
        clientChannel = cf.sync().channel();
        clientChannel.close().sync();
        assertTrue(exception.get() instanceof HttpProxyConnectException);
        HttpProxyConnectException actual = (HttpProxyConnectException) exception.get();
        assertNotNull(actual.headers());
        assertEquals("value", actual.headers().get("name"));
    } finally {
        if (clientChannel != null) {
            clientChannel.close();
        }
        if (serverChannel != null) {
            serverChannel.close();
        }
        if (group != null) {
            group.shutdownGracefully();
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpProxyConnectException(io.netty.handler.proxy.HttpProxyHandler.HttpProxyConnectException) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) InetSocketAddress(java.net.InetSocketAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 40 with HttpResponseEncoder

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

the class HttpServer method start.

public ChannelFuture start() throws Exception {
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new HttpRequestDecoder(), new HttpResponseEncoder(), new HttpObjectAggregator(MAX_CONTENT_LENGTH), new Http1RequestHandler());
        }
    });
    Channel ch = b.bind(PORT).sync().channel();
    return ch.closeFuture();
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) LoggingHandler(io.netty.handler.logging.LoggingHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

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