| 1 |
package URI with Preelaborate is |
| 2 |
|
| 3 |
-- Helper functions to identify URLs and their components. |
| 4 |
-- |
| 5 |
-- See https://tools.ietf.org/html/rfc3986 for full details. |
| 6 |
-- |
| 7 |
-- http://user:[email protected]:80/dir1/dir2/xyz.html?p=8&x=doh#anchor |
| 8 |
-- | | | | | | | |
| 9 |
-- protocol host port path file parameters fragment |
| 10 |
-- |
| 11 |
-- foo://example.com:8042/over/there?name=ferret#nose |
| 12 |
-- \_/ \______________/\_________/ \_________/ \__/ |
| 13 |
-- | | | | | |
| 14 |
-- scheme authority path query fragment |
| 15 |
-- | _____________________|__ |
| 16 |
-- / \ / \ |
| 17 |
-- urn:example:animal:ferret:nose |
| 18 |
|
| 19 |
type Parts is |
| 20 |
(Scheme, |
| 21 |
Authority, |
| 22 |
Path, |
| 23 |
Query, |
| 24 |
Fragment); |
| 25 |
|
| 26 |
--------------- |
| 27 |
-- Low level -- |
| 28 |
--------------- |
| 29 |
|
| 30 |
-- The following functions will return "" for any part not recognized. Note |
| 31 |
-- that no part is mandatory, so any "malformed" URI will be recognized as |
| 32 |
-- a mere Path. |
| 33 |
|
| 34 |
function Extract (This : String; Part : Parts) return String; |
| 35 |
-- Return a specific part of the URL, or "" if not present |
| 36 |
|
| 37 |
function Extract (This : String; First, Last : Parts) return String; |
| 38 |
-- Return a slice of parts, or "" if none exist in the slice. Note that |
| 39 |
-- appropriate separators will be included between parts: ":" after |
| 40 |
-- existing scheme, "//" if authority exists, "?" if query exists, "#" if |
| 41 |
-- fragment exists. Note that separators will not be included before First |
| 42 |
-- nor after Last parts. Note that Scheme .. Path of "file:///path/to" will |
| 43 |
-- return "file:/path/to" because authority is empty. |
| 44 |
-- |
| 45 |
-- NOTES on "file:" URIs. Since the Authority only ends when a third '/' is |
| 46 |
-- found, the following URIs may have unexpected interpretations (but these |
| 47 |
-- are the correct ones!): |
| 48 |
-- |
| 49 |
-- file://hello.txt => authority: hello.txt, path: "" |
| 50 |
-- file:///hello.txt => authority: "", path: /hello.txt |
| 51 |
-- file:../relative => authority: "", path: ../relative |
| 52 |
-- file://../relative => authority: "..", path: /relative |
| 53 |
-- file:/absolute => authority: "", path: /absolute |
| 54 |
-- |
| 55 |
-- TL;DR: to ensure proper interpretation of malformed file: URLs, take |
| 56 |
-- both the slice Authority .. Path as a whole to be the complete path. |
| 57 |
-- Or use Permissive_Path below. |
| 58 |
|
| 59 |
---------------- |
| 60 |
-- High level -- |
| 61 |
---------------- |
| 62 |
|
| 63 |
function Scheme (This : String) return String |
| 64 |
is (Extract (This, Scheme)); |
| 65 |
|
| 66 |
function Permissive_Path (This : String) return String |
| 67 |
is (Extract (This, Authority, Path)); |
| 68 |
|
| 69 |
end URI; |