Class HTTPRequest

java.lang.Object
com.ns.tcpframework.HTTPRequest

public class HTTPRequest extends Object
Represents an HTTP request, encapsulating the HTTP method, path, version, headers, and body.

This class provides an immutable representation of an HTTP request received by the server. It parses and stores all components of the HTTP request including the request line (method, path, version), headers, and message body.

The request body is eagerly read from the input stream during construction and stored as a byte array, making it available for multiple reads without stream concerns.

Key features:

  • Immutable design - all fields are final and cannot be modified after construction
  • Query string parsing - provides path without query parameters via getPath()
  • Host header extraction - handles both hostname and hostname:port formats
  • Body caching - reads body once and caches for repeated access
  • Header storage - maintains all headers with support for multi-value headers

Thread-safety: This class is immutable and therefore thread-safe. Multiple threads can safely read from the same HTTPRequest instance concurrently.

Example usage:

HTTPRequest request = new HTTPRequest(
    HTTPMethode.GET,
    "/api/users?limit=10",
    "HTTP/1.1",
    headers,
    bodyStream,
    "example.com:8080"
);
String path = request.getPath(); // Returns "/api/users" (without query string)
String host = request.getHost(); // Returns "example.com" (without port)
See Also:
  • Constructor Details

    • HTTPRequest

      public HTTPRequest(HTTPMethode method, String path, String httpVersion, Map<String,String[]> headers, InputStream body, String host)
      Constructs an HTTPRequest object with the specified parameters.

      This constructor eagerly reads the entire request body from the provided InputStream and stores it as a byte array. This ensures the body is available for multiple reads and that the input stream can be closed immediately after construction.

      The constructor performs the following operations:

      1. Stores all request line components (method, path, version)
      2. Stores the headers map (reference, not copied)
      3. Reads the complete body from the input stream via readBody(InputStream)
      4. Stores the host header value for virtual host resolution
      Parameters:
      method - The HTTP method (e.g., GET, POST, PUT, DELETE). Must not be null.
      path - The request path, including the query string if present. Must not be null.
      httpVersion - The HTTP version (e.g., "HTTP/1.1"). Must not be null.
      headers - A map of HTTP headers, where the key is the header name and the value is an array of header values. Must not be null, but may be empty.
      body - The InputStream representing the body of the request. May be null for requests without a body.
      host - The Host header value, potentially including port (e.g., "example.com:8080"). Must not be null.
      Throws:
      UncheckedIOException - if an IOException occurs while reading the request body.
  • Method Details

    • getMethod

      public HTTPMethode getMethod()
      Gets the HTTP method of the request.
      Returns:
      The HTTP method (e.g., GET, POST, PUT, DELETE). Never null.
    • getPath

      public String getPath()
      Gets the path of the request, excluding the query string if present.

      This method extracts the path portion of the request URI, removing any query string parameters. The query string is identified by the first occurrence of the '?' character.

      Examples:

      • "/api/users?limit=10" returns "/api/users"
      • "/api/users" returns "/api/users"
      • "/search?q=hello&page=2" returns "/search"
      Returns:
      The path of the request without query parameters. Never null.
    • getBody

      public String getBody()
      Gets the body of the request as a string using the default character encoding.

      This method converts the stored byte array body to a String using the platform's default character encoding. For requests without a body, this returns an empty string.

      Note: For better control over character encoding, consider using the Content-Type header's charset parameter to determine the appropriate encoding, especially for non-ASCII text.

      Returns:
      The body of the request as a String. Never null, but may be empty.
    • getRequestHead

      public String getRequestHead()
      Gets the request head string, which includes the HTTP method and full path.

      This method returns a string representation of the request line's method and path components, formatted as "{METHOD} {PATH}". The path includes any query string if present.

      This is commonly used for logging and debugging purposes to get a concise representation of what resource is being requested and how.

      Examples:

      • "GET /api/users"
      • "POST /api/login"
      • "GET /search?q=hello"
      Returns:
      The request head string in the format "{METHOD} {PATH}". Never null.
    • getHost

      public String getHost()
      Gets the hostname from the Host header, excluding the port number if present.

      This method extracts the hostname portion from the Host header value, removing any port number specification. The port is identified by the first colon (':') character in the host string.

      This is particularly useful for virtual host resolution, where the hostname alone is needed to select the appropriate virtual host configuration, regardless of the port on which the request was received.

      Examples:

      • "example.com:8080" → "example.com"
      • "example.com" → "example.com"
      • "localhost:3000" → "localhost"
      • "192.168.1.1:8080" → "192.168.1.1"
      Returns:
      The hostname without the port number. Never null.
    • toString

      public String toString()
      Returns a string representation of the HTTP request, including the method, path, version, headers, and body.

      This method constructs a detailed string representation of the complete HTTP request in a format similar to the actual HTTP protocol format. This is useful for debugging, logging, and understanding the exact contents of a request.

      The format follows the HTTP message structure:

      {METHOD} {PATH} {VERSION}
      {Header-Name}: {Header-Value(s)}
      ...
      
      {Body}
      

      For headers with multiple values, the values are joined with ", " (comma-space).

      Example output:

      GET /api/users HTTP/1.1
      Host: example.com
      Accept: application/json
      Authorization: Bearer token123
      
      
      Overrides:
      toString in class Object
      Returns:
      A string representation of the HTTP request. Never null.