| author | blanchet | 
| Tue, 19 Feb 2013 15:37:42 +0100 | |
| changeset 51186 | c8721406511a | 
| parent 29606 | fedb8be05f24 | 
| child 54702 | 3daeba5130f0 | 
| permissions | -rw-r--r-- | 
| 6639 | 1  | 
(* Title: Pure/General/url.ML  | 
2  | 
Author: Markus Wenzel, TU Muenchen  | 
|
3  | 
||
| 21515 | 4  | 
Basic URLs, see RFC 1738 and RFC 2396.  | 
| 6639 | 5  | 
*)  | 
6  | 
||
7  | 
signature URL =  | 
|
8  | 
sig  | 
|
| 14909 | 9  | 
datatype T =  | 
10  | 
File of Path.T |  | 
|
11  | 
RemoteFile of string * Path.T |  | 
|
12  | 
Http of string * Path.T |  | 
|
13  | 
Ftp of string * Path.T  | 
|
| 6639 | 14  | 
val append: T -> T -> T  | 
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
15  | 
val implode: T -> string  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
16  | 
val explode: string -> T  | 
| 6639 | 17  | 
end;  | 
18  | 
||
19  | 
structure Url: URL =  | 
|
20  | 
struct  | 
|
21  | 
||
22  | 
(* type url *)  | 
|
23  | 
||
| 14909 | 24  | 
datatype T =  | 
25  | 
File of Path.T |  | 
|
26  | 
RemoteFile of string * Path.T |  | 
|
27  | 
Http of string * Path.T |  | 
|
28  | 
Ftp of string * Path.T;  | 
|
| 6639 | 29  | 
|
30  | 
||
31  | 
(* append *)  | 
|
32  | 
||
| 14909 | 33  | 
fun append (File p) (File p') = File (Path.append p p')  | 
34  | 
| append (RemoteFile (h, p)) (File p') = RemoteFile (h, Path.append p p')  | 
|
35  | 
| append (Http (h, p)) (File p') = Http (h, Path.append p p')  | 
|
36  | 
| append (Ftp (h, p)) (File p') = Ftp (h, Path.append p p')  | 
|
| 6639 | 37  | 
| append _ url = url;  | 
38  | 
||
39  | 
||
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
40  | 
(* implode *)  | 
| 6639 | 41  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
42  | 
fun implode_path p = if Path.is_current p then "" else Path.implode p;  | 
| 6639 | 43  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
44  | 
fun implode_url (File p) = implode_path p  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
45  | 
| implode_url (RemoteFile (h, p)) = "file://" ^ h ^ implode_path p  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
46  | 
| implode_url (Http (h, p)) = "http://" ^ h ^ implode_path p  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
47  | 
| implode_url (Ftp (h, p)) = "ftp://" ^ h ^ implode_path p;  | 
| 6639 | 48  | 
|
49  | 
||
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
50  | 
(* explode *)  | 
| 6639 | 51  | 
|
| 14918 | 52  | 
local  | 
53  | 
||
| 16195 | 54  | 
val scan_host =  | 
| 
23784
 
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
 
wenzelm 
parents: 
21858 
diff
changeset
 | 
55  | 
(Scan.many1 (fn s => s <> "/" andalso Symbol.is_regular s) >> implode) --|  | 
| 14918 | 56  | 
Scan.ahead ($$ "/" || Scan.one Symbol.is_eof);  | 
57  | 
||
| 
23784
 
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
 
wenzelm 
parents: 
21858 
diff
changeset
 | 
58  | 
val scan_path = Scan.many Symbol.is_regular >> (Path.explode o implode);  | 
| 
 
75e6b9dd5336
Symbol.not_eof/sync is superceded by Symbol.is_regular (rules out further control symbols);
 
wenzelm 
parents: 
21858 
diff
changeset
 | 
59  | 
val scan_path_root = Scan.many Symbol.is_regular >> (Path.explode o implode o cons "/");  | 
| 6639 | 60  | 
|
61  | 
val scan_url =  | 
|
| 14909 | 62  | 
Scan.unless (Scan.this_string "file:" ||  | 
63  | 
Scan.this_string "http:" || Scan.this_string "ftp:") scan_path >> File ||  | 
|
| 
15175
 
b62f7b493360
Fix file:/// and file://localhost/ to give absolute paths
 
aspinall 
parents: 
15174 
diff
changeset
 | 
64  | 
Scan.this_string "file:///" |-- scan_path_root >> File ||  | 
| 
 
b62f7b493360
Fix file:/// and file://localhost/ to give absolute paths
 
aspinall 
parents: 
15174 
diff
changeset
 | 
65  | 
Scan.this_string "file://localhost/" |-- scan_path_root >> File ||  | 
| 16195 | 66  | 
Scan.this_string "file://" |-- scan_host -- scan_path >> RemoteFile ||  | 
| 21503 | 67  | 
Scan.this_string "file:/" |-- scan_path_root >> File ||  | 
| 16195 | 68  | 
Scan.this_string "http://" |-- scan_host -- scan_path >> Http ||  | 
69  | 
Scan.this_string "ftp://" |-- scan_host -- scan_path >> Ftp;  | 
|
| 14918 | 70  | 
|
71  | 
in  | 
|
| 6639 | 72  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
73  | 
fun explode_url s = Symbol.scanner "Malformed URL" scan_url (Symbol.explode s);  | 
| 6639 | 74  | 
|
75  | 
end;  | 
|
| 14918 | 76  | 
|
| 
21858
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
77  | 
|
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
78  | 
(*final declarations of this structure!*)  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
79  | 
val implode = implode_url;  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
80  | 
val explode = explode_url;  | 
| 
 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 
wenzelm 
parents: 
21515 
diff
changeset
 | 
81  | 
|
| 14918 | 82  | 
end;  |