Httpz_server.RouteSegment-based HTTP routing.
Provides:
Segment-based HTTP routing with Base_trie.
Routes match against path segments (string lists) rather than raw bytes. Response callbacks use local headers for stack allocation.
open Httpz_server.Route
let routes = of_list [
get_ [] (fun _ctx respond -> html respond "Welcome!");
get_ ["api"; "status"] (fun _ctx respond -> json respond {|{"ok":true}|});
get ("users" / seg End) (fun (user_id, ()) _ctx respond ->
html respond (sprintf "User: %s" user_id));
get ("static" / tail) (fun path _ctx respond ->
serve_file (String.concat "/" path) respond);
] "users" / seg End (* /users/:id *)
"api" / "v1" / seg End (* /api/v1/:resource *)
"static" / tail (* /static/** *)
root (* / *)type body = | Empty| String of string| Bigstring of {buf : Base_bigstring.t;off : int;len : int;}| Stream of {}type resp_header = Httpz.Header_name.t * stringtype respond =
status:Httpz.Res.status ->
headers:resp_header list @ local ->
(body ->
unit) @ localResponse writer. Headers are local_ for stack allocation.
All helpers take local_ respond to enable stack-allocated closures.
val html : respond @ local -> (string -> unit) @ localval json : respond @ local -> (string -> unit) @ localval xml : respond @ local -> (string -> unit) @ localval atom : respond @ local -> (string -> unit) @ localval plain : respond @ local -> (string -> unit) @ localval redirect :
respond @ local ->
(status:Httpz.Res.status ->
(location:string ->
unit) @ local) @ localval not_found : respond @ local -> unitval respond_string :
respond @ local ->
(status:Httpz.Res.status ->
(?headers:resp_header list @ local ->
(string ->
unit) @ local) @ local) @ localval stream :
respond @ local ->
(status:Httpz.Res.status ->
(?headers:resp_header list @ local ->
(?length:int ->
(((string -> unit) -> unit) ->
unit) @ local) @ local) @ local) @ localval meth : ctx -> Httpz.Method.tGet the HTTP method of the request.
val is_head : ctx -> boolReturns true if this is a HEAD request. Use this to skip expensive body generation - the routing layer automatically matches HEAD requests to GET routes.
val path : ctx -> stringGet request path as string (e.g., "/users/123").
val query_param : ctx -> string -> string optionFind first query parameter by name.
val query_params : ctx -> string -> string listFind all query parameters by name.
val query : ctx -> (string * string) listGet all query parameters.
val body : ctx -> Httpz.Span.tbody ctx returns the request body as an unboxed span into the parse buffer. Zero-copy — no allocation until you call body_string. Returns a span with len = 0 for no-body requests. Returns a span with len = -1 for chunked encoding.
val body_string : ctx -> string optionbody_string ctx materializes the body as a string, or None if empty.
val content_length : ctx -> int64content_length ctx returns the Content-Length value, or -1L if absent.
val xml_multistatus : respond @ local -> (string -> unit) @ localxml_multistatus respond xml sends a 207 Multi-Status response with Content-Type: application/xml.
These variants skip body generation for HEAD requests. The thunk is only called for non-HEAD requests. Use these when body generation is expensive.
val root : unit patMatch root path /.
val tail : string list patCapture all remaining segments.
val h0 : unit hdrval (+>) : Httpz.Header_name.t -> 'a hdr -> (string option * 'a) hdrHandler function with local respond for stack allocation.
val get_h1 :
'a pat ->
Httpz.Header_name.t ->
('a -> string option -> ctx -> respond @ local -> unit) ->
routeval post_h1 :
'a pat ->
Httpz.Header_name.t ->
('a -> string option -> ctx -> respond @ local -> unit) ->
routeval route : Httpz.Method.t -> 'a pat -> 'h hdr -> ('a, 'h) handler -> routeval empty : tval dispatch :
bytes ->
meth:Httpz.Method.t ->
target:Httpz.Target.t ->
body:Httpz.Span.t ->
content_length:int64 ->
headers:Httpz.Header.t list @ local ->
(t ->
(respond:respond @ local ->
bool) @ local) @ localdispatch buf ~meth ~target ~body ~content_length ~headers routes ~respond dispatches a request. Returns true if a route matched. body is an unboxed span referencing the request body in buf (zero-copy). content_length is the Content-Length value or -1L if absent.