Class HTTPRequestParser
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:
- Read HTTP headers until the double CRLF (\\r\\n\\r\\n) sequence is encountered
- Parse the request line to extract method, path, and HTTP version
- Parse individual header lines into a map structure
- Create a body stream limited by the Content-Length header
- Construct and return the complete HTTPRequest object
Error handling: The parser throws specific exceptions for various error conditions:
BadRequestException- Malformed requests, missing required componentsNotImplementedException- Unsupported HTTP methodsIOException- Network I/O errors
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic HTTPRequestparseHTTPRequest(Socket socket) Parses a complete HTTP request from the provided socket connection.
-
Constructor Details
-
HTTPRequestParser
public HTTPRequestParser()
-
-
Method Details
-
parseHTTPRequest
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
HTTPRequestobject.The method performs the following operations in sequence:
- Obtains the raw input stream from the socket
- Reads and extracts the HTTP header section (up to \\r\\n\\r\\n)
- Parses the request line (method, path, version)
- Parses all HTTP headers into a map structure
- Creates a body stream limited by Content-Length (0 if not specified)
- Extracts the Host header for virtual host resolution
- 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
HTTPRequestobject 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).
-