author | wenzelm |
Fri, 16 Jun 2023 14:01:30 +0200 | |
changeset 78169 | 5ad1ae8626de |
parent 72680 | b22f1e2b4e94 |
permissions | -rw-r--r-- |
6639 | 1 |
(* Title: Pure/General/url.ML |
72680 | 2 |
Author: Makarius |
6639 | 3 |
|
72680 | 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 |
Http of string * Path.T | |
|
68226 | 12 |
Https of string * Path.T | |
14909 | 13 |
Ftp of string * Path.T |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21515
diff
changeset
|
14 |
val explode: string -> T |
6639 | 15 |
end; |
16 |
||
17 |
structure Url: URL = |
|
18 |
struct |
|
19 |
||
20 |
(* type url *) |
|
21 |
||
14909 | 22 |
datatype T = |
23 |
File of Path.T | |
|
24 |
Http of string * Path.T | |
|
68226 | 25 |
Https of string * Path.T | |
14909 | 26 |
Ftp of string * Path.T; |
6639 | 27 |
|
28 |
||
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21515
diff
changeset
|
29 |
(* explode *) |
6639 | 30 |
|
14918 | 31 |
local |
32 |
||
16195 | 33 |
val scan_host = |
58854 | 34 |
(Scan.many1 (fn s => s <> "/" andalso Symbol.not_eof s) >> implode) --| |
14918 | 35 |
Scan.ahead ($$ "/" || Scan.one Symbol.is_eof); |
36 |
||
58854 | 37 |
val scan_path = Scan.many Symbol.not_eof >> (Path.explode o implode); |
38 |
val scan_path_root = Scan.many Symbol.not_eof >> (Path.explode o implode o cons "/"); |
|
6639 | 39 |
|
40 |
val scan_url = |
|
68226 | 41 |
Scan.unless |
42 |
(Scan.this_string "file:" || Scan.this_string "http:" || |
|
43 |
Scan.this_string "https:" || Scan.this_string "ftp:") scan_path >> File || |
|
15175
b62f7b493360
Fix file:/// and file://localhost/ to give absolute paths
aspinall
parents:
15174
diff
changeset
|
44 |
Scan.this_string "file:///" |-- scan_path_root >> File || |
b62f7b493360
Fix file:/// and file://localhost/ to give absolute paths
aspinall
parents:
15174
diff
changeset
|
45 |
Scan.this_string "file://localhost/" |-- scan_path_root >> File || |
64776 | 46 |
Scan.this_string "file://" |-- scan_host -- scan_path |
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
68226
diff
changeset
|
47 |
>> (fn (h, p) => File (Path.named_root h + p)) || |
21503 | 48 |
Scan.this_string "file:/" |-- scan_path_root >> File || |
16195 | 49 |
Scan.this_string "http://" |-- scan_host -- scan_path >> Http || |
68226 | 50 |
Scan.this_string "https://" |-- scan_host -- scan_path >> Https || |
16195 | 51 |
Scan.this_string "ftp://" |-- scan_host -- scan_path >> Ftp; |
14918 | 52 |
|
53 |
in |
|
6639 | 54 |
|
72680 | 55 |
fun explode s = Symbol.scanner "Malformed URL" scan_url (Symbol.explode s); |
6639 | 56 |
|
57 |
end; |
|
14918 | 58 |
|
59 |
end; |