Class HTTP1MessageReceiver

java.lang.Object
org.omegazero.http.h1.HTTP1MessageReceiver
Direct Known Subclasses:
HTTP1RequestReceiver, HTTP1ResponseReceiver

public abstract class HTTP1MessageReceiver extends Object
This class is used for receiving and parsing HTTP headers.

A usual usage example is as follows:

  1. An application receives HTTP header data (either for a request or response, depending on the type of this HTTP1MessageReceiver)
  2. The application passes this data as (possibly multiple) byte arrays to receive(byte[], int)
  3. Upon receiving a non-negative integer from receive(byte[], int), indicating a full HTTP header block was received, the application retrieves the parsed message using get() (or possible variants of it in subclasses)
  4. 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 Details

  • Constructor Details

  • Method Details

    • receiveStartLine

      protected abstract void receiveStartLine(String[] startLine) throws InvalidHTTPMessageException
      Called when a HTTP/1 start line is received.
      Parameters:
      startLine - The start line components
      Throws:
      InvalidHTTPMessageException - If the start line is malformed
    • receiveHeader

      protected void receiveHeader(String key, String value) throws InvalidHTTPMessageException
      Called when a HTTP header is received.
      Parameters:
      key - The header name
      value - The header value
      Throws:
      InvalidHTTPMessageException - If the header has an invalid value or otherwise invalid
    • receiveVersion

      protected final void receiveVersion(String versionStr) throws InvalidHTTPMessageException
      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

      protected final String getVersion()
      Returns the string representation of the received version.
      Returns:
      The version string
      Throws:
      IllegalStateException - If no version was received
    • get

      public abstract HTTPMessage get()
      Returns the HTTPMessage 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 no HTTPMessage was received yet
      Implementation Requirements:
      msgInit(HTTPMessage) must be called on a new HTTPMessage before returning it
    • receive

      public int receive(byte[] data, int offset) throws InvalidHTTPMessageException
      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 using get(). 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 data
      offset - 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 until reset() is called
    • reset

      public void reset()
      Resets this HTTP1MessageReceiver to allow receipt of additional HTTPMessages.
    • getHeaderSize

      public int getHeaderSize()
      Returns the total amount of bytes received through receive(byte[], int).
      Returns:
      The number of bytes
    • isStartLineReceived

      public boolean isStartLineReceived()
      Returns true if a valid HTTP start line was received in a call to receive(byte[], int) and is available.
      Returns:
      true if a HTTP start line was received
    • msgInit

      protected static <T extends HTTPMessage> T msgInit(T msg)