lib/WWW/Selenium/Utils/CGI.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 86 | 87 | 98.8 |
branch | 31 | 44 | 70.4 |
condition | 4 | 8 | 50.0 |
subroutine | 15 | 15 | 100.0 |
pod | 3 | 4 | 75.0 |
total | 139 | 158 | 87.9 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package WWW::Selenium::Utils::CGI; | ||||||
2 | 2 | 2 | 26169 | use 5.006; | |||
2 | 6 | ||||||
2 | 73 | ||||||
3 | 2 | 2 | 11 | use strict; | |||
2 | 2 | ||||||
2 | 52 | ||||||
4 | 2 | 2 | 9 | use warnings; | |||
2 | 2 | ||||||
2 | 50 | ||||||
5 | 2 | 2 | 12 | use Carp; | |||
2 | 2 | ||||||
2 | 144 | ||||||
6 | 2 | 2 | 9 | use File::Find; | |||
2 | 4 | ||||||
2 | 99 | ||||||
7 | 2 | 2 | 4189 | use CGI qw(:standard); | |||
2 | 42904 | ||||||
2 | 13 | ||||||
8 | 2 | 2 | 6691 | use Config; | |||
2 | 6 | ||||||
2 | 97 | ||||||
9 | 2 | 2 | 2277 | use Data::Dumper; | |||
2 | 14086 | ||||||
2 | 2510 | ||||||
10 | |||||||
11 | require Exporter; | ||||||
12 | our @ISA = qw(Exporter); | ||||||
13 | our @EXPORT_OK = qw(run cat state); | ||||||
14 | |||||||
15 | sub run { | ||||||
16 | 2 | 50 | 2 | 1 | 3113 | my $q = shift or croak("CGI query object is mandatory!"); | |
17 | |||||||
18 | 2 | 8 | my $cmd = $q->param('cmd'); | ||||
19 | 2 | 100 | 15 | return error("cmd is a mandatory parameter!") unless $cmd; | |||
20 | |||||||
21 | 1 | 33 | 20078 | my $results = qx($cmd) || $!; | |||
22 | 1 | 100 | $results =~ s/ | ||||
23 | 1 | 294 | return header . start_html("Output of \"$cmd\"") | ||||
24 | . " " . h1("Output of \"$cmd\":") . " " |
||||||
25 | . " " . pre($results) . " " |
||||||
26 | . end_html; | ||||||
27 | } | ||||||
28 | |||||||
29 | |||||||
30 | sub cat { | ||||||
31 | 3 | 50 | 3 | 1 | 13104 | my $q = shift or croak("CGI query object is mandatory!"); | |
32 | 3 | 8 | my %opts = @_; | ||||
33 | 3 | 33 | 984 | my $basedir = $opts{basedir} || $Config{prefix}; | |||
34 | |||||||
35 | 3 | 3088 | my $file = $q->param('file'); | ||||
36 | 3 | 17 | my $raw = $q->param('raw'); | ||||
37 | |||||||
38 | 3 | 100 | 22 | return error("file is a mandatory parameter!") unless $file; | |||
39 | |||||||
40 | 2 | 50 | 12 | $file = "$basedir/$file" unless $file =~ m#^/#; | |||
41 | 2 | 50 | 59 | return error("Sorry, $file doesn't exist!") unless -e $file; | |||
42 | |||||||
43 | 2 | 3 | my $contents; | ||||
44 | 2 | 50 | 89 | open(my $fh, $file) or return error("Can't open $file: $!"); | |||
45 | { | ||||||
46 | 2 | 5 | local $/ = undef; | ||||
2 | 9 | ||||||
47 | 2 | 54 | $contents = <$fh>; | ||||
48 | } | ||||||
49 | 2 | 50 | 26 | close $fh or return error("Can't close $file: $!"); | |||
50 | |||||||
51 | 2 | 100 | 31 | return header . $contents if $raw; | |||
52 | |||||||
53 | 1 | 3 | $contents =~ s/ | ||||
54 | 1 | 8 | $contents = pre($contents); | ||||
55 | 1 | 500 | return header . start_html("Contents of $file") . $contents . end_html; | ||||
56 | } | ||||||
57 | |||||||
58 | sub state { | ||||||
59 | 7 | 50 | 7 | 1 | 32497 | my $q = shift or croak("CGI query object is mandatory!"); | |
60 | |||||||
61 | 7 | 100 | 1128 | if ($q->param('clear_state')) { | |||
62 | 2 | 20 | _clear_state(); | ||||
63 | 2 | 74 | return header . start_html . h1("State cleared"); | ||||
64 | } | ||||||
65 | |||||||
66 | 5 | 87 | my $key = $q->param('key'); | ||||
67 | 5 | 291 | my $value = $q->param('value'); | ||||
68 | 5 | 100 | 40 | return error('key is a mandatory parameter!') unless $key; | |||
69 | 3 | 100 | 12 | unless ($value) { | |||
70 | 2 | 6 | my $val = _read_state($key); | ||||
71 | 2 | 100 | 12 | return error("'$key' is not a valid key!") unless defined $val; | |||
72 | 1 | 24 | return header . start_html . h1("State for $key") | ||||
73 | . "'$key' is '$val'"; | ||||||
74 | } | ||||||
75 | 1 | 7 | eval { | ||||
76 | 1 | 6 | _store_state( $key => $value ); | ||||
77 | }; | ||||||
78 | 1 | 50 | 4 | if ($@) { | |||
79 | 0 | 0 | return header . start_html . h1("State for $key") | ||||
80 | . "Error saving key $key: $@"; | ||||||
81 | } | ||||||
82 | 1 | 42 | return header . start_html . h1("State for $key") | ||||
83 | . "Stored '$value' in '$key'"; | ||||||
84 | } | ||||||
85 | |||||||
86 | my $statefile = "/tmp/selenium-utils-state"; | ||||||
87 | |||||||
88 | sub _read_state { | ||||||
89 | 3 | 3 | 6 | my $key = shift; | |||
90 | 3 | 8 | my $content = ''; | ||||
91 | 3 | 100 | 70 | if (-e $statefile) { | |||
92 | 2 | 13 | local $/; | ||||
93 | 2 | 50 | 82 | open(my $fh, $statefile) or die "Can't open $statefile: $!"; | |||
94 | 2 | 58 | $content = <$fh>; | ||||
95 | 2 | 50 | 33 | close $fh or die "Can't close $statefile: $!"; | |||
96 | } | ||||||
97 | |||||||
98 | 3 | 5 | my $state; | ||||
99 | 3 | 351 | eval $content; | ||||
100 | 3 | 100 | 20 | $state ||= {}; | |||
101 | 3 | 100 | 18 | return $key ? $state->{$key} : $state; | |||
102 | } | ||||||
103 | |||||||
104 | sub _store_state { | ||||||
105 | 1 | 1 | 4 | my ($key, $val) = @_; | |||
106 | 1 | 4 | my $state = _read_state; | ||||
107 | 1 | 3 | $state->{$key} = $val; | ||||
108 | 1 | 10 | my $tmpstate = "$statefile.$$"; | ||||
109 | 1 | 30 | my $textstate = Data::Dumper->Dump([$state], ["state"]); | ||||
110 | 1 | 50 | 830 | open(my $fh, ">$tmpstate") or die "Can't open $tmpstate: $!"; | |||
111 | 1 | 13 | print $fh $textstate; | ||||
112 | 1 | 50 | 68 | close $fh or die "Can't write $tmpstate: $!"; | |||
113 | 1 | 50 | 80 | rename $tmpstate, $statefile or | |||
114 | die "Can't rename $tmpstate, $statefile: $!"; | ||||||
115 | } | ||||||
116 | |||||||
117 | sub _clear_state { | ||||||
118 | 2 | 2 | 267 | unlink $statefile; | |||
119 | } | ||||||
120 | |||||||
121 | sub error { | ||||||
122 | 5 | 5 | 0 | 16 | my $msg = shift; | ||
123 | 5 | 147 | return header . start_html . h1("Error!") . $msg . end_html; | ||||
124 | } | ||||||
125 | |||||||
126 | 1; | ||||||
127 | __END__ |