6639
|
1 |
(* Title: Pure/General/url.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
8806
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE)
|
6639
|
5 |
|
|
6 |
Basic URLs.
|
|
7 |
*)
|
|
8 |
|
|
9 |
signature URL =
|
|
10 |
sig
|
14909
|
11 |
datatype T =
|
|
12 |
File of Path.T |
|
|
13 |
RemoteFile of string * Path.T |
|
|
14 |
Http of string * Path.T |
|
|
15 |
Ftp of string * Path.T
|
6639
|
16 |
val append: T -> T -> T
|
|
17 |
val pack: T -> string
|
|
18 |
val unpack: string -> T
|
|
19 |
end;
|
|
20 |
|
|
21 |
structure Url: URL =
|
|
22 |
struct
|
|
23 |
|
|
24 |
(* type url *)
|
|
25 |
|
14909
|
26 |
datatype T =
|
|
27 |
File of Path.T |
|
|
28 |
RemoteFile of string * Path.T |
|
|
29 |
Http of string * Path.T |
|
|
30 |
Ftp of string * Path.T;
|
6639
|
31 |
|
|
32 |
|
|
33 |
(* append *)
|
|
34 |
|
14909
|
35 |
fun append (File p) (File p') = File (Path.append p p')
|
|
36 |
| append (RemoteFile (h, p)) (File p') = RemoteFile (h, Path.append p p')
|
|
37 |
| append (Http (h, p)) (File p') = Http (h, Path.append p p')
|
|
38 |
| append (Ftp (h, p)) (File p') = Ftp (h, Path.append p p')
|
6639
|
39 |
| append _ url = url;
|
|
40 |
|
|
41 |
|
|
42 |
(* pack *)
|
|
43 |
|
14909
|
44 |
fun pack_path p = if Path.is_current p then "" else Path.pack p;
|
6639
|
45 |
|
14909
|
46 |
fun pack (File p) = pack_path p
|
|
47 |
| pack (RemoteFile (h, p)) = "file://" ^ h ^ pack_path p
|
|
48 |
| pack (Http (h, p)) = "http://" ^ h ^ pack_path p
|
|
49 |
| pack (Ftp (h, p)) = "ftp://" ^ h ^ pack_path p;
|
6639
|
50 |
|
|
51 |
|
|
52 |
(* unpack *)
|
|
53 |
|
14918
|
54 |
local
|
|
55 |
|
|
56 |
fun gen_host quant = (quant (not_equal "/" andf Symbol.not_eof) >> implode) --|
|
|
57 |
Scan.ahead ($$ "/" || Scan.one Symbol.is_eof);
|
|
58 |
|
|
59 |
val scan_host = gen_host Scan.any;
|
|
60 |
val scan_host1 = gen_host Scan.any1;
|
6639
|
61 |
val scan_path = Scan.any Symbol.not_eof >> (Path.unpack o implode);
|
|
62 |
|
|
63 |
val scan_url =
|
14909
|
64 |
Scan.unless (Scan.this_string "file:" ||
|
|
65 |
Scan.this_string "http:" || Scan.this_string "ftp:") scan_path >> File ||
|
14918
|
66 |
Scan.this_string "file://" |--
|
|
67 |
(scan_host >> (fn "localhost" => "" | h => h)) -- scan_path >> RemoteFile ||
|
|
68 |
Scan.this_string "http://" |-- scan_host1 -- scan_path >> Http ||
|
|
69 |
Scan.this_string "ftp://" |-- scan_host1 -- scan_path >> Ftp;
|
|
70 |
|
|
71 |
in
|
6639
|
72 |
|
|
73 |
fun unpack s = Symbol.scanner "Malformed URL" scan_url (Symbol.explode s);
|
|
74 |
|
|
75 |
end;
|
14918
|
76 |
|
|
77 |
end;
|