CXM_urllib.anubis
3.71 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Created by PyramIDE.
* User: ricard
* Date: 21/11/2009
* Time: 01:27
*
*/
read tools/basis.anubis
read system/lists.anubis
read system/string.anubis
public define String
_url_encode_char
(
Word8 c
)=
"%" +to_upper(to_hexa(c)).
public define List(Word8)
_url_quote
(
List(Word8) string,
List(Word8) safe,
Bool plus,
List(Word8) so_far
) =
if string is
{
[] then reverse(so_far),
[h . t] then
if (h = '-' | h = '_' | h = '.' | contains(safe, h)
| (h >=+ '0' & h +=< '9')
| (h >=+ 'a' & h +=< 'z')
| (h >=+ 'A' & h +=< 'Z') ) then
_url_quote(t, safe, plus, [h . so_far])
else if (h = ' ' & plus) then
_url_quote(t, safe, plus, ['+' . so_far])
else
_url_quote(t, safe, plus, reverse(explode(_url_encode_char(h))) + so_far)
}.
/**
* Replace special characters in string using the "%xx" escape. Letters, digits, and the characters "_.-" are never quoted.
* The optional safe parameter specifies additional characters that should not be quoted -- its default value is '/'.
*/
public define String
url_quote
(
String string,
String safe
) =
implode(_url_quote(explode(string), explode(safe), false, [])).
/**
* Replace special characters in string using the "%xx" escape. Letters, digits, and the characters "_.-" are never quoted.
* The optional safe parameter specifies additional characters that should not be quoted -- its default value is '/'.
*/
public define String
url_quote
(
String string,
) =
url_quote(string, "/").
/**
* Like quote(), but also replaces spaces by plus signs, as required for quoting HTML form values.
* Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'.
*/
public define String
url_quote_plus
(
String string,
String safe
) =
implode(_url_quote(explode(string), explode(safe), true, [])).
/**
* Like quote(), but also replaces spaces by plus signs, as required for quoting HTML form values.
* Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'.
*/
public define String
url_quote_plus
(
String string,
) =
url_quote_plus(string, "").
//-----------
define Word8
_url_decode_char
(
Word8 x1,
Word8 x2
) =
with n1 = if x1 +=< '9' then (x1 - '0') else if x1 +=< 'F' then (x1 - 'A' + 10) else (x1 - 'a' + 10),
n2 = if x2 +=< '9' then (x2 - '0') else if x1 +=< 'F' then (x1 - 'A' + 10) else (x2 - 'a' + 10),
(n1 << 4) + n2.
public define List(Word8)
_url_unquote
(
List(Word8) string,
Bool plus,
List(Word8) so_far
) =
if string is
{
[] then reverse(so_far),
[h . t] then
if h = '%' then
if t is
{
[] then reverse([h . so_far]),
[h1 . t1] then
if t1 is
{
[] then reverse([h1, h . so_far]),
[h2 . t2] then
_url_unquote(t2, plus, [_url_decode_char(h1, h2) . so_far])
}
}
else if (h = '+' & plus) then
_url_unquote(t, plus, [' ' . so_far])
else
_url_unquote(t, plus, [h . so_far])
}.
/**
* Replace "%xx" escapes by their single-character equivalent.
* Example: unquote("/%7Econnolly/") yields "/~connolly/".
*/
public define String
url_unquote
(
String string,
) =
implode(_url_unquote(explode(string), false, [])).
/**
* Like unquote(), but also replaces plus signs by spaces, as required for unquoting HTML form values.
*/
public define String
url_unquote_plus
(
String string,
) =
implode(_url_unquote(explode(string), true, [])).