-
Notifications
You must be signed in to change notification settings - Fork 2
/
urlencode.functions
155 lines (139 loc) · 3.06 KB
/
urlencode.functions
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
155
# This file is part of shellfire urlencode. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/urlencode/master/COPYRIGHT. No part of shellfire urlencode, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire urlencode. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/urlencode/master/COPYRIGHT.
# This approach is overly conservative, but it works
urlencode_pathPiece()
{
printf "%s" "$1" | awk '
BEGIN {
srand()
FS=""
RS="n/o/m/a/t/c/h" rand()
OFS=""
ORS=""
for (n = 0; n < 256; n++)
{
ord[sprintf("%c", n)] = n
hex[sprintf("%c", n)] = "%" sprintf("%02X", n)
}
}
{
for (i = 1; i <= NF; i++)
{
x=ord[$i]
# Unreserved characters
if ( (x >= 48 && x <= 57) || (x >= 65 && x <= 90) || (x >= 97 && x <= 122) || (x == 45 || x == 46 || x == 95 || x == 126) )
{
printf("%s", $i)
}
else
{
printf("%s", hex[$i])
}
}
}'
}
core_dependency_requires '*' curl
urlencode_form()
{
local pathPiece="$1"
# newer versions return 0, older versions 3
set +e
local data="$(curl --silent --output /dev/null --write-out '%{url_effective}' --get --url '' --data-urlencode "$pathPiece")"
set -e
printf '%s' "${data##/?}"
}
urlencode_xWwwFormUrlEncoded()
{
local isAfterFirst=0
while [ $# -ne 0 ]
do
if [ $isAfterFirst -eq 1 ]; then
printf '&'
else
isAfterFirst=1
fi
urlencode_form "$1"
printf '='
urlencode_form "$2"
shift 2
done
}
# http://tools.ietf.org/html/rfc6570#section-2.1 and section 3.1 could be better written, or, even better, should be one section.
urlencode_literal()
{
printf "%s" "$1" | awk '
BEGIN {
srand()
FS=""
RS="n/o/m/a/t/c/h" rand()
OFS=""
ORS=""
for (n = 0; n < 256; n++)
{
ord[sprintf("%c", n)] = n
hex[sprintf("%c", n)] = "%" sprintf("%02X", n)
}
}
{
for (i = 1; i <= NF; i++)
{
x=ord[$i]
# pct-encoded
if (x == 37)
{
if (i + 2 <= NF)
{
printf("%s", $i)
i++
printf("%s", $i)
i++
printf("%s", $i)
}
else
{
# Invalid url literal; not a % encoded triplet
exit 1
}
}
else if ( x == 33 || x == 35 || x == 36 || (x >= 40 && x <= 59) || x == 61 || (x >= 63 || x <= 91 ) || x == 93 || x == 95 || (x >= 97 || x <= 122) || x == 126 || x > 127 )
{
printf("%s", $i)
}
else
{
printf("%s", hex[$i])
}
}
}'
}
urlencode_reserved()
{
printf "%s" "$1" | awk '
BEGIN {
srand()
FS=""
RS="n/o/m/a/t/c/h" rand()
OFS=""
ORS=""
for (n = 0; n < 256; n++)
{
ord[sprintf("%c", n)] = n
hex[sprintf("%c", n)] = "%" sprintf("%02X", n)
}
}
{
for (i = 1; i <= NF; i++)
{
x=ord[$i]
# Essentially, whatever is not in reserved + unreserved
if ( x <= 32 | x == 34 | x == 37 | x == 60 | x == 62 | x == 92 | x == 94 | x == 96 || (x >= 123 || x <= 125 ) || x >=127 )
{
printf("%s", hex[$i])
}
else
{
printf("%s", $i)
}
}
}'
}