aboutsummaryrefslogtreecommitdiff
path: root/bin/urlcheck
blob: 4446f94e29fa1d0bd41e6d134af1c57dc236612f (plain) (blame)
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
#!/usr/bin/env bash
self=urlcheck
tm=${URLCHECK_TIMEOUT:-8}
declare -i ex
head=$(mktemp) || exit
body=$(mktemp) || exit
cleanup() {
    rm -f -- "$head" "$body"
}
trap cleanup EXIT
while read -r url ; do
    [[ $url == 'http'* ]] || continue
    if ! curl -fHLsS -D "$head" -m "$tm" -o "$body" "$url" ; then
        printf '%s: %s raises error\n' \
            "$self" "$url" >&2
        continue
    fi
    while IFS=': ' read -r header value ; do
        [[ $header == 'Location' ]] || continue
        printf '%s: %s redirects to %s\n' \
            "$self" "$url" "$value" >&2
        break
    done < "$head"
    [[ $url == 'http:'* ]] || continue
    securl=${url/http:/https:}
    if curl -fLsS -m "$tm" -o /dev/null -- "$securl" 2>/dev/null ; then
        printf '%s: %s has a working secure version at %s\n' \
            "$self" "$url" "$securl" >&2
        ((ex++))
    fi
done
exit "$((ex > 0))"