Search in sources :

Example 6 with ProtocolException

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);
    }
}
Also used : ProtocolException(org.httpkit.ProtocolException) CloseFrame(org.httpkit.server.Frame.CloseFrame) CloseFrame(org.httpkit.server.Frame.CloseFrame) Date(java.util.Date)

Example 7 with ProtocolException

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()));
    }
}
Also used : ProtocolException(org.httpkit.ProtocolException) LineTooLargeException(org.httpkit.LineTooLargeException) HeaderMap(org.httpkit.HeaderMap) RequestTooLargeException(org.httpkit.RequestTooLargeException)

Aggregations

ProtocolException (org.httpkit.ProtocolException)7 UnknownHostException (java.net.UnknownHostException)2 LineTooLargeException (org.httpkit.LineTooLargeException)2 RequestTooLargeException (org.httpkit.RequestTooLargeException)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 HeaderMap (org.httpkit.HeaderMap)1 HttpMethod (org.httpkit.HttpMethod)1 HttpVersion (org.httpkit.HttpVersion)1 CloseFrame (org.httpkit.server.Frame.CloseFrame)1