Commit 8d0169c031cfe0038684decae3d6ef19357b9a85
1 parent
e744c576
- Add URL quoting and unquoting functions
- Fix URL decoding bug when %xx is upper case
Showing
2 changed files
with
156 additions
and
2 deletions
Show diff stats
calexium_lib/web/CXM_multihost_http_server.anubis
| @@ -1007,8 +1007,8 @@ define Word8 | @@ -1007,8 +1007,8 @@ define Word8 | ||
| 1007 | Word8 x1, | 1007 | Word8 x1, |
| 1008 | Word8 x2 | 1008 | Word8 x2 |
| 1009 | ) = | 1009 | ) = |
| 1010 | - with n1 = if x1 +=< '9' then (x1 - '0') else (x1 - 'A' + 10), | ||
| 1011 | - n2 = if x2 +=< '9' then (x2 - '0') else (x2 - 'A' + 10), | 1010 | + with n1 = if x1 +=< '9' then (x1 - '0') else if x1 +=< 'F' then (x1 - 'A' + 10) else (x1 - 'a' + 10), |
| 1011 | + n2 = if x2 +=< '9' then (x2 - '0') else if x2 +=< 'F' then (x2 - 'A' + 10) else (x2 - 'a' + 10), | ||
| 1012 | (n1 << 4) + n2. | 1012 | (n1 << 4) + n2. |
| 1013 | 1013 | ||
| 1014 | 1014 |
| 1 | +/* | ||
| 2 | + * Created by PyramIDE. | ||
| 3 | + * User: ricard | ||
| 4 | + * Date: 21/11/2009 | ||
| 5 | + * Time: 01:27 | ||
| 6 | + * | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +read tools/basis.anubis | ||
| 10 | +read system/lists.anubis | ||
| 11 | +read system/string.anubis | ||
| 12 | + | ||
| 13 | +public define String | ||
| 14 | + _url_encode_char | ||
| 15 | + ( | ||
| 16 | + Word8 c | ||
| 17 | + )= | ||
| 18 | + "%" +to_upper(to_hexa(c)). | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +public define List(Word8) | ||
| 22 | + _url_quote | ||
| 23 | + ( | ||
| 24 | + List(Word8) string, | ||
| 25 | + List(Word8) safe, | ||
| 26 | + Bool plus, | ||
| 27 | + List(Word8) so_far | ||
| 28 | + ) = | ||
| 29 | + if string is | ||
| 30 | + { | ||
| 31 | + [] then reverse(so_far), | ||
| 32 | + [h . t] then | ||
| 33 | + if (h = '-' | h = '_' | h = '.' | contains(safe, h) | ||
| 34 | + | (h >=+ '0' & h +=< '9') | ||
| 35 | + | (h >=+ 'a' & h +=< 'z') | ||
| 36 | + | (h >=+ 'A' & h +=< 'Z') ) then | ||
| 37 | + _url_quote(t, safe, plus, [h . so_far]) | ||
| 38 | + else if (h = ' ' & plus) then | ||
| 39 | + _url_quote(t, safe, plus, ['+' . so_far]) | ||
| 40 | + else | ||
| 41 | + _url_quote(t, safe, plus, reverse(explode(_url_encode_char(h))) + so_far) | ||
| 42 | + }. | ||
| 43 | + | ||
| 44 | + | ||
| 45 | +/** | ||
| 46 | + * Replace special characters in string using the "%xx" escape. Letters, digits, and the characters "_.-" are never quoted. | ||
| 47 | + * The optional safe parameter specifies additional characters that should not be quoted -- its default value is '/'. | ||
| 48 | + */ | ||
| 49 | +public define String | ||
| 50 | + url_quote | ||
| 51 | + ( | ||
| 52 | + String string, | ||
| 53 | + String safe | ||
| 54 | + ) = | ||
| 55 | + implode(_url_quote(explode(string), explode(safe), false, [])). | ||
| 56 | + | ||
| 57 | +/** | ||
| 58 | + * Replace special characters in string using the "%xx" escape. Letters, digits, and the characters "_.-" are never quoted. | ||
| 59 | + * The optional safe parameter specifies additional characters that should not be quoted -- its default value is '/'. | ||
| 60 | + */ | ||
| 61 | +public define String | ||
| 62 | + url_quote | ||
| 63 | + ( | ||
| 64 | + String string, | ||
| 65 | + ) = | ||
| 66 | + url_quote(string, "/"). | ||
| 67 | + | ||
| 68 | +/** | ||
| 69 | + * Like quote(), but also replaces spaces by plus signs, as required for quoting HTML form values. | ||
| 70 | + * Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'. | ||
| 71 | + */ | ||
| 72 | +public define String | ||
| 73 | + url_quote_plus | ||
| 74 | + ( | ||
| 75 | + String string, | ||
| 76 | + String safe | ||
| 77 | + ) = | ||
| 78 | + implode(_url_quote(explode(string), explode(safe), true, [])). | ||
| 79 | + | ||
| 80 | +/** | ||
| 81 | + * Like quote(), but also replaces spaces by plus signs, as required for quoting HTML form values. | ||
| 82 | + * Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'. | ||
| 83 | + */ | ||
| 84 | +public define String | ||
| 85 | + url_quote_plus | ||
| 86 | + ( | ||
| 87 | + String string, | ||
| 88 | + ) = | ||
| 89 | + url_quote_plus(string, ""). | ||
| 90 | + | ||
| 91 | + | ||
| 92 | +//----------- | ||
| 93 | + | ||
| 94 | +define Word8 | ||
| 95 | + _url_decode_char | ||
| 96 | + ( | ||
| 97 | + Word8 x1, | ||
| 98 | + Word8 x2 | ||
| 99 | + ) = | ||
| 100 | + with n1 = if x1 +=< '9' then (x1 - '0') else if x1 +=< 'F' then (x1 - 'A' + 10) else (x1 - 'a' + 10), | ||
| 101 | + n2 = if x2 +=< '9' then (x2 - '0') else if x1 +=< 'F' then (x1 - 'A' + 10) else (x2 - 'a' + 10), | ||
| 102 | + (n1 << 4) + n2. | ||
| 103 | + | ||
| 104 | + | ||
| 105 | +public define List(Word8) | ||
| 106 | + _url_unquote | ||
| 107 | + ( | ||
| 108 | + List(Word8) string, | ||
| 109 | + Bool plus, | ||
| 110 | + List(Word8) so_far | ||
| 111 | + ) = | ||
| 112 | + if string is | ||
| 113 | + { | ||
| 114 | + [] then reverse(so_far), | ||
| 115 | + [h . t] then | ||
| 116 | + if h = '%' then | ||
| 117 | + if t is | ||
| 118 | + { | ||
| 119 | + [] then reverse([h . so_far]), | ||
| 120 | + [h1 . t1] then | ||
| 121 | + if t1 is | ||
| 122 | + { | ||
| 123 | + [] then reverse([h1, h . so_far]), | ||
| 124 | + [h2 . t2] then | ||
| 125 | + _url_unquote(t2, plus, [_url_decode_char(h1, h2) . so_far]) | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + else if (h = '+' & plus) then | ||
| 129 | + _url_unquote(t, plus, [' ' . so_far]) | ||
| 130 | + else | ||
| 131 | + _url_unquote(t, plus, [h . so_far]) | ||
| 132 | + }. | ||
| 133 | + | ||
| 134 | +/** | ||
| 135 | + * Replace "%xx" escapes by their single-character equivalent. | ||
| 136 | + * Example: unquote("/%7Econnolly/") yields "/~connolly/". | ||
| 137 | + */ | ||
| 138 | +public define String | ||
| 139 | + url_unquote | ||
| 140 | + ( | ||
| 141 | + String string, | ||
| 142 | + ) = | ||
| 143 | + implode(_url_unquote(explode(string), false, [])). | ||
| 144 | + | ||
| 145 | +/** | ||
| 146 | + * Like unquote(), but also replaces plus signs by spaces, as required for unquoting HTML form values. | ||
| 147 | + */ | ||
| 148 | +public define String | ||
| 149 | + url_unquote_plus | ||
| 150 | + ( | ||
| 151 | + String string, | ||
| 152 | + ) = | ||
| 153 | + implode(_url_unquote(explode(string), true, [])). | ||
| 154 | + |