Class HttpParser
- java.lang.Object
-
- org.eclipse.jetty.http.HttpParser
-
- Direct Known Subclasses:
ResponseContentParser.FCGIHttpParser
public class HttpParser extends java.lang.ObjectA Parser for 1.0 and 1.1 as defined by RFC7230This 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 theHttpParser.HttpHandlerinstance the parser is constructed with. If the passed handler is aHttpParser.RequestHandlerthen server side parsing is performed and if it is aHttpParser.ResponseHandler, then client side parsing is done.The contract of the
HttpParser.HttpHandlerAPI is that if a call returns true then the call toparseNext(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 fromparseNext(ByteBuffer)rather than from within the scope of a call likeHttpParser.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 staticHttpHeader.CACHEis 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 ofHttpFieldsfrom 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:
- RFC 7230
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceHttpParser.ComplianceHandlerstatic classHttpParser.FieldStatestatic interfaceHttpParser.HttpHandlerprivate static classHttpParser.IllegalCharacterExceptionstatic interfaceHttpParser.RequestHandlerstatic interfaceHttpParser.ResponseHandlerstatic classHttpParser.State
-
Field Summary
Fields Modifier and Type Field Description private static java.util.EnumSet<HttpParser.State>__completeStatesprivate static java.util.EnumSet<HttpParser.State>__idleStatesstatic java.lang.String__STRICTDeprecated.private static java.util.EnumSet<HttpParser.State>__terminatedStatesprivate int_chunkLengthprivate int_chunkPositionprivate HttpCompliance_complianceprivate HttpParser.ComplianceHandler_complianceHandlerprivate java.util.EnumSet<HttpComplianceSection>_compliancesprivate java.nio.ByteBuffer_contentChunkprivate long_contentLengthprivate long_contentPositionprivate boolean_crprivate HttpTokens.EndOfContent_endOfContentprivate boolean_eofprivate HttpField_fieldprivate Trie<HttpField>_fieldCacheprivate HttpParser.FieldState_fieldStateprivate HttpParser.HttpHandler_handlerprivate boolean_hasContentLengthprivate boolean_hasTransferEncodingprivate HttpHeader_headerprivate int_headerBytesprivate boolean_headerCompleteprivate java.lang.String_headerStringprivate boolean_headResponseprivate boolean_hostprivate int_lengthprivate int_maxHeaderBytesprivate HttpMethod_methodprivate java.lang.String_methodStringprivate HttpParser.RequestHandler_requestHandlerprivate HttpParser.ResponseHandler_responseHandlerprivate int_responseStatusprivate HttpParser.State_stateprivate java.lang.StringBuilder_stringprivate Utf8StringBuilder_uriprivate java.lang.String_valueStringprivate HttpVersion_versionstatic Trie<HttpField>CACHECache 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 booleandebugstatic intINITIAL_URI_LENGTHstatic LoggerLOGprivate static intMAX_CHUNK_LENGTHprivate static Trie<HttpField>NO_CACHE
-
Constructor Summary
Constructors Modifier Constructor Description HttpParser(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidatEOF()Signal that the associated data source is at EOFprotected voidbadMessage(BadMessageException x)protected java.lang.StringcaseInsensitiveHeader(java.lang.String orig, java.lang.String normative)private voidcheckVersion()voidclose()Request that the associated data source be closedprivate static HttpCompliancecompliance()protected booleancomplianceViolation(HttpComplianceSection violation)Check RFC compliance violationprotected booleancomplianceViolation(HttpComplianceSection violation, java.lang.String reason)Check RFC compliance violationprivate longconvertContentLength(java.lang.String valueString)longgetContentLength()longgetContentRead()Trie<HttpField>getFieldCache()HttpParser.HttpHandlergetHandler()intgetHeaderLength()HttpCompliancegetHttpCompliance()HttpParser.StategetState()private booleanhandleContentMessage()private booleanhandleHeaderContentMessage()protected voidhandleViolation(HttpComplianceSection section, java.lang.String reason)booleaninContentState()booleaninHeaderState()booleanisAtEOF()booleanisChunking()booleanisClose()booleanisClosed()booleanisComplete()booleanisIdle()booleanisStart()booleanisState(HttpParser.State state)booleanisTerminated()private HttpTokens.Tokennext(java.nio.ByteBuffer buffer)protected booleanparseContent(java.nio.ByteBuffer buffer)private voidparsedHeader()private voidparsedTrailer()protected booleanparseFields(java.nio.ByteBuffer buffer)private booleanparseLine(java.nio.ByteBuffer buffer)booleanparseNext(java.nio.ByteBuffer buffer)Parse until next Event.private voidquickStart(java.nio.ByteBuffer buffer)voidreset()voidsetHeadResponse(boolean head)Set if a HEAD response is expectedprotected voidsetResponseStatus(int status)protected voidsetState(HttpParser.FieldState state)protected voidsetState(HttpParser.State state)private voidsetString(java.lang.String s)private java.lang.StringtakeString()java.lang.StringtoString()
-
-
-
Field Detail
-
LOG
public static final Logger LOG
-
__STRICT
@Deprecated public static final java.lang.String __STRICT
Deprecated.- See Also:
- Constant Field Values
-
INITIAL_URI_LENGTH
public static final int INITIAL_URI_LENGTH
- See Also:
- Constant Field Values
-
MAX_CHUNK_LENGTH
private static final int MAX_CHUNK_LENGTH
- See Also:
- Constant Field Values
-
CACHE
public static final Trie<HttpField> 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:
-
__idleStates
private static final java.util.EnumSet<HttpParser.State> __idleStates
-
__completeStates
private static final java.util.EnumSet<HttpParser.State> __completeStates
-
__terminatedStates
private static final java.util.EnumSet<HttpParser.State> __terminatedStates
-
debug
private final boolean debug
-
_handler
private final HttpParser.HttpHandler _handler
-
_requestHandler
private final HttpParser.RequestHandler _requestHandler
-
_responseHandler
private final HttpParser.ResponseHandler _responseHandler
-
_complianceHandler
private final HttpParser.ComplianceHandler _complianceHandler
-
_maxHeaderBytes
private final int _maxHeaderBytes
-
_compliance
private final HttpCompliance _compliance
-
_compliances
private final java.util.EnumSet<HttpComplianceSection> _compliances
-
_uri
private final Utf8StringBuilder _uri
-
_field
private HttpField _field
-
_header
private HttpHeader _header
-
_headerString
private java.lang.String _headerString
-
_valueString
private java.lang.String _valueString
-
_responseStatus
private int _responseStatus
-
_headerBytes
private int _headerBytes
-
_host
private boolean _host
-
_headerComplete
private boolean _headerComplete
-
_state
private volatile HttpParser.State _state
-
_fieldState
private volatile HttpParser.FieldState _fieldState
-
_eof
private volatile boolean _eof
-
_method
private HttpMethod _method
-
_methodString
private java.lang.String _methodString
-
_version
private HttpVersion _version
-
_endOfContent
private HttpTokens.EndOfContent _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
private java.nio.ByteBuffer _contentChunk
-
_length
private int _length
-
_string
private final java.lang.StringBuilder _string
-
-
Constructor Detail
-
HttpParser
public HttpParser(HttpParser.RequestHandler handler)
-
HttpParser
public HttpParser(HttpParser.ResponseHandler handler)
-
HttpParser
public HttpParser(HttpParser.RequestHandler handler, int maxHeaderBytes)
-
HttpParser
public HttpParser(HttpParser.ResponseHandler handler, int maxHeaderBytes)
-
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
public HttpParser(HttpParser.RequestHandler handler, HttpCompliance compliance)
-
HttpParser
public HttpParser(HttpParser.RequestHandler handler, int maxHeaderBytes, HttpCompliance compliance)
-
HttpParser
public HttpParser(HttpParser.ResponseHandler handler, int maxHeaderBytes, HttpCompliance compliance)
-
HttpParser
private HttpParser(HttpParser.RequestHandler requestHandler, HttpParser.ResponseHandler responseHandler, int maxHeaderBytes, HttpCompliance compliance)
-
-
Method Detail
-
compliance
private static HttpCompliance compliance()
-
getHandler
public HttpParser.HttpHandler getHandler()
-
getHttpCompliance
public HttpCompliance getHttpCompliance()
-
complianceViolation
protected boolean complianceViolation(HttpComplianceSection violation)
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
protected boolean complianceViolation(HttpComplianceSection violation, java.lang.String reason)
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
protected void handleViolation(HttpComplianceSection section, java.lang.String reason)
-
caseInsensitiveHeader
protected java.lang.String caseInsensitiveHeader(java.lang.String orig, java.lang.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- Parameters:
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)
-
next
private HttpTokens.Token next(java.nio.ByteBuffer buffer)
-
quickStart
private void quickStart(java.nio.ByteBuffer buffer)
-
setString
private void setString(java.lang.String s)
-
takeString
private java.lang.String takeString()
-
handleHeaderContentMessage
private boolean handleHeaderContentMessage()
-
handleContentMessage
private boolean handleContentMessage()
-
parseLine
private boolean parseLine(java.nio.ByteBuffer buffer)
-
checkVersion
private void checkVersion()
-
parsedHeader
private void parsedHeader()
-
parsedTrailer
private void parsedTrailer()
-
convertContentLength
private long convertContentLength(java.lang.String valueString)
-
parseFields
protected boolean parseFields(java.nio.ByteBuffer buffer)
-
parseNext
public boolean parseNext(java.nio.ByteBuffer buffer)
Parse until next Event.- Parameters:
buffer- the buffer to parse- Returns:
- True if an
HttpParser.RequestHandlermethod was called and it returned true;
-
badMessage
protected void badMessage(BadMessageException x)
-
parseContent
protected boolean parseContent(java.nio.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()
-
setState
protected void setState(HttpParser.State state)
-
setState
protected void setState(HttpParser.FieldState state)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-