Example #1
0
  private boolean handleKnownHeaders(ByteBuffer buffer) {
    boolean add_to_connection_trie = false;
    switch (_header) {
      case CONTENT_LENGTH:
        if (_endOfContent != EndOfContent.CHUNKED_CONTENT) {
          try {
            _contentLength = Long.parseLong(_valueString);
          } catch (NumberFormatException e) {
            LOG.ignore(e);
            throw new BadMessage(HttpStatus.BAD_REQUEST_400, "Bad Content-Length");
          }
          if (_contentLength <= 0) _endOfContent = EndOfContent.NO_CONTENT;
          else _endOfContent = EndOfContent.CONTENT_LENGTH;
        }
        break;

      case TRANSFER_ENCODING:
        if (_value == HttpHeaderValue.CHUNKED) _endOfContent = EndOfContent.CHUNKED_CONTENT;
        else {
          if (_valueString.endsWith(HttpHeaderValue.CHUNKED.toString()))
            _endOfContent = EndOfContent.CHUNKED_CONTENT;
          else if (_valueString.indexOf(HttpHeaderValue.CHUNKED.toString()) >= 0) {
            throw new BadMessage(HttpStatus.BAD_REQUEST_400, "Bad chunking");
          }
        }
        break;

      case HOST:
        add_to_connection_trie = _connectionFields != null && _field == null;
        _host = true;
        String host = _valueString;
        int port = 0;
        if (host == null || host.length() == 0) {
          throw new BadMessage(HttpStatus.BAD_REQUEST_400, "Bad Host header");
        }

        int len = host.length();
        loop:
        for (int i = len; i-- > 0; ) {
          char c2 = (char) (0xff & host.charAt(i));
          switch (c2) {
            case ']':
              break loop;

            case ':':
              try {
                len = i;
                port = StringUtil.toInt(host.substring(i + 1));
              } catch (NumberFormatException e) {
                if (DEBUG) LOG.debug(e);
                throw new BadMessage(HttpStatus.BAD_REQUEST_400, "Bad Host header");
              }
              break loop;
          }
        }
        if (host.charAt(0) == '[') {
          if (host.charAt(len - 1) != ']') {
            throw new BadMessage(HttpStatus.BAD_REQUEST_400, "Bad IPv6 Host header");
          }
          host = host.substring(1, len - 1);
        } else if (len != host.length()) host = host.substring(0, len);

        if (_requestHandler != null) _requestHandler.parsedHostHeader(host, port);

        break;

      case CONNECTION:
        // Don't cache if not persistent
        if (_valueString != null && _valueString.indexOf("close") >= 0) {
          _closed = true;
          _connectionFields = null;
        }
        break;

      case AUTHORIZATION:
      case ACCEPT:
      case ACCEPT_CHARSET:
      case ACCEPT_ENCODING:
      case ACCEPT_LANGUAGE:
      case COOKIE:
      case CACHE_CONTROL:
      case USER_AGENT:
        add_to_connection_trie = _connectionFields != null && _field == null;
        break;

      default:
        break;
    }

    if (add_to_connection_trie
        && !_connectionFields.isFull()
        && _header != null
        && _valueString != null) {
      _field = new HttpField(_header, _valueString);
      _connectionFields.put(_field);
    }

    return false;
  }