public class HttpParser extends Object
The is 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 HttpField.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.
If the system property "org.eclipse.jetty.http.HttpParser.STRICT" is set to true, then the parser will strictly pass on the exact strings received for methods and header fields. Otherwise a fast case insensitive string lookup is used that may alter the case of the method and/or headers
Modifier and Type | Class and Description |
---|---|
static interface |
HttpParser.HttpHandler<T> |
static interface |
HttpParser.RequestHandler<T> |
static interface |
HttpParser.ResponseHandler<T> |
static class |
HttpParser.State |
Modifier and Type | Field and Description |
---|---|
static boolean |
__STRICT |
static int |
INITIAL_URI_LENGTH |
static Logger |
LOG |
Constructor and Description |
---|
HttpParser(HttpParser.RequestHandler<ByteBuffer> handler) |
HttpParser(HttpParser.RequestHandler<ByteBuffer> handler,
int maxHeaderBytes) |
HttpParser(HttpParser.RequestHandler<ByteBuffer> handler,
int maxHeaderBytes,
boolean strict) |
HttpParser(HttpParser.ResponseHandler<ByteBuffer> handler) |
HttpParser(HttpParser.ResponseHandler<ByteBuffer> handler,
int maxHeaderBytes) |
HttpParser(HttpParser.ResponseHandler<ByteBuffer> handler,
int maxHeaderBytes,
boolean strict) |
Modifier and Type | Method and Description |
---|---|
void |
close() |
long |
getContentLength() |
long |
getContentRead() |
Trie<HttpField> |
getFieldCache() |
HttpParser.State |
getState() |
boolean |
inContentState() |
boolean |
inHeaderState() |
boolean |
isChunking() |
boolean |
isClosed() |
boolean |
isComplete() |
boolean |
isIdle() |
boolean |
isInContent() |
boolean |
isStart() |
boolean |
isState(HttpParser.State state) |
boolean |
parseNext(ByteBuffer buffer)
Parse until next Event.
|
void |
reset() |
void |
setHeadResponse(boolean head)
Set if a HEAD response is expected
|
boolean |
shutdownInput()
Notifies this parser that I/O code read a -1 and therefore no more data will arrive to be parsed.
|
String |
toString() |
public static final Logger LOG
public static final boolean __STRICT
public static final int INITIAL_URI_LENGTH
public HttpParser(HttpParser.RequestHandler<ByteBuffer> handler)
public HttpParser(HttpParser.ResponseHandler<ByteBuffer> handler)
public HttpParser(HttpParser.RequestHandler<ByteBuffer> handler, int maxHeaderBytes)
public HttpParser(HttpParser.ResponseHandler<ByteBuffer> handler, int maxHeaderBytes)
public HttpParser(HttpParser.RequestHandler<ByteBuffer> handler, int maxHeaderBytes, boolean strict)
public HttpParser(HttpParser.ResponseHandler<ByteBuffer> handler, int maxHeaderBytes, boolean strict)
public long getContentLength()
public long getContentRead()
public void setHeadResponse(boolean head)
head
- public HttpParser.State getState()
public boolean inContentState()
public boolean inHeaderState()
public boolean isInContent()
public boolean isChunking()
public boolean isStart()
public boolean isClosed()
public boolean isIdle()
public boolean isComplete()
public boolean isState(HttpParser.State state)
public boolean parseNext(ByteBuffer buffer)
HttpParser.RequestHandler
method was called and it returned true;public boolean shutdownInput()
HttpParser.HttpHandler.messageComplete()
, for
example when the content is delimited by the close of the connection.
If the parser is already in a state that does not need data (for example, it is idle waiting for
a request/response to be parsed), then calling this method is a no-operation.HttpParser.HttpHandler.messageComplete()
if there has been
one, or false otherwise.public void close()
public void reset()
Copyright © 1995-2013 Mort Bay Consulting. All Rights Reserved.