Class HttpParser
- Direct Known Subclasses:
ResponseContentParser.FCGIHttpParser
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 enumstatic interfacestatic enumstatic interfaceprivate static classstatic interfacestatic interfacestatic enum -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final EnumSet<HttpParser.State> private static final EnumSet<HttpParser.State> static final StringDeprecated.private static final EnumSet<HttpParser.State> private longprivate longprivate booleanprivate intprivate HttpParser.ChunkSizeStateprivate final HttpComplianceprivate final HttpParser.ComplianceHandlerprivate final EnumSet<HttpComplianceSection> private ByteBufferprivate longprivate longprivate booleanprivate HttpTokens.EndOfContentprivate booleanprivate HttpFieldprivate HttpParser.FieldStateprivate final HttpParser.HttpHandlerprivate booleanprivate booleanprivate HttpHeaderprivate intprivate booleanprivate Stringprivate booleanprivate booleanprivate intprivate final intprivate HttpMethodprivate Stringprivate final HttpParser.RequestHandlerprivate final HttpParser.ResponseHandlerprivate intprivate HttpParser.Stateprivate final StringBuilderprivate final Utf8StringBuilderprivate Stringprivate HttpVersionCache of commonHttpFields 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 static final intprivate final booleanprivate static final longprivate static final longprivate static final longstatic final intstatic final Loggerprivate static final intprivate static final longprivate static final longprivate static final long -
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) privateHttpParser(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 TypeMethodDescriptionprivate voidaddAndCheckHeadersSize(int delta) voidatEOF()Signal that the associated data source is at EOFprotected voidprotected StringcaseInsensitiveHeader(String orig, String normative) private voidprivate booleanvoidclose()Request that the associated data source be closedprivate static HttpComplianceprotected booleancomplianceViolation(HttpComplianceSection violation) Check RFC compliance violationprotected booleancomplianceViolation(HttpComplianceSection violation, String reason) Check RFC compliance violationprivate longconvertContentLength(String valueString) longlongintgetState()private booleanprivate booleanprotected voidhandleViolation(HttpComplianceSection section, String reason) booleanbooleanbooleanisAtEOF()private booleanBad white space is defined in RFC-9110[5.6.3].booleanbooleanisClose()booleanisClosed()booleanbooleanisIdle()private booleanText inside quotes (qdtext) is defined in RFC-9110[5.6.4].private booleanEscaped text inside quotes (quoted-pair) is defined in RFC-9110[5.6.4].booleanisStart()booleanisState(HttpParser.State state) private booleanToken and token characters are defined in RFC-9110[5.6.2].booleanprivate HttpTokens.Tokennext(ByteBuffer buffer) private booleanparseChunkSize(ByteBuffer buffer) protected booleanparseContent(ByteBuffer buffer) private voidprivate voidprotected booleanparseFields(ByteBuffer buffer) private booleanparseLine(ByteBuffer buffer) booleanparseNext(ByteBuffer buffer) Parse until next Event.private voidquickStart(ByteBuffer buffer) private booleanquickStartRequestLine(ByteBuffer buffer) private booleanquickStartResponseLine(ByteBuffer buffer) voidreset()private voidvoidsetHeadResponse(boolean head) Set if a HEAD response is expectedprotected voidsetResponseStatus(int status) protected voidsetState(HttpParser.FieldState state) protected voidsetState(HttpParser.State state) private voidprivate static longprivate StringtoString()
-
Field Details
-
LOG
-
__STRICT
-
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 commonHttpFields 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
-
HTTP_1_0_AS_LONG
private static final long HTTP_1_0_AS_LONG -
HTTP_1_1_AS_LONG
private static final long HTTP_1_1_AS_LONG -
GET_SLASH_HT_AS_LONG
private static final long GET_SLASH_HT_AS_LONG -
TP_SLASH_1_0_CRLF_AS_LONG
private static final long TP_SLASH_1_0_CRLF_AS_LONG -
TP_SLASH_1_1_CRLF_AS_LONG
private static final long TP_SLASH_1_1_CRLF_AS_LONG -
SPACE_200_OK_CR_AS_LONG
private static final long SPACE_200_OK_CR_AS_LONG -
CRLF_AS_SHORT
private static final int CRLF_AS_SHORT- See Also:
-
__idleStates
-
__completeStates
-
__terminatedStates
-
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 -
_chunkSizeDigits
private int _chunkSizeDigits -
_chunkLength
private long _chunkLength -
_chunkPosition
private long _chunkPosition -
_headResponse
private boolean _headResponse -
_cr
private boolean _cr -
_contentChunk
-
_fieldCache
-
_length
private int _length -
_string
-
_chunkSizeState
-
_chunkQuotedEscape
private boolean _chunkQuotedEscape
-
-
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
-
stringAsLong
-
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() -
isTerminated
public boolean isTerminated() -
isState
-
next
-
addAndCheckHeadersSize
private void addAndCheckHeadersSize(int delta) -
quickStartRequestLine
-
quickStartResponseLine
-
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.RequestHandlermethod was called and it returned true;
-
badMessage
-
parseContent
-
parseChunkSize
-
chunkSizeEnd
-
isBWS
Bad white space is defined in RFC-9110[5.6.3].- Parameters:
t- the token- Returns:
- whether the token is a bad white space
-
isTchar
Token and token characters are defined in RFC-9110[5.6.2].- Parameters:
t- the token- Returns:
- whether the token is a token character
-
isQdText
Text inside quotes (qdtext) is defined in RFC-9110[5.6.4].- Parameters:
t- the token- Returns:
- whether the token is quoted text
-
isQuotedPair
Escaped text inside quotes (quoted-pair) is defined in RFC-9110[5.6.4].- Parameters:
t- the token- Returns:
- whether the token is a quoted pair
-
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
-
setChunkSizeState
-
getFieldCache
-
toString
-