package piaf

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file multipart.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
(*----------------------------------------------------------------------------
 * Copyright (c) 2020, António Nuno Monteiro
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *---------------------------------------------------------------------------*)

open Lwt.Infix

module List = struct
  include List

  let concat_map f l =
    let rec aux f acc = function
      | [] ->
        rev acc
      | x :: l ->
        let xs = f x in
        aux f (rev_append xs acc) l
    in
    aux f [] l
end

module Pp = struct
  let pp_extension formatter t =
    let payload = match t with `Ietf_token x | `X_token x -> x in
    Format.fprintf formatter "%s" payload

  let pp_ty = Multipart_form.Content_type.Type.pp

  let pp_subty formatter t =
    let payload =
      match t with `Ietf_token x | `Iana_token x | `X_token x -> x
    in
    Format.fprintf formatter "%s" payload

  let pp_content_type formatter { Multipart_form.Content_type.ty; subty; _ } =
    Format.fprintf formatter "%a/%a" pp_ty ty pp_subty subty

  let pp_value formatter t =
    let ty, payload =
      match t with
      | Multipart_form.Content_type.Parameters.String x ->
        "String", x
      | Token x ->
        "Token", x
    in
    Format.fprintf formatter "%s: %s" ty payload

  let pp_disposition_type formatter t =
    let ty =
      match t with
      | `Inline ->
        "inline"
      | `Attachment ->
        "attachment"
      | (`Ietf_token _ | `X_token _) as ext ->
        Format.asprintf "%a" pp_extension ext
    in
    Format.fprintf formatter "%s" ty

  let pp_unstructured formatter t =
    let pp_one formatter (t : Unstrctrd.elt) =
      let s =
        match t with
        | `Uchar u ->
          Format.asprintf "Uchar: %c" (Uchar.to_char u)
        | `CR ->
          "CR"
        | `LF ->
          "LF"
        | `WSP s ->
          Format.asprintf "WSP: %s" (s :> string)
        | `FWS wsp ->
          Format.asprintf "FWS: %s" (wsp :> string)
        | `d0 ->
          "d0"
        | `OBS_NO_WS_CTL obs ->
          Format.asprintf "OB_NO_WS_CTL: %c" (obs :> char)
        | `Invalid_char invalid_char ->
          Format.asprintf "Invalid_char: %c" (invalid_char :> char)
      in
      Format.fprintf formatter "%s" s
    in
    Format.pp_print_list
      ~pp_sep:(fun fmt () -> Format.fprintf fmt "; ")
      pp_one
      formatter
      t

  let pp_field formatter t =
    let open Multipart_form.Field in
    match t with
    | Field (_, Content_type, { ty; subty; parameters }) ->
      Format.fprintf
        formatter
        "Content-Type { ty: %a; subty: %a; params: [ %a ] }"
        pp_ty
        ty
        pp_subty
        subty
        (Format.pp_print_list
           ~pp_sep:(fun fmt () -> Format.fprintf fmt "; ")
           (fun fmt (name, value) ->
             Format.fprintf fmt "%s, %a" name pp_value value))
        parameters
    | Field (_, Content_encoding, enc) ->
      Format.fprintf
        formatter
        "Encoding: %a"
        Multipart_form.Content_encoding.pp
        enc
    | Field (_, Content_disposition, dispo) ->
      Format.fprintf
        formatter
        "Content-Disposition: %a"
        Multipart_form.Content_disposition.pp
        dispo
    | Field (field_name, Field, unstructured) ->
      Format.fprintf
        formatter
        "Field (%a, %a)"
        Multipart_form.Field_name.pp
        field_name
        pp_unstructured
        (unstructured :> Unstrctrd.elt list)

  (* let rec pp_contents formatter t = *)
  (* let pp_atom formatter { Multipart_form.header; body } = *)
  (* Format.fprintf *)
  (* formatter *)
  (* "{ fields: [ %a ]; contents: %a }" *)
  (* (Format.pp_print_list *)
  (* ~pp_sep:(fun fmt () -> Format.fprintf fmt "; ") *)
  (* pp_field) *)
  (* header *)
  (* (pp_option ~pp:pp_contents) *)
  (* body *)
  (* in *)
  (* match t with *)
  (* | Multipart_form.Leaf x -> *)
  (* Format.fprintf formatter "{ Contents: %a }" pp_atom x *)
  (* | Multipart lst -> *)
  (* Format.fprintf *)
  (* formatter *)
  (* "{ Multipart: [ %a ] }" *)
  (* (Format.pp_print_list *)
  (* ~pp_sep:(fun fmt () -> Format.fprintf fmt "; ") *)
  (* pp_atom) *)
  (* lst *)
end

(* type t = string option Multipart_form.t *)

let content_type header =
  let open Multipart_form in
  Header.content_type header

let content_disposition header =
  let open Multipart_form in
  Header.content_disposition header

let name_of_header header =
  let open Multipart_form in
  match Header.content_disposition header with
  | Some cdispo ->
    Multipart_form.Content_disposition.name cdispo
  | None ->
    None

let rec result_headers t =
  let open Multipart_form in
  let atom_to_headers acc header =
    let open Field in
    let headers =
      List.filter_map
        (function
          | Field.Field (_, Content_type, { ty; subty; _ }) ->
            let ty = Format.asprintf "%a" Pp.pp_ty ty in
            let subty =
              match subty with `Ietf_token s | `Iana_token s | `X_token s -> s
            in
            Some
              ( (Field_name.content_type :> string)
              , Format.asprintf "%s/%s" ty subty )
          | Field.Field (_, Content_encoding, encoding) ->
            let encoding =
              match encoding with
              | `Ietf_token s | `X_token s ->
                s
              | #Content_encoding.t ->
                Format.asprintf "%a" Content_encoding.pp encoding
            in
            Some ((Field_name.content_transfer_encoding :> string), encoding)
          | Field.Field (_, Content_disposition, cdispo) ->
            let ty =
              match Content_disposition.disposition_type cdispo with
              | `Ietf_token s | `X_token s ->
                s
              | ty ->
                Format.asprintf "%a" Pp.pp_disposition_type ty
            in
            let name = Content_disposition.name cdispo in
            let filename = Content_disposition.filename cdispo in
            let value =
              Format.asprintf
                "%s%a%a"
                ty
                (Format.pp_print_option
                   ~none:(fun _fmt () -> ())
                   (fun fmt name -> Format.fprintf fmt "; name=%S" name))
                name
                (Format.pp_print_option
                   ~none:(fun _fmt () -> ())
                   (fun fmt filename ->
                     Format.fprintf fmt "; filename=%S" filename))
                filename
            in
            Some ((Field_name.content_disposition :> string), value)
          | Field.Field (_, Field, _unstructured) ->
            None)
        (Header.assoc Field_name.content_disposition header
        @ Header.assoc Field_name.content_type header
        @ Header.assoc Field_name.content_transfer_encoding header)
    in
    headers @ acc
  in
  match t with
  | Multipart { header; body } ->
    Format.eprintf "nameof: %B@." (Option.is_some (name_of_header header));
    let headers = atom_to_headers [] header in
    (match body with
    | [] ->
      headers
    | xs ->
      atom_to_headers [] header
      @ List.concat_map (function None -> [] | Some t -> result_headers t) xs)
  | Leaf { header; _ } ->
    atom_to_headers [] header

let result_fields t =
  let open Multipart_form in
  let rec inner acc t =
    let atom_to_fields acc header =
      match name_of_header header with
      | Some name ->
        (name, header) :: acc
      | None ->
        acc
    in
    match t with
    | Multipart { header; body : 'a t option list } ->
      let acc =
        List.concat_map (inner acc) (List.filter_map (fun x -> x) body)
      in
      let atom_headers = atom_to_fields acc header in
      atom_headers
    | Leaf { header; _ } ->
      atom_to_fields acc header
  in
  inner [] t

let parse_content_type ct = Multipart_form.Content_type.of_string (ct ^ "\r\n")

let blit src src_off dst dst_off len =
  Bigstringaf.blit src ~src_off dst ~dst_off ~len

module Qe = Ke.Rke
module AU = Angstrom.Unbuffered

let extract_parts ~emit ~finish ~max_chunk_size ~content_type stream =
  (* min chunk size is 1KB. *)
  let max_chunk_size = max max_chunk_size 0x400 in
  let emitters header =
    let stream, push = Lwt_stream.create () in
    let key = name_of_header header in
    emit key stream;
    push, key
  in
  let state =
    AU.parse (Multipart_form.parser ~max_chunk_size ~emitters content_type)
  in
  let ke = Qe.create ~capacity:max_chunk_size Bigarray.char in
  let real_capacity = Qe.capacity ke in
  let max_capacity = 4 * real_capacity in
  let rec on_eof state =
    match state with
    | AU.Partial { continue; committed } ->
      Qe.N.unsafe_shift ke committed;
      if committed = 0 then Qe.compress ke;
      let next_state =
        match Qe.N.peek ke with
        | [] ->
          continue Bigstringaf.empty ~off:0 ~len:0 Complete
        | [ slice ] ->
          continue slice ~off:0 ~len:(Bigstringaf.length slice) Complete
        | slice :: _ ->
          continue slice ~off:0 ~len:(Bigstringaf.length slice) Incomplete
      in
      on_eof next_state
    | Fail (pos, marks, msg) ->
      Error
        (Format.asprintf
           "multipart parser failed on position %d. Error: %s ([%s])"
           pos
           msg
           (String.concat "; " marks))
    | Done (_, v) ->
      finish ();
      Ok v
  in
  let rec parse state =
    Lwt_stream.get stream >>= fun t ->
    match t with
    | None ->
      (* Stream ended. Still need to check `error`. *)
      Lwt.return (on_eof state)
    | Some { Faraday.buffer; off; len } ->
      (match state with
      | Partial { continue; committed } ->
        Qe.N.unsafe_shift ke committed;
        if committed = 0 then Qe.compress ke;
        Qe.N.push ke ~blit ~length:Bigstringaf.length ~off ~len buffer;
        if Qe.capacity ke > max_capacity then
          Lwt.return_error "POST buffer has grown too much"
        else if not (Qe.is_empty ke) then
          (* XXX(anmonteiro): It's OK to only read the first slice of the
           * queue. Ke's implementation returns at most 2 buffers from `peek`:
           * the second one is returned if the buffer has wrapped around its
           * capacity. *)
          let[@warning "-8"] (slice :: _) = Qe.N.peek ke in
          let next_state =
            continue slice ~off:0 ~len:(Bigstringaf.length slice) Incomplete
          in
          parse next_state
        else
          parse state
      | Fail (pos, marks, msg) ->
        Lwt.return_error
          (Format.asprintf
             "multipart parser failed on position %d. Error: %s ([%s])"
             pos
             msg
             (String.concat "; " marks))
      | Done (_, v) ->
        Lwt.return_ok v)
  in
  parse state >|= function
  | Ok t ->
    (* Both headers and the Hash Table are indexed by the `name` in *
       `content-disposition`*)
    Ok t
  | Error msg ->
    Lwt.async (fun () -> Lwt_stream.junk_while (fun _ -> true) stream);
    Error (`Msg msg)

type t = string option Multipart_form.t

let parse_multipart_form
    ~content_type ~max_chunk_size ~emit ?(finish = ignore) stream
  =
  match parse_content_type content_type with
  | Ok content_type ->
    extract_parts ~emit ~finish ~max_chunk_size ~content_type stream
  | Error e ->
    Lwt.return_error e
OCaml

Innovation. Community. Security.