Source file content_type.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
exception Invalid_token
let error_msgf fmt = Format.kasprintf (fun msg -> Error (`Msg msg)) fmt
let is_tspecials = function
| '(' | ')' | '<' | '>' | '@' | ',' | ';' | ':' | '\\' | '"' | '/' | '[' | ']'
| '?' | '=' ->
true
| _ -> false
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 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
module Type = struct
type discrete = [ `Text | `Image | `Audio | `Video | `Application ]
type composite = [ `Message | `Multipart ]
type extension = [ `Ietf_token of string | `X_token of string ]
type t = [ discrete | composite | extension ]
let text = `Text
let image = `Image
let audio = `Audio
let video = `Video
let application = `Application
let message = `Message
let multipart = `Multipart
let is_discrete = function #discrete -> true | _ -> false
let is_multipart = function `Multipart -> true | _ -> false
let is_message = function `Message -> true | _ -> false
let ietf token =
if Iana.Map.mem (String.lowercase_ascii token) Iana.database then
Ok (`Ietf_token token)
else error_msgf "%S is not an IETF token" token
let extension token =
if String.length token < 3 then
error_msgf "Extension token MUST have, at least, 3 bytes: %S" token
else
match (token.[0], token.[1]) with
| ('x' | 'X'), '-' -> (
try
String.iter
(fun chr -> if not (is_token chr) then raise Invalid_token)
(String.sub token 2 (String.length token - 2));
Ok (`X_token token)
with Invalid_token ->
error_msgf "Extension token %S does not respect standards" token)
| _ -> error_msgf "An extension token MUST be prefixed by [X-]: %S" token
let pp ppf = function
| `Text -> Format.pp_print_string ppf "text"
| `Image -> Format.pp_print_string ppf "image"
| `Audio -> Format.pp_print_string ppf "audio"
| `Video -> Format.pp_print_string ppf "video"
| `Application -> Format.pp_print_string ppf "application"
| `Message -> Format.pp_print_string ppf "message"
| `Multipart -> Format.pp_print_string ppf "multipart"
| `Ietf_token token -> Format.fprintf ppf "ietf:%s" token
| `X_token token -> Format.fprintf ppf "x:%s" token
let to_string = function
| `Text -> "text"
| `Image -> "image"
| `Audio -> "audio"
| `Video -> "video"
| `Application -> "application"
| `Message -> "message"
| `Multipart -> "multipart"
| `Ietf_token token | `X_token token -> token
let of_string str =
match String.lowercase_ascii str with
| "text" -> Ok `Text
| "image" -> Ok `Image
| "audio" -> Ok `Audio
| "video" -> Ok `Video
| "application" -> Ok `Application
| "message" -> Ok `Message
| "multipart" -> Ok `Multipart
| str -> (
match (ietf str, extension str) with
| Ok ietf, _ -> Ok ietf
| _, Ok extension -> Ok extension
| _ -> error_msgf "Invalid type: %S" str)
let compare a b =
String.(
compare (lowercase_ascii (to_string a)) (lowercase_ascii (to_string b)))
let equal a b = compare a b = 0
let default = `Text
end
module Subtype = struct
type t = [ `Ietf_token of string | `Iana_token of string | `X_token of string ]
let ietf token =
Ok (`Ietf_token token)
let iana ty token =
let ty = Type.to_string ty in
match Iana.Map.find (String.lowercase_ascii ty) Iana.database with
| database ->
if Iana.Set.mem (String.lowercase_ascii token) database then
Ok (`Iana_token token)
else error_msgf "Subtype %S does not exist (type: %s)" token ty
| exception Not_found -> error_msgf "Type %S does not exist" ty
let iana_exn ty token =
match iana ty token with Ok v -> v | Error (`Msg err) -> invalid_arg err
let v ty token = iana_exn ty token
let extension token =
if String.length token < 3 then
error_msgf "Extension token MUST have, at least, 3 bytes: %S" token
else
match (token.[0], token.[1]) with
| ('x' | 'X'), '-' -> (
try
String.iter
(fun chr -> if not (is_token chr) then raise Invalid_token)
(String.sub token 2 (String.length token - 2));
Ok (`X_token token)
with Invalid_token ->
error_msgf "Extension token %S does not respect standards" token)
| _ -> error_msgf "An extension token MUST be prefixed by [X-]: %S" token
let pp ppf = function
| `Ietf_token token -> Format.fprintf ppf "ietf:%s" token
| `Iana_token token -> Format.fprintf ppf "iana:%s" token
| `X_token token -> Format.fprintf ppf "x:%s" token
let to_string = function
| `Ietf_token token -> token
| `Iana_token token -> token
| `X_token token -> token
let compare a b =
match (a, b) with
| ( (`Ietf_token a | `Iana_token a | `X_token a),
(`Ietf_token b | `Iana_token b | `X_token b) ) ->
String.(compare (lowercase_ascii a) (lowercase_ascii b))
let equal a b = compare a b = 0
let default = `Iana_token "plain"
end
module Parameters = struct
module Map = Map.Make (String)
type key = string
type value = [ `String of string | `Token of string ]
type t = value Map.t
let key key =
try
String.iter
(fun chr -> if not (is_token chr) then raise Invalid_token)
key;
Ok (String.lowercase_ascii key)
with Invalid_token -> error_msgf "Key %S does not respect standards" key
let key_exn x =
match key x with Ok v -> v | Error (`Msg err) -> invalid_arg err
let k x = key_exn x
exception Invalid_utf_8
let value v =
let to_token x =
try
String.iter
(fun chr -> if not (is_token chr) then raise Invalid_token)
x;
Ok (`Token x)
with Invalid_token -> error_msgf "Value %S does not respect standards" v
in
let need_to_escape = function
| '\008' | '\009' | '\010' | '\013' | '\034' | '\092' -> true
| _ -> false
in
let of_escaped_character = function
| '\008' -> 'b'
| '\009' -> 't'
| '\010' -> 'n'
| '\013' -> 'r'
| c -> c
in
let _escape_characters x =
let len = String.length x in
let buf = Buffer.create len in
String.iter
(fun chr ->
if need_to_escape chr then (
Buffer.add_char buf '\\';
Buffer.add_char buf (of_escaped_character chr))
else Buffer.add_char buf chr)
x;
Buffer.contents buf
in
let utf_8 x =
try
Uutf.String.fold_utf_8
(fun () _pos -> function
| `Malformed _ -> raise Invalid_utf_8 | `Uchar _ -> ())
() x;
Ok x
with Invalid_utf_8 ->
error_msgf "Value %S is not a valid UTF-8 string" x
in
match to_token v with
| Ok _ as v -> v
| Error _ ->
Result.map (fun x -> `String x) (utf_8 v)
let value_exn x =
match value x with Ok v -> v | Error (`Msg err) -> invalid_arg err
let v x = value_exn x
let empty = Map.empty
let mem key t =
Map.mem key t
let add key value t = Map.add key value t
let singleton key value = Map.singleton key value
let remove key t = Map.remove key t
let find key t =
match Map.find key t with x -> Some x | exception Not_found -> None
let iter f t = Map.iter f t
let pp_key : Format.formatter -> key -> unit = Format.pp_print_string
let pp_value ppf = function
| `Token token -> Format.pp_print_string ppf token
| `String value -> Format.fprintf ppf "%S" value
let pp_list ?(sep = fun ppf () -> Format.fprintf ppf "") pp ppf lst =
let rec go = function
| [] -> ()
| [ x ] -> Format.fprintf ppf "%a" pp x
| x :: r ->
Format.fprintf ppf "%a%a" pp x sep ();
go r
in
go lst
let pp ppf t =
let pp ppf (key, value) =
Format.fprintf ppf "%a=%a" pp_key key pp_value value
in
pp_list
~sep:(fun ppf () -> Format.fprintf ppf ";@ ")
pp ppf (Map.bindings t)
let of_escaped_character = function
| '\x61' -> '\x07'
| '\x62' -> '\x08'
| '\x74' -> '\x09'
| '\x6E' -> '\x0A'
| '\x76' -> '\x0B'
| '\x66' -> '\x0C'
| '\x72' -> '\x0D'
| c -> c
let value_unescape x =
let len = String.length x in
let res = Buffer.create len in
let pos = ref 0 in
while !pos < len do
if
x.[!pos] = '\\' && !pos < len - 1
then (
Buffer.add_char res (of_escaped_character x.[!pos + 1]);
pos := !pos + 2)
else (
Buffer.add_char res x.[!pos];
incr pos)
done;
Buffer.contents res
let value_compare a b =
match (a, b) with
| `Token a, `Token b -> String.compare a b
| `String a, `Token b | `Token b, `String a ->
String.compare (value_unescape a) b
| `String a, `String b ->
String.compare (value_unescape a) (value_unescape b)
let value_equal a b =
match (a, b) with
| `Token a, `Token b -> String.equal a b
| `String a, `Token b | `Token b, `String a ->
String.equal (value_unescape a) b
| `String a, `String b -> String.equal (value_unescape a) (value_unescape b)
let compare = Map.compare value_compare
let equal = Map.equal value_equal
let of_list lst =
List.fold_left (fun a (key, value) -> Map.add key value a) Map.empty lst
let to_list t = Map.bindings t
let default = Map.add "charset" (`Token "us-ascii") Map.empty
end
type t =
{ ty : Type.t;
subty : Subtype.t;
parameters : (string * Parameters.value) list
}
let default =
{ ty = Type.default;
subty = Subtype.default;
parameters = Parameters.to_list Parameters.default
}
let ty { ty; _ } = ty
let subty { subty; _ } = subty
let parameters { parameters; _ } = parameters
let is_discrete { ty; _ } = Type.is_discrete ty
let is_multipart { ty; _ } = Type.is_multipart ty
let is_message { ty; _ } = Type.is_message ty
let with_type : t -> Type.t -> t = fun t ty -> { t with ty }
let with_subtype : t -> Subtype.t -> t = fun t subty -> { t with subty }
let with_parameter : t -> Parameters.key * Parameters.value -> t =
fun t (k, v) ->
let parameters = Parameters.of_list ((k, v) :: t.parameters) in
{ t with parameters = Parameters.to_list parameters }
let boundary { parameters; _ } =
match List.assoc_opt "boundary" parameters with
| Some (`Token v | `String v) -> Some v
| None -> None
let make ty subty parameters =
{ ty; subty; parameters = Parameters.to_list parameters }
let pp ppf { ty; subty; parameters } =
Format.fprintf ppf "%a/%a @[<hov>%a@]" Type.pp ty Subtype.pp subty
Parameters.pp
(Parameters.of_list parameters)
let equal a b =
Type.equal a.ty b.ty
&& Subtype.equal a.subty b.subty
&& Parameters.(equal (of_list a.parameters) (of_list b.parameters))
module Decoder = struct
open Angstrom
let invalid_token token = Format.kasprintf fail "invalid token: %s" token
let of_string s a =
match parse_string ~consume:Consume.All a s with
| Ok v -> Some v
| Error _ -> None
let is_wsp = function ' ' | '\t' -> true | _ -> false
let token = take_while1 is_token
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 ty =
token >>= fun s ->
match String.lowercase_ascii s with
| "text" -> return `Text
| "image" -> return `Image
| "audio" -> return `Audio
| "video" -> return `Video
| "application" -> return `Application
| "message" -> return `Message
| "multipart" -> return `Multipart
| _ -> (
match of_string s extension_token with
| Some v -> return v
| None -> invalid_token s)
let subty ty =
token >>= fun s ->
try
let v =
`Iana_token
(Iana.Set.find s (Iana.Map.find (Type.to_string ty) Iana.database))
in
return v
with Not_found -> (
match of_string s extension_token with
| Some v -> return v
| None -> invalid_token s)
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 >>| Parameters.of_escaped_character >>| String.make 1
let quoted_string =
char '"'
*> many
(quoted_pair
<|> utf_8_and is_qtext
<|> (satisfy is_wsp >>| String.make 1))
<* char '"'
>>| String.concat ""
let value =
quoted_string >>| (fun v -> `String v) <|> (token >>| fun v -> `Token v)
let parameter =
attribute >>= fun attribute ->
skip_while is_wsp *> char '=' *> skip_while is_wsp *> value >>| fun value ->
(attribute, value)
let content =
skip_while is_wsp *> ty <* skip_while is_wsp >>= fun ty ->
char '/' >>= fun _ ->
skip_while is_wsp *> subty ty <* skip_while is_wsp >>= fun subty ->
many (skip_while is_wsp *> char ';' *> skip_while is_wsp *> parameter)
>>| fun parameters -> { ty; subty; parameters }
end
module Encoder = struct
open Prettym
let ty ppf = function
| `Text -> string ppf "text"
| `Image -> string ppf "image"
| `Audio -> string ppf "audio"
| `Video -> string ppf "video"
| `Application -> string ppf "application"
| `Message -> string ppf "message"
| `Multipart -> string ppf "multipart"
| `Ietf_token v -> string ppf v
| `X_token v -> using (fun v -> "X-" ^ v) string ppf v
let subty ppf = function
| `Ietf_token v -> string ppf v
| `Iana_token v -> string ppf v
| `X_token v -> using (fun v -> "X-" ^ v) string ppf v
let cut : type a. unit -> (a, a) order = fun () -> break ~indent:1 ~len:0
let value =
using
(function `Token x -> `Atom x | `String x -> `String x)
Mailbox.Encoder.word
let parameter ppf (key, v) =
eval ppf [ box; !!string; cut (); char $ '='; cut (); !!value; close ] key v
let parameters ppf parameters =
let sep ppf () = eval ppf [ char $ ';'; fws ] in
eval ppf [ box; !!(list ~sep:(sep, ()) parameter); close ] parameters
let content_type ppf t =
match t.parameters with
| [] ->
eval ppf
[ bbox; !!ty; cut (); char $ '/'; cut (); !!subty; close ]
t.ty t.subty
| _ ->
eval ppf
[ bbox;
!!ty;
cut ();
char $ '/';
cut ();
!!subty;
cut ();
char $ ';';
fws;
!!parameters;
close
]
t.ty t.subty t.parameters
end