| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # PODNAME: mech-dump | ||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Dumps information about a web page | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use WWW::Mechanize (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use HTTP::Cookies; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | my @actions; | |||||||||||||||||||||||||||
| 14 | my $absolute; | ||||||||||||||||||||||||||||
| 15 | 2 | my $all; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | my $user; | |||||||||||||||||||||||||||
| 18 | 2 | my $pass; | |||||||||||||||||||||||||||
| 19 | 2 | my $agent; | |||||||||||||||||||||||||||
| 20 | 2 | my $agent_alias; | |||||||||||||||||||||||||||
| 21 | 2 | my $cookie_filename; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my %command_line_options = ( | ||||||||||||||||||||||||||||
| 24 | 'user=s' => \$user, | ||||||||||||||||||||||||||||
| 25 | 'password=s' => \$pass, | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | headers => sub { push( @actions, \&dump_headers ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | forms => sub { push( @actions, \&dump_forms ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | links => sub { push( @actions, \&dump_links ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | images => sub { push( @actions, \&dump_images ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | all => sub { | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $all++; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 0 | push( | |||||||||||||||||||||||||||
| 33 | @actions, | ||||||||||||||||||||||||||||
| 34 | \&dump_headers, \&dump_forms, \&dump_links, \&dump_images | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | text => sub { push( @actions, \&dump_text ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 'absolute!' => \$absolute, | ||||||||||||||||||||||||||||
| 39 | 'agent=s' => \$agent, | ||||||||||||||||||||||||||||
| 40 | 'agent-alias=s' => \$agent_alias, | ||||||||||||||||||||||||||||
| 41 | 'cookie-file=s' => \$cookie_filename, | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | help => sub { pod2usage(1); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | version => sub { print STDERR $WWW::Mechanize::VERSION, "\n"; exit 0; }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | 'completions' => sub { completions(@ARGV); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | 2 | ); | |||||||||||||||||||||||||||
| 46 | 2 | ▶ | GetOptions(%command_line_options) or pod2usage(2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub completions { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my (@words) = @_; | ||||||||||||||||||||||||||
|
Subroutine
completions: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 50 | 0 | my @opts; | |||||||||||||||||||||||||||
| 51 | 0 | foreach ( sort keys %command_line_options ) { | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | if (m/^ (?<opt> [^!]+) ! $/msx) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 0 | push @opts, $+{opt}, 'no-' . $+{opt}; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | elsif (m/^ (?<opt> [^=]+) = [siof]{1} $/msx) { | ||||||||||||||||||||||||||||
| 56 | 0 | push @opts, ( split qr{\|}, $+{opt} ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 0 | push @opts, $_; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | print join "\n", ( map { q{--} . $_ } @opts ); | |||||||||||||||||||||||||||
| 63 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 64 | 0 | exit 0; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | ▶ | my @uris = @ARGV | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | or die "Must specify a URL or file to check. See --help for details.\n"; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | @actions = ( \&dump_forms ) unless @actions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 2 | binmode( STDOUT, ':utf8' ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | my $mech = WWW::Mechanize->new( autocheck => 0 ); | |||||||||||||||||||||||||||
| 76 | 2 | ▶ | if ( defined $agent ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 77 | 0 | $mech->agent($agent); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | elsif ( defined $agent_alias ) { | ||||||||||||||||||||||||||||
| 80 | 0 | $mech->agent_alias($agent_alias); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 2 | ▶ | if ( defined $cookie_filename ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | my $cookies = HTTP::Cookies->new( | |||||||||||||||||||||||||||
| 84 | file => $cookie_filename, | ||||||||||||||||||||||||||||
| 85 | autosave => 1, | ||||||||||||||||||||||||||||
| 86 | ignore_discard => 1 | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | 0 | $cookies->load(); | |||||||||||||||||||||||||||
| 89 | 0 | $mech->cookie_jar($cookies); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | 2 | $mech->cookie_jar(undef); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 2 | $mech->env_proxy(); | |||||||||||||||||||||||||||
| 96 | 2 | foreach my $uri (@uris) { | |||||||||||||||||||||||||||
| 97 | 4 | my $no_ct_check; | |||||||||||||||||||||||||||
| 98 | 4 | ▶ | if ( -e $uri ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 2 | require URI::file; | |||||||||||||||||||||||||||
| 100 | 2 | $uri = URI::file->new_abs($uri)->as_string; | |||||||||||||||||||||||||||
| 101 | 2 | $no_ct_check = 1; # we don't have to check the content type | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | my $response = $mech->get($uri); | |||||||||||||||||||||||||||
| 105 | 4 | ▶ | if ( !$response->is_success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | if ( defined( $response->www_authenticate ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ( !defined $user or !defined $pass ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $user and defined $pass
MC/DC0%0/2
defined $user and defined $pass
defined $user
defined $pass
Truth table0%0/3
A and B
A= defined $userB= defined $pass
| |||||||||||||||||||||||||||||
| 108 | 0 | die( | |||||||||||||||||||||||||||
| 109 | "Page requires username and password, but none specified.\n" | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 0 | $mech->credentials( $user, $pass ); | |||||||||||||||||||||||||||
| 113 | 0 | $response = $mech->get($uri); | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | $response->is_success | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | or die "Can't fetch $uri with username and password\n", | ||||||||||||||||||||||||||||
| 116 | $response->status_line, "\n"; | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||
| 119 | 1 | die "$uri returns status ", $response->code, "\n"; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3 | ▶ | unless ($no_ct_check) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | $mech->is_html | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | or die qq{$uri returns type "}, $mech->ct, | ||||||||||||||||||||||||||||
| 126 | qq{", not "text/html"\n}; | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 3 | foreach my $action (@actions) { | |||||||||||||||||||||||||||
| 130 | 9 | $action->($mech); | |||||||||||||||||||||||||||
| 131 | 9 | ▶ | print "\n" if @actions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub dump_headers { | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_headers: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | print "--> Headers:\n" if $all; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | $mech->dump_headers(undef); | |||||||||||||||||||||||||||
| 139 | 0 | return; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | sub dump_forms { | ||||||||||||||||||||||||||||
| 143 | 3 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_forms: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 144 | 3 | ▶ | print "--> Forms:\n" if $all; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 3 | $mech->dump_forms(undef); | |||||||||||||||||||||||||||
| 146 | 3 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub dump_links { | ||||||||||||||||||||||||||||
| 150 | 3 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_links: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 151 | 3 | ▶ | print "--> Links:\n" if $all; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 3 | $mech->dump_links( undef, $absolute ); | |||||||||||||||||||||||||||
| 153 | 3 | return; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub dump_images { | ||||||||||||||||||||||||||||
| 157 | 3 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_images: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 158 | 3 | ▶ | print "--> Images:\n" if $all; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 3 | $mech->dump_images( undef, $absolute ); | |||||||||||||||||||||||||||
| 160 | 3 | return; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub dump_text { | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_text: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 165 | 0 | $mech->dump_text(); | |||||||||||||||||||||||||||
| 166 | 0 | return; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||