From 6586b7dbb5ae25583ee3c34af8b79ed3dadfb669 Mon Sep 17 00:00:00 2001 From: Marc Wrobel Date: Mon, 1 Aug 2022 20:54:39 +0200 Subject: [PATCH] Indicates if using HTTPS is OK for an HTTP link --- checklinks | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/checklinks b/checklinks index 678d43d..376d8cf 100755 --- a/checklinks +++ b/checklinks @@ -1,5 +1,7 @@ #!/usr/bin/env bash +set -uo pipefail + USER_AGENT='Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0' RETRY=1 RETRY_DELAY=10 @@ -29,23 +31,33 @@ javax.xml.XMLConstants)" RED='\033[0;31m' GREEN='\033[0;32m' BLUE='\033[0;34m' -GRAY='\033[1;34m' NC='\033[0m' # No Color +function doCurl() { + curl -o /dev/null --silent --connect-timeout "$TIMEOUT" --retry $RETRY --retry-delay $RETRY_DELAY --user-agent "$USER_AGENT" --location --write-out '%{http_code}' "$1" +} + [ ! -d "$1" ] && echo "'$1' is not a directory" && exit 1 +URLS_AND_FILES=$(grep -RioE --exclude-dir={.git,.idea,target,output} --exclude=*.{class,svg} "$URL_REGEX" .) + # Links are processed in a random order to reduce the risk of being blacklisted and temporarily blocked -for url in $(grep -RioEh --exclude-dir={.git,.idea,target,output} --exclude=*.{class,svg} "$URL_REGEX" | sort | uniq | sort -R); do +for url in $(echo "$URLS_AND_FILES" | cut -d ':' -f 2- | sort | uniq | sort -R); do if [[ "$url" =~ $EXCLUDED_URLS ]]; then - echo -e "${GRAY}$url (IGNORED)${NC}" + echo -e "${GREEN}$url (IGNORED)${NC}" else # we could use --head, but it is not always supported... - status=$(curl -o /dev/null --silent --connect-timeout "$TIMEOUT" --retry $RETRY --retry-delay $RETRY_DELAY --user-agent "$USER_AGENT" --location --write-out '%{http_code}' "$url") + status=$(doCurl "$url") if [ "$status" = "200" ]; then if [[ $url =~ "http://" ]]; then - echo -e "${BLUE}$url ($status)${NC}" + https_status=$(doCurl "${url/http:\/\//https:\/\/}") + if [ "$https_status" = "200" ]; then + echo -e "${BLUE}$url ($status - HTTPS OK)${NC}" + else + echo -e "${BLUE}$url ($status - HTTPS KO)${NC}" + fi else echo -e "${GREEN}$url ($status)${NC}" fi