package http-multipart-formdata

  1. Overview
  2. Docs
Http multipart/formdata parser

Install

Dune Dependency

Authors

Maintainers

Sources

http-multipart-formdata-v1.0.0.tbz
sha256=73e43c4e31560499f6998ebddd117f76b5e9cfe5edafa390f4a088e1f4fa7fa2
sha512=fc24b6238406aba0a452262ee7879e58f2fc307341dad43baae61a39e5eae0f4ce54913a06d0d26af871267827f95e7ed4281d38f6b50177fdf91b6ee6828118

doc/http-multipart-formdata/Http_multipart_formdata/index.html

Module Http_multipart_formdataSource

Types

Sourcemodule Map : Map.S with type key = string

An ocaml Stdlib Map with string as key.

Sourcemodule Part : sig ... end

Represents a parsed multipart part. A part corresponds to a submitted form field data in a HTTP request.

Sourcetype t = Part.t list Map.t

Represents a parsed HTTP multipart/form-data request as a key/value map. Submitted form field name is the key value.

A key may be associated in zero or more values.

Sourceexception Multipart of string

Represents error while parsing http multipart formdata.

Parse

Sourceval parse : content_type_header:string -> body:string -> t

parse ~content_type_header ~body returns a parsed HTTP multiparts such that it can be queried using ocaml Stdlib.Map functions.

content_type_header is the HTTP request Content-Type header. Note the value contains both the header name and value. It is used to parse a boundary value.

body is the raw HTTP POST request body content.

Examples

  module M = Http_multipart_formdata

  ;;
  let content_type_header =
    "Content-Type: multipart/form-data; \
     boundary=---------------------------735323031399963166993862150"
  in
  let body =
    [ {||}
    ; {|-----------------------------735323031399963166993862150|}
    ; {|Content-Disposition: form-data; name="text1"|}
    ; {||}
    ; {|text default|}
    ; {|-----------------------------735323031399963166993862150|}
    ; {|Content-Disposition: form-data; name="text2"|}
    ; {||}
    ; {|aωb|}
    ; {|-----------------------------735323031399963166993862150|}
    ; {|Content-Disposition: form-data; name="file1"; filename="a.txt"|}
    ; {|Content-Type: text/plain|}
    ; {||}
    ; {|Content of a.txt.|}
    ; {||}
    ; {|-----------------------------735323031399963166993862150|}
    ; {|Content-Disposition: form-data; name="file2"; filename="a.html"|}
    ; {|Content-Type: text/html|}
    ; {||}
    ; {|<!DOCTYPE html><title>Content of a.html.</title>|}
    ; {||}
    ; {|-----------------------------735323031399963166993862150|}
    ; {|Content-Disposition: form-data; name="file3"; filename="binary"|}
    ; {|Content-Type: application/octet-stream|}
    ; {||}
    ; {|aωb|}
    ; {|-----------------------------735323031399963166993862150--|}
    ]
    |> String.concat "\r\n"
  in
  let mp = M.parse ~content_type_header ~body in
  let file1_1 = M.Map.find "file1" mp in
  let file1_2 =
    [ { M.Part.body = Bytes.of_string "\r\nContent of a.txt.\r\n\r\n"
      ; name = "file1"
      ; content_type = "text/plain"
      ; filename = Some "a.txt"
      ; parameters = M.Map.empty
      }
    ]
  in
  M.equal_parts file1_1 file1_2

Pretty Printers

Sourceval pp_parts : Format.formatter -> Part.t list -> unit

pp_parts fmt parts pretty prints a list of Part.t

Sourceval pp : Format.formatter -> t -> unit

pp fmt part pretty prints a part.

Equals

Sourceval equal_parts : Part.t list -> Part.t list -> bool

equal_parts parts1 parts2 returns true if parts1 and parts2 are equal, false otherwise.

Sourceval equal : t -> t -> bool

equal t1 t2 returns true if Part. t1 and t2 are equal, false otherwise.

OCaml

Innovation. Community. Security.