-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-redirects.sh
executable file
·61 lines (51 loc) · 1.26 KB
/
check-redirects.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
#!/bin/sh
debug() { test "$verbose" && echo "$@"; }
file=
domain=
verbose=
while [ $# -gt 0 ]
do
case "$1" in
-d) shift; domain=$1 ;;
-v) verbose=1 ;;
*) file=$1 ;;
esac
shift
done
test "$domain" || {
domain=${file%.txt}
domain=${domain##*redirects-}
}
test -e "$file" || { echo "No such redirects file: $file"; exit 1; }
debug "\nTesting '$file' with domain $domain\n"
cat "$file" | while read line
do
# Skip comment line.
test "$line" = "${line#\#}" -a "$line" || continue
old=${line% *}
new=${line#* }
debug "[$old -> $new]"
url="https://$domain$old"
if [ "$url" = "$new" ]
then
# Skip no-op redirect.
debug "[SKIPPED]\n"
continue
fi
# Do the check.
debug "$ curl -Is 'https://$domain$old'"
response=$(curl -Is "https://$domain$old")
debug "$(echo "$response" | tr -d '\r' | sed '/^\s*$/d')"
# Extract location from response string.
result=$(echo "$response" | grep '^Location: ' | sed 's/^Location: //' | tr -d '\r')
# Report the result.
if [ "$result" = "$new" ]
then
debug "[SUCCESS]\n"
else
test "$result" &&
echo "[FAIL] expected '$old' -> '$new' but was '$result'" ||
echo "[FAIL] expected '$old' -> '$new' but got '$(echo "$response" | tr -d '\r' | head -n1)'"
fi
done
echo DONE