]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/thirdparty/curl/perl/contrib/crawlink.pl
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / thirdparty / curl / perl / contrib / crawlink.pl
1 #!/usr/bin/perl
2 #
3 # crawlink.pl
4 #
5 # This script crawls across all found links below the given "root" URL.
6 # It reports all good and bad links to stdout. This code was based on the
7 # checklink.pl script I wrote ages ago.
8 #
9 # Written to use 'curl' for URL checking.
10 #
11 # Author: Daniel Stenberg <daniel@haxx.se>
12 # Version: 0.3 Jan 3, 2001
13 #
14 # HISTORY
15 #
16 # 0.3 - The -i now adds regexes that if a full URL link matches one of those,
17 #       it is not followed. This can then be used to prevent this script from
18 #       following '.*\.cgi', specific pages or whatever.
19 #
20 # 0.2 - Made it only HEAD non html files (i.e skip the GET). Makes it a lot
21 #       faster to skip large non HTML files such as pdfs or big RFCs! ;-)
22 #       Added a -c option that allows me to pass options to curl.
23 #
24 # 0.1 - The given url works as the root. This script will only continue
25 #       and check other URLs if the leftmost part of the new URL is identical
26 #       to the root URL.
27 #
28
29 use strict;
30
31 my $in="";
32 my $verbose=0;
33 my $usestdin;
34 my $linenumber;
35 my $help;
36 my $external;
37 my $curlopts;
38
39 my @ignorelist;
40
41  argv:
42 if($ARGV[0] eq "-v" ) {
43     $verbose++;
44     shift @ARGV;
45     goto argv;
46 }
47 elsif($ARGV[0] eq "-c" ) {
48     $curlopts=$ARGV[1];
49     shift @ARGV;
50     shift @ARGV;
51     goto argv;
52 }
53 elsif($ARGV[0] eq "-i" ) {
54     push @ignorelist, $ARGV[1];
55     shift @ARGV;
56     shift @ARGV;
57     goto argv;
58 }
59 elsif($ARGV[0] eq "-l" ) {
60     $linenumber = 1;
61     shift @ARGV;
62     goto argv;
63 }
64 elsif($ARGV[0] eq "-h" ) {
65     $help = 1;
66     shift @ARGV;
67     goto argv;
68 }
69 elsif($ARGV[0] eq "-x" ) {
70     $external = 1;
71     shift @ARGV;
72     goto argv;
73 }
74
75 my $geturl = $ARGV[0];
76 my $firsturl= $geturl;
77
78 #
79 # Define a hash array to hold all root URLs to visit/we have visited
80 #
81 my %rooturls;
82 $rooturls{$ARGV[0]}=1;
83
84 if(($geturl eq "") || $help) {
85     print  "Usage: $0 [-hilvx] <full URL>\n",
86     " Use a traling slash for directory URLs!\n",
87     " -c [data]  Pass [data] as argument to every curl invoke\n",
88     " -h         This help text\n",
89     " -i [regex] Ignore root links that match this pattern\n",
90     " -l         Line number report for BAD links\n",
91     " -v         Verbose mode\n",
92     " -x         Check non-local (external?) links only\n";
93     exit;
94 }
95
96 my $proxy;
97 if($curlopts ne "") {
98     $proxy=" $curlopts";
99     #$proxy =" -x 194.237.142.41:80";
100 }
101
102 # linkchecker, URL will be appended to the right of this command line
103 # this is the one using HEAD:
104 my $linkcheck = "curl -s -m 20 -I$proxy";
105
106 # as a second attempt, this will be used. This is not using HEAD but will
107 # get the whole frigging document!
108 my $linkcheckfull = "curl -s -m 20 -i$proxy";
109
110 # htmlget, URL will be appended to the right of this command line
111 my $htmlget = "curl -s$proxy";
112
113 # Parse the input URL and split it into the relevant parts:
114
115 my $getprotocol;
116 my $getserver;
117 my $getpath;
118 my $getdocument;
119
120 my %done;
121 my %tagtype;
122 my $allcount=0;
123 my $badlinks=0;
124
125 sub SplitURL {
126     my $inurl = $_[0];
127     if($inurl=~ /^([^:]+):\/\/([^\/]*)\/(.*)\/(.*)/ ) {
128         $getprotocol = $1;
129         $getserver = $2;
130         $getpath = $3;
131         $getdocument = $4;
132     }
133     elsif ($inurl=~ /^([^:]+):\/\/([^\/]*)\/(.*)/ ) {
134         $getprotocol = $1;
135         $getserver = $2;
136         $getpath = $3;
137         $getdocument = "";
138
139         if($getpath !~ /\//) {
140             $getpath ="";
141             $getdocument = $3;
142         }
143
144     }
145     elsif ($inurl=~ /^([^:]+):\/\/(.*)/ ) {
146         $getprotocol = $1;
147         $getserver = $2;
148         $getpath = "";
149         $getdocument = "";
150     }
151     else {
152         print "Couldn't parse the specified URL, retry please!\n";
153         exit;
154     }
155 }
156
157 my @indoc;
158
159 sub GetRootPage {
160     my $geturl = $_[0];
161     my $in="";
162     my $code=200;
163     my $type="text/plain";
164
165     my $pagemoved=0;
166     open(HEADGET, "$linkcheck $geturl|") ||
167         die "Couldn't get web page for some reason";
168
169     while(<HEADGET>) {
170         #print STDERR $_;
171         if($_ =~ /HTTP\/1\.[01] (\d\d\d) /) {
172             $code=$1;
173             if($code =~ /^3/) {
174                 $pagemoved=1;
175             }
176         }
177         elsif($_ =~ /^Content-Type: ([\/a-zA-Z]+)/) {
178             $type=$1;
179         }
180         elsif($pagemoved &&
181                 ($_ =~ /^Location: (.*)/)) {
182             $geturl = $1;
183
184             &SplitURL($geturl);
185
186             $pagemoved++;
187             last;
188         }
189     }
190     close(HEADGET);
191
192     if($pagemoved == 1) {
193         print "Page is moved but we don't know where. Did you forget the ",
194             "traling slash?\n";
195         exit;
196     }
197
198     if($type ne "text/html") {
199         # there no point in getting anything but HTML
200         $in="";
201     }
202     else {
203         open(WEBGET, "$htmlget $geturl|") ||
204             die "Couldn't get web page for some reason";
205         while(<WEBGET>) {
206             my $line = $_;
207             push @indoc, $line;
208             $line=~ s/\n/ /g;
209             $line=~ s/\r//g;
210             $in=$in.$line;
211         }
212         close(WEBGET);
213     }
214     return ($in, $code, $type);
215 }
216
217 sub LinkWorks {
218     my $check = $_[0];
219
220 #   URL encode:
221 #    $check =~s/([^a-zA-Z0-9_:\/.-])/uc sprintf("%%%02x",ord($1))/eg;
222
223     my @doc = `$linkcheck \"$check\"`;
224
225     my $head = 1;
226
227 #    print "COMMAND: $linkcheck \"$check\"\n";
228 #    print $doc[0]."\n";
229
230   boo:
231     if( $doc[0] =~ /^HTTP[^ ]+ (\d+)/ ) {
232         my $error = $1;
233
234         if($error < 400 ) {
235             return "GOOD";
236         }
237         else {
238
239             if($head && ($error >= 500)) {
240                 # This server doesn't like HEAD!
241                 @doc = `$linkcheckfull \"$check\"`;
242                 $head = 0;
243                 goto boo;
244             }
245             return "BAD";
246         }
247     }
248     return "BAD";
249 }
250
251
252 sub GetLinks {
253     my $in = $_[0];
254     my @result;
255
256     while($in =~ /[^<]*(<[^>]+>)/g ) {
257         # we have a tag in $1
258         my $tag = $1;
259
260         if($tag =~ /^<!--/) {
261             # this is a comment tag, ignore it
262         }
263         else {
264             if($tag =~ /(src|href|background|archive) *= *(\"[^\"]\"|[^ \)>]*)/i) {
265                 my $url=$2;
266                 if($url =~ /^\"(.*)\"$/) {
267                     # this was a "string" now $1 has removed the quotes:
268                     $url=$1;
269                 }
270
271
272                 $url =~ s/([^\#]*)\#.*/$1/g;
273
274                 if($url eq "") {
275                     # if the link was nothing than a #-link it may now have
276                     # been emptied completely so then we skip the rest
277                     next;
278                 }
279
280                 if($done{$url}) {
281                     # if this url already is done, do next
282                     $done{$url}++;
283                     if($verbose) {
284                         print " FOUND $url but that is already checked\n";
285                     }
286                     next;
287                 }
288
289                 $done{$url} = 1; # this is "done"
290
291                 push @result, $url;
292                 if($tag =~ /< *([^ ]+)/) {
293                     $tagtype{$url}=$1;
294                 }
295             }
296         }
297     }
298     return @result;
299 }
300
301
302 while(1) {
303     $geturl=-1;
304     for(keys %rooturls) {
305         if($rooturls{$_} == 1) {
306             if($_ !~ /^$firsturl/) {
307                 $rooturls{$_} += 1000; # don't do this, outside our scope
308                 if($verbose) {
309                     print "SKIP: $_\n";
310                 }
311                 next;
312             }
313             $geturl=$_;
314             last;
315         }
316     }
317     if($geturl == -1) {
318         last;
319     }
320
321     #
322     # Splits the URL in its different parts
323     #
324     &SplitURL($geturl);
325
326     #
327     # Returns the full HTML of the root page
328     #
329     my ($in, $error, $ctype) = &GetRootPage($geturl);
330
331     $rooturls{$geturl}++; # increase to prove we have already got it
332
333     if($ctype ne "text/html") {
334         # this is not HTML, we skip this
335         if($verbose == 2) {
336             print "Non-HTML link, skipping\n";
337             next;
338         }
339     }
340
341     if($error >= 400) {
342         print "ROOT page $geturl returned $error\n";
343         next;
344     }
345
346     print "    ==== $geturl ====\n";
347
348     if($verbose == 2) {
349         printf("Error code $error, Content-Type: $ctype, got %d bytes\n",
350                length($in));
351     }
352
353     #print "protocol = $getprotocol\n";
354     #print "server = $getserver\n";
355     #print "path = $getpath\n";
356     #print "document = $getdocument\n";
357     #exit;
358
359     #
360     # Extracts all links from the given HTML buffer
361     #
362     my @links = &GetLinks($in);
363
364     for(@links) {
365         my $url = $_;
366         my $link;
367
368         if($url =~ /^([^:]+):/) {
369             my $prot = $1;
370             if($prot !~ /http/i) {
371                 # this is an unsupported protocol, we ignore this
372                 next;
373             }
374             $link = $url;
375         }
376         else {
377             if($external) {
378                 next;
379             }
380
381             # this is a link on the same server:
382             if($url =~ /^\//) {
383                 # from root
384                 $link = "$getprotocol://$getserver$url";
385             }
386             else {
387                 # from the scanned page's dir
388                 my $nyurl=$url;
389
390                 if(length($getpath) &&
391                    ($getpath !~ /\/$/) &&
392                    ($nyurl !~ /^\//)) {
393                     # lacks ending slash, add one to the document part:
394                     $nyurl = "/".$nyurl;
395                 }
396                 $link = "$getprotocol://$getserver/$getpath$nyurl";
397             }
398         }
399
400         my $success = &LinkWorks($link);
401
402         my $count = $done{$url};
403
404         $allcount += $count;
405
406         print "$success $count <".$tagtype{$url}."> $link $url\n";
407
408         if("BAD" eq $success) {
409             $badlinks++;
410             if($linenumber) {
411                 my $line =1;
412                 for(@indoc) {
413                     if($_ =~ /$url/) {
414                         print " line $line\n";
415                     }
416                     $line++;
417                 }
418             }
419         }
420         else {
421             # the link works, add it if it isn't in the ingore list
422             my $ignore=0;
423             for(@ignorelist) {
424                 if($link =~ /$_/) {
425                     $ignore=1;
426                 }
427             }
428             if(!$ignore) {
429                 # not ignored, add
430                 $rooturls{$link}++; # check this if not checked already
431             }
432         }
433
434     }
435 }
436
437 if($verbose) {
438     print "$allcount links were checked";
439     if($badlinks > 0) {
440         print ", $badlinks were found bad";
441     }
442     print "\n";
443 }