Package org.omegazero.http.h1
Class HTTP1MessageReceiver
java.lang.Object
org.omegazero.http.h1.HTTP1MessageReceiver
- Direct Known Subclasses:
HTTP1RequestReceiver
,HTTP1ResponseReceiver
This class is used for receiving and parsing HTTP headers.
A usual usage example is as follows:
- An application receives HTTP header data (either for a request or response, depending on the type of this
HTTP1MessageReceiver
) - The application passes this data as (possibly multiple) byte arrays to
receive(byte[], int)
- Upon receiving a non-negative integer from
receive(byte[], int)
, indicating a full HTTP header block was received, the application retrieves the parsed message usingget()
(or possible variants of it in subclasses) - The application calls
reset()
to prepare for the receipt of the next HTTP message (if applicable), and processes the current HTTP message and body
- Since:
- 1.2.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The default value formaxHeaderSize
, if not passed in the constructor.protected HTTPHeaderContainer
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newHTTP1MessageReceiver
with the default maximum message size.HTTP1MessageReceiver
(int maxHeaderSize) Creates a newHTTP1MessageReceiver
. -
Method Summary
Modifier and TypeMethodDescriptionabstract HTTPMessage
get()
Returns theHTTPMessage
after successfully receiving it (receive(byte[], int)
returns a non-negative integer).int
Returns the total amount of bytes received throughreceive(byte[], int)
.protected final String
Returns the string representation of the received version.boolean
Returnstrue
if a valid HTTP start line was received in a call toreceive(byte[], int)
and is available.protected static <T extends HTTPMessage>
TmsgInit
(T msg) int
receive
(byte[] data, int offset) Parses the given data containing a full or partial HTTP header block.protected void
receiveHeader
(String key, String value) Called when a HTTP header is received.protected abstract void
receiveStartLine
(String[] startLine) Called when a HTTP/1 start line is received.protected final void
receiveVersion
(String versionStr) Called when the version string in the start line is received.void
reset()
Resets thisHTTP1MessageReceiver
to allow receipt of additionalHTTPMessage
s.
-
Field Details
-
DEFAULT_MAX_HEADER_SIZE
public static final int DEFAULT_MAX_HEADER_SIZEThe default value formaxHeaderSize
, if not passed in the constructor.- See Also:
-
lHeaders
-
-
Constructor Details
-
HTTP1MessageReceiver
public HTTP1MessageReceiver()Creates a newHTTP1MessageReceiver
with the default maximum message size. -
HTTP1MessageReceiver
public HTTP1MessageReceiver(int maxHeaderSize) Creates a newHTTP1MessageReceiver
.- Parameters:
maxHeaderSize
- The maximum size in bytes of a single HTTP message. Seereceive(byte[], int)
-
-
Method Details
-
receiveStartLine
Called when a HTTP/1 start line is received.- Parameters:
startLine
- The start line components- Throws:
InvalidHTTPMessageException
- If the start line is malformed
-
receiveHeader
Called when a HTTP header is received.- Parameters:
key
- The header namevalue
- The header value- Throws:
InvalidHTTPMessageException
- If the header has an invalid value or otherwise invalid
-
receiveVersion
Called when the version string in the start line is received.- Parameters:
versionStr
- The version string- Throws:
InvalidHTTPMessageException
- If the version string is invalid
-
getVersion
Returns the string representation of the received version.- Returns:
- The version string
- Throws:
IllegalStateException
- If no version was received
-
get
Returns theHTTPMessage
after successfully receiving it (receive(byte[], int)
returns a non-negative integer).A call to this method should be followed by a call to
reset()
to allow the next HTTP message to be received.It is unspecified whether multiple calls to this method return the same
HTTPMessage
instance.- Returns:
- The
HTTPMessage
- Throws:
IllegalStateException
- If noHTTPMessage
was received yet- Implementation Requirements:
msgInit(HTTPMessage)
must be called on a newHTTPMessage
before returning it
-
receive
Parses the given data containing a full or partial HTTP header block.If a full and valid HTTP header was received, this method returns a non-negative integer and the received
HTTPMessage
may be retrieved usingget()
. The returned integer indicates the index at which the HTTP message body starts in the given array, which may be equal to the length of the array (if the given array contains no body data).If the given data is invalid or the total amount of data received exceeds the maximum configured in the constructor, an
InvalidHTTPMessageException
is thrown.- Parameters:
data
- The dataoffset
- The index to start reading at- Returns:
- The index at which the body data starts, if a full HTTP header block was received, or
-1
if no full message was received yet - Throws:
InvalidHTTPMessageException
- If the given data could not be identified as valid HTTP data, or the total amount of data received exceeds the limit. In this case, this method should no longer be called untilreset()
is called
-
reset
public void reset()Resets thisHTTP1MessageReceiver
to allow receipt of additionalHTTPMessage
s. -
getHeaderSize
public int getHeaderSize()Returns the total amount of bytes received throughreceive(byte[], int)
.- Returns:
- The number of bytes
-
isStartLineReceived
public boolean isStartLineReceived()Returnstrue
if a valid HTTP start line was received in a call toreceive(byte[], int)
and is available.- Returns:
true
if a HTTP start line was received
-
msgInit
-