Net/Prober/Probe/HTTP.pm

filter: Statementstmt 88.5% 85 / 96 Branchbran 56.5% 26 / 46 Conditioncond 48.5% 17 / 35 MC/DCmcdc 23.8% 5 / 21 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 67.7% 143 / 211 CC 37 SCAR 33.8
CC
37
Cov
72%
probe
47.3
_prepare_request
18.3
agent
2.0
CRAP 29.4
« Net/Prober/imap.pm ↑ Summary Net/Prober/ping.pm »
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'
l!l
30
MC/DC0%0/1
$Net::Prober::VERSION || 'dev'
$Net::Prober::VERSION
Truth table50%1/2
A
A= $Net::Prober::VERSION
Aresult
11
00
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"
l!l
40
MC/DC0%0/1
$method ||= "GET"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
49  
50 4 ▶ if (defined $scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scheme) 4 0 4
51 4 ▶ if ($scheme eq 'http') {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'http') { } 4 0 4
elsif ($scheme eq 'https') { } 0 0 0
52 4 ▶ $port ||= 80;
Condition100%2/2
$port ||= 80
l!l
31
MC/DC100%1/1
$port ||= 80
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
53 }
54 elsif ($scheme eq 'https') {
55 0 ▶ $port ||= 443;
Condition0%0/2
$port ||= 443
l!l
00
MC/DC0%0/1
$port ||= 443
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
56 }
57 }
58  
59 4 ▶ if (defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) 4 0 4
60 4 ▶ $scheme = $port == 443 ? "https" : "http";
Branch: 1 branch
BranchTrueFalseTotal
$port == 443 ? : 0 4 4
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
BranchTrueFalseTotal
if ($port != 80 and $port != 443) 1 3 4
Condition66%2/3
$port != 80 and $port != 443
!ll&&!rl&&r
301
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($headers and ref $headers eq "ARRAY") 1 3 4
Condition66%2/3
$headers and ref $headers eq "ARRAY"
!ll&&!rl&&r
301
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($body) 0 4 4
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
BranchTrueFalseTotal
if (defined $timeout and $timeout > 0) 3 0 3
Condition33%1/3
defined $timeout and $timeout > 0
!ll&&!rl&&r
003
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (not $up_status_re && defined $status && $status) { } 0 3 3
elsif ($up_status_re and defined $status) { } 3 0 3
Condition33%2/6
$up_status_re && defined $status && $status
!ll&&!rl&&r
003
$up_status_re and defined $status
!ll&&!rl&&r
003
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
ABCresult
0XX0
10X0
1100
1111
114 0 ▶ $good = $resp->is_redirect() || $resp->is_success();
Condition0%0/3
$resp->is_redirect || $resp->is_success
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
115 0 ▶ if (! $good) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($good) 0 0 0
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
!ll&&!rl&&r
003
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
ABresult
0X0
100
111
120 3 my $match_re;
121 eval {
122 3 $match_re = qr{$up_status_re}ms;
123 3 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $match_re = qr/$up_status_re/ms } } or do { Carp::croak("Invalid regex for HTTP status match '${up_status_re}'\n") } 0 3 3
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
BranchTrueFalseTotal
unless ($good) 1 2 3
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
BranchTrueFalseTotal
if ($good and defined $expected_md5) 0 3 3
Condition66%2/3
$good and defined $expected_md5
!ll&&!rl&&r
120
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
ABresult
0X0
100
111
133 0 my $md5 = Digest::MD5::md5_hex($content);
134 0 ▶ if ($md5 ne $expected_md5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($md5 ne $expected_md5) 0 0 0
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
BranchTrueFalseTotal
if ($good and defined $content_match) 1 2 3
Condition100%3/3
$good and defined $content_match
!ll&&!rl&&r
111
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
ABresult
0X0
100
111
141 1 my $match_re;
142 eval {
143 1 $match_re = qr{$content_match}ms;
144 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $match_re = qr/$content_match/ms } } or do { Carp::croak("Invalid regex for http content match '${content_match}'\n") } 0 1 1
145 0 Carp::croak("Invalid regex for http content match '$content_match'\n");
146 };
147 1 ▶ if ($content !~ $match_re) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content =~ /$match_re/ms) 1 0 1
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
BranchTrueFalseTotal
$content ? : 3 0 3
160 ? Digest::MD5::md5_hex($content)
161 : undef;
162  
163 3 ▶ $status{md5} = $md5 if $md5;
Branch: 1 branch
BranchTrueFalseTotal
if $md5 3 0 3
164 3 ▶ $status{reason} = $reason if defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
if defined $reason 2 1 3
165  
166 3 ▶ if ($good) {
Branch: 1 branch
BranchTrueFalseTotal
if ($good) 1 2 3
167 1 return $self->probe_ok(%status);
168 }
169  
170 2 return $self->probe_failed(%status);
171 }
172  
173 2 1;
174  
175 __END__
« Net/Prober/imap.pm ↑ Summary Net/Prober/ping.pm »