···94949595# Software engineering
96969797-We will go through a multi step process to build this library. We are currently at STEP 2.
9797+We will go through a multi step process to build this library.
989899991) we will generate OCaml interface files only, and no module implementations. The purpose here is to write and document the necessary type signatures. Once we generate these, we can check that they work with "dune build @check". Once that succeeds, we will build HTML documentation with "dune build @doc" in order to ensure the interfaces are reasonable.
100100
+9
lib/fastcgi.mli
···11+(** FastCGI protocol implementation for OCaml using Eio.
22+33+ This library provides a complete implementation of the FastCGI protocol
44+ for building high-performance web applications in OCaml. *)
55+66+(** {1 Core Protocol Components} *)
77+88+(** Record-level protocol handling *)
99+module Record = Fastcgi_record
+123
lib/fastcgi_record.mli
···11+(** FastCGI record handling for parsing and creating protocol messages.
22+33+ This module provides the core functionality for handling FastCGI records,
44+ which are the fundamental units of communication in the FastCGI protocol.
55+ Records multiplex multiple requests over a single connection and provide
66+ independent data streams within each request. *)
77+88+(** {1 Record Structure} *)
99+1010+(** FastCGI protocol version *)
1111+type version = int
1212+1313+(** Record types define the purpose and interpretation of record content *)
1414+type record =
1515+ | Begin_request (** Start a new FastCGI request *)
1616+ | Abort_request (** Abort an existing request *)
1717+ | End_request (** Terminate a request *)
1818+ | Params (** Name-value pairs (environment variables) *)
1919+ | Stdin (** Standard input data stream *)
2020+ | Stdout (** Standard output data stream *)
2121+ | Stderr (** Standard error data stream *)
2222+ | Data (** Additional data stream for Filter role *)
2323+ | Get_values (** Query application capabilities *)
2424+ | Get_values_result (** Response to capability query *)
2525+ | Unknown_type (** Unknown record type response *)
2626+2727+(** [record_type_to_int rt] converts record type to its protocol integer value *)
2828+val record_to_int : record -> int
2929+3030+(** [record_type_of_int i] converts protocol integer to record type.
3131+ Raises Invalid_argument for unknown values. *)
3232+val record_of_int : int -> record
3333+3434+(** [pp_record ppf rt] pretty-prints a record type *)
3535+val pp_record : Format.formatter -> record -> unit
3636+3737+(** Request identifier for multiplexing multiple requests over one connection.
3838+ Request ID 0 is reserved for management records. *)
3939+type request_id = int
4040+4141+(** A complete FastCGI record containing header and content.
4242+ Records consist of an 8-byte fixed header followed by variable-length
4343+ content and optional padding for alignment. *)
4444+type t = {
4545+ version : version; (** Protocol version (always 1) *)
4646+ record_type : record; (** Type of this record *)
4747+ request_id : request_id; (** Request identifier *)
4848+ content : string; (** Record content data *)
4949+}
5050+5151+(** [pp ppf record] pretty-prints a FastCGI record *)
5252+val pp : Format.formatter -> t -> unit
5353+5454+(** {1 Record Operations} *)
5555+5656+(** [read buf_read] reads a complete FastCGI record from the input buffer.
5757+ Returns the parsed record or raises an exception if the record is malformed
5858+ or if there's insufficient data. The padding is automatically handled and
5959+ discarded during parsing. *)
6060+val read : Eio.Buf_read.t -> t
6161+6262+(** [write buf_write record] writes a FastCGI record to the output buffer.
6363+ The record header is automatically constructed from the record fields,
6464+ and appropriate padding is added to align the record on 8-byte boundaries
6565+ for optimal performance. *)
6666+val write : Eio.Buf_write.t -> t -> unit
6767+6868+(** [create ~version ~record ~request_id ~content] creates a new record
6969+ with the specified parameters. The content length is automatically
7070+ calculated from the content string. *)
7171+val create : version:version -> record:record ->
7272+ request_id:request_id -> content:string -> t
7373+7474+(** {1 Key-Value Pairs} *)
7575+7676+(** Key-value pairs are used to transmit environment variables and other
7777+ key-value data in FCGI_PARAMS and other record types. The FastCGI protocol
7878+ uses a specific encoding where lengths are variable-width (1 or 4 bytes). *)
7979+8080+module KV : sig
8181+ (** Type for key-value pair collections *)
8282+ type t
8383+8484+ (** [empty] creates an empty key-value pair collection *)
8585+ val empty : t
8686+8787+ (** [add name value pairs] adds a key-value pair to the collection *)
8888+ val add : string -> string -> t -> t
8989+9090+ (** [remove name pairs] removes all pairs with the given key *)
9191+ val remove : string -> t -> t
9292+9393+ (** [find name pairs] returns the value associated with the given key.
9494+ Raises Not_found if the key is not present. *)
9595+ val find : string -> t -> string
9696+9797+ (** [find_opt name pairs] returns Some value or None if not found *)
9898+ val find_opt : string -> t -> string option
9999+100100+ (** [to_seq pairs] converts to a sequence of (key, value) tuples *)
101101+ val to_seq : t -> (string * string) Seq.t
102102+103103+ (** [of_seq pairs] creates from a sequence of (key, value) tuples *)
104104+ val of_seq : (string * string) Seq.t -> t
105105+106106+ (** [read buf_read] reads key-value pairs from a buffer.
107107+ Handles the FastCGI variable-length encoding where lengths ≤ 127 bytes
108108+ use 1 byte, longer lengths use 4 bytes with the high bit set. *)
109109+ val read : Eio.Buf_read.t -> t
110110+111111+ (** [write buf_write pairs] writes key-value pairs to a buffer using
112112+ the FastCGI encoding format *)
113113+ val write : Eio.Buf_write.t -> t -> unit
114114+115115+ (** [encode pairs] returns the encoded byte string representation *)
116116+ val encode : t -> string
117117+118118+ (** [decode content] parses encoded key-value pairs from a string *)
119119+ val decode : string -> t
120120+121121+ (** [pp ppf pairs] pretty-prints key-value pairs *)
122122+ val pp : Format.formatter -> t -> unit
123123+end