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
module Multipart_form = Piaf_multipart_form.Multipart_form
module Pp = Pp
let content_type =
let open Multipart_form in
Header.content_type header
let content_disposition =
let open Multipart_form in
Header.content_disposition header
let =
let open Multipart_form in
match Header.content_disposition header with
| Some cdispo -> Multipart_form.Content_disposition.name cdispo
| None -> None
let rec t =
let open Multipart_form in
let acc =
let open Field in
let =
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 { ; body } ->
let = 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 { ; _ } -> atom_to_headers [] header
let result_fields t =
let open Multipart_form in
let rec inner acc t =
let atom_to_fields acc =
match name_of_header header with
| Some name -> (name, header) :: acc
| None -> acc
in
match t with
| Multipart { ; body : 'a t option list } ->
let acc =
List.concat_map (inner acc) (List.filter_map (fun x -> x) body)
in
let = atom_to_fields acc header in
atom_headers
| Leaf { ; _ } -> 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 ~emit ~finish ~max_chunk_size ~content_type stream =
let max_chunk_size = max max_chunk_size 0x400 in
let emitters =
let stream, push = Piaf_stream.create 128 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 =
match Piaf_stream.take stream with
| None ->
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 Error "POST buffer has grown too much"
else if not (Qe.is_empty ke)
then
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) ->
Error
(Format.asprintf
"multipart parser failed on position %d. Error: %s ([%s])"
pos
msg
(String.concat "; " marks))
| Done (_, v) -> Ok v)
in
match parse state with
| Ok t ->
Ok t
| Error msg -> 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 -> Error e