| 1 | package Net::Prober::Probe::HTTP; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | $Net::Prober::Probe::HTTP::VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use base 'Net::Prober::Probe::TCP'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Digest::MD5 (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use LWPx::ParanoidAgent (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | sub defaults { | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | my $defaults = $self->SUPER::defaults; | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | my %http_defaults = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | %{ $defaults }, | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | headers => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | md5 => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | method => 'GET', | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | port => 80, | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | scheme => 'http', | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | url => '/', | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | match => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | body => undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | up_status_re => '^[23]\d\d$', | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | return \%http_defaults; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | sub agent { | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | my $ua = LWPx::ParanoidAgent->new(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
agent: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | ▶ | my $ver = $Net::Prober::VERSION || 'dev'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$Net::Prober::VERSION || 'dev'
MC/DC0%0/1
$Net::Prober::VERSION || 'dev'
$Net::Prober::VERSION
Truth table50%1/2
A
A= $Net::Prober::VERSION
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | $ua->agent("Net::Prober/$ver"); | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | $ua->max_redirect(0); | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | return $ua; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | sub _prepare_request { | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare_request: called CC=14 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | my ($host, $port, $timeout, $scheme, $url, $method, $body, $headers) = | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | $self->parse_args($args, qw(host port timeout scheme url method body headers)); | ||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | $method ||= "GET"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$method ||= "GET"
MC/DC0%0/1
$method ||= "GET"
$method
Truth table50%1/2
A
A= $method
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | ▶ | if (defined $scheme) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | if ($scheme eq 'http') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | $port ||= 80; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$port ||= 80
MC/DC100%1/1
$port ||= 80
$port
Truth table100%2/2
A
A= $port
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | elsif ($scheme eq 'https') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $port ||= 443; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$port ||= 443
MC/DC0%0/1
$port ||= 443
$port
Truth table0%0/2
A
A= $port
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | ▶ | if (defined $port) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | $scheme = $port == 443 ? "https" : "http"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | $url =~ s{^/+}{}; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # We don't want to add :80 or :443 because some pesky Asian CDN | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # doesn't like when Host header contains those default ports | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 4 | my $probe_url = "$scheme://$host/$url"; | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | if ($port != 80 && $port != 443) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$port != 80 and $port != 443
MC/DC50%1/2
$port != 80 and $port != 443
$port != 80
$port != 443
Truth table66%2/3
A and B
A= $port != 80B= $port != 443
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | $probe_url = "$scheme://$host:$port/$url"; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | my @req_args = ($method, $probe_url); | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 4 | ▶ | if ($headers && ref $headers eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$headers and ref $headers eq "ARRAY"
MC/DC50%1/2
$headers and ref $headers eq "ARRAY"
$headers
ref $headers eq "ARRAY"
Truth table66%2/3
A and B
A= $headersB= ref $headers eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | my $req_headers = HTTP::Headers->new(); | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | $req_headers->header(@{ $headers }); | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | push @req_args, $req_headers; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | if ($body) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | push @req_args, $body; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | return HTTP::Request->new(@req_args); | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | sub probe { | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
probe: called CC=23 SCAR=38.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | my ($expected_md5, $content_match, $up_status_re, $timeout) = | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | $self->parse_args($args, qw(md5 match up_status_re timeout)); | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | $self->time_now(); | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | my $ua = $self->agent(); | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | ▶ | if (defined $timeout && $timeout > 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $timeout and $timeout > 0
MC/DC0%0/2
defined $timeout and $timeout > 0
defined $timeout
$timeout > 0
Truth table33%1/3
A and B
A= defined $timeoutB= $timeout > 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | $ua->timeout($timeout); | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | my $req = $self->_prepare_request($args); | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # Fire in the hole! | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | my $resp = $ua->request($req); | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | my $elapsed = $self->time_elapsed(); | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | my $content = $resp->content(); | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | my $status = $resp->code(); | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | my $good = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | my $reason; | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | if (! $up_status_re || ! defined $status || ! $status) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$up_status_re && defined $status && $status
$up_status_re and defined $status
MC/DC0%0/3
$up_status_re && defined $status && $status
$up_status_re
defined $status
$status
Truth table25%1/4
A and B && C
A= $up_status_reB= defined $statusC= $status
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $good = $resp->is_redirect() || $resp->is_success(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$resp->is_redirect || $resp->is_success
MC/DC0%0/2
$resp->is_redirect || $resp->is_success
$resp->is_redirect
$resp->is_success
Truth table0%0/3
A || B
A= $resp->is_redirectB= $resp->is_success
| |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if (! $good) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $reason = "Response HTTP status code wasn't successful (2xx or 3xx)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | ▶ | elsif ($up_status_re && defined $status) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$up_status_re and defined $status
MC/DC0%0/2
$up_status_re and defined $status
$up_status_re
defined $status
Truth table33%1/3
A and B
A= $up_status_reB= defined $status
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | my $match_re; | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 3 | $match_re = qr{$up_status_re}ms; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | Carp::croak("Invalid regex for HTTP status match '$up_status_re'\n"); | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | $good = $status =~ $match_re; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | ▶ | if (! $good) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | $reason = "Response HTTP status code didn't match the specified regex ('$up_status_re')"; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | ▶ | if ($good and defined $expected_md5) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$good and defined $expected_md5
MC/DC0%0/2
$good and defined $expected_md5
$good
defined $expected_md5
Truth table66%2/3
A and B
A= $goodB= defined $expected_md5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my $md5 = Digest::MD5::md5_hex($content); | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ($md5 ne $expected_md5) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $good = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | $reason = "Response body MD5 sum wasn't the expected ($expected_md5)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 3 | ▶ | if ($good and defined $content_match) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$good and defined $content_match
MC/DC100%2/2
$good and defined $content_match
$good
defined $content_match
Truth table100%3/3
A and B
A= $goodB= defined $content_match
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | my $match_re; | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | eval { | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | $match_re = qr{$content_match}ms; | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | Carp::croak("Invalid regex for http content match '$content_match'\n"); | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | ▶ | if ($content !~ $match_re) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | $good = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | $reason = "Content didn't match the specified '$content_match' regex"; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | my %status = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | status => $resp->status_line, | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | content => $content, | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | elapsed => $elapsed, | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 3 | ▶ | my $md5 = $content | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | ? Digest::MD5::md5_hex($content) | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | : undef; | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 3 | ▶ | $status{md5} = $md5 if $md5; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 3 | ▶ | $status{reason} = $reason if defined $reason; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | ▶ | if ($good) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | return $self->probe_ok(%status); | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 2 | return $self->probe_failed(%status); | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||