HTTP request parsing turns an incoming request string into an HttpRequest value containing a method, resource path, version, headers, and message body. A Rust server models these parts with enums, a struct, and a HashMap. section 2.3.1
A compact implementation builds the parser in layers:
- Convert GET and POST strings into Method variants, defaulting unknown methods to Uninitialized. section 2.3.1
- Convert HTTP/1.1 into Version::V1_1 and retain an Uninitialized variant for unsupported versions. section 2.3.1
- Split the request line on whitespace to extract method, resource path, and version; split header lines at the colon separator. section 2.3.1
- Implement From<String> for HttpRequest so the parser can convert a complete request into the server’s data structure. section 2.3.1
Testing a parsed request
Feed the parser a GET /greeting HTTP/1.1 request with Host, User-Agent, and Accept headers, then assert the method, version, and resource path. Run cargo test -p http and verify all three tests pass. section 2.3.1
Table 2.1 The data structures for incoming HTTP requests (view table figure)
Browse the request data structures and their roles before following the parsing implementation. section 2.3.1
