| 1 | package Nagios::WebTransact; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use vars qw($VERSION) ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | $VERSION = '0.16'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use HTTP::Request::Common qw(GET POST HEAD) ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use HTTP::Cookies ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use LWP::UserAgent ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Carp ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use constant FALSE => 0 ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use constant TRUE => ! FALSE ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use constant FAIL => FALSE ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use constant OK => TRUE ; # ie Normal Perl semantics. 'Success' is TRUE (1). | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | # Caller must map this to Unix/Nagios return codes. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use constant Field_Refs => { Method => { is_ref => FALSE, type => '' }, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | Url => { is_ref => FALSE, type => '' }, | ||||||||||||||||||||||||||||
| 22 | Qs_var => { is_ref => TRUE, type => 'ARRAY' }, | ||||||||||||||||||||||||||||
| 23 | Qs_fixed => { is_ref => TRUE, type => 'ARRAY' }, | ||||||||||||||||||||||||||||
| 24 | Exp => { is_ref => FALSE, type => 'ARRAY' }, | ||||||||||||||||||||||||||||
| 25 | Exp_Fault => { is_ref => FALSE, type => '' }, | ||||||||||||||||||||||||||||
| 26 | 4 | } ; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use constant AGENT => 'Mozilla/4.7' ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=17 SCAR=34.3
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 2 | my ($obj, $urls_ar) = @_ ; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # $urls_ar is a ref to a list of hashes (representing a request record) in a partic format. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # If a hash is __not__ in that format it's much better to croak since it is | ||||||||||||||||||||||||||||
| 36 | # hard to interpret 'not an array ref' messages (from check::make_req) caused | ||||||||||||||||||||||||||||
| 37 | # by mis spelled or mistaken field names. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | &outahere( $urls_ar, 'URL list is not an array reference.' ) if ref $urls_ar ne 'ARRAY' ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 2 | my @urls = @$urls_ar ; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | foreach my $url ( @urls ) { | |||||||||||||||||||||||||||
| 43 | 8 | ▶ | &outahere( $url, 'Request record is not a hash.' ) if ref $url ne 'HASH' ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 8 | my @keys = keys %$url ; | |||||||||||||||||||||||||||
| 45 | 8 | foreach my $key ( @keys ) { | |||||||||||||||||||||||||||
| 46 | 48 | ▶ | if ( ! exists Field_Refs->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | warn "Expected keys: ", join " ", keys %{ (Field_Refs) } ; | |||||||||||||||||||||||||||
| 48 | 0 | &outahere( $url, "Unexpected key \"$key\" in record." ) ; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 48 | my $ref_type = '' ; | |||||||||||||||||||||||||||
| 51 | 48 | ▶ | if ( ($ref_type = ref $url->{$key}) && ( $ref_type ne Field_Refs->{$key}{type} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ref_type = ref $url->{$key} and $ref_type ne {"Exp" => {"is_ref" => 0,"type" => "ARRAY"},"Exp_Fault" => {"is_ref" => 0,"type" => ""},"Method" => {"is_ref" => 0,"type" => ""},"Qs_fixed" => {"is_ref" => !!1,"type" => "ARRAY"},"Qs_var" => {"is_ref" => !!1,"type" => "ARRAY"},"Url" => {"is_ref" => 0,"type" => ""}}->{$key}{"type"}
MC/DC0%0/2
$ref_type = ref $url->{$key} and $ref_type ne {"Exp" => {"is_ref" => 0,"type" => "ARRAY"},"Exp_Fault" => {"is_ref" => 0,"type" => ""},"Method" => {"is_ref" => 0,"type" => ""},"Qs_fixed" => {"is_ref" => !!1,"type" => "ARRAY"},"Qs_var" => {"is_ref" => !!1,"type" => "ARRAY"},"Url" => {"is_ref" => 0,"type" => ""}}->{$key}{"type"}
$ref_type = ref $url->{$key}
$ref_type ne {"Exp" => {"is_ref" => 0,"type" => "ARRAY"},"Exp_Fault" => {"is_ref" => 0,"type" => ""},"Method" => {"is_ref" => 0,"type" => ""},"Qs_fixed" => {"is_ref" => !!1,"type" => "ARRAY"},"Qs_var" => {"is_ref" => !!1,"type" => "ARRAY"},"Url" => {"is_ref" => 0,"type" => ""}}->{$key}{"type"}
Truth table66%2/3
A and B
A= $ref_type = ref $url->{$key}B= $ref_type ne {"Exp" => {"is_ref" => 0,"type" => "ARRAY"},"Exp_Fault" => {"is_ref" => 0,"type" => ""},"Method" => {"is_ref" => 0,"type" => ""},"Qs_fixed" => {"is_ref" => !!1,"type" => "ARRAY"},"Qs_var" => {"is_ref" => !!1,"type" => "ARRAY"},"Url" => {"is_ref" => 0,"type" => ""}}->{$key}{"type"}
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | warn "Expected key \"$key\" to be ", Field_Refs->{$key}{type} ? Field_Refs->{$key}{type} . ' ref' : 'non ref', "\n" ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | &outahere( $url, "Field \"$key\" has wrong reference type" ) ; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 48 | ▶ | if ( ! ref $url->{$key} and Field_Refs->{$key}{is_ref} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $url->{$key} and {"Exp" => {"is_ref" => 0,"type" => "ARRAY"},"Exp_Fault" => {"is_ref" => 0,"type" => ""},"Method" => {"is_ref" => 0,"type" => ""},"Qs_fixed" => {"is_ref" => !!1,"type" => "ARRAY"},"Qs_var" => {"is_ref" => !!1,"type" => "ARRAY"},"Url" => {"is_ref" => 0,"type" => ""}}->{$key}{"is_ref"}
MC/DC0%0/2
not ref $url->{$key} and {"Exp" => {"is_ref" => 0,"type" => "ARRAY"},"Exp_Fault" => {"is_ref" => 0,"type" => ""},"Method" => {"is_ref" => 0,"type" => ""},"Qs_fixed" => {"is_ref" => !!1,"type" => "ARRAY"},"Qs_var" => {"is_ref" => !!1,"type" => "ARRAY"},"Url" => {"is_ref" => 0,"type" => ""}}->{$key}{"is_ref"}
not ref $url->{$key}
{"Exp" => {"is_ref" => 0,"type" => "ARRAY"},"Exp_Fault" => {"is_ref" => 0,"type" => ""},"Method" => {"is_ref" => 0,"type" => ""},"Qs_fixed" => {"is_ref" => !!1,"type" => "ARRAY"},"Qs_var" => {"is_ref" => !!1,"type" => "ARRAY"},"Url" => {"is_ref" => 0,"type" => ""}}->{$key}{"is_ref"}
Truth table66%2/3
not(A) and B
A= not ref $url->{$key}B= {"Exp" => {"is_ref" => 0,"type" => "ARRAY"},"Exp_Fault" => {"is_ref" => 0,"type" => ""},"Method" => {"is_ref" => 0,"type" => ""},"Qs_fixed" => {"is_ref" => !!1,"type" => "ARRAY"},"Qs_var" => {"is_ref" => !!1,"type" => "ARRAY"},"Url" => {"is_ref" => 0,"type" => ""}}->{$key}{"is_ref"}
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | warn "Expected key \"$key\" to be ", Field_Refs->{$key}{type} ? Field_Refs->{$key}{type} . ' ref' : 'non ref', "\n" ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | &outahere( $url, "Key \"$key\" not a reference" ) ; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | my $class = ref($obj) || $obj ; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $obj || $obj
MC/DC0%0/2
ref $obj || $obj
ref $obj
$obj
Truth table33%1/3
A || B
A= ref $objB= $obj
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | my $accessor_stash_slot = $class . '::' . 'get_urls' ; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | no strict 'refs' ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | unless ( ref *$accessor_stash_slot{CODE} eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 2 | foreach my $accessor ( qw(urls matches) ) { | |||||||||||||||||||||||||||
| 69 | 4 | my $full_name = $class . '::' . $accessor ; | |||||||||||||||||||||||||||
| 70 | 4 | ▶ | *{$full_name} = sub { my $self = shift @_ ; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | $self->{$accessor} = shift @_ if @_ ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 3 | $self->{$accessor} | |||||||||||||||||||||||||||
| 73 | 4 | } ; | |||||||||||||||||||||||||||
| 74 | 4 | foreach my $acc_pre (qw(get set)) { | |||||||||||||||||||||||||||
| 75 | 8 | $full_name = $class . '::' . $acc_pre . '_' . $accessor ; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | *{$full_name} = $acc_pre eq 'get' ? sub { my $self = shift @_; $self->{$accessor} } : | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub { my $self = shift @_; $self->{$accessor} = shift @_ } ; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | bless { urls => $urls_ar, matches => [], number_of_images_downloaded => 0 }, $class ; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # The field urls contains a ref to a list of (hashes) | ||||||||||||||||||||||||||||
| 86 | # records representing the web transaction. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # self->my_match() will update $self->{matches} ; | ||||||||||||||||||||||||||||
| 89 | # with the set of matches it finds by matching | ||||||||||||||||||||||||||||
| 90 | # patterns with memory (ie patterns in paren) from | ||||||||||||||||||||||||||||
| 91 | # the Exp field against the request response. | ||||||||||||||||||||||||||||
| 92 | # An array ref to the array containing the matches is | ||||||||||||||||||||||||||||
| 93 | # stored in the field 'matches'. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Qs_var = [ form_name_1 => 0, form_name_2 => 1 ..] | ||||||||||||||||||||||||||||
| 96 | # will lead to a query_string like | ||||||||||||||||||||||||||||
| 97 | # form_name_1=$matches[0] form_name_2=$matches[1] .. | ||||||||||||||||||||||||||||
| 98 | # in $self->make_req() by | ||||||||||||||||||||||||||||
| 99 | # @matches = $self->matches(); and using 0, 1 etc as indices | ||||||||||||||||||||||||||||
| 100 | # of @matches. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # XXX FIXME | ||||||||||||||||||||||||||||
| 103 | # Construct the useragent object and cache it so that the check method can reuse it for | ||||||||||||||||||||||||||||
| 104 | # multiple lists of URLs | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=21 SCAR=38.7
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 2 | my ($self, $cgi_parm_vals_hr) = @_ ; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | my %defaults = ( cookies => TRUE, | |||||||||||||||||||||||||||
| 112 | debug => TRUE, | ||||||||||||||||||||||||||||
| 113 | timeout => 30, | ||||||||||||||||||||||||||||
| 114 | agent => AGENT, | ||||||||||||||||||||||||||||
| 115 | proxy => {}, | ||||||||||||||||||||||||||||
| 116 | download_images => FALSE, | ||||||||||||||||||||||||||||
| 117 | indent_level => 0, | ||||||||||||||||||||||||||||
| 118 | fail_if_1 => TRUE ) ; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # check semantics. | ||||||||||||||||||||||||||||
| 121 | # $fail_if_1 ? return FAIL if any URL fails | ||||||||||||||||||||||||||||
| 122 | # ! $fail_if_1 ? return FAIL if all URLs fail | ||||||||||||||||||||||||||||
| 123 | # (same as return OK if any URL ok) | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | my %parms = (%defaults, @_) ; | |||||||||||||||||||||||||||
| 126 | # remaining (minus first 2) elts in @_ are the check params such as debug | ||||||||||||||||||||||||||||
| 127 | 2 | my (%downloaded, $ua, $debug, $ok, $indent_level, $resp_string, $res) ; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 2 | keys %downloaded = 128 ; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | $debug = $parms{debug} ; | |||||||||||||||||||||||||||
| 132 | 2 | ▶ | $ok = $parms{fail_if_1} ? TRUE : FALSE ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 2 | $indent_level = $parms{indent_level} ; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | ▶ | unless ( exists $self->{ua} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 2 | $ua = new LWP::UserAgent ; | |||||||||||||||||||||||||||
| 137 | 2 | $ua->agent($parms{agent}) ; | |||||||||||||||||||||||||||
| 138 | 2 | $ua->timeout($parms{timeout}) ; | |||||||||||||||||||||||||||
| 139 | $ua->cookie_jar(HTTP::Cookies->new) | ||||||||||||||||||||||||||||
| 140 | 2 | ▶ | if $parms{cookies} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | $ua->proxy(['http', 'ftp'] => $parms{proxy}{server}) | ||||||||||||||||||||||||||||
| 142 | 2 | ▶ | if exists $parms{proxy}{server} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 2 | $self->{ua} = $ua ; | |||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||
| 146 | 0 | $ua = $self->{ua} ; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | foreach my $url_r ( @{ $self->{urls} } ) { | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 2 | ▶ | my $url = $url_r->{Url} ? $url_r->{Url} : &next_url($res, $resp_string) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | my $req = $self->make_req( $url_r->{Method}, $url, $url_r->{Qs_var}, $url_r->{Qs_fixed}, $cgi_parm_vals_hr ) ; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | ▶ | $req->proxy_authorization_basic( $parms{proxy}{account}, $parms{proxy}{pass} ) if exists $parms{proxy}{account} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | print STDERR ' ' x $indent_level, '... ', $req->as_string, "\n" if $debug ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | $res = $ua->request($req) ; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | ▶ | print STDERR ' ' x $indent_level, '... ', $res->as_string, "\n" if $debug ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 2 | ▶ | if ( $parms{fail_if_1} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 2 | ▶ | unless ( $res->is_success or $res->is_redirect) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$res->is_success or $res->is_redirect
MC/DC0%0/2
$res->is_success or $res->is_redirect
$res->is_success
$res->is_redirect
Truth table33%1/3
A or B
A= $res->is_successB= $res->is_redirect
| |||||||||||||||||||||||||||||
| 165 | 0 | $resp_string = $res->as_string ; | |||||||||||||||||||||||||||
| 166 | 0 | $resp_string =~ s#'#_#g ; | |||||||||||||||||||||||||||
| 167 | # Deal with __Can't__ from LWP. | ||||||||||||||||||||||||||||
| 168 | # Otherwise notification fails because /bin/sh is called to | ||||||||||||||||||||||||||||
| 169 | # printf '$OUTPUT' and sh cannot deal with nested quotes (eg Can't echo ''') | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | return (FAIL, &error_message( $req->method . ' ' . $req->uri, 'Transaction failed: other than HTTP 200. ', $resp_string )) ; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | $ok = TRUE if $res->is_success ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | $resp_string = $res->as_string ; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # Check that the response is what we expect. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 2 | ▶ | if ( $self->my_match( $url_r->{Exp_Fault}, $resp_string) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 182 | 0 | my $fault_ind = $url_r->{Exp_Fault} ; | |||||||||||||||||||||||||||
| 183 | 0 | my ($bad_stuff) = $resp_string =~ /($fault_ind.*\n.*\n)/ ; | |||||||||||||||||||||||||||
| 184 | # Do not want to pick up any HTML with the fault indication. | ||||||||||||||||||||||||||||
| 185 | 0 | return (FAIL, &error_message( $req->method . ' ' . $req->uri, 'Transaction failed: fault indication in response. ', $bad_stuff )) ; | |||||||||||||||||||||||||||
| 186 | } elsif ( ! $self->my_match( $url_r->{Exp}, $resp_string) ) { | ||||||||||||||||||||||||||||
| 187 | 1 | my $exp_type = ref $url_r->{Exp} ; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | my $exp_str = $exp_type eq 'ARRAY' ? "@{$url_r->{Exp}}" : $url_r->{Exp} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 1 | return(FAIL, &error_message( $req->method . ' ' . $req->uri, " Transaction failed: \"$exp_str\" not in response. ", $resp_string )) ; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | ▶ | if ( $parms{download_images} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 0 | my ($image_dl_ok, $image_dl_msg, $number_imgs_dl ) = &download_images($res, \%parms, \%downloaded) ; | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | return (FAIL, $image_dl_msg) unless $image_dl_ok ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | $self->{number_of_images_downloaded} += $number_imgs_dl ; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | ▶ | my $trx_ok = $parms{download_images} ? "Transaction completed Ok - downloaded $self->{number_of_images_downloaded} images." : 'Transaction completed Ok.' ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 1 | ▶ | return ($ok, $ok ? $trx_ok : 'Transaction failed.') ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 1 | ▶ | sub error_message { | ||||||||||||||||||||||||||
|
Subroutine
error_message: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 1 | my ($req_string, $message, $resp_string) = @_ ; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1 | my $failure_message = "\"$req_string\" $message $resp_string" ; | |||||||||||||||||||||||||||
| 209 | # my $failure_message = "++ $req_string: $message $resp_string" ; | ||||||||||||||||||||||||||||
| 210 | 1 | $failure_message =~ s/\n/ /g ; # GOTCHA | |||||||||||||||||||||||||||
| 211 | # return here with the number of "\n" chars substituted ! | ||||||||||||||||||||||||||||
| 212 | 1 | return $failure_message ; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 2 | ▶ | sub make_req { | ||||||||||||||||||||||||||
|
Subroutine
make_req: called CC=9 SCAR=29.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 216 | 2 | my ($self, $method, $url, $qs_var_ar, $qs_fixed_ar, $name_vals_hr) = @_ ; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # $qs_var_ar is an array reference containing | ||||||||||||||||||||||||||||
| 219 | # the name value pairs of any parameters whose | ||||||||||||||||||||||||||||
| 220 | # value is known only at run time | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # the format of $qs_var_ar is | ||||||||||||||||||||||||||||
| 223 | # [cgi_parm_name => val, cg_parm_name => val ..] | ||||||||||||||||||||||||||||
| 224 | # where cgi_parm_name is the name of a fill out | ||||||||||||||||||||||||||||
| 225 | # form parameter and val is a string used as a | ||||||||||||||||||||||||||||
| 226 | # key in %$name_vals_hr to get the value of the | ||||||||||||||||||||||||||||
| 227 | # cgi_parameter. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | # eg [p_tm_number, tmno] has the parameter name | ||||||||||||||||||||||||||||
| 230 | # 'p_tm_number' and val 'tmno'. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # If $name_vals_hr = { tmno = > 1 }, | ||||||||||||||||||||||||||||
| 233 | # the query_sring becomes p_tm_number=1 | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # when the val is a digit, that digit is | ||||||||||||||||||||||||||||
| 236 | # interpreted as a relative match in the last | ||||||||||||||||||||||||||||
| 237 | # set of matches found by ->my_match eg | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # [p_tm_number => 1] means get the | ||||||||||||||||||||||||||||
| 240 | # second match (from the last set of matches) | ||||||||||||||||||||||||||||
| 241 | # and use it as the value of p_tm_number. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # If the value is a array ref eg | ||||||||||||||||||||||||||||
| 244 | # [p_tm_number, [0, sub { $_[0] . 'Blah' }] | ||||||||||||||||||||||||||||
| 245 | # then the query_string becomes | ||||||||||||||||||||||||||||
| 246 | # p_tm_number => $ar->[1]( $name_vals{$ar->[0]} ) | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # qs_fixed is an array_ref containing name | ||||||||||||||||||||||||||||
| 249 | # value pairs | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 2 | my ($req, @query_string, $query_string, @qs_var, @qs_fixed, %name_vals, @nvp) ; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 2 | my @matches = @{ $self->matches() } ; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 2 | @qs_var = @$qs_var_ar ; | |||||||||||||||||||||||||||
| 257 | 2 | @qs_fixed = @$qs_fixed_ar ; | |||||||||||||||||||||||||||
| 258 | 2 | %name_vals = %$name_vals_hr ; | |||||||||||||||||||||||||||
| 259 | 2 | @name_vals{0 .. $#matches} = @matches ; # add the matches as (over the top if some of the | |||||||||||||||||||||||||||
| 260 | # name_val keys are eq '0', '1' ..) keys to %name_vals | ||||||||||||||||||||||||||||
| 261 | 2 | @query_string = () ; | |||||||||||||||||||||||||||
| 262 | 2 | @nvp = () ; | |||||||||||||||||||||||||||
| 263 | 2 | $query_string = '' ; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 2 | ▶ | while ( my ($name, $val) = splice(@qs_fixed, 0, 2) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | splice(@query_string, scalar @query_string, 0, ($name, $val)) ; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | # a cgi var name must be in qs_var for it's value to | ||||||||||||||||||||||||||||
| 269 | # be changed (otherwise it doesn't get in the form | ||||||||||||||||||||||||||||
| 270 | # query string) | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 2 | ▶ | while ( my ($name, $val) = splice(@qs_var, 0, 2) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 0 | @nvp = ref $val eq 'ARRAY' ? ( $name, &{ $val->[1] }($name_vals{$val->[0]}) ) : | |||||||||||||||||||||||||||
| 275 | 0 | ▶ | ( $name, $name_vals{$val} ) ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 0 | splice(@query_string, scalar @query_string, 0, @nvp) ; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 2 | ▶ | if ( $method eq 'GET' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 281 | 2 | ▶ | while ( my ($name, $val) = splice(@query_string, 0, 2) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 0 | $query_string .= "$name=$val&" ; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | 2 | ▶ | if ($query_string) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 0 | chop($query_string) ; | |||||||||||||||||||||||||||
| 286 | 0 | $req = GET $url . '?' . $query_string ; | |||||||||||||||||||||||||||
| 287 | # Referer header seemingly not necessary | ||||||||||||||||||||||||||||
| 288 | # $req = GET $url . '?' . $query_string, Referer => $self->{urls}[0]{Url} ; | ||||||||||||||||||||||||||||
| 289 | } else { | ||||||||||||||||||||||||||||
| 290 | 2 | $req = GET $url ; | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | } elsif ( $method eq 'POST' ) { | ||||||||||||||||||||||||||||
| 294 | 0 | $req = POST $url, [ @query_string ] ; | |||||||||||||||||||||||||||
| 295 | } elsif ( $method eq 'HEAD' ) { | ||||||||||||||||||||||||||||
| 296 | 0 | $req = HEAD $url ; | |||||||||||||||||||||||||||
| 297 | } else { | ||||||||||||||||||||||||||||
| 298 | # do something to indicate no such method | ||||||||||||||||||||||||||||
| 299 | 0 | outahere( $self, "Unexpected method \"$method\" for url \"$url\"" ) ; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 0 | ▶ | sub next_url { | ||||||||||||||||||||||||||
|
Subroutine
next_url: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 304 | 0 | my ($resp, $resp_string) = @_ ; | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | # FIXME. Some applications (eg IIS module for | ||||||||||||||||||||||||||||
| 308 | # SAP R3) have an action field relative to | ||||||||||||||||||||||||||||
| 309 | # hostname. | ||||||||||||||||||||||||||||
| 310 | # Others (eg ADDS v2) use a refresh header | ||||||||||||||||||||||||||||
| 311 | # relative to hostname/path .. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | ▶ | if ( $resp_string =~ m#META\s+http-equiv="refresh"\s+content="\d+;\s+url=([^"]+)"# ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 314 | 0 | my $rel_url = $1 ; | |||||||||||||||||||||||||||
| 315 | 0 | my $base = $resp->base ; | |||||||||||||||||||||||||||
| 316 | 0 | $base =~ m#(http://.+/).+?$# ; | |||||||||||||||||||||||||||
| 317 | 0 | my $url = $1 . $rel_url ; | |||||||||||||||||||||||||||
| 318 | 0 | return $url ; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | ▶ | elsif ( $resp_string =~ m#form name="[^"]+"\s+method="post"\s+action="([^"]+)"#i or | |||||||||||||||||||||||||||
|
Condition0%0/3
$resp_string =~ /form name="[^"]+"\s+method="post"\s+action="([^"]+)"/i or $resp_string =~ /form\s+method="post"\s+action="([^"]+)"/i
MC/DC0%0/2
$resp_string =~ /form name="[^"]+"\s+method="post"\s+action="([^"]+)"/i or $resp_string =~ /form\s+method="post"\s+action="([^"]+)"/i
$resp_string =~ /form name="[^"]+"\s+method="post"\s+action="([^"]+)"/i
$resp_string =~ /form\s+method="post"\s+action="([^"]+)"/i
Truth table0%0/3
A or B
A= $resp_string =~ /form name="[^"]+"\s+method="post"\s+action="([^"]+)"/iB= $resp_string =~ /form\s+method="post"\s+action="([^"]+)"/i
| |||||||||||||||||||||||||||||
| 321 | $resp_string =~ m#form\s+method="post"\s+action="([^"]+)"#i ) { | ||||||||||||||||||||||||||||
| 322 | # Attachmate eVWP product doesn't have a form name. | ||||||||||||||||||||||||||||
| 323 | 0 | my $rel_url = $1 ; | |||||||||||||||||||||||||||
| 324 | 0 | my $base = $resp->base ; | |||||||||||||||||||||||||||
| 325 | 0 | $base =~ m#(http://.+?)/# ; # only want hostname | |||||||||||||||||||||||||||
| 326 | 0 | my $url = $1 . $rel_url ; | |||||||||||||||||||||||||||
| 327 | 0 | return $url ; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | else { | ||||||||||||||||||||||||||||
| 330 | 0 | return '' ; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 4 | ▶ | sub my_match { | ||||||||||||||||||||||||||
|
Subroutine
my_match: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 336 | 4 | my ($self, $pat, $str) = @_ ; | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 4 | my $found = 0 ; | |||||||||||||||||||||||||||
| 339 | 4 | my @matches = () ; | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 4 | ▶ | if ( ref $pat eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | # foreach my $m (map { $str =~ m#$_#s; defined $1 ? $1 : '' } @$pat) { | ||||||||||||||||||||||||||||
| 344 | 1 | foreach my $p (@$pat) { | |||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 3 | ▶ | if ( $str =~ m#$p# ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # matches are expected to save whatever they want with | ||||||||||||||||||||||||||||
| 349 | # parentheses eg (\w+). | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # the string that matches each pattern (and is saved in $1) | ||||||||||||||||||||||||||||
| 352 | # is stored in the object as $self->{matches}[0], [1], .. | ||||||||||||||||||||||||||||
| 353 | # If a pattern fails to match then the corresp match is ''. | ||||||||||||||||||||||||||||
| 354 | 2 | ▶ | push @matches, $1 unless not defined $1 ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 2 | $found++ ; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | 1 | $self->matches(\@matches) ; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | } else { | ||||||||||||||||||||||||||||
| 361 | 3 | $found = ($str =~ m#$pat#) ; | |||||||||||||||||||||||||||
| 362 | # $found = ($str =~ /$pat/o) ; # Don't use /o because the pattern will be set to the | ||||||||||||||||||||||||||||
| 363 | # first value (and will never change). | ||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 4 | return $found ; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 0 | ▶ | sub outahere { | ||||||||||||||||||||||||||
|
Subroutine
outahere: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 371 | 0 | my ($dumpit, $message) = @_ ; | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 0 | require Dumpvalue ; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | my $dumper = new Dumpvalue ; # dump is a poorly chosen variable name | |||||||||||||||||||||||||||
| 376 | # since it is also a Perl verb with a dramatic | ||||||||||||||||||||||||||||
| 377 | # effect | ||||||||||||||||||||||||||||
| 378 | 0 | $dumper->dumpValue($dumpit) ; | |||||||||||||||||||||||||||
| 379 | 0 | croak $message ; | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | sub download_images { | ||||||||||||||||||||||||||
|
Subroutine
download_images: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 0 | my ($res, $parms_hr, $downloaded_hr) = @_ ; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 0 | require HTML::LinkExtor ; | |||||||||||||||||||||||||||
| 388 | 0 | require URI::URL ; | |||||||||||||||||||||||||||
| 389 | 0 | URI::URL->import(qw(url)) ; | |||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 0 | my @imgs = () ; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | my $cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 394 | 0 | my($tag, %attr) = @_; | |||||||||||||||||||||||||||
| 395 | 0 | ▶ | return if $tag ne 'img'; # we only look closer at <img ...> | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | 0 | push(@imgs, $attr{src}); | |||||||||||||||||||||||||||
| 397 | 0 | } ; | |||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 0 | my $p = HTML::LinkExtor->new($cb) ; | |||||||||||||||||||||||||||
| 400 | 0 | $p->parse($res->as_string) ; | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 0 | my $base = $res->base; | |||||||||||||||||||||||||||
| 403 | 0 | my @imgs_abs = grep ! $downloaded_hr->{$_}++, map { my $x = url($_, $base)->abs; } @imgs; | |||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | my @img_urls = map { Method => 'GET', Url => $_->as_string, Qs_var => [], Qs_fixed => [], Exp => '.', Exp_Fault => 'NeverInAnImage' }, @imgs_abs ; | |||||||||||||||||||||||||||
| 406 | # url() returns an array ref containing the abs url and the base. | ||||||||||||||||||||||||||||
| 407 | 0 | ▶ | if ( my $number_of_images_not_already_downloaded = scalar @img_urls ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | 0 | my $img_trx = __PACKAGE__->new(\@img_urls) ; | |||||||||||||||||||||||||||
| 409 | 0 | my %image_dl_parms = (%$parms_hr, fail_if_1 => FALSE, download_images => FALSE, indent_level => 1) ; | |||||||||||||||||||||||||||
| 410 | 0 | return ( $img_trx->check( {}, %image_dl_parms), $number_of_images_not_already_downloaded ) ; | |||||||||||||||||||||||||||
| 411 | } else { | ||||||||||||||||||||||||||||
| 412 | 0 | return (OK, 'Downloaded all __zero__ images found in ' . $res->base, 0) ; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 4 | 1 ; | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | __END__ | ||||||||||||||||||||||||||||