sirius.web.http.LowLevelHandler Maven / Gradle / Ivy
Show all versions of sirius-web Show documentation
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.web.http;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
/**
* Handler for low-level events in the HTTP pipeline.
*
* Performs statistical tasks and performs basic filtering based on firewall rules.
*/
@ChannelHandler.Sharable
class LowLevelHandler extends ChannelDuplexHandler {
static final LowLevelHandler INSTANCE = new LowLevelHandler();
@Override
public void connect(ChannelHandlerContext ctx,
SocketAddress remoteAddress,
SocketAddress localAddress,
ChannelPromise future) throws Exception {
WebServer.connections++;
if (WebServer.connections < 0) {
WebServer.connections = 0;
}
IPRange.RangeSet filter = WebServer.getIPFilter();
if (!filter.isEmpty()) {
if (!filter.accepts(((InetSocketAddress) remoteAddress).getAddress())) {
WebServer.blocks++;
if (WebServer.blocks < 0) {
WebServer.blocks = 0;
}
ctx.channel().close();
future.setSuccess();
return;
}
}
super.connect(ctx, remoteAddress, localAddress, future);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf) {
int messageSize = ((ByteBuf) msg).readableBytes();
WebServer.bytesIn += messageSize;
if (WebServer.bytesIn < 0) {
WebServer.bytesIn = 0;
}
WebServer.messagesIn++;
if (WebServer.messagesIn < 0) {
WebServer.messagesIn = 0;
}
ctx.pipeline().get(WebServerHandler.class).inbound(messageSize);
}
super.channelRead(ctx, msg);
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof ByteBuf) {
int messageSize = ((ByteBuf) msg).readableBytes();
WebServer.bytesOut += messageSize;
if (WebServer.bytesOut < 0) {
WebServer.bytesOut = 0;
}
WebServer.messagesOut++;
if (WebServer.messagesOut < 0) {
WebServer.messagesOut = 0;
}
ctx.pipeline().get(WebServerHandler.class).outbound(messageSize);
}
super.write(ctx, msg, promise);
}
}