-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspidercache.pl
More file actions
152 lines (120 loc) · 3.49 KB
/
Copy pathspidercache.pl
File metadata and controls
152 lines (120 loc) · 3.49 KB
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
#!/usr/bin/perl
#
# spidercache.pl
#
# SpiderCache crawls a URI and determines the HIT ratio (based on value in X-Cache header)
#
# http://github.com/djinns/SpiderCache
#
# Copyright (C) 2011 djinns@chninkel.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#------------------------------------------------
# LIBS
use warnings;
use strict;
use LWP::UserAgent;
use HTML::Parse;
use Getopt::Long;
use URI;
#------------------------------------------------
# options
my $o_url;
my $o_matchurl;
#------------------------------------------------
# parameters
my $version="0.1";
my $userAgent="SpiderCache - version ". $version ." - https://github.com/djinns/SpiderCache";
my $timeout=60;
my $validcode="(200|304)";
my $cacheHeader="X-Cache";
my $cachePattern="HIT";
#------------------------------------------------
# vars
my $hit=0;
my $miss=0;
my $total=1; # because there is always at least 1 request !
#------------------------------------------------
# functions
sub check_options {
Getopt::Long::Configure ("bundling");
GetOptions(
'u:s' => \$o_url, 'url:s' => \$o_url,
);
if (!defined($o_url)) {
die('invalid options !');
}
}
#------------------------------------------------
# Main
check_options();
my $ua = new LWP::UserAgent;
my $uri = URI->new($o_url);
print "\nSpider Cache - version $version\n\n";
print "\tMiss URL:\n\n";
$ua->agent($userAgent);
$ua->timeout($timeout);
my $request = HTTP::Request->new('GET');
$request->url($o_url);
my $response = $ua->request($request);
# Page exits or response valid
if($response->code =~ /$validcode/) {
if(defined($response->header($cacheHeader))) {
if($response->header($cacheHeader) =~ /$cachePattern/) {
$hit++;
}
} else {
$miss++;
print "\t\t$o_url (".$response->code.")\n";
}
my $body = $response->content;
my $parsed_html = HTML::Parse::parse_html($body);
# inspect HTML head and body parts: img, script, style tags
for (@{ $parsed_html->extract_links(qw(head body img script style)) }) {
my ($link) = @$_;
$total++;
my $ua2 = new LWP::UserAgent;
$ua2->agent($userAgent);
$ua2->timeout($timeout);
my $request2 = HTTP::Request->new('GET');
my $url2;
if($link =~ /^\//) {
$url2="http://".$uri->host."".$link;
} elsif ($link =~ /^http/) {
$url2=$link;
} elsif ($link !~ /^\//) {
$url2="$o_url/$link";
} else {
die('link ?!');
}
$request2->url($url2);
my $response2 = $ua2->request($request2);
if(defined($response2->header($cacheHeader))) {
if($response2->header($cacheHeader) =~ /$cachePattern/) {
$hit++;
}
} else {
$miss++;
print "\t\t$url2 (".$response2->code.")\n";
}
}
print "\n\tCache stats:\n";
print "\t\tRequests : ". $total ."\n";
print "\t\tHit : ". $hit ."\n";
print "\t\tMiss : ". $miss ."\n\n";
printf("\t\tHit rate : %.2f%%\n\n", ($hit*100)/$total);
} else {
print "bad response code: $o_url : ". $response->code ."\n\n";
}