Class HTTPRequestParser

java.lang.Object
com.ns.tcpframework.HTTPRequestParser

public class HTTPRequestParser extends Object
Utility class for parsing raw HTTP requests from socket input streams.

This parser implements HTTP/1.1 request parsing according to RFC 7230 and RFC 7231. It reads raw bytes from a socket, separates headers from the body, and constructs a structured HTTPRequest object containing all request components.

The parser handles:

  • Request line parsing (method, path, HTTP version)
  • Header parsing with support for multi-value headers
  • Body stream creation with Content-Length based limitation
  • Host header extraction for virtual host resolution
  • Header name normalization (conversion to lowercase)
  • Multi-value header merging (comma-separated values)

Parsing process:

  1. Read HTTP headers until the double CRLF (\\r\\n\\r\\n) sequence is encountered
  2. Parse the request line to extract method, path, and HTTP version
  3. Parse individual header lines into a map structure
  4. Create a body stream limited by the Content-Length header
  5. Construct and return the complete HTTPRequest object

Error handling: The parser throws specific exceptions for various error conditions:

Thread-safety: This class is stateless with only static methods, making it inherently thread-safe for concurrent use across multiple request processing threads.

See Also:
  • Constructor Details

    • HTTPRequestParser

      public HTTPRequestParser()
  • Method Details

    • parseHTTPRequest

      public static HTTPRequest parseHTTPRequest(Socket socket) throws Exception
      Parses a complete HTTP request from the provided socket connection.

      This is the main entry point for HTTP request parsing. It orchestrates the complete parsing process by reading the raw request from the socket, extracting all components, and constructing a fully populated HTTPRequest object.

      The method performs the following operations in sequence:

      1. Obtains the raw input stream from the socket
      2. Reads and extracts the HTTP header section (up to \\r\\n\\r\\n)
      3. Parses the request line (method, path, version)
      4. Parses all HTTP headers into a map structure
      5. Creates a body stream limited by Content-Length (0 if not specified)
      6. Extracts the Host header for virtual host resolution
      7. Constructs and returns the HTTPRequest object

      Host header handling: If the Host header is missing, "404" is used as a placeholder value, which typically results in a 404 response during virtual host resolution.

      Parameters:
      socket - The socket connection from which to read the HTTP request. Must not be null and should have an open input stream.
      Returns:
      A fully constructed HTTPRequest object containing all parsed components.
      Throws:
      BadRequestException - If the request is empty, malformed, or missing required components.
      NotImplementedException - If the HTTP method is not supported by the server.
      IOException - If an I/O error occurs while reading from the socket.
      Exception - If any other error occurs during parsing (e.g., number format for Content-Length).