-
Notifications
You must be signed in to change notification settings - Fork 36
/
http_download.sh
59 lines (57 loc) · 1.29 KB
/
http_download.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
# http_download_curl
#
# on error: displays a message on STDERR and returns non-zero code
http_download_curl() {
local_file=$1
source_url=$2
header=$3
if [ -z "$header" ]; then
curl -fsSL -o "$local_file" "$source_url"
else
curl -fsSL -H "$header" -o "$local_file" "$source_url"
fi
}
# http_download_wget
#
# unable to get server response code in a portable manner
# busybox wget (used on alpine linux) does not support "--server-response"
#
http_download_wget() {
local_file=$1
source_url=$2
header=$3
if [ -z "$header" ]; then
wget -q -O "$local_file" "$source_url"
else
wget -q --header "$header" -O "$local_file" "$source_url"
fi
}
#
# http_download [local-file] [url] [optional extra header]
#
# if arg3 is not empty it will add it as an extra HTTP header
# must be in the form "foo: bar"
#
http_download() {
log_debug "http_download $2"
if is_command curl; then
http_download_curl "$@"
return
elif is_command wget; then
http_download_wget "$@"
return
fi
log_crit "http_download unable to find wget or curl"
return 1
}
# http_copy - copies contents of a URL to stdout or fail
#
# needed since curl is broken
#
http_copy() {
tmp=$(mktemp)
http_download "${tmp}" "$1" "$2" || return 1
body=$(cat "$tmp")
rm -f "${tmp}"
echo "$body"
}