forked from donnemartin/dev-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpcompression
More file actions
executable file
·183 lines (145 loc) · 5.52 KB
/
Copy pathhttpcompression
File metadata and controls
executable file
·183 lines (145 loc) · 5.52 KB
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
#!/usr/bin/env bash
# DESCRIPTION:
#
# Provides the content encoding the specified
# resources are served with.
#
# USAGE:
#
# httpcompression URL ...
#
# USEFUL LINKS:
#
# * HTTP/1.1 (RFC 2616) - Content-Encoding
# https://tools.ietf.org/html/rfc2616#section-14.11
#
# * SDCH Specification:
# https://lists.w3.org/Archives/Public/ietf-http-wg/2008JulSep/att-0441/Shared_Dictionary_Compression_over_HTTP.pdf
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
declare -r -a CURL_DEFAULT_OPTIONS=(
--connect-timeout 30
--header "Accept-Encoding: gzip, deflate, sdch"
--header "Cache-Control: no-cache" # Prevent intermediate proxies
# from caching the response
--location # If the page was moved to a
# different location, redo the
# request
--max-time 150
--show-error
--silent
--user-agent "Mozilla/5.0 Gecko" # Send a fake UA string for sites
# that sniff it instead of using
# the Accept-Encoding header
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
check_for_sdch() {
declare availDicts=""
declare currentHeaders="$1"
declare dict=""
declare dictClientID=""
declare dicts=""
declare i=""
declare url="$2"
# Check if the server advertised any dictionaries
dicts="$( printf "%s" "$currentHeaders" |
grep -i 'Get-Dictionary:' |
cut -d':' -f2 |
sed s/,/\ /g )"
# If it does, check to see if it really supports SDCH
if [ -n "$dicts" ]; then
for i in $dicts; do
dict=""
# Check if the dictionary location is specified as a path,
# and if it is, construct it's URL from the host name of the
# referrer URL
[[ "$i" != http* ]] \
&& dict="$( printf "%s" "$url" |
sed -En 's/([^/]*\/\/)?([^/]*)\/?.*/\1\2/p' )"
dict="$dict$i"
# Request the dictionaries from the server and
# construct the `Avail-Dictionary` header value
#
# [ The user agent identifier for a dictionary is defined
# as the URL-safe base64 encoding (as described in RFC
# 3548, section 4 [RFC3548]) of the first 48 bits (bits
# 0..47) of the dictionary's SHA-256 digest ]
dictClientID="$( curl "${CURL_DEFAULT_OPTIONS[@]}" "$dict" |
openssl dgst -sha256 -binary |
openssl base64 |
cut -c 1-8 |
sed -e 's/\+/-/' -e 's/\//_/' )"
[ -n "$availDicts" ] && availDicts="$availDicts,$dictClientID" \
|| availDicts="$dictClientID"
done
# Redo the request (advertising the available dictionaries)
# and replace the old resulted headers with the new ones
printf "$( curl "${CURL_DEFAULT_OPTIONS[@]}" \
-H "Avail-Dictionary: $availDicts" \
--dump-header - \
--output /dev/null \
"$url" )"
else
printf "%s" "$currentHeaders"
fi
}
get_content_encoding() {
declare currentHeaders=""
declare encoding=""
declare headers=""
declare indent=""
declare tmp=""
declare url="$1"
headers="$(curl "${CURL_DEFAULT_OPTIONS[@]}" \
--dump-header - \
--output /dev/null \
"$url" )" \
&& ( \
# Iterate over the headers of all redirects
while [ -n "$headers" ]; do
# Get headers for the "current" URL
currentHeaders="$( printf "%s" "$headers" | sed -n '1,/^HTTP/p' )"
# Remove the headers for the "current" URL
headers="${headers/"$currentHeaders"/}"
currentHeaders="$(check_for_sdch "$currentHeaders" "$url")"
# Get the value of the `Content-Encoding` header
encoding="$( printf "%s" "$currentHeaders" |
grep -i 'Content-Encoding:' |
cut -d' ' -f2 |
tr "\r" "," |
tr -d "\n" |
sed 's/,$//' )"
# Print the output for the "current" URL
[ -n "$encoding" ] && encoding="[$encoding]"
if [ "$url" != "$1" ]; then
printf "$indent$url $encoding\n"
indent=" $indent"
else
printf "\n * $1 $encoding\n"
indent=" ↳ "
fi
# Get the next URL from the series of redirects
tmp="$url"
url="$( printf "%s" "$currentHeaders" |
grep -i 'Location' |
sed -e 's/Location://' |
sed 's/^ *//' |
tr -d '\r' )"
# In case the `Location` header is specified as a path
[[ "$url" != http* ]] && url="$tmp$url"
done
)
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
main() {
# Check if cURL is installed
if [ -x "$(command -v "curl")" ]; then
while [ $# -ne 0 ]; do
get_content_encoding "$1"
shift
done
printf "\n"
else
printf "cURL is required, please install it!\n"
fi
}
main $@