Source file content_disposition.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
type disposition_type =
[ `Inline | `Attachment | `Ietf_token of string | `X_token of string ]
type t = {
ty : disposition_type;
filename : string option;
creation : date option;
modification : date option;
read : date option;
size : int option;
parameters : (string * value) list;
}
and value = String of string | Token of string
and date = unit
let v ?filename ?(kind = `Ietf_token "form-data") ?size name =
{
ty = kind;
filename;
creation = None;
modification = None;
read = None;
size;
parameters = [ ("name", String name) ];
}
let pp_disposition_type ppf = function
| `Inline -> Fmt.string ppf "inline"
| `Attachment -> Fmt.string ppf "attachment"
| `Ietf_token v -> Fmt.pf ppf "<ietf:%s>" v
| `X_token v -> Fmt.pf ppf "X-%s" v
let pp_value ppf = function
| String v -> Fmt.pf ppf "%S" v
| Token v -> Fmt.string ppf v
let pp ppf t =
Fmt.pf ppf
"{ @[<hov>type= %a;@ filename= %a;@ creation= %a;@ modification= %a;@ \
read= %a;@ size= %a;@ parameters= @[<hov>%a@];@] }"
pp_disposition_type t.ty
Fmt.(option string)
t.filename
Fmt.(option (unit "#date"))
t.creation
Fmt.(option (unit "#date"))
t.modification
Fmt.(option (unit "#date"))
t.read
Fmt.(option int)
t.size
Fmt.(
Dump.iter_bindings
(fun f -> List.iter (fun (k, v) -> f k v))
(always "parameters") string pp_value)
t.parameters
let name t =
match List.assoc_opt "name" t.parameters with
| Some (String v | Token v) -> Some v
| None -> None
let filename { filename; _ } = filename
let size { size; _ } = size
let disposition_type { ty; _ } = ty
let of_escaped_character = function
| '\x61' -> '\x07'
| '\x62' -> '\x08'
| '\x74' -> '\x09'
| '\x6E' -> '\x0A'
| '\x76' -> '\x0B'
| '\x66' -> '\x0C'
| '\x72' -> '\x0D'
| c -> c
let is_obs_no_ws_ctl = function
| '\001' .. '\008' | '\011' | '\012' | '\014' .. '\031' | '\127' -> true
| _ -> false
let is_qtext = function
| '\033' | '\035' .. '\091' | '\093' .. '\126' -> true
| c -> is_obs_no_ws_ctl c
let is_wsp = function ' ' | '\t' -> true | _ -> false
module Rfc2045 = struct
open Angstrom
let _3 x y z = (x, y, z)
let _4 a b c d = (a, b, c, d)
let ( .![]<- ) = Bytes.set
let utf_8_tail = satisfy @@ function '\x80' .. '\xbf' -> true | _ -> false
let utf_8_0 =
satisfy (function '\xc2' .. '\xdf' -> true | _ -> false) >>= fun b0 ->
utf_8_tail >>= fun b1 ->
let res = Bytes.create 2 in
res.![0] <- b0 ;
res.![1] <- b1 ;
return (Bytes.unsafe_to_string res)
let utf_8_1 =
lift3 _3 (char '\xe0')
(satisfy @@ function '\xa0' .. '\xbf' -> true | _ -> false)
utf_8_tail
<|> lift3 _3
(satisfy @@ function '\xe1' .. '\xec' -> true | _ -> false)
utf_8_tail utf_8_tail
<|> lift3 _3 (char '\xed')
(satisfy @@ function '\x80' .. '\x9f' -> true | _ -> false)
utf_8_tail
<|> lift3 _3
(satisfy @@ function '\xee' .. '\xef' -> true | _ -> false)
utf_8_tail utf_8_tail
let utf_8_1 =
utf_8_1 >>= fun (b0, b1, b2) ->
let res = Bytes.create 3 in
res.![0] <- b0 ;
res.![1] <- b1 ;
res.![2] <- b2 ;
return (Bytes.unsafe_to_string res)
let utf_8_2 =
lift4 _4 (char '\xf0')
(satisfy @@ function '\x90' .. '\xbf' -> true | _ -> false)
utf_8_tail utf_8_tail
<|> lift4 _4
(satisfy @@ function '\xf1' .. '\xf3' -> true | _ -> false)
utf_8_tail utf_8_tail utf_8_tail
<|> lift4 _4 (char '\xf4')
(satisfy @@ function '\x80' .. '\x8f' -> true | _ -> false)
utf_8_tail utf_8_tail
let utf_8_2 =
utf_8_2 >>= fun (b0, b1, b2, b3) ->
let res = Bytes.create 4 in
res.![0] <- b0 ;
res.![1] <- b1 ;
res.![2] <- b2 ;
res.![3] <- b3 ;
return (Bytes.unsafe_to_string res)
let utf_8_and is =
satisfy is >>| String.make 1 <|> utf_8_0 <|> utf_8_1 <|> utf_8_2
let quoted_pair =
char '\\' *> any_char >>| of_escaped_character >>| String.make 1
let quoted_string =
char '"' *> many (quoted_pair <|> utf_8_and is_qtext)
<* char '"'
>>| String.concat ""
end
module Decoder = struct
type parameter =
| Filename of value
| Creation of unit
| Modification of unit
| Read of unit
| Size of int
| Parameter of (string * value)
open Angstrom
let is_tspecials = function
| '(' | ')' | '<' | '>' | '@' | ',' | ';' | ':' | '\\' | '"' | '/' | '['
| ']' | '?' | '=' ->
true
| _ -> false
let invalid_token token = Fmt.kstrf fail "invalid token: %s" token
let nothing_to_do = Fmt.kstrf fail "nothing to do"
let is_ctl = function '\000' .. '\031' | '\127' -> true | _ -> false
let is_space = ( = ) ' '
let is_ascii = function '\000' .. '\127' -> true | _ -> false
let is_token c =
is_ascii c && (not (is_tspecials c)) && (not (is_ctl c)) && not (is_space c)
let token = take_while1 is_token
let is_digit = function '0' .. '9' -> true | _ -> false
let attribute = token >>| String.lowercase_ascii
let ietf_token = token
let x_token =
satisfy (function 'x' | 'X' -> true | _ -> false) *> char '-' *> token
let extension_token =
peek_char >>= function
| Some 'X' | Some 'x' -> x_token >>| fun v -> `X_token v
| _ -> ietf_token >>| fun v -> `Ietf_token v
let value =
Rfc2045.quoted_string
>>| (fun v -> String v)
<|> (token >>| fun v -> Token v)
let of_string s a =
match parse_string ~consume:All a s with Ok v -> Some v | Error _ -> None
let disposition_type =
token >>= fun s ->
match String.lowercase_ascii s with
| "inline" -> return `Inline
| "attachment" -> return `Attachment
| _ ->
match of_string s extension_token with
| Some v -> return v
| None -> invalid_token s
let parameter =
attribute >>= fun attribute ->
skip_while is_wsp *> char '=' *> skip_while is_wsp *> value >>| fun value ->
(attribute, value)
let quoted_date_time = value >>| fun _ -> ()
let parm parm value =
string parm *> skip_while is_wsp *> char '=' *> skip_while is_wsp *> value
let filename_parm = parm "filename" value
let creation_date_parm = parm "creation-date" quoted_date_time
let modification_date_parm = parm "modification-date" quoted_date_time
let read_date_parm = parm "read-date" quoted_date_time
let size_parm = parm "read-date" (take_while1 is_digit >>| int_of_string)
let disposition_parm =
filename_parm
>>| (fun v -> Filename v)
<|> (creation_date_parm >>| fun v -> Creation v)
<|> (modification_date_parm >>| fun v -> Modification v)
<|> (read_date_parm >>| fun v -> Read v)
<|> (size_parm >>| fun v -> Size v)
<|> (parameter >>| fun v -> Parameter v)
let disposition =
skip_while is_wsp *> disposition_type <* skip_while is_wsp >>= fun ty ->
many (skip_while is_wsp *> char ';' *> skip_while is_wsp *> disposition_parm)
>>= fun parameters ->
let filename = ref None in
let creation = ref None in
let modification = ref None in
let read = ref None in
let size = ref None in
let parameters =
List.fold_left
(fun a -> function
| Filename (String v) | Filename (Token v) ->
filename := Some v ;
a
| Creation v ->
creation := Some v ;
a
| Modification v ->
modification := Some v ;
a
| Read v ->
read := Some v ;
a
| Size v ->
size := Some v ;
a
| Parameter v -> v :: a)
[] parameters in
return
{
ty;
filename = !filename;
creation = !creation;
modification = !modification;
read = !read;
size = !size;
parameters;
}
end
module Encoder = struct
open Prettym
let disposition_type ppf = function
| `Attachment -> eval ppf [ string $ "attachment" ]
| `Inline -> eval ppf [ string $ "inline" ]
| `Ietf_token v -> eval ppf [ !!string ] v
| `X_token v -> eval ppf [ string $ "X-"; !!string ] v
let token ppf str = eval ppf [ !!string ] str
let value ppf = function
| Token v -> token ppf v
| String v -> eval ppf [ char $ '"'; !!string; char $ '"' ] v
type 'a param =
| Filename : string param
| Creation : date param
| Modification : date param
| Read : date param
| Size : int param
| Parameter : (string * value) param
type v = V : ('a param * 'a option) -> v
let disposition_parm : v Prettym.t =
fun ppf (V v) ->
match v with
| Filename, Some v ->
eval ppf
[ string $ "filename"; cut; char $ '='; !!token; char $ ';'; fws ]
v
| Filename, None -> ppf
| Creation, _ -> ppf
| Modification, _ -> ppf
| Read, _ -> ppf
| Size, Some v ->
eval ppf
[ string $ "size"; cut; char $ '='; !!string; char $ ';'; fws ]
(string_of_int v)
| Size, None -> ppf
| Parameter, Some (k, v) ->
eval ppf [ !!string; cut; char $ '='; !!value; char $ ';'; fws ] k v
| Parameter, None -> ppf
let disposition ppf v =
let sep ppf () = eval ppf [ cut ] in
eval ppf
[
!!disposition_type;
cut;
char $ ';';
fws;
!!(list ~sep:(sep, ()) disposition_parm);
]
v.ty
([
V (Filename, v.filename);
V (Creation, v.creation);
V (Modification, v.modification);
V (Read, v.read);
V (Size, v.size);
]
@ List.map (fun (k, v) -> V (Parameter, Some (k, v))) v.parameters)
end