-
Notifications
You must be signed in to change notification settings - Fork 0
/
GET.sh
executable file
·182 lines (157 loc) · 5.27 KB
/
GET.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
#!/bin/bash
#
# This is a very simple HTTP client talking to some server on STDIN/STDOUT
# and passing back the response via FD 3.
# It apparently works with Debian repositories.
#
# It needs some environment variables:
#
# MODE informational http or https
# Host informational hostname to connect to
# PORT informational PORT number to connect to
# URL mandatory The URL PATH without the host part
# HEADS mandatory all the headers, already correctly preformatted with \r\n in between
# like with: printf -vHEADS '%s\r\n' "${HEADERS[@]}"
# (because bash cannot pass arrays into subshells)
# TIMEOUT default=60 Timeout in seconds
# PARENT informational $SOCKLINGER_NR of the parent (or something similar)
#
# This needs some of my own tools:
#
# https://github.com/hilbix/unbuffered
# https://github.com/hilbix/timeout
LONG_TIMEOUT=60
DOWNGRADE="/tmp/APT-CACHER-NG-PROXY.https.tmp"
STDOUT() { local a b; printf -va '[%s] %q' "${PARENT:-${SOCKLINGER_NR:-$PPID}}" "$1"; [ 1 -ge $# ] || printf -vb ' %q' "${@:2}"; printf '%s%s\n' "$a" "$b"; }
STDERR() { STDOUT "$@" >&2; }
OOPS() { STDERR OOPS: "$@"; exit 23; }
x() { "$@"; }
o() { x "$@" || OOPS fail $?: "$@"; }
# Downgrade https location header
# Remember the downgrade to upgrade to HTTPS later again
location()
{
local dest
STDERR location "$hd"
case "$hd" in
(https://*) ;;
(*) return;;
esac
hd="${hd#https://}"
dest="${hd%%/*}"
hd="http://$hd"
h="$ht: $hd"
STDERR DOWNGRADE "$dest"
# Add the downgrade to the upgrade list
x fgrep -svf "$DOWNGRADE" >> "$DOWNGRADE" <<< "$dest"
}
get-headers()
{
HEADS=()
CURLHEADS=()
LAYER=normal
while read -rt${TIMEOUT:-10} h && h="${h%$'\r'}" && [ -n "$h" ]
do
ht="${h%%: *}"
hd="${h#*: }"
o test ".$h" = ".$ht: $hd"
ht="${ht,,}"
case "$ht:$hd" in
(proxy-connection:*) continue;;
(connection:*) continue;;
(cache-control:*) continue;;
(content-length:*) ContentLength="$hd";;
(content-type:*) ContentType="$hd";;
(transfer-encoding:*chunked*) LAYER=chunked;; # JFROG uses this, sigh
(transfer-encoding:*) ;;
(location:*) location;;
# request
# (accept:*) Accept="$hd";;
# (host:*) Host="$hd";;
# (user-agent:*) UserAgent="$hd";;
# (accept-encoding:*) ;;
# (range:*) ;;
# (if-range:*) ;;
# (referer:*) ;;
# (accept-language:*) ;;
# response
(upgrade:*) continue;;
(accept-ranges:*) ;;
(date:*) ;;
(expires:*) ;;
(etag:*) ;;
(content-range:*) ContentRange="$hd";;
(last-modified:*) ;;
(server:*) ;;
(age:*) ;;
(via:*) ;;
(vary:*) ;;
(permissions-policy:*) ;;
(referrer-policy:*) ;;
(x-frame-options:*) ;;
(x-xss-protection:*) ;;
(content-disposition:*) ;;
# WTF?
(x-content-type-options:*) ;;
(x-clacks-overhead:*) ;; # GNU Terry Pratchett
(x-served-by:*) ;;
(x-cache:*) ;;
(x-cache-hits:*) ;;
(x-timer:*) ;;
# JFROG
(x-jfrog-version:*) ;;
(x-artifactory-id:*) ;;
(x-artifactory-node-id:*) ;;
(x-request-id:*) ;;
(x-checksum-sha1:*) ;;
(x-checksum-sha256:*) ;;
(x-checksum-md5:*) ;;
(x-artifactory-filename:*) ;;
(*) STDERR head "$ht:" "$hd";;
esac
CURLHEADS+=(-H "$h")
HEADS+=("$h")
done
}
LAYER-normal()
{
[ -n "$ContentLength" ] || OOPS missing ContentLength
cnt="$(set -o pipefail; head -c "$ContentLength" | /usr/local/bin/timeout "$LONG_TIMEOUT" - | /usr/local/bin/unbuffered -o3 | wc -c)" || OOPS transfer failed
}
LAYER-chunked()
{
[ -z "$ContentLength" ] || OOPS chunked with ContentLength
while read -rt "$LONG_TIMEOUT" -n30 n || OOPS unexpected EOF at $cnt
n="${n%$'\r'}"
# STDERR chunk "$cnt" "$n"
printf '%s\r\n' "$n"
[ 0 = "$cnt" ] || { let cnt+=n && head -c "$n" | /usr/local/bin/timeout "$LONG_TIMEOUT" -; } || OOPS transfer failed at $cnt
read -rt "$LONG_TIMEOUT" -n2 t || OOPS unexpected EOF at $cnt
[ -z "${t%$'\r'}" ] || OOPS unexpected chunk at $cnt: "$t"
printf '\r\n'
[ 0 != "$n" ]
do :
done >&3
}
printf 'GET %s HTTP/1.1\r\n' "$URL"
printf 'connection: close\r\n'
printf '%s\r\n' "$HEADS"
read -rt${TIMEOUT:-10} HTTP CODE OK || OOPS no response: "$MODE" "$Host" "$PORT" "$URL"
OK="${OK%$'\r'}"
get-headers
STDERR GOT "$MODE" "$Host" "$PORT" "$URL" "$HTTP" "$CODE" "$OK" "$ContentType" "$ContentLength" "$ContentRange"
#STDERR "${HEADS[@]}"
# Now pass everything to the requestor
# Note that apt-cacher-ng fails if OK is an empty string
printf '%s %s %s\r\n' "$HTTP" "$CODE" "${OK:-OK}" >&3 || OOPS EOF
#printf 'connection: close\r\n' # ignored by Apt-Cacher-NG?!?
#printf 'proxy-connection: close\r\n' # ignored by Apt-Cacher-NG?!?
printf '%s\r\n' "${HEADS[@]}" '' >&3 || OOPS EOF
# Use `head` to restrict the number of incoming bytes.
# Because apparently some hosts seem to ignore "connection: close".
cnt=0
"LAYER-$LAYER"
# We probably should store the body above before passing it back
# such that we can test it for correctness before handing it to apt-cacher-ng
STDERR DONE "$cnt"
[ -z "$ContentLength" ] || [ "$cnt" = "$ContentLength" ] || OOPS content got "$cnt" length "$ContentLength" range "$ContentRange"