Skip to content

Commit

Permalink
Indicates if using HTTPS is OK for an HTTP link
Browse files Browse the repository at this point in the history
  • Loading branch information
marcwrobel committed Aug 1, 2022
1 parent c20fc6a commit 6586b7d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions checklinks
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6586b7d

Please sign in to comment.