POEGateway.pm

filter: Statementstmt 29.2% 40 / 137 Branchbran 2.0% 1 / 50 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 65.0% 13 / 20 Podpod 33.3% 1 / 3 totaltotal 22.1% 55 / 248 CC 35 SCAR 53.7
CC
35
Cov
19%
spawn
462.0
got_req
156.0
_start
12.0
CRAP 215.4
« POEGateway/Mailer.pm ↑ Summary POEGateway/Mailer/SMTP.pm »
1 # Declare our package
2 package Test::Reporter::POEGateway;
3 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Initialize our version
6 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = '0.04';
8  
9 # Import what we need from the POE namespace
10 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POE::Component::Server::SimpleHTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use base 'POE::Session::AttributeBased';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Misc stuff
15 1 ▶use HTTP::Request::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Set some constants
18 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
19 1 ▶ if ( ! defined &DEBUG ) { *DEBUG = sub () { 0 } }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &Test::Reporter::POEGateway::DEBUG) 1 0 1
20 }
21  
22 # starts the component!
23 0 ▶sub spawn {
Subroutine spawn: not called CC=21 SCAR=61.4
Pod undocumented
24 0 my $class = shift;
25  
26 # The options hash
27 0 my %opt;
28  
29 # Support passing in a hash ref or a regular hash
30 0 ▶ if ( ( @_ & 1 ) and ref $_[0] and ref( $_[0] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1 and ref $_[0] and ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/6
@_ & 1 and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ & 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ & 1 and ref $_[0] and ref $_[0] eq "HASH"
@_ & 1 ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/4
A and B and C
A= @_ & 1B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
31 0 %opt = %{ $_[0] };
32 } else {
33 # Sanity checking
34 0 ▶ if ( @_ & 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1) 0 0 0
35 0 warn __PACKAGE__ . ' requires an even number of options passed to spawn()';
36 0 return 0;
37 }
38  
39 0 %opt = @_;
40 }
41  
42 # lowercase keys
43 0 %opt = map { lc($_) => $opt{$_} } keys %opt;
44  
45 # setup the HTTPD opts
46 0 ▶ if ( ! exists $opt{'httpd'} or ! defined $opt{'httpd'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{'httpd'} && defined $opt{'httpd'}) { } 0 0 0
Condition0%0/3
exists $opt{'httpd'} && defined $opt{'httpd'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{'httpd'} && defined $opt{'httpd'}
exists $opt{'httpd'} defined $opt{'httpd'}
Truth table0%0/3
A && B
A= exists $opt{'httpd'}B= defined $opt{'httpd'}
ABresult
0X0
100
111
47 0 if ( DEBUG ) {
48 warn 'Using default HTTPD = { ALIAS => HTTPD, PORT => 11_111, HOSTNAME => POEGateway.net }';
49 }
50  
51 # Set the default
52 0 $opt{'httpd'} = {};
53 } else {
54 0 ▶ if ( ref( $opt{'httpd'} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{"httpd"} ne "HASH") 0 0 0
55 0 warn "The httpd argument is not a valid HASH reference!";
56 0 return 0;
57 }
58 }
59  
60 # Cleanup the httpd opts
61 $opt{'httpd'} = {
62 'ALIAS' => 'HTTPD',
63 'PORT' => 11_111,
64 'HOSTNAME' => 'POEGateway.net',
65 0 %{ $opt{'httpd'} },
66 };
67 0 ▶ delete $opt{'httpd'}->{'HANDLERS'} if exists $opt{'httpd'}->{'HANDLERS'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"httpd"}{"HANDLERS"} 0 0 0
68  
69 # Do we have a mailer session?
70 0 ▶ if ( exists $opt{'mailer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{'mailer'}) { } 0 0 0
71 0 ▶ if ( ! defined $opt{'mailer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opt{'mailer'}) { } 0 0 0
72 0 warn "Please supply a valid mailer session!";
73 0 return 0;
74 } else {
75 0 if ( DEBUG ) {
76 warn "Using '$opt{'mailer'}' as the mailer session - not using REPORTS!";
77 }
78 }
79  
80 # Remove any reports argument
81 0 ▶ if ( exists $opt{'reports'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"reports"}) 0 0 0
82 0 warn "You cannot use REPORTS with MAILER at the same time, preferring MAILER!";
83 0 delete $opt{'reports'};
84 }
85 } else {
86 # setup the path to store reports
87 0 ▶ if ( ! exists $opt{'reports'} or ! defined $opt{'reports'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"reports"} and defined $opt{"reports"}) 0 0 0
Condition0%0/3
exists $opt{"reports"} and defined $opt{"reports"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{"reports"} and defined $opt{"reports"}
exists $opt{"reports"} defined $opt{"reports"}
Truth table0%0/3
A and B
A= exists $opt{"reports"}B= defined $opt{"reports"}
ABresult
0X0
100
111
88 0 require File::Spec;
89 0 my $path = File::Spec->catdir( $ENV{HOME}, 'cpan_reports' );
90 0 if ( DEBUG ) {
91 warn "Using default REPORTS = '$path'";
92 }
93  
94 # Set the default
95 0 $opt{'reports'} = $path;
96 }
97  
98 # validate the report path
99 0 ▶ if ( ! -d $opt{'reports'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $opt{"reports"}) 0 0 0
100 0 warn "The report path does not exist ($opt{'reports'}), please make sure it is a writable directory!";
101 0 return 0;
102 }
103 }
104  
105 # setup the alias
106 0 ▶ if ( ! exists $opt{'alias'} or ! defined $opt{'alias'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"alias"} and defined $opt{"alias"}) 0 0 0
Condition0%0/3
exists $opt{"alias"} and defined $opt{"alias"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{"alias"} and defined $opt{"alias"}
exists $opt{"alias"} defined $opt{"alias"}
Truth table0%0/3
A and B
A= exists $opt{"alias"}B= defined $opt{"alias"}
ABresult
0X0
100
111
107 0 if ( DEBUG ) {
108 warn 'Using default ALIAS = POEGateway';
109 }
110  
111 # Set the default
112 0 $opt{'alias'} = 'POEGateway';
113 }
114  
115 # setup the key callback
116 0 ▶ if ( ! exists $opt{'key_cb'} or ! defined $opt{'key_cb'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{'key_cb'} && defined $opt{'key_cb'}) { } 0 0 0
Condition0%0/3
exists $opt{'key_cb'} && defined $opt{'key_cb'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{'key_cb'} && defined $opt{'key_cb'}
exists $opt{'key_cb'} defined $opt{'key_cb'}
Truth table0%0/3
A && B
A= exists $opt{'key_cb'}B= defined $opt{'key_cb'}
ABresult
0X0
100
111
117 0 if ( DEBUG ) {
118 warn 'Using default KEY_CB = { 1 }';
119 }
120  
121 # Set the default
122 0 ▶ $opt{'key_cb'} = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 } else {
124 # make sure it's a sub reference
125 0 ▶ if ( ref( $opt{'key_cb'} ) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{"key_cb"} ne "CODE") 0 0 0
126 0 warn "The key_cb is not a valid code reference!";
127 0 return 0;
128 }
129 }
130  
131 # Create our session
132 POE::Session->create(
133 __PACKAGE__->inline_states(),
134 'heap' => {
135 'ALIAS' => $opt{'alias'},
136 'HTTPD_OPT' => $opt{'httpd'},
137 'KEY_CB' => $opt{'key_cb'},
138 0 ▶ ( exists $opt{'mailer'} ? ( 'MAILER' => $opt{'mailer'} ) : ( 'REPORTS' => $opt{'reports'} ) ),
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'mailer'} ? : 0 0 0
139 },
140 );
141  
142 # return success
143 0 return 1;
144 }
145  
146 # This starts the component
147 0 ▶sub _start : State {
Subroutine _start: not called CC=3 SCAR=24.8
148 0 if ( DEBUG ) {
149 warn 'Starting alias "' . $_[HEAP]->{'ALIAS'} . '"';
150 }
151  
152 # Set up the alias for ourself
153 0 $_[KERNEL]->alias_set( $_[HEAP]->{'ALIAS'} );
154  
155 # spawn the httpd
156 POE::Component::Server::SimpleHTTP->new(
157 0 ▶ %{ $_[HEAP]->{'HTTPD_OPT'} },
Branch: 1 branch
BranchTrueFalseTotal
"POE::Component::Server::SimpleHTTP"->new(%{$_[3]{"HTTPD_OPT"};}, "HANDLERS", [{"DIR", ".*", "SESSION", $_[1], "EVENT", "got_req"}]) or die "Unable to create httpd" 0 0 0
158 'HANDLERS' => [
159 {
160 'DIR' => '.*',
161 'SESSION' => $_[SESSION],
162 'EVENT' => 'got_req',
163 },
164 ],
165 ) or die 'Unable to create httpd';
166  
167 # load the file-related modules we need
168 0 ▶ if ( exists $_[HEAP]->{'REPORTS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"REPORTS"}) 0 0 0
169 0 require YAML::Tiny;
170 0 YAML::Tiny->import( qw( DumpFile ) );
171  
172 0 require Digest::SHA1;
173 0 Digest::SHA1->import( qw( sha1_hex ) );
174  
175 0 require File::Spec;
176 }
177  
178 0 return;
179 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 # POE Handlers
182 0 ▶sub _stop : State {
Subroutine _stop: not called CC=1 SCAR=6.9
183 0 if ( DEBUG ) {
184 warn 'Stopping alias "' . $_[HEAP]->{'ALIAS'} . '"';
185 }
186  
187 0 return;
188 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 0 ▶sub _child : State {
Subroutine _child: not called CC=1 SCAR=6.9
191 0 return;
192 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
193  
194 0 ▶sub shutdown : State {
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod documented
195 # cleanup some stuff
196 0 $_[KERNEL]->alias_remove( $_[HEAP]->{'ALIAS'} );
197  
198 # tell poco-simplehttp to shutdown
199 0 $_[KERNEL]->post( $_[HEAP]->{'HTTPD_OPT'}->{'ALIAS'}, 'SHUTDOWN' );
200  
201 0 return;
202 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
203  
204 # Got a HTTP request
205 0 ▶sub got_req : State {
Subroutine got_req: not called CC=12 SCAR=50.5
Pod undocumented
206 # ARG0 = HTTP::Request object, ARG1 = HTTP::Response object, ARG2 = the DIR that matched
207 0 my( $request, $response, $dirmatch ) = @_[ ARG0 .. ARG2 ];
208 ## no critic ( ProhibitAccessOfPrivateData )
209  
210 # a sane Test::Reporter submission?
211 # mostly copied from Test::Reporter::HTTPGateway, thanks!
212 0 my $form = HTTP::Request::Params->new({ req => $request })->params;
213 0 foreach my $v ( qw( from subject via report ) ) {
214 0 ▶ if ( ! exists $form->{ $v } or ! defined $form->{ $v } or ! length( $form->{ $v } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $form->{$v} and defined $form->{$v} and length $form->{$v}) 0 0 0
Condition0%0/6
exists $form->{$v} and defined $form->{$v} and length $form->{$v}
!ll&&!rl&&r
000
exists $form->{$v} and defined $form->{$v}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $form->{$v} and defined $form->{$v} and length $form->{$v}
exists $form->{$v} defined $form->{$v} length $form->{$v}
Truth table0%0/4
A and B and C
A= exists $form->{$v}B= defined $form->{$v}C= length $form->{$v}
ABCresult
0XX0
10X0
1100
1111
215 0 $response->code( 500 );
216 0 $response->content( "ERROR: Missing $v field" );
217 0 last;
218 }
219  
220 0 ▶ next if $v eq 'report';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "report" 0 0 0
221 0 ▶ if ( $form->{ $v } =~ /[\r\n]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->{$v} =~ /[\r\n]/) 0 0 0
222 0 $response->code( 500 );
223 0 $response->content( "ERROR: Malformed $v field" );
224 0 last;
225 }
226 }
227  
228 # Do we need to check key?
229 0 ▶ if ( ! $_[HEAP]->{'KEY_CB'}->( $form->{'key'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[3]{"KEY_CB"}->($form->{"key"})) 0 0 0
230 0 $response->code( 401 );
231 0 $response->content( 'Access denied, please supply a correct key.' );
232 }
233  
234 # not a malformed request...
235 0 ▶ if ( ! defined $response->code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->code) 0 0 0
236 # add some misc info
237 0 $form->{'_sender'} = $response->connection->remote_ip;
238 0 $form->{'via'} .= ', via ' . __PACKAGE__ . ' ' . $VERSION;
239 0 ▶ delete $form->{'key'} if exists $form->{'key'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $form->{"key"} 0 0 0
240  
241 # Do we have a mailer?
242 0 ▶ if ( exists $_[HEAP]->{'MAILER'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{'MAILER'}) { } 0 0 0
243 # send it off!
244 0 $_[KERNEL]->post( $_[HEAP]->{'MAILER'}, 'http_report', $form );
245  
246 0 if ( DEBUG ) {
247 warn "Sent $form->{subject} report to the mailer";
248 }
249 } else {
250 # calculate the filename
251 0 my $filename = File::Spec->catfile( $_[HEAP]->{'REPORTS'}, time() . '.' . sha1_hex( $form->{'report'} ) );
252 0 eval {
253 0 DumpFile( $filename, $form );
254 };
255 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
256 0 warn "Unable to save $form->{subject} report to $filename: $@";
257 } else {
258 0 if ( DEBUG ) {
259 warn "Saved $form->{subject} report to $filename";
260 }
261 }
262 }
263  
264 # Do our stuff to HTTP::Response
265 0 $response->code( 200 );
266 0 $response->content( 'Report Submitted.' );
267 }
268  
269 # We are done!
270 0 $_[KERNEL]->post( 'HTTPD', 'DONE', $response );
271  
272 0 return;
273 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275 1 1;
276 __END__
« POEGateway/Mailer.pm ↑ Summary POEGateway/Mailer/SMTP.pm »