-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulle-url.sh
398 lines (347 loc) · 8.99 KB
/
mulle-url.sh
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# shellcheck shell=bash
# shellcheck disable=SC2236
# shellcheck disable=SC2166
# shellcheck disable=SC2006
#
# Copyright (c) 2015-2017 Nat! - Mulle kybernetiK
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of Mulle kybernetiK nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#
if ! [ ${MULLE_URL_SH+x} ]
then
MULLE_URL_SH='included'
# RESET
# NOCOLOR
#
# parse URLs and retrieve components of an URL
#
# Functions prefixed "r_" return the result in the global variable RVAL.
# The return value 0 indicates success.
# TITLE INTRO
# COLOR
#
# r_url_encode <url>
#
# Escape unsafe characters of <url> as %XX hex.
# Works nicely if there are few characters that need encoding.
# "foo bar" -> "foo%20bar"
#
function r_url_encode()
{
local s="$1"
local c
local safe
local encode
RVAL=
while :
do
safe="${s%%[^a-zA-Z0-9.~_-]*}"
RVAL="${RVAL}${safe}"
s="${s#"${safe}"}"
if [ -z "${s}" ]
then
break
fi
c="${s:0:1}"
s="${s:1}"
printf -v encode '%%%02X' "'${c}'"
RVAL="${RVAL}${encode}"
done
}
#
# r_url_remove_scheme <url>
#
# Remove scheme: from <url>. Doesn't work nicely,
# if there is no scheme in the URL
# https://www.foo.com/bar?x#22 -> //www.foo.com/bar?x#22
#
function r_url_remove_scheme()
{
RVAL="${1#*:}"
}
#
# r_url_remove_query <url>
#
# Remove query part from <url>.
# https://www.foo.com/bar?x#22 -> https://www.foo.com/bar
#
function r_url_remove_query()
{
RVAL="${1%\?*}"
}
#
# r_url_remove_fragment <url>
#
# Remove fragment part from <url>.
# https://www.foo.com/bar?x#22 -> https://www.foo.com/bar?x
#
function r_url_remove_fragment()
{
RVAL="${1%#*}"
}
#
# url_has_file_compression_extension <url>
#
# Heuristic to figure out if an <url> has a file compression extension.
# zip is not a proper file compression but an archive format (like tar).
# tgz is the same, so they are not covered.
#
function url_has_file_compression_extension()
{
r_url_remove_query "$1"
case "${RVAL}" in
*.z|*.gz|*.bz2|*.xz)
return 0
;;
esac
return 1
}
#
# r_url_remove_file_compression_extension <url>
#
# Remove the single file compression extension from <url>
#
# zip is not a proper file compression but an archive format like tar
# tgz is the same, so they are not covered.
#
function r_url_remove_file_compression_extension()
{
local url="$1"
r_url_remove_query "${url}"
url="${RVAL}"
RVAL="${url%.z}"
[ "${RVAL}" != "$url" ] && return
RVAL="${url%.gz}"
[ "${RVAL}" != "$url" ] && return
RVAL="${url%.bz2}"
[ "${RVAL}" != "$url" ] && return
RVAL="${url%.xz}"
}
# Following regex is based on https://tools.ietf.org/html/rfc3986#appendix-B with
# additional sub-expressions to split authority into userinfo, host and port
#
MULLE_URI_REGEX='^(([^:/?#]+):)?(//((([^:/?#]+)@)?([^:/?#]+)(:([0-9]+))?))?(/([^?#]*))(\?([^#]*))?(#(.*))?'
# ↑↑ ↑ ↑↑↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
# |2 scheme | ||6 userinfo 7 host | 9 port | 11 rpath | 13 query | 15 fragment
# 1 scheme: | |5 userinfo@ 8 :… 10 path 12 ?… 14 #…
# | 4 authority
# 3 //…
#
# __url_parse <url>
#
# Parse <url> into "global" variables. Before calling this function, define
# a local variable block like this:
#
# local _scheme
# local _userinfo
# local _host
# local _port
# local _path
# local _query
# local _fragment
#
# __url_parse "${URL}"
#
function __url_parse()
{
log_entry "__url_parse" "$@"
local url="$1"
if [ ${ZSH_VERSION+x} ]
then
setopt local_options BASH_REMATCH
setopt local_options KSH_ARRAYS
fi
case "${url}" in
file://*)
_scheme="file"
_userinfo=""
_host=""
_port=""
_path="${url#file://}"
_query=""
case "${_path}" in
*\#*)
_fragment="${_path#*\#}"
_path="${_path%%*\#}"
;;
esac
;;
*://*)
if ! [[ "${url}" =~ ${MULLE_URI_REGEX} ]]
then
return 1
fi
# if [ ${ZSH_VERSION+x} ]
# then
# _scheme="${BASH_REMATCH[3]}"
# _userinfo="${BASH_REMATCH[7]}"
# _host="${BASH_REMATCH[8]}"
# _port="${BASH_REMATCH[10]}"
# _path="${BASH_REMATCH[11]}"
# _query="${BASH_REMATCH[14]}"
# _fragment="${BASH_REMATCH[16]}"
# else
_scheme="${BASH_REMATCH[2]}"
_userinfo="${BASH_REMATCH[6]}"
_host="${BASH_REMATCH[7]}"
_port="${BASH_REMATCH[9]}"
_path="${BASH_REMATCH[10]}"
_query="${BASH_REMATCH[13]}"
_fragment="${BASH_REMATCH[15]}"
# fix for https://unknown.com which gets parsed as _path
if [ -z "${_host}" ]
then
case "${_path}" in
//[^/]*/)
;;
//*)
_host="${_path}"
_path=""
;;
esac
fi
# fi
if [ -z "${_userinfo}${_host}${_port}" -a "${_path:0:3}" = "///" ]
then
_path="${_path#//}"
fi
;;
# hack for git@github.com:mulle-kybernetik-tv/nanovg.git
*:*)
_scheme=
_host="${url%:*}"
r_url_remove_query "${url##*:}"
r_url_remove_fragment "${RVAL}"
_path=${RVAL}
_userinfo=
_port=
case "${_host}" in
*@*)
_userinfo="${_host%%@*}"
_host="${_host#*@}"
;;
esac
case "${_host}" in
*:*)
_port="${_host%%:*}"
_host="${_host#*:}"
;;
esac
_query=
_fragment=
;;
*)
_scheme="${url%:*}"
_host=
r_url_remove_query "${url##*:}"
r_url_remove_fragment "${RVAL}"
_path=${RVAL}
_userinfo=
_port=
_query=
_fragment=
;;
esac
}
#
# r_url_get_path <url>
#
# Get path off <url>. <url> can also just be a pathname for a file
# https://www.foo.com/bar?x#22 -> "/bar"
#
function r_url_get_path()
{
log_entry "r_url_get_path" "$@"
local url="$1"
local _scheme
local _userinfo
local _host
local _port
local _path
local _query
local _fragment
if __url_parse "${url}"
then
RVAL="${_path}"
return
fi
return 1
}
#
# r_url_escaped_path <path>
#
# Hex escape some path characters so that they can be put into the path
# part of the URL. Only the following characters are considered:
# ' ' !"#%'*<>?[\]`|
#
function r_url_escaped_path()
{
local s="$1"
s="${s// /%20}"
s="${s//!/%21}"
s="${s//\"/%22}"
s="${s//#/%23}"
s="${s//%/%25}"
s="${s//\'/%27}"
s="${s//\*/%2A}"
s="${s//</%3C}"
s="${s//>/%3E}"
s="${s//\?/%3F}"
s="${s//\[/%5B}"
s="${s//\\/%5C}"
s="${s//\]/%5D}"
s="${s//\`/%60}"
s="${s//|/%7C}"
RVAL="$s"
}
#
# r_url_escaped_path <path>
#
# Unescape a path previously escaped by r_url_escaped_path
#
function r_url_unescaped_path()
{
local s="$1"
s="${s//%20/ }"
s="${s//%21/!}"
s="${s//%22/\"}"
s="${s//%23/#}"
s="${s//%25/%}"
s="${s//%27/\'}"
s="${s//%2A/*}"
s="${s//%3C/<}"
s="${s//%3E/>}"
s="${s//%3F/?}"
s="${s//%5B/[}"
s="${s//%5C/\\}"
s="${s//%5D/]}"
s="${s//%60/\`}"
s="${s//%7C/|}"
RVAL="$s"
}
fi
: