-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.functions
247 lines (191 loc) · 8.01 KB
/
template.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# 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 is an implementation of RFC 6570 (http://tools.ietf.org/html/rfc6570)
# It implements URL Templates up to Level 4, with exceptions:
# - the binding of key-value maps is not supported
# - %-encoded parameters are not supported (but %-encoding of unencoded parameters is)
core_usesIn urlencode
# http://tools.ietf.org/html/rfc6570
core_dependency_requires '*' sed
urlencode_template()
{
# We assume templates are already-encoded
local template="$1"
# -E for BSD, -r for GNU / Busybox extended
#echo 'hello{name}john{more}end' | gsed -r -e 's,\$,\\\$,g' -e 's,",\\",g' -e "s,^([^}]+)\{,urlencode_literal '\1';{,g" -e "s,\}([^{]+)\$,}urlencode_literal '\1',g" -e "s,\}([^}{]+)\{,}urlencode_literal '\1';{,g" -e "s,\{([^{}]+)\},urlencode_template_replace '\1';,g"
# Whilst using sed and eval works, $ remains a problem and so is escaped
eval "$(printf '%s' "$template" | sed -r -e 's,\$,\\\$,g' -e 's,",\\",g' -e 's,^([^}]+)\{,urlencode_literal "\1";{,g' -e 's,\}([^{]+)\$,}urlencode_literal "\1",g' -e 's,\}([^}{]+)\{,}urlencode_literal "\1";{,g' -e 's,\{([^{}]+)\},urlencode_template_replace "\1";,g')"
}
core_dependency_requires '*' sed cat
urlencode_template_replace()
{
local expandedExpresion="$1"
if core_variable_isUnset _urlencode_template_temporaryFile; then
local TMP_FILE
core_temporaryFiles_newFileToRemoveOnExit
_urlencode_template_temporaryFile="$TMP_FILE"
fi
# There is no prefix if the result of expansion is an empty string
local prefix
local potentialOperator="$(core_variable_firstCharacter "$1")"
{
case "$potentialOperator" in
# we'll use urlencode_template_replace_XXXX for variable names
'+')
prefix=''
urlencode_template_replace_operate "$(core_variable_allButFirst "$expandedExpresion")" ',' urlencode_reserved urlencode_template_replace_operator_printNameEqualsNothing
;;
'#')
prefix='#'
urlencode_template_replace_operate "$(core_variable_allButFirst "$expandedExpresion")" ',' urlencode_reserved urlencode_template_replace_operator_printNameEqualsNothing
;;
'.')
prefix='.'
urlencode_template_replace_operate "$(core_variable_allButFirst "$expandedExpresion")" '.' urlencode_pathPiece urlencode_template_replace_operator_printNameEqualsNothing
;;
'/')
prefix='/'
urlencode_template_replace_operate "$(core_variable_allButFirst "$expandedExpresion")" '/' urlencode_pathPiece urlencode_template_replace_operator_printNameEqualsNothing
;;
';')
prefix=';'
urlencode_template_replace_operate "$(core_variable_allButFirst "$expandedExpresion")" ';' urlencode_pathPiece urlencode_template_replace_operator_printNameEqualsVarEquals
;;
'?')
prefix='?'
urlencode_template_replace_operate "$(core_variable_allButFirst "$expandedExpresion")" '&' urlencode_pathPiece urlencode_template_replace_operator_printNameEqualsVarEquals
;;
'&')
prefix='&'
urlencode_template_replace_operate "$(core_variable_allButFirst "$expandedExpresion")" '&' urlencode_pathPiece urlencode_template_replace_operator_printNameEqualsVarEquals
;;
'='|','|'!'|'@'|'|')
prefix=''
core_message WARN "Reserved operator '$potentialOperator' is not supported"
return 1
;;
*)
prefix=''
urlencode_template_replace_operate "$expandedExpresion" ',' urlencode_pathPiece urlencode_template_replace_operator_printNameEqualsNothing
;;
esac
} >"$_urlencode_template_temporaryFile"
if [ -s "$_urlencode_template_temporaryFile" ]; then
printf '%s' "$prefix"
cat "$_urlencode_template_temporaryFile"
fi
}
# Not supported:-
# - keys (maps)
# - percent-encoded variable names (silly idea - clearly they don't need to be)
urlencode_template_replace_operate()
{
local expressionLessOperator="$1"
local separator="$2"
local encodingFunction="$3"
local printNameEquals="$4"
local stringSeparated="$(core_variable_escape "$expressionLessOperator" ',' ' ')"
IFS=' ' set -- $stringSeparated
local variableName
local ourVariableName
local isAfterFirst=0
local core_variable_indirectValue_result
for variableName in "$@"
do
if [ $isAfterFirst -eq 1 ]; then
printf '%s' "$separator"
else
isAfterFirst=1
fi
# Level 4 Exploded
if core_variable_endsWith "$variableName" '*'; then
variableName="$(core_variable_allButLast "$variableName")"
ourVariableName=urlencode_template_variable_${variableName}
if core_variable_array_isSet "$ourVariableName"; then
local listIsAfterFirst=0
urlencode_template_replace_operate_callback1()
{
if [ $listIsAfterFirst -eq 1 ]; then
printf '%s' "$separator"
else
listIsAfterFirst=1
fi
$printNameEquals
$encodingFunction "$core_variable_array_element"
}
core_variable_array_iterate "$ourVariableName" urlencode_template_replace_operate_callback1
elif core_variable_isSet "$ourVariableName"; then
# exploded single value
$printNameEquals
core_variable_indirectValue "$ourVariableName"
$encodingFunction "$core_variable_indirectValue_result"
else
core_message INFO "Exploded Level 4 variable '$variableName' is unset; ignoring"
fi
# Level 4 Substring
elif core_variable_contains "$variableName" ':'; then
local length
IFS=':' read -r variableName length <<-EOF
${variableName}
EOF
case "$length" in
*[!0-9]*)
core_message WARN "Substring Level 4 variable '$variableName' has a non-integer length"
return 1
;;
esac
if [ $length -gt 10000 ]; then
core_message WARN "Substring Level 4 variable '$variableName' has exceeds maximum length ($length)"
return 1
fi
ourVariableName=urlencode_template_variable_${variableName}
if core_variable_array_isSet "$ourVariableName"; then
core_message WARN "Variable '$variableName' is an array with a substring"
return 1
elif core_variable_isUnset "$ourVariableName"; then
core_message INFO "Variable '$variableName' with a substring is unset; ignoring"
continue
fi
$printNameEquals
core_variable_indirectValue "$ourVariableName"
local totalLength="${#core_variable_indirectValue_result}"
if [ $length -ge $totalLength ]; then
$encodingFunction "$core_variable_indirectValue_result"
else
$encodingFunction "$(core_variable_allButLastN "$core_variable_indirectValue_result" $((totalLength-length)))"
fi
# Level 4 but no oddities
else
ourVariableName=urlencode_template_variable_${variableName}
if core_variable_array_isSet "$ourVariableName"; then
$printNameEquals
local listIsAfterFirst=0
urlencode_template_replace_operate_callback2()
{
if [ $listIsAfterFirst -eq 1 ]; then
printf ','
else
listIsAfterFirst=1
fi
$encodingFunction "$core_variable_array_element"
}
core_variable_array_iterate "$ourVariableName" urlencode_template_replace_operate_callback2
elif core_variable_isSet "$ourVariableName"; then
$printNameEquals
core_variable_indirectValue "$ourVariableName"
$encodingFunction "$core_variable_indirectValue_result"
else
core_message INFO "Variable '$variableName' is unset; ignoring"
fi
fi
done
}
urlencode_template_replace_operator_printNameEqualsNothing()
{
:
}
urlencode_template_replace_operator_printNameEqualsVarEquals()
{
# Won't work is the variableName is ALREADY %-encoded, which, apparently, is allowed...
printf '%s=' "$($encodingFunction "$variableName")"
}