public class HttpParser
extends java.lang.Object
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:
Modifier and Type | Class and Description |
---|---|
static interface |
HttpParser.ComplianceHandler |
static class |
HttpParser.FieldState |
static interface |
HttpParser.HttpHandler |
private static class |
HttpParser.IllegalCharacterException |
static interface |
HttpParser.RequestHandler |
static interface |
HttpParser.ResponseHandler |
static class |
HttpParser.State |
Modifier and Type | Field and Description |
---|---|
private static java.util.EnumSet<HttpParser.State> |
__completeStates |
private static java.util.EnumSet<HttpParser.State> |
__idleStates |
static java.lang.String |
__STRICT
Deprecated.
|
private int |
_chunkLength |
private int |
_chunkPosition |
private HttpCompliance |
_compliance |
private HttpParser.ComplianceHandler |
_complianceHandler |
private java.util.EnumSet<HttpComplianceSection> |
_compliances |
private java.nio.ByteBuffer |
_contentChunk |
private long |
_contentLength |
private long |
_contentPosition |
private boolean |
_cr |
private HttpTokens.EndOfContent |
_endOfContent |
private boolean |
_eof |
private HttpField |
_field |
private Trie<HttpField> |
_fieldCache |
private HttpParser.FieldState |
_fieldState |
private HttpParser.HttpHandler |
_handler |
private boolean |
_hasContentLength |
private HttpHeader |
_header |
private int |
_headerBytes |
private boolean |
_headerComplete |
private java.lang.String |
_headerString |
private boolean |
_headResponse |
private boolean |
_host |
private int |
_length |
private int |
_maxHeaderBytes |
private HttpMethod |
_method |
private java.lang.String |
_methodString |
private HttpParser.RequestHandler |
_requestHandler |
private HttpParser.ResponseHandler |
_responseHandler |
private int |
_responseStatus |
private HttpParser.State |
_state |
private java.lang.StringBuilder |
_string |
private Utf8StringBuilder |
_uri |
private java.lang.String |
_valueString |
private HttpVersion |
_version |
static Trie<HttpField> |
CACHE
Cache of common
HttpField 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
|
private boolean |
debug |
static int |
INITIAL_URI_LENGTH |
static Logger |
LOG |
private static int |
MAX_CHUNK_LENGTH |
Modifier | Constructor and Description |
---|---|
|
HttpParser(HttpParser.RequestHandler handler) |
|
HttpParser(HttpParser.RequestHandler handler,
HttpCompliance compliance) |
private |
HttpParser(HttpParser.RequestHandler requestHandler,
HttpParser.ResponseHandler responseHandler,
int maxHeaderBytes,
HttpCompliance compliance) |
|
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.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) |
Modifier and Type | Method and Description |
---|---|
void |
atEOF()
Signal that the associated data source is at EOF
|
protected void |
badMessage(BadMessageException x) |
protected java.lang.String |
caseInsensitiveHeader(java.lang.String orig,
java.lang.String normative) |
private void |
checkVersion() |
void |
close()
Request that the associated data source be closed
|
private static HttpCompliance |
compliance() |
protected boolean |
complianceViolation(HttpComplianceSection violation)
Check RFC compliance violation
|
protected boolean |
complianceViolation(HttpComplianceSection violation,
java.lang.String reason)
Check RFC compliance violation
|
private long |
convertContentLength(java.lang.String valueString) |
long |
getContentLength() |
long |
getContentRead() |
Trie<HttpField> |
getFieldCache() |
HttpParser.HttpHandler |
getHandler() |
HttpParser.State |
getState() |
private boolean |
handleContentMessage() |
private boolean |
handleHeaderContentMessage() |
protected void |
handleViolation(HttpComplianceSection section,
java.lang.String reason) |
boolean |
inContentState() |
boolean |
inHeaderState() |
boolean |
isAtEOF() |
boolean |
isChunking() |
boolean |
isClose() |
boolean |
isClosed() |
boolean |
isComplete() |
boolean |
isIdle() |
boolean |
isStart() |
boolean |
isState(HttpParser.State state) |
private HttpTokens.Token |
next(java.nio.ByteBuffer buffer) |
protected boolean |
parseContent(java.nio.ByteBuffer buffer) |
private void |
parsedHeader() |
private void |
parsedTrailer() |
protected boolean |
parseFields(java.nio.ByteBuffer buffer) |
private boolean |
parseLine(java.nio.ByteBuffer buffer) |
boolean |
parseNext(java.nio.ByteBuffer buffer)
Parse until next Event.
|
private boolean |
quickStart(java.nio.ByteBuffer buffer) |
void |
reset() |
void |
setHeadResponse(boolean head)
Set if a HEAD response is expected
|
protected void |
setResponseStatus(int status) |
protected void |
setState(HttpParser.FieldState state) |
protected void |
setState(HttpParser.State state) |
private void |
setString(java.lang.String s) |
private java.lang.String |
takeString() |
java.lang.String |
toString() |
public static final Logger LOG
@Deprecated public static final java.lang.String __STRICT
public static final int INITIAL_URI_LENGTH
private static final int MAX_CHUNK_LENGTH
public static final Trie<HttpField> CACHE
HttpField
s including: private static final java.util.EnumSet<HttpParser.State> __idleStates
private static final java.util.EnumSet<HttpParser.State> __completeStates
private final boolean debug
private final HttpParser.HttpHandler _handler
private final HttpParser.RequestHandler _requestHandler
private final HttpParser.ResponseHandler _responseHandler
private final HttpParser.ComplianceHandler _complianceHandler
private final int _maxHeaderBytes
private final HttpCompliance _compliance
private final java.util.EnumSet<HttpComplianceSection> _compliances
private HttpField _field
private HttpHeader _header
private java.lang.String _headerString
private java.lang.String _valueString
private int _responseStatus
private int _headerBytes
private boolean _host
private boolean _headerComplete
private volatile HttpParser.State _state
private volatile HttpParser.FieldState _fieldState
private volatile boolean _eof
private HttpMethod _method
private java.lang.String _methodString
private HttpVersion _version
private Utf8StringBuilder _uri
private HttpTokens.EndOfContent _endOfContent
private boolean _hasContentLength
private long _contentLength
private long _contentPosition
private int _chunkLength
private int _chunkPosition
private boolean _headResponse
private boolean _cr
private java.nio.ByteBuffer _contentChunk
private int _length
private final java.lang.StringBuilder _string
public HttpParser(HttpParser.RequestHandler handler)
public HttpParser(HttpParser.ResponseHandler handler)
public HttpParser(HttpParser.RequestHandler handler, int maxHeaderBytes)
public HttpParser(HttpParser.ResponseHandler handler, int maxHeaderBytes)
@Deprecated public HttpParser(HttpParser.RequestHandler handler, int maxHeaderBytes, boolean strict)
@Deprecated public HttpParser(HttpParser.ResponseHandler handler, int maxHeaderBytes, boolean strict)
public HttpParser(HttpParser.RequestHandler handler, HttpCompliance compliance)
public HttpParser(HttpParser.RequestHandler handler, int maxHeaderBytes, HttpCompliance compliance)
public HttpParser(HttpParser.ResponseHandler handler, int maxHeaderBytes, HttpCompliance compliance)
private HttpParser(HttpParser.RequestHandler requestHandler, HttpParser.ResponseHandler responseHandler, int maxHeaderBytes, HttpCompliance compliance)
private static HttpCompliance compliance()
public HttpParser.HttpHandler getHandler()
protected boolean complianceViolation(HttpComplianceSection violation)
violation
- The compliance section violationprotected boolean complianceViolation(HttpComplianceSection violation, java.lang.String reason)
violation
- The compliance section violationreason
- The reason for the violationprotected void handleViolation(HttpComplianceSection section, java.lang.String reason)
protected java.lang.String caseInsensitiveHeader(java.lang.String orig, java.lang.String normative)
public long getContentLength()
public long getContentRead()
public void setHeadResponse(boolean head)
head
- true if head response is expectedprotected void setResponseStatus(int status)
public HttpParser.State getState()
public boolean inContentState()
public boolean inHeaderState()
public boolean isChunking()
public boolean isStart()
public boolean isClose()
public boolean isClosed()
public boolean isIdle()
public boolean isComplete()
public boolean isState(HttpParser.State state)
private HttpTokens.Token next(java.nio.ByteBuffer buffer)
private boolean quickStart(java.nio.ByteBuffer buffer)
private void setString(java.lang.String s)
private java.lang.String takeString()
private boolean handleHeaderContentMessage()
private boolean handleContentMessage()
private boolean parseLine(java.nio.ByteBuffer buffer)
private void checkVersion()
private void parsedHeader()
private void parsedTrailer()
private long convertContentLength(java.lang.String valueString)
protected boolean parseFields(java.nio.ByteBuffer buffer)
public boolean parseNext(java.nio.ByteBuffer buffer)
buffer
- the buffer to parseHttpParser.RequestHandler
method was called and it returned true;protected void badMessage(BadMessageException x)
protected boolean parseContent(java.nio.ByteBuffer buffer)
public boolean isAtEOF()
public void atEOF()
public void close()
public void reset()
protected void setState(HttpParser.State state)
protected void setState(HttpParser.FieldState state)
public java.lang.String toString()
toString
in class java.lang.Object