Class HttpParser
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
Trie.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:
- RFC7230
- (default) Compliance with RFC7230
- RFC2616
- Wrapped headers and HTTP/0.9 supported
- LEGACY
- (aka STRICT) 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
static enum
static interface
private static class
static interface
static interface
static enum
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final EnumSet
<HttpParser.State> private static final EnumSet
<HttpParser.State> static final String
Deprecated.private int
private int
private final HttpCompliance
private final HttpParser.ComplianceHandler
private final EnumSet
<HttpComplianceSection> private ByteBuffer
private long
private long
private boolean
private HttpTokens.EndOfContent
private boolean
private HttpField
private HttpParser.FieldState
private final HttpParser.HttpHandler
private boolean
private boolean
private HttpHeader
private int
private boolean
private String
private boolean
private boolean
private int
private final int
private HttpMethod
private String
private final HttpParser.RequestHandler
private final HttpParser.ResponseHandler
private int
private HttpParser.State
private final StringBuilder
private final Utf8StringBuilder
private String
private HttpVersion
Cache of commonHttpField
s 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 cachedprivate final boolean
static final int
static final Logger
private static final int
-
Constructor Summary
ConstructorsModifierConstructorDescriptionHttpParser
(HttpParser.RequestHandler handler) HttpParser
(HttpParser.RequestHandler handler, int maxHeaderBytes) HttpParser
(HttpParser.RequestHandler handler, int maxHeaderBytes, boolean strict) Deprecated.HttpParser
(HttpParser.RequestHandler handler, int maxHeaderBytes, HttpCompliance compliance) HttpParser
(HttpParser.RequestHandler handler, HttpCompliance compliance) private
HttpParser
(HttpParser.RequestHandler requestHandler, HttpParser.ResponseHandler responseHandler, int maxHeaderBytes, HttpCompliance compliance) HttpParser
(HttpParser.ResponseHandler handler) HttpParser
(HttpParser.ResponseHandler handler, int maxHeaderBytes) HttpParser
(HttpParser.ResponseHandler handler, int maxHeaderBytes, boolean strict) Deprecated.HttpParser
(HttpParser.ResponseHandler handler, int maxHeaderBytes, HttpCompliance compliance) -
Method Summary
Modifier and TypeMethodDescriptionvoid
atEOF()
Signal that the associated data source is at EOFprotected void
protected String
caseInsensitiveHeader
(String orig, String normative) private void
void
close()
Request that the associated data source be closedprivate static HttpCompliance
protected boolean
complianceViolation
(HttpComplianceSection violation) Check RFC compliance violationprotected boolean
complianceViolation
(HttpComplianceSection violation, String reason) Check RFC compliance violationprivate long
convertContentLength
(String valueString) long
long
int
getState()
private boolean
private boolean
protected void
handleViolation
(HttpComplianceSection section, String reason) boolean
boolean
boolean
isAtEOF()
boolean
boolean
isClose()
boolean
isClosed()
boolean
boolean
isIdle()
boolean
isStart()
boolean
isState
(HttpParser.State state) private HttpTokens.Token
next
(ByteBuffer buffer) protected boolean
parseContent
(ByteBuffer buffer) private void
private void
protected boolean
parseFields
(ByteBuffer buffer) private boolean
parseLine
(ByteBuffer buffer) boolean
parseNext
(ByteBuffer buffer) Parse until next Event.private boolean
quickStart
(ByteBuffer buffer) void
reset()
void
setHeadResponse
(boolean head) Set if a HEAD response is expectedprotected void
setResponseStatus
(int status) protected void
setState
(HttpParser.FieldState state) protected void
setState
(HttpParser.State state) private void
private String
toString()
-
Field Details
-
LOG
-
__STRICT
Deprecated.- See Also:
-
INITIAL_URI_LENGTH
public static final int INITIAL_URI_LENGTH- See Also:
-
MAX_CHUNK_LENGTH
private static final int MAX_CHUNK_LENGTH- See Also:
-
CACHE
Cache of commonHttpField
s 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
- Common static combinations such as:
-
NO_CACHE
-
__idleStates
-
__completeStates
-
debug
private final boolean debug -
_handler
-
_requestHandler
-
_responseHandler
-
_complianceHandler
-
_maxHeaderBytes
private final int _maxHeaderBytes -
_compliance
-
_compliances
-
_uri
-
_field
-
_header
-
_headerString
-
_valueString
-
_responseStatus
private int _responseStatus -
_headerBytes
private int _headerBytes -
_host
private boolean _host -
_headerComplete
private boolean _headerComplete -
_state
-
_fieldState
-
_eof
private volatile boolean _eof -
_method
-
_methodString
-
_version
-
_endOfContent
-
_hasContentLength
private boolean _hasContentLength -
_hasTransferEncoding
private boolean _hasTransferEncoding -
_contentLength
private long _contentLength -
_contentPosition
private long _contentPosition -
_chunkLength
private int _chunkLength -
_chunkPosition
private int _chunkPosition -
_headResponse
private boolean _headResponse -
_cr
private boolean _cr -
_contentChunk
-
_fieldCache
-
_length
private int _length -
_string
-
-
Constructor Details
-
HttpParser
-
HttpParser
-
HttpParser
-
HttpParser
-
HttpParser
@Deprecated public HttpParser(HttpParser.RequestHandler handler, int maxHeaderBytes, boolean strict) Deprecated. -
HttpParser
@Deprecated public HttpParser(HttpParser.ResponseHandler handler, int maxHeaderBytes, boolean strict) Deprecated. -
HttpParser
-
HttpParser
-
HttpParser
public HttpParser(HttpParser.ResponseHandler handler, int maxHeaderBytes, HttpCompliance compliance) -
HttpParser
private HttpParser(HttpParser.RequestHandler requestHandler, HttpParser.ResponseHandler responseHandler, int maxHeaderBytes, HttpCompliance compliance)
-
-
Method Details
-
compliance
-
getHandler
-
getHttpCompliance
-
complianceViolation
Check RFC compliance violation- Parameters:
violation
- The compliance section violation- Returns:
- True if the current compliance level is set so as to Not allow this violation
-
complianceViolation
Check RFC compliance violation- Parameters:
violation
- The compliance section violationreason
- The reason for the violation- Returns:
- True if the current compliance level is set so as to Not allow this violation
-
handleViolation
-
caseInsensitiveHeader
-
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- Parameters:
head
- true if head response is expected
-
setResponseStatus
protected void setResponseStatus(int status) -
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() -
isState
-
next
-
quickStart
-
setString
-
takeString
-
handleHeaderContentMessage
private boolean handleHeaderContentMessage() -
handleContentMessage
private boolean handleContentMessage() -
parseLine
-
checkVersion
private void checkVersion() -
parsedHeader
private void parsedHeader() -
parsedTrailer
private void parsedTrailer() -
convertContentLength
-
parseFields
-
parseNext
Parse until next Event.- Parameters:
buffer
- the buffer to parse- Returns:
- True if an
HttpParser.RequestHandler
method was called and it returned true;
-
badMessage
-
parseContent
-
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() -
setState
-
setState
-
getFieldCache
-
toString
-