Skip to main content

Zero-allocation HTTP/1.1 for OxCaml

High-performance HTTP parser achieving true zero heap allocations using OxCaml’s unboxed types and stack allocation. Parse at 6.5M requests/sec with RFC 7230 security compliance.

Quick start

Get up and running with httpz in minutes

1

Install OxCaml compiler

httpz requires the OxCaml compiler with support for unboxed types. Install from oxcaml.org.
# Verify you have OCaml 5.2+ with OxCaml support
ocaml --version
2

Add httpz to your project

Install httpz via opam:
opam install httpz
Add to your dune file:
(executable
 (name my_server)
 (libraries httpz async))
3

Parse your first request

Create a simple parser that achieves zero allocations:
open Httpz

let parse_request socket_data =
  let buf = Httpz.create_buffer () in
  (* Read data into buffer *)
  let len = read_socket_into buf socket_data in
  
  (* Parse with zero heap allocations *)
  let #(status, req, headers) = 
    Httpz.parse buf ~len ~limits:Httpz.default_limits 
  in
  
  match status with
  | Buf_read.Complete ->
    (* All values are stack-allocated *)
    Printf.printf "Method: %s\n" (Method.to_string req.#meth);
    Printf.printf "Target: %s\n" (Span.to_string buf req.#target);
    List.iter (fun hdr ->
      Printf.printf "Header: %s\n" 
        (Header_name.canonical hdr.Header.name)
    ) headers
  | _ -> 
    Printf.eprintf "Parse failed\n"
The #(...) syntax creates an unboxed tuple, and req.#field accesses unboxed record fields. Everything stays on the stack!
4

Try the static file server

httpz includes a production-ready static file server:
# Serve current directory on port 8080
dune exec bin/httpz_server.exe

# Custom directory and port
dune exec bin/httpz_server.exe -- -d /var/www -p 3000
The server handles 10,000 concurrent connections with zero-copy I/O and automatic MIME type detection.

Key features

Built for performance with OxCaml’s advanced type system

True zero allocations

Parser results are entirely stack-allocated using unboxed records and local lists. No GC pressure.

Unboxed types throughout

Uses int16#, int64#, and char# for offsets and lengths. No boxing overhead.

RFC 7230 security

Prevents request smuggling with bare CR detection and ambiguous framing checks.

Complete HTTP/1.1 support

Chunked encoding, ETags, range requests, keep-alive, and conditional requests.

6.5M requests/sec

Benchmarked at 154ns for minimal requests with 45x fewer allocations than alternatives.

Direct bigstring I/O

Read and write directly to bigarrays for zero-copy integration with Async.

Ready to build high-performance HTTP servers?

Start using httpz to parse HTTP/1.1 requests with zero allocations and maximum throughput.

Get Started Now