Zero-Copy Protocol Parser
The HTTP/1.1 protocol parser in src/proto/parser.zig is designed for high throughput, zero heap allocation, and strict RFC 9112 conformance.
1. Zero-Copy In-Place Slicing
Traditional parsers copy strings into newly allocated buffers for method names, header keys, and values.
Lighting takes a different approach:
- The connection read buffer (
conn_buf) receives raw socket data. - As
parseRequestHeadvalidates HTTP tokens, it produces slices[]const u8pointing directly intoconn_buf. - Zero heap memory is requested from the OS or GPA during request line and header parsing.
2. Request Smuggling Defenses (RFC 9112 Section 6.3)
HTTP request smuggling occurs when upstream proxies and downstream servers disagree on request body boundaries. Lighting enforces strict RFC 9112 defenses:
- Both
Transfer-EncodingandContent-LengthPresent:- RFC 9112 §6.3 mandates that if a request contains both headers,
Transfer-Encodingtakes precedence. - To prevent desynchronization attacks against naive proxies, Lighting strips the
Content-Lengthheader entirely whenchunkedis detected.
- RFC 9112 §6.3 mandates that if a request contains both headers,
- Invalid
Transfer-Encoding:- If
Transfer-Encodingcontains an unsupported coding (e.g.gzipas transfer coding), Lighting immediately rejects the request with501 Not Implemented.
- If
- Invalid
Content-Length:- If
Content-Lengthcontains non-numeric characters, multiple values, or exceeds max integer bounds, Lighting immediately returns400 Bad Request.
- If
3. Pipelining & CRLF Boundary Detection
HTTP/1.1 pipelining allows clients to send multiple requests in a single TCP stream without waiting for responses.
- After a request finishes, leftover bytes in
conn_bufare scanned for the\r\n\r\nboundary. - If a full header frame is already present in the buffer, Lighting parses it immediately without incurring an unnecessary
recvsyscall. - If a partial frame is present, remaining bytes are shifted forward to the buffer head via
std.mem.copyForwardsbefore the next socket read.