agpl_1.0.0_b5da3320/src/agpl-url.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
with Ada.Strings.Maps;

package Agpl.URL is

   use Ada;

   --  The general URL form as described in RFC2616 is:
   --
   --  http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]
   --
   --  Note also that there are different RFC describing URL like the 2616 and
   --  1738 but they use different terminologies. Here we try to follow the
   --  names used in RFC2616 but we have implemented some extensions at the
   --  end of this package. For example the way Path and File are separated or
   --  the handling of user/password which is explicitly not allowed in the
   --  RFC but are used and supported in many browsers. Here are the extended
   --  URL supported:
   --
   --  http://username:password@www.here.com:80/dir1/dir2/xyz.html?p=8&x=doh
   --   |                            |       | |          |       |
   --   protocol                     host port path       file    parameters
   --
   --                                          <--  pathname  -->

   --
   --  URL Encoding and Decoding
   --

   Default_Encoding_Set   : constant Strings.Maps.Character_Set;
   Requiring_Encoding_Set : constant Strings.Maps.Character_Set;

   function Normalize (URL : in String) return String;
   --  Remove . and .. instances

   function Encode
     (Str          : in String;
      Encoding_Set : in Strings.Maps.Character_Set := Default_Encoding_Set)
      return String;
   --  Encode Str into a URL-safe form. Many characters are forbiden into an
   --  URL and needs to be encoded. A character is encoded by %XY where XY is
   --  the character's ASCII hexadecimal code. For example a space is encoded
   --  as %20.

   function Decode (Str : in String) return String;
   --  This is the oposite of Encode above

   function Requires_Encoding
     (Str : String;
      Set : Strings.Maps.Character_Set := Requiring_Encoding_Set)
      return Boolean;

private

   use type Ada.Strings.Maps.Character_Set;

   Default_Encoding_Set : constant Strings.Maps.Character_Set
     := Strings.Maps.To_Set
         (Span => (Low  => Character'Val (128),
                   High => Character'Val (Character'Pos (Character'Last))))
     or
       --  Strings.Maps.To_Set (";/?:@&=+$,<>#%""{}|\^[]` ");
     Strings.Maps.To_Set (";?:@&=+$,<>#%""{}|\^[]`' ");
   --  Removed '/' and added ''' which may be problematic

   --  Is as previous but without %, which doesnt trigger encoding
   Requiring_Encoding_Set : constant Strings.Maps.Character_Set
     := Strings.Maps.To_Set
         (Span => (Low  => Character'Val (128),
                   High => Character'Val (Character'Pos (Character'Last))))
     or
     Strings.Maps.To_Set (";?:@&=+$,<>#""{}|\^[]`' ");

end Agpl.URL;