package http-multipart-formdata
Module Http_multipart_formdataSource
Http_multipart_formdata is a non-blocking, streaming HTTP multipart/formdata parser. Its design is based on two main ideas:
- The parser should stream the results as soon as possible in a non-buffered, non-backtracking manner; and
- The parser input must be non-blocking and incremental in nature.
The parser implements HTTP multipart/form-data standard as defined in RFC 7578.
Types
reader represents a HTTP multipart formdata reader.
and read = [ | `End(*The reader has completed reading.
*)| `Header of part_header(*Multipart part header data.
*)| `Body of Cstruct.t(*Multipart part body data.
*)| `Body_end(*
*)readerhas completed reading the Multipart body data.| `Awaiting_input of [ `Cstruct of Cstruct.t | `Eof ] -> read(*The reader is waiting for it to be provided with input data. This is only returned when
*)`Incrementalis chosen asinput.| `Error of string
]read represents both the current state and data read by a reader.
Represents a parsed multipart part header data.
Represents the multipart boundary value.
A form field name
A Multipart body
Mulipart Boundary parser
boundary content_type parses content_type to extract boundary value. content_type is the HTTP request Content-Type header value.
let content_type =
"multipart/form-data; \
boundary=---------------------------735323031399963166993862150"
in
Http_multipart_formdata.boundary content_typeStreaming Multipart
API to stream multipart parts. Use these functions when you have to handle HTTP form submissions which has large file uploads and at the same time be memory efficient.
reader ?read_buffer_size boundary input creates reader. The default value for read_buffer_size is 1KB.
unconsumed reader returns any leftover data still remaining after reader returns `End.
Non-Streaming Multipart
Use these functions if the HTTP form submission is of a relatively small size.
val parts :
boundary ->
string ->
((field_name * (part_header * part_body)) list, string) resultparts boundary http_body returns a list of HTTP multipart parts parsed in http_body.
The returned parts list is keyed to a form field name so that one can do:
let parts_kv = parts boundary http_body in
match List.assoc_opt "field1" parts_vk with
| Some v -> ...
| None -> ..Part header
name t returns the form field name.
content_type t returns the part content-type.
filename t returns the uploaded filename if the multipart is a file.
find name t returns the multipart parameter value associated with name.