33823
|
1 |
(* Title: Tools/WWW_Find/mime.ML
|
33817
|
2 |
Author: Timothy Bourke, NICTA
|
|
3 |
|
|
4 |
Rudimentary support for mime_types.
|
|
5 |
*)
|
|
6 |
|
|
7 |
signature MIME =
|
|
8 |
sig
|
|
9 |
datatype t = Type of {
|
|
10 |
main : string,
|
|
11 |
sub : string,
|
|
12 |
params : (string * string) list
|
|
13 |
}
|
|
14 |
|
|
15 |
val plain : t
|
|
16 |
val html : t
|
|
17 |
|
|
18 |
val parse_type : string -> t option
|
|
19 |
val show_type : t -> string
|
|
20 |
end;
|
|
21 |
|
|
22 |
structure Mime: MIME =
|
|
23 |
struct
|
|
24 |
|
|
25 |
datatype t = Type of {
|
|
26 |
main : string,
|
|
27 |
sub : string,
|
|
28 |
params : (string * string) list
|
|
29 |
};
|
|
30 |
|
|
31 |
val strip =
|
|
32 |
Substring.dropl Char.isSpace
|
|
33 |
#> Substring.dropr Char.isSpace;
|
|
34 |
|
|
35 |
val split_fields =
|
|
36 |
Substring.splitl (fn c => c <> #"=")
|
|
37 |
#> apsnd (Substring.triml 1)
|
|
38 |
#> pairself (Substring.string o strip);
|
|
39 |
|
|
40 |
fun show_param (n, v) = concat ["; ", n, "=", v];
|
|
41 |
|
|
42 |
fun show_type (Type {main, sub, params}) =
|
|
43 |
concat ([main, "/", sub] @ map show_param params);
|
|
44 |
|
|
45 |
fun parse_type s =
|
|
46 |
(case Substring.fields (Char.contains "/;") (Substring.full s) of
|
|
47 |
t::s::ps => SOME (Type { main = (Substring.string o strip) t,
|
|
48 |
sub = (Substring.string o strip) s,
|
|
49 |
params = map split_fields ps })
|
|
50 |
| _ => NONE);
|
|
51 |
|
|
52 |
val plain = the (parse_type "text/plain; charset=utf-8");
|
|
53 |
val html = the (parse_type "text/html; charset=utf-8");
|
|
54 |
|
|
55 |
end;
|
|
56 |
|