use of org.httpkit.ProtocolException in project http-kit by http-kit.
the class HttpServer method decodeWs.
private void decodeWs(WsAtta atta, SelectionKey key) {
try {
do {
Frame frame = atta.decoder.decode(buffer);
if (frame instanceof TextFrame || frame instanceof BinaryFrame) {
handler.handle(atta.channel, frame);
atta.decoder.reset();
} else if (frame instanceof PingFrame) {
atta.decoder.reset();
tryWrite(key, WsEncode(WSDecoder.OPCODE_PONG, frame.data));
} else if (frame instanceof PongFrame) {
atta.decoder.reset();
tryWrite(key, WsEncode(WSDecoder.OPCODE_PING, frame.data));
} else if (frame instanceof CloseFrame) {
handler.clientClose(atta.channel, ((CloseFrame) frame).getStatus());
// close the TCP connection after sent
atta.keepalive = false;
atta.decoder.reset();
tryWrite(key, WsEncode(WSDecoder.OPCODE_CLOSE, frame.data));
}
} while (// consume all
buffer.hasRemaining());
} catch (ProtocolException e) {
System.err.printf("%s [%s] WARN - %s\n", new Date(), THREAD_NAME, e.getMessage());
// TODO more specific error
closeKey(key, CLOSE_MESG_BIG);
}
}
use of org.httpkit.ProtocolException in project http-kit by http-kit.
the class HttpServer method decodeHttp.
private void decodeHttp(HttpAtta atta, SelectionKey key, SocketChannel ch) {
try {
boolean sentContinue = false;
do {
AsyncChannel channel = atta.channel;
HttpRequest request = atta.decoder.decode(buffer);
if (request != null) {
channel.reset(request);
if (request.isWebSocket) {
key.attach(new WsAtta(channel, maxWs));
} else {
atta.keepalive = request.isKeepAlive;
}
request.channel = channel;
request.remoteAddr = (InetSocketAddress) ch.socket().getRemoteSocketAddress();
handler.handle(request, new RespCallback(key, this));
// pipelining not supported : need queue to ensure order
atta.decoder.reset();
} else if (!sentContinue && atta.decoder.requiresContinue()) {
tryWrite(key, HttpEncode(100, new HeaderMap(), null));
sentContinue = true;
}
} while (// consume all
buffer.hasRemaining());
} catch (ProtocolException e) {
closeKey(key, -1);
} catch (RequestTooLargeException e) {
atta.keepalive = false;
tryWrite(key, HttpEncode(413, new HeaderMap(), e.getMessage()));
} catch (LineTooLargeException e) {
// close after write
atta.keepalive = false;
tryWrite(key, HttpEncode(414, new HeaderMap(), e.getMessage()));
}
}
Aggregations