コード例 #1
0
ファイル: PerMessageDeflate.java プロジェクト: sqtds/tomcat7
 @Override
 public boolean validateRsv(int rsv, byte opCode) {
   if (Util.isControl(opCode)) {
     if ((rsv & RSV_BITMASK) > 0) {
       return false;
     } else {
       if (next == null) {
         return true;
       } else {
         return next.validateRsv(rsv, opCode);
       }
     }
   } else {
     int rsvNext = rsv;
     if ((rsv & RSV_BITMASK) > 0) {
       rsvNext = rsv ^ RSV_BITMASK;
     }
     if (next == null) {
       return true;
     } else {
       return next.validateRsv(rsvNext, opCode);
     }
   }
 }
コード例 #2
0
ファイル: WsFrameBase.java プロジェクト: xiaobao2110/tomcat
  /**
   * @return <code>true</code> if sufficient data was present to process all of the initial header
   */
  private boolean processInitialHeader() throws IOException {
    // Need at least two bytes of data to do this
    if (writePos - readPos < 2) {
      return false;
    }
    int b = inputBuffer[readPos++];
    fin = (b & 0x80) > 0;
    rsv = (b & 0x70) >>> 4;
    opCode = (byte) (b & 0x0F);
    if (!transformation.validateRsv(rsv, opCode)) {
      throw new WsIOException(
          new CloseReason(
              CloseCodes.PROTOCOL_ERROR,
              sm.getString("wsFrame.wrongRsv", Integer.valueOf(rsv), Integer.valueOf(opCode))));
    }

    if (Util.isControl(opCode)) {
      if (!fin) {
        throw new WsIOException(
            new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.controlFragmented")));
      }
      if (opCode != Constants.OPCODE_PING
          && opCode != Constants.OPCODE_PONG
          && opCode != Constants.OPCODE_CLOSE) {
        throw new WsIOException(
            new CloseReason(
                CloseCodes.PROTOCOL_ERROR,
                sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode))));
      }
    } else {
      if (continuationExpected) {
        if (!Util.isContinuation(opCode)) {
          throw new WsIOException(
              new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.noContinuation")));
        }
      } else {
        try {
          if (opCode == Constants.OPCODE_BINARY) {
            // New binary message
            textMessage = false;
            int size = wsSession.getMaxBinaryMessageBufferSize();
            if (size != messageBufferBinary.capacity()) {
              messageBufferBinary = ByteBuffer.allocate(size);
            }
            binaryMsgHandler = wsSession.getBinaryMessageHandler();
            textMsgHandler = null;
          } else if (opCode == Constants.OPCODE_TEXT) {
            // New text message
            textMessage = true;
            int size = wsSession.getMaxTextMessageBufferSize();
            if (size != messageBufferText.capacity()) {
              messageBufferText = CharBuffer.allocate(size);
            }
            binaryMsgHandler = null;
            textMsgHandler = wsSession.getTextMessageHandler();
          } else {
            throw new WsIOException(
                new CloseReason(
                    CloseCodes.PROTOCOL_ERROR,
                    sm.getString("wsFrame.invalidOpCode", Integer.valueOf(opCode))));
          }
        } catch (IllegalStateException ise) {
          // Thrown if the session is already closed
          throw new WsIOException(
              new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.sessionClosed")));
        }
      }
      continuationExpected = !fin;
    }
    b = inputBuffer[readPos++];
    // Client data must be masked
    if ((b & 0x80) == 0 && isMasked()) {
      throw new WsIOException(
          new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.notMasked")));
    }
    payloadLength = b & 0x7F;
    state = State.PARTIAL_HEADER;
    return true;
  }