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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#!/usr/bin/env perl
#***************************************************************************
# _ _ ____ _
# Project ___| | | | _ \| |
# / __| | | | |_) | |
# | (__| |_| | _ <| |___
# \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
use strict;
use warnings;
my %whitelist = (
'https://curl.se' => 1,
'https://curl.se/' => 1,
'https://curl.se/bug/' => 1,
'https://curl.se/bug/view.cgi' => 1,
'https://curl.se/changes.html' => 1,
'https://curl.se/dev/advisory.html' => 1,
'https://curl.se/dev/builds.html' => 1,
'https://curl.se/dev/code-style.html' => 1,
'https://curl.se/dev/contribute.html' => 1,
'https://curl.se/dev/internals.html' => 1,
'https://curl.se/dev/secprocess.html' => 1,
'https://curl.se/dev/sourceactivity.html' => 1,
'https://curl.se/docs/' => 1,
'https://curl.se/docs/bugbounty.html' => 1,
'https://curl.se/docs/caextract.html' => 1,
'https://curl.se/docs/copyright.html' => 1,
'https://curl.se/docs/http-cookies.html' => 1,
'https://curl.se/docs/install.html' => 1,
'https://curl.se/docs/knownbugs.html' => 1,
'https://curl.se/docs/manpage.html' => 1,
'https://curl.se/docs/releases.html' => 1,
'https://curl.se/docs/security.html' => 1,
'https://curl.se/docs/ssl-ciphers.html' => 1,
'https://curl.se/docs/ssl-compared.html' => 1,
'https://curl.se/docs/sslcerts.html' => 1,
'https://curl.se/docs/thanks.html' => 1,
'https://curl.se/docs/todo.html' => 1,
'https://curl.se/docs/vulnerabilities.html' => 1,
'https://curl.se/download.html' => 1,
'https://curl.se/libcurl/' => 1,
'https://curl.se/libcurl/c/CURLOPT_SSL_CIPHER_LIST.html' => 1,
'https://curl.se/libcurl/c/CURLOPT_SSLVERSION.html' => 1,
'https://curl.se/libcurl/c/CURLOPT_TLS13_CIPHERS.html' => 1,
'https://curl.se/libcurl/c/libcurl.html' => 1,
'https://curl.se/libcurl/c/threadsafe.html' => 1,
'https://curl.se/logo/curl-logo.svg' => 1,
'https://curl.se/mail/' => 1,
'https://curl.se/mail/etiquette.html' => 1,
'https://curl.se/mail/list.cgi?list=curl-distros' => 1,
'https://curl.se/mail/list.cgi?list=curl-library' => 1,
'https://curl.se/rfc/cookie_spec.html' => 1,
'https://curl.se/rfc/rfc2255.txt' => 1,
'https://curl.se/sponsors.html' => 1,
'https://curl.se/support.html' => 1,
'https://curl.se/windows/' => 1,
'https://testclutch.curl.se/' => 1,
'https://github.com/curl/curl-fuzzer' => 1,
'https://github.com/curl/curl-www' => 1,
'https://github.com/curl/curl/wcurl' => 1,
);
my %url;
my %flink;
my $dry;
if(defined $ARGV[0] && $ARGV[0] eq "--dry-run") {
$dry = 1;
shift @ARGV;
}
# list all files to scan for links
my @files=`git ls-files docs include lib scripts src`;
sub storelink {
my ($f, $line, $link) = @_;
my $o = $link;
if($link =~ /^\#/) {
# ignore local-only links
return;
}
# cut off any anchor
$link =~ s:\#.*\z::;
if($link =~ /^(https|http):/) {
if($whitelist{$link}) {
#print "-- whitelisted: $link\n";
$whitelist{$link}++;
}
# example.com is just example
elsif($link =~ /^https:\/\/(.*)example.(com|org|net)/) {
#print "-- example: $link\n";
}
# so is using the .example TLD
elsif($link =~ /^https:\/\/(.*)\.example(\/|$|:)/) {
#print "-- .example: $link\n";
}
# so is using anything on localhost
elsif($link =~ /^http(s|):\/\/localhost/) {
#print "-- localhost: $link\n";
}
# ignore all links to curl's github repo
elsif($link =~ /^https:\/\/github.com\/curl\/curl(\/|$)/) {
#print "-- curl github repo: $link\n";
}
elsif($link =~ /^(https|http):\/\/[0-9.]+(\/|$)/) {
#print "-- IPv4 number: $link\n";
}
else {
#print "ADD '$link'\n";
$url{$link} .= "$f:$line ";
}
return;
}
# a file link
my $dir = $f;
$dir =~ s:([^/]*\z)::;
if($link =~ s/(^\/)//) {
# link starts with a slash, now removed
$dir = "";
}
else {
while($link =~ s:^\.\.\/::) {
$dir =~ s:([^/]*)\/\z::;
}
}
$flink{"./$dir$link"} .= "$f:$line ";
}
sub findlinks {
my ($f) = @_;
my $line = 1;
open(F, "<:crlf", "$f") ||
return;
# is it a markdown extension?
my $md = ($f =~ /\.md$/i);
while(<F>) {
chomp;
if($md && /\]\(([^)]*)/) {
my $link = $1;
#print "$f:$line $link\n";
storelink($f, $line, $link);
}
# ignore trailing: dot, double quote, single quote, asterisk, hash,
# comma, question mark, colon, closing parenthesis, backslash,
# closing angle bracket, whitespace, pipe, backtick, semicolon
elsif(/(https:\/\/[a-z0-9.\/:%_+@-]+[^."'*\#,?:\)> \t|`;\\])/i) {
#print "RAW '$_'\n";
storelink($f, $line, $1);
}
$line++;
}
close(F);
}
sub checkurl {
my ($url) = @_;
if($whitelist{$url}) {
#print STDERR "$url is whitelisted\n";
return 0;
}
$url =~ s/\+/%2B/g;
my @content;
if(open(my $fh, '-|', 'curl', '-ILfsm10', '--retry', '2', '--retry-delay', '5',
'-A', 'Mozilla/curl.se link-probe', $url)) {
@content = <$fh>;
close $fh;
}
if(!$content[0]) {
print "FAIL: $url\n";
return 1; # fail
}
print "OK: $url\n";
return 0; # ok
}
for my $f (@files) {
chomp $f;
if($f !~ /\/mdlinkcheck$/) {
findlinks($f);
}
}
for my $u (sort keys %whitelist) {
if($whitelist{$u} == 1) {
printf STDERR "warning: unused whitelist entry: '$u'\n";
}
}
if($dry) {
for my $u (sort keys %url) {
print "$u\n";
}
exit;
}
my $error;
my @errlist;
for my $u (sort keys %url) {
my $r = checkurl($u);
if($r) {
for my $f (split(/ /, $url{$u})) {
push @errlist, sprintf "%s ERROR links to missing URL %s\n", $f, $u;
$error++;
}
}
}
for my $l (sort keys %flink) {
if(! -r $l) {
for my $f (split(/ /, $flink{$l})) {
push @errlist, sprintf "%s ERROR links to missing file %s\n", $f, $l;
$error++;
}
}
}
printf "Checked %d URLs\n", scalar(keys %url);
if($error) {
print "$error URLs had problems:\n";
for(@errlist) {
print $_;
}
}
exit 1 if($error);
|