Hcs.Multipart

hcs · API reference

Multipart form-data parsing.

Use this module for file uploads and form submissions encoded as multipart/form-data. It consumes request body streams and returns parsed fields/files according to configured limits.

Multipart form data parsing.

The parser keeps at most one input chunk plus a boundary-sized suffix while streaming a body. Mutable scanning state is confined to parser; callers only observe immutable part metadata and pull streams.

module Hmf = Http_multipart_formdata
type part = {
  name : string;
  filename : string option;
  content_type : string;
  data : string;
}
type error = 
  | Missing_content_type
  | Not_multipart
  | Missing_boundary
  | Invalid_boundary of string
  | Parse_error of string
val error_to_string : error -> string
val is_multipart : Request.t -> bool
val has_boundary_parameter : string -> bool
val boundary : Request.t -> (Hmf.boundary, error) result
val find_part : string -> part list -> part option
val find_file : string -> part list -> part option
val to_assoc : part list -> (string * part) list
type stream_part = {
  name : string;
  filename : string option;
  content_type : string;
  body : Cstruct.t Stream.Async.t;
}
type phase = 
  | Preamble
  | Header
  | Body
  | End
  | Failed
type parser = {
  hmf_boundary : Hmf.boundary;
  opening_boundary : string;
  body_boundary : string;
  body_stream : unit -> Cstruct.t option;
  mutable pending : string;
  mutable input_ended : bool;
  mutable phase : phase;
  mutable active_body : bool;
  mutable failure : string option;
}
val fail : parser -> string -> unit
val append_input : parser -> unit
val drop_prefix : parser -> int -> unit
val substring_at : string -> string -> int -> bool
type boundary_match = 
  | Found of int * bool
  | Need_more
  | Not_found
val find_boundary : string -> string -> boundary_match
val seek_opening_boundary : parser -> unit
val find_substring : string -> string -> int option
val read_header_block : parser -> string option
val parse_header : 
  parser ->
  string ->
  (string * string option * string, string) result
val finish_boundary : parser -> bool -> unit
val make_body_stream : parser -> Cstruct.t Stream.Async.t
val create_parser : Request.t -> (parser, error) result
val next_part : parser -> stream_part option
val parser_result : parser -> 'a -> ('a, error) result
val iter_parts : (stream_part -> 'a) -> parser -> (unit, error) result
val fold_parts : 
  (stream_part -> 'a -> 'a) ->
  'a ->
  parser ->
  ('a, error) result
val collect_body : stream_part -> string
val parse : Request.t -> (part list, error) result
val parse_assoc : Request.t -> ((string * part) list, error) result