| 1 | #!/usr/local/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | mech-dump - Dumps information about a web page | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =cut | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use WWW::Mechanize (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use HTTP::Cookies; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | my @actions; | |||||||||||||||||||||||||||
| 17 | my $absolute; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my $user; | |||||||||||||||||||||||||||
| 20 | 1 | my $pass; | |||||||||||||||||||||||||||
| 21 | 1 | my $agent; | |||||||||||||||||||||||||||
| 22 | 1 | my $agent_alias; | |||||||||||||||||||||||||||
| 23 | 1 | my $cookie_filename; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | GetOptions( | ||||||||||||||||||||||||||||
| 26 | 'user=s' => \$user, | ||||||||||||||||||||||||||||
| 27 | 'password=s' => \$pass, | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | headers => sub { push( @actions, \&dump_headers ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | forms => sub { push( @actions, \&dump_forms ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | links => sub { push( @actions, \&dump_links ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | images => sub { push( @actions, \&dump_images ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | all => sub { push( @actions, \&dump_headers, \&dump_forms, \&dump_links, \&dump_images ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | text => sub { push( @actions, \&dump_text ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 34 | absolute => \$absolute, | ||||||||||||||||||||||||||||
| 35 | 'agent=s' => \$agent, | ||||||||||||||||||||||||||||
| 36 | 'agent-alias=s' => \$agent_alias, | ||||||||||||||||||||||||||||
| 37 | 'cookie-file=s' => \$cookie_filename, | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | help => sub { pod2usage(1); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | ) or pod2usage(2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | mech-dump [options] [file|url] | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Options: | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | --headers Dump HTTP response headers | ||||||||||||||||||||||||||||
| 48 | --forms Dump table of forms (default action) | ||||||||||||||||||||||||||||
| 49 | --links Dump table of links | ||||||||||||||||||||||||||||
| 50 | --images Dump table of images | ||||||||||||||||||||||||||||
| 51 | --all Dump all four of the above, in that order | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | --text Dumps the textual part of the web page | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | --user=user Set the username | ||||||||||||||||||||||||||||
| 56 | --password=pass Set the password | ||||||||||||||||||||||||||||
| 57 | --cookie-file=filename Set the filename to use for persistent cookies | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | --agent=agent Specify the UserAgent to pass | ||||||||||||||||||||||||||||
| 60 | --agent-alias=alias | ||||||||||||||||||||||||||||
| 61 | Specify the alias for the UserAgent to pass. | ||||||||||||||||||||||||||||
| 62 | Pick one of: | ||||||||||||||||||||||||||||
| 63 | * Windows IE 6 | ||||||||||||||||||||||||||||
| 64 | * Windows Mozilla | ||||||||||||||||||||||||||||
| 65 | * Mac Safari | ||||||||||||||||||||||||||||
| 66 | * Mac Mozilla | ||||||||||||||||||||||||||||
| 67 | * Linux Mozilla | ||||||||||||||||||||||||||||
| 68 | * Linux Konqueror | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | --absolute Show URLs as absolute, even if relative in the page | ||||||||||||||||||||||||||||
| 71 | --help Show this message | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | The order of the options specified is relevant. Repeated options | ||||||||||||||||||||||||||||
| 74 | get repeated dumps. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | my $uri = shift or die "Must specify a URL or file to check. See --help for details.\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | if ( -e $uri ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | require URI::file; | |||||||||||||||||||||||||||
| 81 | 0 | $uri = URI::file->new_abs( $uri )->as_string; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | ▶ | @actions = (\&dump_forms) unless @actions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | my $mech = WWW::Mechanize->new(); | |||||||||||||||||||||||||||
| 87 | 1 | ▶ | if ( defined $agent ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | 0 | $mech->agent( $agent ); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | elsif ( defined $agent_alias ) { | ||||||||||||||||||||||||||||
| 91 | 0 | $mech->agent_alias( $agent_alias ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 1 | ▶ | if ( defined $cookie_filename ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | my $cookies = HTTP::Cookies->new( file => $cookie_filename, autosave => 1, ignore_discard => 1 ); | |||||||||||||||||||||||||||
| 95 | 0 | $cookies->load() ; | |||||||||||||||||||||||||||
| 96 | 0 | $mech->cookie_jar($cookies); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||
| 99 | 1 | $mech->cookie_jar(undef) ; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | $mech->env_proxy(); | |||||||||||||||||||||||||||
| 103 | 1 | my $response = $mech->get( $uri ); | |||||||||||||||||||||||||||
| 104 | 1 | ▶ | if (!$response->is_success and defined ($response->www_authenticate)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $response->is_success and defined $response->www_authenticate
MC/DC0%0/2
not $response->is_success and defined $response->www_authenticate
not $response->is_success
defined $response->www_authenticate
Truth table33%1/3
not(A) and B
A= not $response->is_successB= defined $response->www_authenticate
| |||||||||||||||||||||||||||||
| 105 | 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
| |||||||||||||||||||||||||||||
| 106 | 0 | die("Page requires username and password, but none specified.\n"); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | $mech->credentials($user,$pass); | |||||||||||||||||||||||||||
| 109 | 0 | $response = $mech->get( $uri ); | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | $response->is_success or die "Can't fetch $uri with username and password\n", $response->status_line, "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 1 | ▶ | $mech->is_html or die qq{$uri returns type "}, $mech->ct, qq{", not "text/html"\n}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | ▶ | while ( my $action = shift @actions ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | $action->( $mech ); | |||||||||||||||||||||||||||
| 116 | 1 | ▶ | print "\n" if @actions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | sub dump_headers { | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 122 | 0 | $mech->dump_headers( undef ); | |||||||||||||||||||||||||||
| 123 | 0 | return; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub dump_forms { | ||||||||||||||||||||||||||||
| 127 | 1 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_forms: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 1 | $mech->dump_forms( undef, $absolute ); | |||||||||||||||||||||||||||
| 129 | 1 | return; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub dump_links { | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_links: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 134 | 0 | $mech->dump_links( undef, $absolute ); | |||||||||||||||||||||||||||
| 135 | 0 | return; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub dump_images { | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_images: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 140 | 0 | $mech->dump_images( undef, $absolute ); | |||||||||||||||||||||||||||
| 141 | 0 | return; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | sub dump_text { | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my $mech = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_text: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 146 | 0 | $mech->dump_text(); | |||||||||||||||||||||||||||
| 147 | 0 | return; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | L<WWW::Mechanize> | ||||||||||||||||||||||||||||