Creating an HTTP/2 frame parser using Eio.Buf_read in OCaml involves understanding both the structure of HTTP/2 frames and how to use Eio's Buf_read module for parsing binary data from a flow (e.g., a socket).
⚠️ This is a simplified implementation that focuses on reading and parsing HTTP/2 frames. It assumes you're working with a properly established HTTP/2 connection (i.e., preface handled, TLS/NPN negotiated, etc.). We will focus only on reading frames from the stream.
🔧 Prerequisites
Make sure you have the following packages installed:
opam install eio
We'll be using:
Eio.Buf_read: for buffered reading of binary data.
Eio.Net.Sockaddr.Stream_flow: for reading/writing over a socket-like flow.
📦 HTTP/2 Frame Structure (RFC 7540)
Each HTTP/2 frame has the following layout:
| Length (24 bits) | Type (8 bits) | Flags (8 bits) | Reserved (1 bit) | Stream Identifier (31 bits) | Frame Payload (variable) |
Total: 9 bytes header + payload.
🧱 Step-by-step Plan
- Read 9 bytes to parse the frame header.
- Extract length, type, flags, and stream ID.
- Read the remaining payload based on the length field.
- Return a parsed
Http2_frame.t type.
📄 Code Implementation
Here’s a complete example:
open Eio.Std
module Http2_frame = struct
type t = {
length : int;
typ : int;
flags : int;
stream_id : int32;
payload : Cstruct.t;
}
let read_from_flow flow =
let open Buf_read.Infix in
let reader = Buf_read.create ~capacity:65536 () in
let rec read_exact n cs =
if Cstruct.length cs < n then
match Flow.single_read flow with
| `Eof -> Error "Unexpected EOF"
| `Ok b ->
Buf_read.add_string reader (Cstruct.to_string b);
read_exact n (Cstruct.append cs b)
else Ok (Cstruct.sub cs 0 n)
in
let read_header () =
let buf = Cstruct.create 9 in
match read_exact 9 buf with
| Error _ as e -> e
| Ok h ->
let length =
let b0 = Char.code (Cstruct.get_char h 0) in
let b1 = Char.code (Cstruct.get_char h 1) in
let b2 = Char.code (Cstruct.get_char h 2) in
(b0 lsl 16) lor (b1 lsl 8) lor b2
in
let typ = Char.code (Cstruct.get_char h 3) in
let flags = Char.code (Cstruct.get_char h 4) in
let stream_id =
let b0 = Char.code (Cstruct.get_char h 5) in
let b1 = Char.code (Cstruct.get_char h 6) in
let b2 = Char.code (Cstruct.get_char h 7) in
let b3 = Char.code (Cstruct.get_char h 8) in
Int32.of_int ((b0 lsl 24) lor (b1 lsl 16) lor (b2 lsl 8) lor b3)
in
Ok (length, typ, flags, stream_id)
in
match read_header () with
| Error _ as e -> e
| Ok (length, typ, flags, stream_id) ->
let payload_len = length in
let payload = Cstruct.create payload_len in
read_exact payload_len payload
|> Result.map (fun p ->
{ length; typ; flags; stream_id; payload = p })
end
(* Example usage *)
let run_http2_parser (flow : Eio.Net.stream_socket) =
match Http2_frame.read_from_flow flow with
| Error err -> traceln "Error parsing HTTP/2 frame: %s" err
| Ok frame ->
traceln "Frame read:";
traceln " Length: %d" frame.Http2_frame.length;
traceln " Type: 0x%x" frame.Http2_frame.typ;
traceln " Flags: 0x%x" frame.Http2_frame.flags;
traceln " Stream ID: %ld" frame.Http2_frame.stream_id;
traceln " Payload size: %d bytes" (Cstruct.length frame.Http2_frame.payload)
🧪 Notes
- The code reads exactly one HTTP/2 frame per call.
- You’ll want to loop this in production code to process multiple frames.
- For real-world use, consider integrating with h2, which already implements HTTP/2 in OCaml.
🧩 Integration with Eio
To integrate with Eio's runtime:
let () =
Eio_main.run @@ fun env ->
let ipv4 = Eio.Net.Ipaddr.V4 (Ipaddr.V4.of_string_exn "127.0.0.1") in
let flow = Eio.Net.open_tcp流 env.net ~read_buffer_size:(Some 65536) (ipv4, 8080) in
run_http2_parser flow
Replace "127.0.0.1" and 8080 with your actual server address and port.
📌 Summary
This implementation demonstrates:
- Reading binary data using
Eio.Buf_read
- Parsing the fixed-size HTTP/2 frame header
- Handling variable-length payloads
- Returning a structured
Http2_frame.t value
Let me know if you'd like to extend this to support specific frame types (like HEADERS, DATA, or SETTINGS) or implement writing frames too!