Skip to content

Commit

Permalink
Promoted probe() method to parent class
Browse files Browse the repository at this point in the history
  • Loading branch information
cosimo committed Feb 2, 2012
1 parent 158e338 commit 3920313
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/Net/Prober/Probe/HTTP.pm
Expand Up @@ -5,6 +5,8 @@ use warnings;

use base 'Net::Prober::Probe::TCP';

use Carp ();
use Digest::MD5 ();
use LWPx::ParanoidAgent ();

sub defaults {
Expand Down Expand Up @@ -33,4 +35,72 @@ sub agent {
return $ua;
}

sub probe {
my ($self, $args) = @_;

my ($host, $port, $timeout, $scheme, $url, $expected_md5, $content_match) =
$self->parse_args($args, qw(host port timeout scheme url md5 match));

if (defined $scheme) {
if ($scheme eq 'http') {
$port ||= 80;
}
elsif ($scheme eq 'https') {
$port ||= 443;
}
}
elsif (defined $port) {
$scheme = $port == 443 ? "https" : "http";
}

$url =~ s{^/+}{};

my $probe_url = "$scheme://$host:$port/$url";

$self->time_now();

my $ua = $self->agent();
my $resp = $ua->get($probe_url);
my $elapsed = $self->time_elapsed();
my $content = $resp->content();
my $good = $resp->is_redirect() || $resp->is_success();

if ($good and defined $expected_md5) {
my $md5 = Digest::MD5::md5_hex($content);
if ($md5 ne $expected_md5) {
$good = 0;
}
}

if ($good and defined $content_match) {
my $match_re;
eval {
$match_re = qr{$content_match}ms;
} or do {
Carp::croak("Invalid regex for http content match '$content_match'\n");
};
if ($content !~ $match_re) {
$good = 0;
}
}

my %status = (
status => $resp->status_line,
content => $content,
elapsed => $elapsed,
);

my $md5 = $content
? Digest::MD5::md5_hex($content)
: undef;

$status{md5} = $md5 if $md5;

if ($good) {
return $self->probe_ok(%status);
}

return $self->probe_failed(%status);
}

1;

0 comments on commit 3920313

Please sign in to comment.