Java Code Examples for javax.websocket.CloseReason.CloseCodes#PROTOCOL_ERROR

The following examples show how to use javax.websocket.CloseReason.CloseCodes#PROTOCOL_ERROR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WsFrameBase.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * @return <code>true</code> if sufficient data was present to complete the
 *         processing of the header
 */
private boolean processRemainingHeader() throws IOException {
    // Ignore the 2 bytes already read. 4 for the mask
    int headerLength;
    if (isMasked()) {
        headerLength = 4;
    } else {
        headerLength = 0;
    }
    // Add additional bytes depending on length
    if (payloadLength == 126) {
        headerLength += 2;
    } else if (payloadLength == 127) {
        headerLength += 8;
    }
    if (inputBuffer.remaining() < headerLength) {
        return false;
    }
    // Calculate new payload length if necessary
    if (payloadLength == 126) {
        payloadLength = byteArrayToLong(inputBuffer.array(),
                inputBuffer.arrayOffset() + inputBuffer.position(), 2);
        inputBuffer.position(inputBuffer.position() + 2);
    } else if (payloadLength == 127) {
        payloadLength = byteArrayToLong(inputBuffer.array(),
                inputBuffer.arrayOffset() + inputBuffer.position(), 8);
        inputBuffer.position(inputBuffer.position() + 8);
    }
    if (Util.isControl(opCode)) {
        if (payloadLength > 125) {
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.controlPayloadTooBig", Long.valueOf(payloadLength))));
        }
        if (!fin) {
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.controlNoFin")));
        }
    }
    if (isMasked()) {
        inputBuffer.get(mask, 0, 4);
    }
    state = State.DATA;
    return true;
}
 
Example 2
Source File: WsFrameBase.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private boolean processDataControl() throws IOException {
    TransformationResult tr = transformation.getMoreData(opCode, fin, rsv, controlBufferBinary);
    if (TransformationResult.UNDERFLOW.equals(tr)) {
        return false;
    }
    // Control messages have fixed message size so
    // TransformationResult.OVERFLOW is not possible here

    controlBufferBinary.flip();
    if (opCode == Constants.OPCODE_CLOSE) {
        open = false;
        String reason = null;
        int code = CloseCodes.NORMAL_CLOSURE.getCode();
        if (controlBufferBinary.remaining() == 1) {
            controlBufferBinary.clear();
            // Payload must be zero or 2+ bytes long
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.oneByteCloseCode")));
        }
        if (controlBufferBinary.remaining() > 1) {
            code = controlBufferBinary.getShort();
            if (controlBufferBinary.remaining() > 0) {
                CoderResult cr = utf8DecoderControl.decode(controlBufferBinary,
                        controlBufferText, true);
                if (cr.isError()) {
                    controlBufferBinary.clear();
                    controlBufferText.clear();
                    throw new WsIOException(new CloseReason(
                            CloseCodes.PROTOCOL_ERROR,
                            sm.getString("wsFrame.invalidUtf8Close")));
                }
                // There will be no overflow as the output buffer is big
                // enough. There will be no underflow as all the data is
                // passed to the decoder in a single call.
                controlBufferText.flip();
                reason = controlBufferText.toString();
            }
        }
        wsSession.onClose(new CloseReason(Util.getCloseCode(code), reason));
    } else if (opCode == Constants.OPCODE_PING) {
        if (wsSession.isOpen()) {
            wsSession.getBasicRemote().sendPong(controlBufferBinary);
        }
    } else if (opCode == Constants.OPCODE_PONG) {
        MessageHandler.Whole<PongMessage> mhPong = wsSession.getPongMessageHandler();
        if (mhPong != null) {
            try {
                mhPong.onMessage(new WsPongMessage(controlBufferBinary));
            } catch (Throwable t) {
                handleThrowableOnSend(t);
            } finally {
                controlBufferBinary.clear();
            }
        }
    } else {
        // Should have caught this earlier but just in case...
        controlBufferBinary.clear();
        throw new WsIOException(new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode))));
    }
    controlBufferBinary.clear();
    newFrame();
    return true;
}
 
Example 3
Source File: Util.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
static CloseCode getCloseCode(int code) {
    if (code > 2999 && code < 5000) {
        return CloseCodes.getCloseCode(code);
    }
    switch (code) {
        case 1000:
            return CloseCodes.NORMAL_CLOSURE;
        case 1001:
            return CloseCodes.GOING_AWAY;
        case 1002:
            return CloseCodes.PROTOCOL_ERROR;
        case 1003:
            return CloseCodes.CANNOT_ACCEPT;
        case 1004:
            // Should not be used in a close frame
            // return CloseCodes.RESERVED;
            return CloseCodes.PROTOCOL_ERROR;
        case 1005:
            // Should not be used in a close frame
            // return CloseCodes.NO_STATUS_CODE;
            return CloseCodes.PROTOCOL_ERROR;
        case 1006:
            // Should not be used in a close frame
            // return CloseCodes.CLOSED_ABNORMALLY;
            return CloseCodes.PROTOCOL_ERROR;
        case 1007:
            return CloseCodes.NOT_CONSISTENT;
        case 1008:
            return CloseCodes.VIOLATED_POLICY;
        case 1009:
            return CloseCodes.TOO_BIG;
        case 1010:
            return CloseCodes.NO_EXTENSION;
        case 1011:
            return CloseCodes.UNEXPECTED_CONDITION;
        case 1012:
            // Not in RFC6455
            // return CloseCodes.SERVICE_RESTART;
            return CloseCodes.PROTOCOL_ERROR;
        case 1013:
            // Not in RFC6455
            // return CloseCodes.TRY_AGAIN_LATER;
            return CloseCodes.PROTOCOL_ERROR;
        case 1015:
            // Should not be used in a close frame
            // return CloseCodes.TLS_HANDSHAKE_FAILURE;
            return CloseCodes.PROTOCOL_ERROR;
        default:
            return CloseCodes.PROTOCOL_ERROR;
    }
}
 
Example 4
Source File: WsFrameBase.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * @return <code>true</code> if sufficient data was present to complete the
 *         processing of the header
 */
private boolean processRemainingHeader() throws IOException {
    // Ignore the 2 bytes already read. 4 for the mask
    int headerLength;
    if (isMasked()) {
        headerLength = 4;
    } else {
        headerLength = 0;
    }
    // Add additional bytes depending on length
    if (payloadLength == 126) {
        headerLength += 2;
    } else if (payloadLength == 127) {
        headerLength += 8;
    }
    if (writePos - readPos < headerLength) {
        return false;
    }
    // Calculate new payload length if necessary
    if (payloadLength == 126) {
        payloadLength = byteArrayToLong(inputBuffer, readPos, 2);
        readPos += 2;
    } else if (payloadLength == 127) {
        payloadLength = byteArrayToLong(inputBuffer, readPos, 8);
        readPos += 8;
    }
    if (Util.isControl(opCode)) {
        if (payloadLength > 125) {
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.controlPayloadTooBig",
                            Long.valueOf(payloadLength))));
        }
        if (!fin) {
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.controlNoFin")));
        }
    }
    if (isMasked()) {
        System.arraycopy(inputBuffer, readPos, mask, 0, 4);
        readPos += 4;
    }
    state = State.DATA;
    return true;
}
 
Example 5
Source File: WsFrameBase.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private boolean processDataControl() throws IOException {
    TransformationResult tr = transformation.getMoreData(opCode, fin, rsv, controlBufferBinary);
    if (TransformationResult.UNDERFLOW.equals(tr)) {
        return false;
    }
    // Control messages have fixed message size so
    // TransformationResult.OVERFLOW is not possible here

    controlBufferBinary.flip();
    if (opCode == Constants.OPCODE_CLOSE) {
        open = false;
        String reason = null;
        int code = CloseCodes.NORMAL_CLOSURE.getCode();
        if (controlBufferBinary.remaining() == 1) {
            controlBufferBinary.clear();
            // Payload must be zero or greater than 2
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.oneByteCloseCode")));
        }
        if (controlBufferBinary.remaining() > 1) {
            code = controlBufferBinary.getShort();
            if (controlBufferBinary.remaining() > 0) {
                CoderResult cr = utf8DecoderControl.decode(
                        controlBufferBinary, controlBufferText, true);
                if (cr.isError()) {
                    controlBufferBinary.clear();
                    controlBufferText.clear();
                    throw new WsIOException(new CloseReason(
                            CloseCodes.PROTOCOL_ERROR,
                            sm.getString("wsFrame.invalidUtf8Close")));
                }
                // There will be no overflow as the output buffer is big
                // enough. There will be no underflow as all the data is
                // passed to the decoder in a single call.
                controlBufferText.flip();
                reason = controlBufferText.toString();
            }
        }
        wsSession.onClose(new CloseReason(Util.getCloseCode(code), reason));
    } else if (opCode == Constants.OPCODE_PING) {
        if (wsSession.isOpen()) {
            wsSession.getBasicRemote().sendPong(controlBufferBinary);
        }
    } else if (opCode == Constants.OPCODE_PONG) {
        MessageHandler.Whole<PongMessage> mhPong =
                wsSession.getPongMessageHandler();
        if (mhPong != null) {
            try {
                mhPong.onMessage(new WsPongMessage(controlBufferBinary));
            } catch (Throwable t) {
                handleThrowableOnSend(t);
            } finally {
                controlBufferBinary.clear();
            }
        }
    } else {
        // Should have caught this earlier but just in case...
        controlBufferBinary.clear();
        throw new WsIOException(new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode",
                        Integer.valueOf(opCode))));
    }
    controlBufferBinary.clear();
    newFrame();
    return true;
}
 
Example 6
Source File: Util.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
static CloseCode getCloseCode(int code) {
    if (code > 2999 && code < 5000) {
        return CloseCodes.getCloseCode(code);
    }
    switch (code) {
        case 1000:
            return CloseCodes.NORMAL_CLOSURE;
        case 1001:
            return CloseCodes.GOING_AWAY;
        case 1002:
            return CloseCodes.PROTOCOL_ERROR;
        case 1003:
            return CloseCodes.CANNOT_ACCEPT;
        case 1004:
            // Should not be used in a close frame
            // return CloseCodes.RESERVED;
            return CloseCodes.PROTOCOL_ERROR;
        case 1005:
            // Should not be used in a close frame
            // return CloseCodes.NO_STATUS_CODE;
            return CloseCodes.PROTOCOL_ERROR;
        case 1006:
            // Should not be used in a close frame
            // return CloseCodes.CLOSED_ABNORMALLY;
            return CloseCodes.PROTOCOL_ERROR;
        case 1007:
            return CloseCodes.NOT_CONSISTENT;
        case 1008:
            return CloseCodes.VIOLATED_POLICY;
        case 1009:
            return CloseCodes.TOO_BIG;
        case 1010:
            return CloseCodes.NO_EXTENSION;
        case 1011:
            return CloseCodes.UNEXPECTED_CONDITION;
        case 1012:
            // Not in RFC6455
            // return CloseCodes.SERVICE_RESTART;
            return CloseCodes.PROTOCOL_ERROR;
        case 1013:
            // Not in RFC6455
            // return CloseCodes.TRY_AGAIN_LATER;
            return CloseCodes.PROTOCOL_ERROR;
        case 1015:
            // Should not be used in a close frame
            // return CloseCodes.TLS_HANDSHAKE_FAILURE;
            return CloseCodes.PROTOCOL_ERROR;
        default:
            return CloseCodes.PROTOCOL_ERROR;
    }
}
 
Example 7
Source File: WsFrameBase.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * @return <code>true</code> if sufficient data was present to complete the
 *         processing of the header
 */
private boolean processRemainingHeader() throws IOException {
    // Ignore the 2 bytes already read. 4 for the mask
    int headerLength;
    if (isMasked()) {
        headerLength = 4;
    } else {
        headerLength = 0;
    }
    // Add additional bytes depending on length
    if (payloadLength == 126) {
        headerLength += 2;
    } else if (payloadLength == 127) {
        headerLength += 8;
    }
    if (writePos - readPos < headerLength) {
        return false;
    }
    // Calculate new payload length if necessary
    if (payloadLength == 126) {
        payloadLength = byteArrayToLong(inputBuffer, readPos, 2);
        readPos += 2;
    } else if (payloadLength == 127) {
        payloadLength = byteArrayToLong(inputBuffer, readPos, 8);
        readPos += 8;
    }
    if (Util.isControl(opCode)) {
        if (payloadLength > 125) {
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.controlPayloadTooBig",
                            Long.valueOf(payloadLength))));
        }
        if (!fin) {
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.controlNoFin")));
        }
    }
    if (isMasked()) {
        System.arraycopy(inputBuffer, readPos, mask, 0, 4);
        readPos += 4;
    }
    state = State.DATA;
    return true;
}
 
Example 8
Source File: WsFrameBase.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private boolean processDataControl() throws IOException {
    TransformationResult tr = transformation.getMoreData(opCode, fin, rsv, controlBufferBinary);
    if (TransformationResult.UNDERFLOW.equals(tr)) {
        return false;
    }
    // Control messages have fixed message size so
    // TransformationResult.OVERFLOW is not possible here

    controlBufferBinary.flip();
    if (opCode == Constants.OPCODE_CLOSE) {
        open = false;
        String reason = null;
        int code = CloseCodes.NORMAL_CLOSURE.getCode();
        if (controlBufferBinary.remaining() == 1) {
            controlBufferBinary.clear();
            // Payload must be zero or greater than 2
            throw new WsIOException(new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.oneByteCloseCode")));
        }
        if (controlBufferBinary.remaining() > 1) {
            code = controlBufferBinary.getShort();
            if (controlBufferBinary.remaining() > 0) {
                CoderResult cr = utf8DecoderControl.decode(
                        controlBufferBinary, controlBufferText, true);
                if (cr.isError()) {
                    controlBufferBinary.clear();
                    controlBufferText.clear();
                    throw new WsIOException(new CloseReason(
                            CloseCodes.PROTOCOL_ERROR,
                            sm.getString("wsFrame.invalidUtf8Close")));
                }
                // There will be no overflow as the output buffer is big
                // enough. There will be no underflow as all the data is
                // passed to the decoder in a single call.
                controlBufferText.flip();
                reason = controlBufferText.toString();
            }
        }
        wsSession.onClose(new CloseReason(Util.getCloseCode(code), reason));
    } else if (opCode == Constants.OPCODE_PING) {
        if (wsSession.isOpen()) {
            wsSession.getBasicRemote().sendPong(controlBufferBinary);
        }
    } else if (opCode == Constants.OPCODE_PONG) {
        MessageHandler.Whole<PongMessage> mhPong =
                wsSession.getPongMessageHandler();
        if (mhPong != null) {
            try {
                mhPong.onMessage(new WsPongMessage(controlBufferBinary));
            } catch (Throwable t) {
                handleThrowableOnSend(t);
            } finally {
                controlBufferBinary.clear();
            }
        }
    } else {
        // Should have caught this earlier but just in case...
        controlBufferBinary.clear();
        throw new WsIOException(new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode",
                        Integer.valueOf(opCode))));
    }
    controlBufferBinary.clear();
    newFrame();
    return true;
}
 
Example 9
Source File: Util.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
static CloseCode getCloseCode(int code) {
    if (code > 2999 && code < 5000) {
        return CloseCodes.getCloseCode(code);
    }
    switch (code) {
        case 1000:
            return CloseCodes.NORMAL_CLOSURE;
        case 1001:
            return CloseCodes.GOING_AWAY;
        case 1002:
            return CloseCodes.PROTOCOL_ERROR;
        case 1003:
            return CloseCodes.CANNOT_ACCEPT;
        case 1004:
            // Should not be used in a close frame
            // return CloseCodes.RESERVED;
            return CloseCodes.PROTOCOL_ERROR;
        case 1005:
            // Should not be used in a close frame
            // return CloseCodes.NO_STATUS_CODE;
            return CloseCodes.PROTOCOL_ERROR;
        case 1006:
            // Should not be used in a close frame
            // return CloseCodes.CLOSED_ABNORMALLY;
            return CloseCodes.PROTOCOL_ERROR;
        case 1007:
            return CloseCodes.NOT_CONSISTENT;
        case 1008:
            return CloseCodes.VIOLATED_POLICY;
        case 1009:
            return CloseCodes.TOO_BIG;
        case 1010:
            return CloseCodes.NO_EXTENSION;
        case 1011:
            return CloseCodes.UNEXPECTED_CONDITION;
        case 1012:
            // Not in RFC6455
            // return CloseCodes.SERVICE_RESTART;
            return CloseCodes.PROTOCOL_ERROR;
        case 1013:
            // Not in RFC6455
            // return CloseCodes.TRY_AGAIN_LATER;
            return CloseCodes.PROTOCOL_ERROR;
        case 1015:
            // Should not be used in a close frame
            // return CloseCodes.TLS_HANDSHAKE_FAILURE;
            return CloseCodes.PROTOCOL_ERROR;
        default:
            return CloseCodes.PROTOCOL_ERROR;
    }
}