aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-06-18 13:58:14 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-06-18 13:58:14 +1200
commitf1c7b28ce059d61c4ca8fcb5e8b4a6c3ef67db4f (patch)
treedc7259c6479d19c71422911396325f4e7a6bf0a6 /bin
parentAdd option terminators to curl(1) calls (diff)
downloaddotfiles-f1c7b28ce059d61c4ca8fcb5e8b4a6c3ef67db4f.tar.gz
dotfiles-f1c7b28ce059d61c4ca8fcb5e8b4a6c3ef67db4f.zip
Commenting for urlcheck
Diffstat (limited to 'bin')
-rwxr-xr-xbin/urlcheck37
1 files changed, 32 insertions, 5 deletions
diff --git a/bin/urlcheck b/bin/urlcheck
index 21f41c7e..9ecfe0fc 100755
--- a/bin/urlcheck
+++ b/bin/urlcheck
@@ -1,32 +1,59 @@
#!/usr/bin/env bash
+
+# Name self
self=urlcheck
+
+# cURL request timeout
tm=${URLCHECK_TIMEOUT:-8}
-declare -i ex
+
+# Create temporary files for headers and body content
head=$(mktemp) || exit
body=$(mktemp) || exit
+
+# Set up cleanup function to remove temporary files on exit
cleanup() {
rm -f -- "$head" "$body"
}
trap cleanup EXIT
+
+# Error count
+declare -i errc
+
+# Iterate through input; ignore leading/trailing whitespace
while read -r url ; do
+
+ # Skip anything that doesn't start wit HTTP
[[ $url == 'http'* ]] || continue
+
+ # Make initial request, log head and body to files, cry and skip on error
if ! curl -fHLsS -D "$head" -m "$tm" -o "$body" -- "$url" ; then
printf '%s: %s raises error\n' \
"$self" "$url" >&2
+ ((errc++))
continue
fi
+
+ # Iterate through header file, cry about the first redirect we find
while IFS=': ' read -r header value ; do
[[ $header == 'Location' ]] || continue
printf '%s: %s redirects to %s\n' \
"$self" "$url" "$value" >&2
+ ((errc++))
break
done < "$head"
- [[ $url == 'http:'* ]] || continue
+
+ # Skip anything that's already secure
+ [[ $url == 'https:'* ]] && continue
+
+ # Form a naïve attempt at a possible secure URL and try to request it,
+ # point it out if it actually works
securl=${url/http:/https:}
if curl -fLsS -D "$head" -m "$tm" -o "$body" -- "$securl" 2>/dev/null ; then
printf '%s: %s has a working secure version at %s\n' \
"$self" "$url" "$securl" >&2
- ((ex++))
+ ((errc++))
fi
-done
-exit "$((ex > 0))"
+done < <(cat -- "${@:-/dev/stdin}") ## shellcheck disable=SC2002
+
+# Exit if any errors
+exit "$((errc > 0))"