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
    final Headers
    The HTTP headers.
    Method of operation..
    Socket the request was read from.
    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.
    byte[]
    Returns the body of this HTTP request as a byte array.
    Returns an InputStream for reading the body of this HTTP request.
    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.
    Returns a Map containing the query parameters of this request.
    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 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
      Method of operation..
    • uri

      public String uri
      The URI requested.
    • version

      public String version
      The HTTP version.
    • headers

      public final Headers headers
      The HTTP 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

    • parseRequest

      public static void parseRequest(PushbackInputStream in, HttpRequest req) throws IOException
      Parses the HTTP request 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:
      IOException - if an I/O error occurs while parsing the request
    • getBodyInputStream

      public InputStream getBodyInputStream()
      Returns an InputStream for reading the body of this HTTP request.
      Returns:
      an InputStream for reading the request body
    • getBody

      public byte[] getBody() throws IOException
      Returns the body of this HTTP request as a byte array.

      Caches body for subsequent processing.

      Returns:
      the request body as a byte array
      Throws:
      IOException - if an I/O error occurs while reading the request body
    • consumeBody

      public void consumeBody() throws IOException
      Consumes the body of this HTTP request by reading and discarding the remaining data.
      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()
      Returns a Map containing the query parameters of this request.
      Returns:
      a Map containing the query 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