-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.pl
executable file
·455 lines (357 loc) · 12 KB
/
diff.pl
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use feature ':5.10';
use Mojo::UserAgent;
use Mojo::IOLoop;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use Text::Diff;
use Data::Dumper;
use Data::TreeDumper;
use Time::HiRes ('sleep');
my %results = ();
my (@errors, @mismatches) = ();
# ----------- Configure -----------
# Quiet
my $quiet = 1;
my $show_diff = 1;
# Limit parallel requests
my $max_conn = 50;
# Request timeout period
my $connection_timeout = 10;
# Limit spider depth
my $max_depth = 2;
# Backends to make the http requests against
my @backends = (
"varnish1.foo.com",
"varnish2.foo.com",
"drupal1.foo.com",
"drupal2.foo.com",
);
my @starting_urls = (
'http://www.foo.com',
);
# URL matcher; if the URL does not match this regex, it will never be spidered
my $url_matcher = '^https?:\/\/www\.foo\.com';
# Normalize content
# Strip any elements from the response which is expected to differ between identical requests
sub normalize_content {
my ($mojo) = @_;
# Some filtering is required for Drupal
# form build id ------------------------------------------------------------
foreach my $node ($mojo->res->dom->find('[name=form_build_id]')->each) {
# say $node;
delete $node->{value};
}
# Unique class name Drupal generates view-dom-id-<id> --------------------
foreach my $node ($mojo->res->dom->find('div.view')->each) {
# say '------------------------';
# say $node->attr('class');
my $class = $node->attr('class');
$class =~ s/ view\-dom\-id\-[a-f0-9]+//;
$node->attr('class', $class);
# say $node->attr('class');
}
# Drupal seems to screw up even/odd once in a while --------------------
foreach my $node ($mojo->res->dom->find('.even')->each) {
# say '------------------------';
# say $node->attr('class');
my $class = $node->attr('class');
$class =~ s/ even / /;
$node->attr('class', $class);
# say $node->attr('class');
}
# Drupal seems to screw up even/odd once in a while --------------------
foreach my $node ($mojo->res->dom->find('.odd')->each) {
# say '------------------------';
# say $node->attr('class');
my $class = $node->attr('class');
$class =~ s/ odd / /;
$node->attr('class', $class);
# say $node->attr('class');
}
my $content = Encode::encode_utf8($mojo->res->dom->content);
# Theme Token ------------------------------------------------------------
$content =~ s/"theme_token":"[a-zA-Z0-9\-_]+"//;
return $content;
}
# Find the URLs we want to spider through
sub get_urls {
my ($mojo) = @_;
my @urls = ();
foreach my $node ($mojo->res->dom->find('a[href]')->each) {
my $url = Mojo::URL->new($node->attr('href'));
# say $url;
if (!$url->is_abs) {
$url
->base($mojo->req->url)
->host($mojo->req->content->headers->host)
;
}
$url = $url->to_abs->to_string;
# Remove anchor portion of the URL
$url =~ s/#.*//;
# say $url;
if ($url =~ /$url_matcher/) {
# say 'matched -----------------------'
push @urls, $url;
}
}
foreach my $node ($mojo->res->dom->find('link[href]')->each) {
my $url = Mojo::URL->new($node->attr('href'));
# say $url;
if (!$url->is_abs) {
$url
->base($mojo->req->url)
->host($mojo->req->content->headers->host)
;
}
$url = $url->to_abs->to_string;
# Remove anchor portion of the URL
$url =~ s/#.*//;
# say $url;
if ($url =~ /$url_matcher/) {
# say 'matched -----------------------'
push @urls, $url;
}
}
return @urls;
}
# ----------- Run -----------
# Disable line buffering
# $| = 1;
# FIFO queue
my @queue = ();
# This tracks any URL that has been queued in the past so we only queue it once
my %queued_urls = ();
# Keep track of active connections
my $active = 0;
my $ua = Mojo::UserAgent->new;
# Ignore all cookies
$ua->cookie_jar->ignore(sub { 1 });
$ua = $ua->max_connections(10);
$ua = $ua->max_redirects(0);
$ua = $ua->request_timeout($connection_timeout);
# Iterate over each starting URL and add to the queue
foreach my $url (@starting_urls) {
push @queue, {
url => $url,
backend => $backends[0],
callback => 'main_request_callback',
level => 0,
};
$queued_urls{get_req_hash('GET', $url)} = 1;
}
# Callback for backend[0]
# This callback will trigger a crawl of the URLs on the page
# This will also trigger a request to each backend to compare the results
sub main_request_callback {
my ($ua, $mojo) = @_;
# say '--$active!';
--$active;
unless ($mojo->success) {
push @errors, 'Error: ' . $mojo->res->error->{message} . ' ' . $mojo->req->url;
say STDERR 'Error: ' . $mojo->res->error->{message} . ' ' . $mojo->req->url;
# say STDERR DumpTree($mojo);
return;
}
# Replace the host in the URL with the Host sent in the header for clarity
my $backend = $mojo->req->url->host;
my $host = $mojo->req->content->headers->host;
my $current_level = $mojo->req->content->headers->referrer;
$mojo->req->url->host($mojo->req->content->headers->host);
if (
$mojo->res->code != 200 &&
$mojo->res->code != 301 &&
$mojo->res->code != 302
) {
push @errors, sprintf('%s at %s; backend: %s', $mojo->res->code, $mojo->req->url->to_string, $host);
say STDERR sprintf('%s at %s; backend: %s', $mojo->res->code, $mojo->req->url->to_string, $host);
return;
}
store_results($ua, $mojo, $backend);
# Make a new request for each backend
foreach my $backend_host (@backends[1 .. $#backends]) {
# say 'Request to another backend ' . $mojo->req->url->to_string . ' ' . $backend_host;
push @queue, {
url => $mojo->req->url->to_string,
backend => $backend_host,
callback => 'sub_request_callback',
level => $current_level,
};
}
# Don't try to fetch URLs if we've reached our maximum recursion depth
return if $current_level >= $max_depth;
# Don't try to fetch URLs from pages that are not text/html
return if $mojo->res->headers->content_type !~ /^text\/html.*/;
# Now do recursion and fetch pages linked to from this page
foreach my $url (get_urls($mojo)) {
# Check if we already queued this page
my $key = get_req_hash('GET', $url);
if ($queued_urls{$key}) {
# say sprintf('%s: Already queued this page; skipping.', $url);
next;
}
# say 'Queuing ' . $url . ' for backend[0]';
push @queue, {
url => $url,
backend => $backends[0],
callback => 'main_request_callback',
level => $current_level + 1,
};
$queued_urls{get_req_hash('GET', $url)} = 1;
}
return;
}
# Call back for each backend other than backend[0]
# This call back will compare the results to the other backend
sub sub_request_callback {
my ($ua, $mojo) = @_;
# say '--$active!';
--$active;
unless ($mojo->success) {
push @errors, 'Error: ' . $mojo->res->error->{message} . ' ' . $mojo->req->url;
say STDERR 'Error: ' . $mojo->res->error->{message} . ' ' . $mojo->req->url;
# say STDERR DumpTree($mojo);
return;
}
# Replace the host in the URL with the Host sent in the header for clarity
my $backend = $mojo->req->url->host;
my $host = $mojo->req->content->headers->host;
$mojo->req->url->host($mojo->req->content->headers->host);
if (
$mojo->res->code != 200 &&
$mojo->res->code != 301 &&
$mojo->res->code != 302
) {
push @errors, sprintf('%s at %s', $mojo->res->code, $mojo->req->url->to_string);
say STDERR sprintf('%s at %s', $mojo->res->code, $mojo->req->url->to_string);
return;
}
store_results($ua, $mojo, $backend);
my $key = get_req_hash($mojo->req->method, $mojo->req->url->to_string);
# Compare the results
if ($mojo->res->code == 301 or $mojo->res->code == 302) {
if (
$results{$backend}{$key}{'code'} != $results{$backends[0]}{$key}{'code'}
or
$results{$backend}{$key}{'location'} ne $results{$backends[0]}{$key}{'location'}
) {
push @mismatches, sprintf('Mismatch at %s, backend: %s', $mojo->req->url->to_string, $backend);
say STDERR sprintf('Mismatch at %s, backend: %s', $mojo->req->url->to_string, $backend);
say STDERR '- ' . $results{$backends[0]}{$key}{'code'} . ' ' . $results{$backends[0]}{$key}{'location'};
say STDERR '+ ' . $results{$backend}{$key}{'code'} . ' ' . $results{$backend}{$key}{'location'};
}
} else {
if ($results{$backend}{$key}{'hash'} ne $results{$backends[0]}{$key}{'hash'}) {
my $diff = diff(\$results{$backends[0]}{$key}{'content'} => \$results{$backend}{$key}{'content'});
push @mismatches, sprintf('Mismatch at %s, backend: %s', $mojo->req->url->to_string, $backend);
say STDERR sprintf('Mismatch at %s, backend: %s', $mojo->req->url->to_string, $backend);
if ($show_diff) {
say STDERR $diff;
}
}
}
# Delete the stored content for the non-primary backends after we are done with it (possible diff)
delete $results{$backend}{$key}{'content'};
}
# Store the results
sub store_results {
my ($ua, $mojo, $backend) = @_;
# say sprintf('Storing %s for %s', $mojo->req->url->to_string, $backend);
my $key = get_req_hash($mojo->req->method, $mojo->req->url->to_string);
# Handle redirects
if ($mojo->res->code == 301 or $mojo->res->code == 302) {
my $location = $mojo->res->headers->location;
$results{$backend}{$key} = {
url => $mojo->req->url->to_string,
code => $mojo->res->code,
location => $location,
};
unless ($quiet) {
say $mojo->res->code . ' ' . $mojo->req->url->to_string . ' -> ' . $location . ' ' . $backend;
# print $mojo->res->code . ' ' . $mojo->req->url->to_string . ' -> ' . $location . ' ' . $backend . "\r";
}
return;
}
my ($content, $hash, $title);
if ($mojo->res->headers->content_type =~ /^text\/html.*/) {
$content = normalize_content($mojo);
$hash = md5_hex($content);
$title = $mojo->res->dom->at('title')->text;
} elsif ($mojo->res->headers->content_type =~ /text\/.+/) {
$content = $mojo->res->content->get_body_chunk;
$hash = md5_hex($content);
$title = $mojo->req->url->to_string;
} else {
$content = 'Binary: ' . $mojo->res->content->body_size . ' bytes';
$hash = md5_hex($mojo->res->content->get_body_chunk);
$title = $mojo->req->url->to_string;
}
$results{$backend}{$key} = {
url => $mojo->req->url->to_string,
code => $mojo->res->code,
title => $title,
hash => $hash,
content => $content,
};
unless ($quiet) {
say $mojo->req->url->to_string . ' ' . $mojo->res->code . ' ' . $backend . ' ' . md5_hex($content);
# print $mojo->req->url->to_string . ' ' . $mojo->res->code . ' ' . $backend . ' ' . md5_hex($content) . "\r";
}
}
# Create a standard identifier for a request
# md5 of the request method and URL
sub get_req_hash {
my ($method, $url) = @_;
# Remove any anchor/fragment
$url =~ s/#.*//;
return md5_hex($method . $url);
}
Mojo::IOLoop->recurring(
0 => sub {
# say $active, ' ', scalar @queue;
# If we've reached our maximum number of connections, pause
if ($active >= $max_conn) {
return;
}
# Empty queue, but still active requests
if ($active and not scalar @queue) {
return;
}
# Check if we are finished and print summary if so
if ($active == 0 and scalar @queue == 0) {
say '';
say '--------------------------------------------------------';
say '---------------------- Summary -------------------------';
say '--------------------------------------------------------';
say '';
say '------------- Errors ---------------' if scalar @errors;
foreach my $error (sort @errors) {
say $error;
}
say '------------ Mismatches ------------' if scalar @mismatches;
foreach my $mismatch (sort @mismatches) {
say $mismatch;
}
say 'Everything OK!' unless (scalar @errors or scalar @mismatches);
Mojo::IOLoop->stop;
return;
}
# say '++$active!';
++$active;
my $item = shift @queue;
my $url = Mojo::URL->new($item->{url});
# Replace the host portion of the URL with the backend
my $host = $url->host;
$url->host($item->{backend});
# say $url->to_string;
# Set Host header to the host we want (since the url is to the backend)
# Slight hack to keep track of the tree depth; set Referer to the depth of the request
# allowing us to check it in the callback.
$ua->get($url => {Host => $host, referer => $item->{level}} => \&{$item->{callback}});
}
);
# Start event loop
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;