public class HttpParser extends 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:
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
HttpParser.ComplianceHandler |
static class |
HttpParser.FieldState |
static interface |
HttpParser.HttpHandler |
static interface |
HttpParser.RequestHandler |
static interface |
HttpParser.ResponseHandler |
static class |
HttpParser.State |
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
__STRICT
已过时。
|
static Trie<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
|
static int |
INITIAL_URI_LENGTH |
static org.slf4j.Logger |
LOG |
| 构造器和说明 |
|---|
HttpParser(HttpParser.RequestHandler handler) |
HttpParser(HttpParser.RequestHandler handler,
HttpCompliance compliance) |
HttpParser(HttpParser.RequestHandler handler,
int maxHeaderBytes) |
HttpParser(HttpParser.RequestHandler handler,
int maxHeaderBytes,
boolean strict)
已过时。
|
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)
已过时。
|
HttpParser(HttpParser.ResponseHandler handler,
int maxHeaderBytes,
HttpCompliance compliance) |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
atEOF()
Signal that the associated data source is at EOF
|
protected void |
badMessage(BadMessageException x) |
void |
close()
Request that the associated data source be closed
|
protected boolean |
complianceViolation(HttpCompliance compliance,
String reason)
Check RFC compliance violation
|
long |
getContentLength() |
long |
getContentRead() |
Trie<HttpField> |
getFieldCache() |
HttpParser.HttpHandler |
getHandler() |
HttpParser.State |
getState() |
boolean |
inContentState() |
boolean |
inHeaderState() |
boolean |
isAtEOF() |
boolean |
isChunking() |
boolean |
isClose() |
boolean |
isClosed() |
boolean |
isComplete() |
boolean |
isIdle() |
boolean |
isStart() |
boolean |
isState(HttpParser.State state) |
protected String |
legacyString(String orig,
String cached) |
protected boolean |
parseContent(ByteBuffer buffer) |
protected boolean |
parseFields(ByteBuffer buffer) |
boolean |
parseNext(ByteBuffer buffer)
Parse until next Event.
|
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) |
String |
toString() |
public static final org.slf4j.Logger LOG
@Deprecated public static final String __STRICT
public static final int INITIAL_URI_LENGTH
public static final Trie<HttpField> CACHE
HttpFields including: 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)
public HttpParser.HttpHandler getHandler()
protected boolean complianceViolation(HttpCompliance compliance, String reason)
compliance - The compliance level violatedreason - The reason for the violationpublic 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)
protected boolean parseFields(ByteBuffer buffer)
public boolean parseNext(ByteBuffer buffer)
buffer - the buffer to parseHttpParser.RequestHandler method was called and it returned true;protected void badMessage(BadMessageException x)
protected boolean parseContent(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)
Copyright © 2017. All rights reserved.