Class HttpParser


public class HttpParser extends Object
A Parser for 1.0 and 1.1 as defined by RFC7230

This parser parses HTTP client and server messages from buffers passed in the parseNext(ByteBuffer) method. The parsed elements of the HTTP message are passed as event calls to the HttpParser.HttpHandler instance the parser is constructed with. If the passed handler is a HttpParser.RequestHandler then server side parsing is performed and if it is a HttpParser.ResponseHandler, then client side parsing is done.

The contract of the HttpParser.HttpHandler API is that if a call returns true then the call to parseNext(ByteBuffer) will return as soon as possible also with a true response. Typically this indicates that the parsing has reached a stage where the caller should process the events accumulated by the handler. It is the preferred calling style that handling such as calling a servlet to process a request, should be done after a true return from parseNext(ByteBuffer) rather than from within the scope of a call like HttpParser.HttpHandler.messageComplete()

For performance, the parse is heavily dependent on the Index.getBest(ByteBuffer, int, int) method to look ahead in a single pass for both the structure ( : and CRLF ) and semantic (which header and value) of a header. Specifically the static HttpHeader.CACHE is used to lookup common combinations of headers and values (eg. "Connection: close"), or just header names (eg. "Connection:" ). For headers who's value is not known statically (eg. Host, COOKIE) then a per parser dynamic Trie of HttpFields from previous parsed messages is used to help the parsing of subsequent messages.

The parser can work in varying compliance modes:

(default) Compliance with RFC7230
Wrapped headers and HTTP/0.9 supported
Adherence to Servlet Specification requirement for exact case of header names, bypassing the header caches, which are case insensitive, otherwise equivalent to RFC2616
See Also:
  • Field Details

    • LOG

      public static final org.slf4j.Logger LOG

      public static final int INITIAL_URI_LENGTH
      See Also:
    • CACHE

      public static final Index<HttpField> CACHE
      Cache of common HttpFields including:
      • Common static combinations such as:
        • Connection: close
        • Accept-Encoding: gzip
        • Content-Length: 0
      • Combinations of Content-Type header for common mime types by common charsets
      • Most common headers with null values so that a lookup will at least determine the header name even if the name:value combination is not cached
  • Constructor Details

  • Method Details

    • getBeginNanoTime

      public long getBeginNanoTime()
    • getHandler

      public HttpParser.HttpHandler getHandler()
    • getHeaderCacheSize

      public int getHeaderCacheSize()
    • setHeaderCacheSize

      public void setHeaderCacheSize(int headerCacheSize)
    • isHeaderCacheCaseSensitive

      public boolean isHeaderCacheCaseSensitive()
    • setHeaderCacheCaseSensitive

      public void setHeaderCacheCaseSensitive(boolean headerCacheCaseSensitive)
    • checkViolation

      protected void checkViolation(HttpCompliance.Violation violation) throws BadMessageException
    • reportComplianceViolation

      protected void reportComplianceViolation(HttpCompliance.Violation violation)
    • reportComplianceViolation

      protected void reportComplianceViolation(HttpCompliance.Violation violation, String reason)
    • caseInsensitiveHeader

      protected String caseInsensitiveHeader(String orig, String normative)
    • getContentLength

      public long getContentLength()
    • getContentRead

      public long getContentRead()
    • getHeaderLength

      public int getHeaderLength()
    • setHeadResponse

      public void setHeadResponse(boolean head)
      Set if a HEAD response is expected
      head - true if head response is expected
    • setResponseStatus

      protected void setResponseStatus(int status)
    • getState

      public HttpParser.State getState()
    • inContentState

      public boolean inContentState()
    • inHeaderState

      public boolean inHeaderState()
    • isChunking

      public boolean isChunking()
    • isStart

      public boolean isStart()
    • isClose

      public boolean isClose()
    • isClosed

      public boolean isClosed()
    • isIdle

      public boolean isIdle()
    • isComplete

      public boolean isComplete()
    • isTerminated

      public boolean isTerminated()
    • isState

      public boolean isState(HttpParser.State state)
    • parseFields

      protected boolean parseFields(ByteBuffer buffer)
    • parseNext

      public boolean parseNext(ByteBuffer buffer)
      Parse until next Event.
      buffer - the buffer to parse
      True if an HttpParser.RequestHandler method was called and it returned true;
    • badMessage

      protected void badMessage(BadMessageException x)
    • parseContent

      protected boolean parseContent(ByteBuffer buffer)
    • isAtEOF

      public boolean isAtEOF()
    • atEOF

      public void atEOF()
      Signal that the associated data source is at EOF
    • close

      public void close()
      Request that the associated data source be closed
    • reset

      public void reset()
    • servletUpgrade

      public void servletUpgrade()
    • setState

      protected void setState(HttpParser.State state)
    • setState

      protected void setState(HttpParser.FieldState state)
    • getFieldCache

      public Index<HttpField> getFieldCache()
    • toString

      public String toString()
      toString in class Object