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();
}
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;
}
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);
}
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();
}
}
}
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();
}
Aggregations