Ejemplo n.º 1
0
  /*
   * Quick lookahead for the start state looking for a request method or a
   * HTTP version, otherwise skip white space until something else to parse.
   */
  private boolean quickStart(ByteBuffer buffer) {
    if (_requestHandler != null) {
      _method = HttpMethod.lookAheadGet(buffer);
      if (_method != null) {
        _methodString = _method.asString();
        buffer.position(buffer.position() + _methodString.length() + 1);

        setState(State.SPACE1);
        return false;
      }
    } else if (_responseHandler != null) {
      _version = HttpVersion.lookAheadGet(buffer);
      if (_version != null) {
        buffer.position(buffer.position() + _version.asString().length() + 1);
        setState(State.SPACE1);
        return false;
      }
    }

    // Quick start look
    while (_state == State.START && buffer.hasRemaining()) {
      int ch = next(buffer);

      if (ch > SPACE) {
        _string.setLength(0);
        _string.append((char) ch);
        setState(_requestHandler != null ? State.METHOD : State.RESPONSE_VERSION);
        return false;
      } else if (ch == 0) break;
      else if (ch < 0) throw new BadMessageException();

      // count this white space as a header byte to avoid DOS
      if (_maxHeaderBytes > 0 && ++_headerBytes > _maxHeaderBytes) {
        log.warn("padding is too large > {}", _maxHeaderBytes);
        throw new BadMessageException(HttpStatus.BAD_REQUEST_400);
      }
    }
    return false;
  }