Skip to content

Instantly share code, notes, and snippets.

@smoser
Created November 5, 2018 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoser/9eb1dcf9770c772c2dfea9ee6a84b1a4 to your computer and use it in GitHub Desktop.
Save smoser/9eb1dcf9770c772c2dfea9ee6a84b1a4 to your computer and use it in GitHub Desktop.
test a url for gzip encoding

test gzip encoding of url

This is a simple PASS / FAIL test indicating if a url is providing gzip encoded content.

#!/bin/bash
Usage() {
cat <<EOF
Usage: ${0##*/} url
Indicate whether or not a url seems to support gzip encoding.
options:
-v | --verbose: show debug info.
EOF
}
[ "$1" = "--help" -o "$1" = "-h" ] && { Usage; exit 0; }
[ "$1" = "-v" -o "$1" = "--verbose" ] && verbose=true && shift ||
verbose=false
TEMP_D="$(mktemp -d)"
cleanup() { rm -Rf "${TEMP_D}"; }
trap cleanup EXIT
url="$1"
set -e
cd "$TEMP_D"
cmd=( curl -v
--header "Accept-Encoding: gzip"
--dump-header headers-received
--output content
"$url"
)
! $verbose || echo "${cmd[@]}" 1>&2
"${cmd[@]}" 2> stderr
sed -n 's/^> //p' stderr > headers-sent
mime=$(file --brief --mime content)
mime=${mime%%;*}
size=$(stat "--format=%s" content)
sed -i 's/\r//' headers-received
encoding=$(awk '$1 == "Content-Encoding:" { print $2 }' headers-received)
if [ "$encoding" = "gzip" -a "$mime" = "application/gzip" ]; then
echo "PASS: encoding='${encoding}' mime='${mime}' size=$size"
RET=0
else
echo "FAIL: encoding='${encoding}' mime='${mime}' size=$size"
RET=1
fi
$verbose || exit $RET
echo == headers-sent ==
cat headers-sent
echo == headers-received ==
cat headers-received
exit $RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment