-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProtocolHandler.java
44 lines (38 loc) · 1.52 KB
/
ProtocolHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package handlers;
import handlers.Http200OkHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DecoderResultProvider;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpServerCodec;
public class ProtocolHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
in.retain();
in.markReaderIndex();
EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder());
assert ch.writeInbound(msg) : "failed to write inbound message";
DecoderResultProvider httpRequest = ch.readInbound();
in.resetReaderIndex();
ChannelPipeline pipeline = ctx.pipeline();
boolean failedToDecodeHttp = httpRequest.decoderResult().isFailure();
pipeline.remove(this);
ChannelPipeline nextPipe;
if (failedToDecodeHttp) {
nextPipe = pipeline.addLast(new TcpEchoHandler());
} else {
pipeline.addLast(new HttpServerCodec());
nextPipe = pipeline.addLast(new Http200OkHandler());
}
nextPipe.fireChannelRead(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}