Class HttpRequest

java.lang.Object
net.targetr.wtm3.net.http.HttpRequest

public class HttpRequest extends Object
Parses, stores and reads HTTP requests.
Author:
Dr Michael Gardiner
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Number of bytes read from the request.
    long
    Number of bytes in the request headers.
    final Headers
    The HTTP headers.
    HTTP request method.
    Socket the request was read from.
    boolean
    Whether the request was upgraded to WebSocket.
    The URI requested.
    The HTTP version.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new HttpRequest object with an empty Headers object.
    Constructs a new HttpRequest object by parsing the given raw HTTP request string.
    HttpRequest(String method, String uri, String version, Headers headers)
    Constructs a new HttpRequest object with the specified method, URI, version, and headers.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Consumes the body of this HTTP request by reading and discarding the remaining data.
    static String
    Generates a query string, UTF-8 URL-encoding values but leaving keys unchanged.
    byte[]
    Reads and caches a body with a non-negative Content-Length.
    Returns the cached body stream, decoding chunk framing or limiting reads to Content-Length.
    long
    Returns the Content-Length value of this request, or -1 if not present.
    Returns a Map containing the cookies of this request.
    Returns the path portion of the URI of this request.
    Lazily parses query parameters into a mutable map.
    boolean
    Returns true if this is a GET request, false otherwise.
    boolean
    Returns true if this is a HEAD request, false otherwise.
    boolean
    Returns true if this is an OPTIONS request, false otherwise.
    boolean
    Returns true if this is a POST request, false otherwise.
    boolean
    Returns true if this is a PUT request, false otherwise.
    static void
    Parses the HTTP request header from the given PushbackInputStream and stores the parsed data in the given HttpRequest object.
    Returns a string representation of this HTTP request.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • socket

      public SuperSocket socket
      Socket the request was read from.
    • method

      public String method
      HTTP request method.
    • uri

      public String uri
      The URI requested.
    • version

      public String version
      The HTTP version.
    • upgradedToWebSocket

      public boolean upgradedToWebSocket
      Whether the request was upgraded to WebSocket.
    • headers

      public final Headers headers
      The HTTP headers.
    • bytesRead

      public final AtomicLong bytesRead
      Number of bytes read from the request.
    • headerLength

      public long headerLength
      Number of bytes in the request headers.
  • Constructor Details

    • HttpRequest

      public HttpRequest()
      Constructs a new HttpRequest object with an empty Headers object.
    • HttpRequest

      public HttpRequest(String method, String uri, String version, Headers headers)
      Constructs a new HttpRequest object with the specified method, URI, version, and headers.
      Parameters:
      method - the HTTP method of the request
      uri - the URI of the request
      version - the HTTP version of the request
      headers - the headers of the request
    • HttpRequest

      public HttpRequest(String raw) throws IOException
      Constructs a new HttpRequest object by parsing the given raw HTTP request string.
      Parameters:
      raw - the raw HTTP request string
      Throws:
      IOException - if an I/O error occurs while parsing the request
  • Method Details

    • parseRequestHeader

      public static void parseRequestHeader(PushbackInputStream in, HttpRequest req) throws IllegalArgumentException, IOException
      Parses the HTTP request header from the given PushbackInputStream and stores the parsed data in the given HttpRequest object.
      Parameters:
      in - the PushbackInputStream containing the HTTP request data
      req - the HttpRequest object to store the parsed data
      Throws:
      IllegalArgumentException - if the request contains multiple Content-Length or Transfer-Encoding headers, or both.
      IOException - if an I/O error occurs while parsing the request
    • getBodyInputStream

      public InputStream getBodyInputStream()
      Returns the cached body stream, decoding chunk framing or limiting reads to Content-Length. WebSocket upgrades expose the underlying stream.
      Returns:
      the body stream, or null if no supported body framing is present
    • getBody

      public byte[] getBody() throws IOException
      Reads and caches a body with a non-negative Content-Length. Use getBodyInputStream() for chunked bodies without a Content-Length.
      Returns:
      the cached body, or null if Content-Length is absent or negative
      Throws:
      IOException - if an I/O error occurs while reading the request body
      IllegalArgumentException - if Content-Length exceeds the configured size limit
    • consumeBody

      public void consumeBody() throws IOException
      Consumes the body of this HTTP request by reading and discarding the remaining data. (unless upgraded to websocket)
      Throws:
      IOException - if an I/O error occurs while consuming the request body
    • isHead

      public boolean isHead()
      Returns true if this is a HEAD request, false otherwise.
      Returns:
      true if this is a HEAD request, false otherwise
    • isOptions

      public boolean isOptions()
      Returns true if this is an OPTIONS request, false otherwise.
      Returns:
      true if this is an OPTIONS request, false otherwise
    • isGet

      public boolean isGet()
      Returns true if this is a GET request, false otherwise.
      Returns:
      true if this is a GET request, false otherwise
    • isPost

      public boolean isPost()
      Returns true if this is a POST request, false otherwise.
      Returns:
      true if this is a POST request, false otherwise
    • isPut

      public boolean isPut()
      Returns true if this is a PUT request, false otherwise.
      Returns:
      true if this is a PUT request, false otherwise
    • getPath

      public String getPath()
      Returns the path portion of the URI of this request.
      Returns:
      the path portion of the URI
    • getContentLength

      public long getContentLength()
      Returns the Content-Length value of this request, or -1 if not present.
      Returns:
      the Content-Length value, or -1 if not present
    • getQuery

      public Map<String,String> getQuery()
      Lazily parses query parameters into a mutable map. Values are UTF-8 URL-decoded and trimmed; keys are not decoded. Empty values become null and later duplicates win.
      Returns:
      a Map containing the query parameters
    • generateQueryString

      public static String generateQueryString(Map<String,String> query)
      Generates a query string, UTF-8 URL-encoding values but leaving keys unchanged.
      Parameters:
      query - key-value pairs with non-null values; may be null or empty
      Returns:
      a string beginning with ?, or an empty string for no parameters
    • getCookies

      public Map<String,String> getCookies()
      Returns a Map containing the cookies of this request.
      Returns:
      a Map containing the cookies
    • toString

      public String toString()
      Returns a string representation of this HTTP request.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this HTTP request