Example #1
0
  /**
   * Parse until next Event.
   *
   * @return True if an {@link RequestHandler} method was called and it returned true;
   */
  public boolean parseNext(ByteBuffer buffer) {
    if (DEBUG) LOG.debug("parseNext s={} {}", _state, BufferUtil.toDetailString(buffer));
    try {
      boolean handle = false;

      // Start a request/response
      if (_state == State.START) {
        _version = null;
        _method = null;
        _methodString = null;
        _endOfContent = EndOfContent.UNKNOWN_CONTENT;
        _header = null;
        handle = quickStart(buffer);
      }

      // Request/response line
      if (!handle
          && _state.ordinal() >= State.START.ordinal()
          && _state.ordinal() < State.HEADER.ordinal()) handle = parseLine(buffer);

      // parse headers
      if (!handle
          && _state.ordinal() >= State.HEADER.ordinal()
          && _state.ordinal() < State.CONTENT.ordinal()) handle = parseHeaders(buffer);

      // parse content
      if (!handle
          && _state.ordinal() >= State.CONTENT.ordinal()
          && _state.ordinal() < State.END.ordinal()) {
        // Handle HEAD response
        if (_responseStatus > 0 && _headResponse) {
          setState(State.END);
          handle = _handler.messageComplete();
        } else handle = parseContent(buffer);
      }

      // handle end states
      if (_state == State.END) {
        // eat white space
        while (buffer.remaining() > 0 && buffer.get(buffer.position()) <= HttpTokens.SPACE)
          buffer.get();
      } else if (_state == State.CLOSED) {
        if (BufferUtil.hasContent(buffer)) {
          // Just ignore data when closed
          _headerBytes += buffer.remaining();
          BufferUtil.clear(buffer);
          if (_headerBytes > _maxHeaderBytes) {
            // Don't want to waste time reading data of a closed request
            throw new IllegalStateException("too much data after closed");
          }
        }
      }

      // Handle EOF
      if (_eof && !buffer.hasRemaining()) {
        switch (_state) {
          case CLOSED:
            break;

          case START:
            _handler.earlyEOF();
            setState(State.CLOSED);
            break;

          case END:
            setState(State.CLOSED);
            break;

          case EOF_CONTENT:
            handle = _handler.messageComplete() || handle;
            setState(State.CLOSED);
            break;

          case CONTENT:
          case CHUNKED_CONTENT:
          case CHUNK_SIZE:
          case CHUNK_PARAMS:
          case CHUNK:
            _handler.earlyEOF();
            setState(State.CLOSED);
            break;

          default:
            if (DEBUG) LOG.debug("{} EOF in {}", this, _state);
            _handler.badMessage(400, null);
            setState(State.CLOSED);
            break;
        }
      }

      return handle;
    } catch (BadMessage e) {
      BufferUtil.clear(buffer);

      LOG.warn(
          "badMessage: "
              + e._code
              + (e._message != null ? " " + e._message : "")
              + " for "
              + _handler);
      if (DEBUG) LOG.debug(e);
      setState(State.CLOSED);
      _handler.badMessage(e._code, e._message);
      return false;
    } catch (Exception e) {
      BufferUtil.clear(buffer);

      LOG.warn("badMessage: " + e.toString() + " for " + _handler);
      if (DEBUG) LOG.debug(e);

      if (_state.ordinal() <= State.END.ordinal()) {
        setState(State.CLOSED);
        _handler.badMessage(400, null);
      } else {
        _handler.earlyEOF();
        setState(State.CLOSED);
      }

      return false;
    }
  }
Example #2
0
  /**
   * Parse until next Event.
   *
   * @param buffer the buffer to parse
   * @return True if an {@link RequestHandler} method was called and it returned true;
   */
  public boolean parseNext(ByteBuffer buffer) {
    if (DEBUG) log.debug("parseNext s={} {}", _state, BufferUtils.toDetailString(buffer));
    try {
      // Start a request/response
      if (_state == State.START) {
        _version = null;
        _method = null;
        _methodString = null;
        _endOfContent = EndOfContent.UNKNOWN_CONTENT;
        _header = null;
        if (quickStart(buffer)) return true;
      }

      // Request/response line
      if (_state.ordinal() >= State.START.ordinal() && _state.ordinal() < State.HEADER.ordinal()) {
        if (parseLine(buffer)) return true;
      }

      // parse headers
      if (_state.ordinal() >= State.HEADER.ordinal()
          && _state.ordinal() < State.CONTENT.ordinal()) {
        if (parseHeaders(buffer)) return true;
      }

      // parse content
      if (_state.ordinal() >= State.CONTENT.ordinal() && _state.ordinal() < State.END.ordinal()) {
        // Handle HEAD response
        if (_responseStatus > 0 && _headResponse) {
          setState(State.END);
          return _handler.messageComplete();
        } else {
          if (parseContent(buffer)) return true;
        }
      }

      // handle end states
      if (_state == State.END) {
        // eat white space
        while (buffer.remaining() > 0 && buffer.get(buffer.position()) <= HttpTokens.SPACE)
          buffer.get();
      } else if (_state == State.CLOSE) {
        // Seeking EOF
        if (BufferUtils.hasContent(buffer)) {
          // Just ignore data when closed
          _headerBytes += buffer.remaining();
          BufferUtils.clear(buffer);
          if (_maxHeaderBytes > 0 && _headerBytes > _maxHeaderBytes) {
            // Don't want to waste time reading data of a closed
            // request
            throw new IllegalStateException("too much data seeking EOF");
          }
        }
      } else if (_state == State.CLOSED) {
        BufferUtils.clear(buffer);
      }

      // Handle EOF
      if (_eof && !buffer.hasRemaining()) {
        switch (_state) {
          case CLOSED:
            break;

          case START:
            setState(State.CLOSED);
            _handler.earlyEOF();
            break;

          case END:
          case CLOSE:
            setState(State.CLOSED);
            break;

          case EOF_CONTENT:
            setState(State.CLOSED);
            return _handler.messageComplete();

          case CONTENT:
          case CHUNKED_CONTENT:
          case CHUNK_SIZE:
          case CHUNK_PARAMS:
          case CHUNK:
            setState(State.CLOSED);
            _handler.earlyEOF();
            break;

          default:
            if (DEBUG) log.debug("{} EOF in {}", this, _state);
            setState(State.CLOSED);
            _handler.badMessage(400, null);
            break;
        }
      }
    } catch (BadMessageException e) {
      BufferUtils.clear(buffer);

      Throwable cause = e.getCause();
      boolean stack =
          log.isDebugEnable()
              || (!(cause instanceof NumberFormatException)
                  && (cause instanceof RuntimeException || cause instanceof Error));

      if (stack)
        log.warn(
            "bad HTTP parsed: "
                + e._code
                + (e.getReason() != null ? " " + e.getReason() : "")
                + " for "
                + _handler,
            e);
      else
        log.warn(
            "bad HTTP parsed: "
                + e._code
                + (e.getReason() != null ? " " + e.getReason() : "")
                + " for "
                + _handler);
      setState(State.CLOSE);
      _handler.badMessage(e.getCode(), e.getReason());
    } catch (NumberFormatException | IllegalStateException e) {
      BufferUtils.clear(buffer);
      log.warn("parse exception: {} in {} for {}", e.toString(), _state, _handler);
      if (DEBUG) log.debug("parse exception", e);

      switch (_state) {
        case CLOSED:
          break;
        case CLOSE:
          _handler.earlyEOF();
          break;
        default:
          setState(State.CLOSE);
          _handler.badMessage(400, null);
      }
    } catch (Exception | Error e) {
      BufferUtils.clear(buffer);

      log.warn("parse exception: " + e.toString() + " for " + _handler, e);

      switch (_state) {
        case CLOSED:
          break;
        case CLOSE:
          _handler.earlyEOF();
          break;
        default:
          setState(State.CLOSE);
          _handler.badMessage(400, null);
      }
    }
    return false;
  }