MtPolicyd/Request.pm

filter: Statementstmt 61.7% 50 / 81 Branchbran 44.1% 15 / 34 Conditioncond 50.0% 12 / 24 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 80.0% 8 / 10 Podpod 100% 6 / 6 totaltotal 55.8% 95 / 170 CC 27 SCAR 30.7
CC
27
Cov
55%
new_from_fh
72.0
get
12.9
is_attr_defined
7.0
CRAP 21.6
« MtPolicyd/Client/Response.pm ↑ Summary MtPolicyd/Plugin/Stress.pm »
1 package Mail::MtPolicyd::Request;
2  
3 21 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 our $VERSION = '2.01'; # VERSION
9 # ABSTRACT: the request object
10  
11  
12 21 has 'attributes' => (
13 is => 'ro', isa => 'HashRef', required => 1,
14 traits => [ 'Hash' ],
15 handles => { 'attr' => 'get' },
16 );
17  
18  
19 # gets attached later
20 21 has 'session' => ( is => 'rw', isa => 'Maybe[HashRef]' );
21  
22  
23 21 has 'server' => (
24 is => 'ro', isa => 'Net::Server', required => 1,
25 handles => {
26 'log' => 'log',
27 }
28 );
29  
30  
31 has 'type' => (
32 is => 'ro',
33 isa => 'Str',
34 lazy => 1,
35 default => sub {
36 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 return( $self->attr('request') );
38 }
39 21 );
40  
41  
42 21 has 'use_caching' => ( is => 'rw', isa => 'Bool', default => 1 );
43  
44  
45 sub dump_attr {
46 0 ▶ my $self = shift;
Subroutine dump_attr: not called CC=1 SCAR=6.9
Pod documented
47 0 my $attr = $self->attributes;
48 0 return( join(', ', map { $_.'='.$attr->{$_} } keys %$attr ) );
49 }
50  
51  
52 sub get {
53 11 ▶ my ( $self, $value ) = @_;
Subroutine get: called CC=10 SCAR=25.5
Pod documented
54 11 my ($scope, $name);
55  
56 11 ▶ if( ! defined $value || $value eq '' ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or $value eq "") 0 11 11
Condition33%1/3
not defined $value or $value eq ""
l!l&&r!l&&!r
0011
MC/DC0%0/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table33%1/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
57  
58 11 my @params = split(':', $value, 2);
59 11 ▶ if( scalar(@params) == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @params == 2) { } 10 1 11
elsif (scalar @params == 1) { } 1 0 1
60 10 ( $scope, $name ) = @params;
61 } elsif( scalar(@params) == 1) {
62 1 ( $scope, $name ) = ( 'request', @params );
63 }
64  
65 11 ▶ if( $scope eq 'session' || $scope eq 's' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scope eq 'session' or $scope eq 's') { } 6 5 11
elsif ($scope eq 'request' or $scope eq 'r') { } 5 0 5
Condition100%3/3
$scope eq "session" or $scope eq "s"
l!l&&r!l&&!r
155
MC/DC100%2/2
$scope eq "session" or $scope eq "s"
$scope eq "session" $scope eq "s"
Truth table100%3/3
A or B
A= $scope eq "session"B= $scope eq "s"
ABresult
1X1
011
000
66 6 ▶ if( ! defined $self->session ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->session) 0 6 6
67 0 return;
68 }
69 6 return $self->session->{$name};
70 ▶ } elsif( $scope eq 'request' || $scope eq 'r' ) {
Condition66%2/3
$scope eq "request" or $scope eq "r"
l!l&&r!l&&!r
230
MC/DC0%0/2
$scope eq "request" or $scope eq "r"
$scope eq "request" $scope eq "r"
Truth table66%2/3
A or B
A= $scope eq "request"B= $scope eq "r"
ABresult
1X1
011
000
71 5 return $self->attr( $name );
72 }
73  
74 0 die("unknown scope $scope while retrieving variable for $value");
75  
76 0 return;
77 }
78  
79  
80 sub new_from_fh {
81 0 ▶ my ( $class, $fh ) = ( shift, shift );
Subroutine new_from_fh: not called CC=8 SCAR=42.8
Pod documented
82 0 my $attr = {};
83 0 my $complete = 0;
84 0 my $line;
85 0 ▶ while( defined( $line = $fh->getline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $fh->getline)) 0 0 0
86 0 $line =~ s/\r?\n$//;
87 0 ▶ if( $line eq '') { $complete = 1 ; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 0 0
88 0 my ( $name, $value ) = split('=', $line, 2);
89 0 ▶ if( ! defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
90 0 die('error parsing request');
91 }
92 0 $attr->{$name} = $value;
93 }
94 0 ▶ if( $fh->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->error) 0 0 0
95 0 die('while reading request: '.$fh->error);
96 }
97 0 ▶ if( ! defined $line && ! $complete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line or $complete) 0 0 0
Condition0%0/3
defined $line or $complete
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $line or $complete
defined $line $complete
Truth table0%0/3
A or B
A= defined $lineB= $complete
ABresult
1X1
011
000
98 0 die('connection closed by peer');
99 }
100 0 ▶ if( ! $complete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($complete) 0 0 0
101 0 die('could not parse request');
102 }
103 0 my $obj = $class->new(
104 'attributes' => $attr,
105 @_
106 );
107 0 return $obj;
108 }
109  
110  
111 sub do_cached {
112 17 ▶ my ( $self, $key, $call ) = @_;
Subroutine do_cached: called CC=4 SCAR=14.0
Pod documented
113 17 my $session = $self->session;
114  
115 # we cant cache a result without session
116 17 ▶ if( ! defined $session || ! $self->use_caching ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session and $self->use_caching) 16 1 17
Condition66%2/3
defined $session and $self->use_caching
!ll&&!rl&&r
0161
MC/DC50%1/2
defined $session and $self->use_caching
defined $session $self->use_caching
Truth table66%2/3
A and B
A= defined $sessionB= $self->use_caching
ABresult
0X0
100
111
117 16 return( $call->() );
118 }
119 1 ▶ if( ! defined $session->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session->{$key}) 1 0 1
120 1 $session->{$key} = [ $call->() ];
121 }
122 1 return( @{$session->{$key}} );
123 }
124  
125  
126 sub is_already_done {
127 47 ▶ my ( $self, $key ) = @_;
Subroutine is_already_done: called CC=4 SCAR=14.2
Pod documented
128 47 my $session = $self->session;
129  
130 # we cant cache a result without session
131 47 ▶ if( ! defined $session || ! $self->use_caching ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session and $self->use_caching) 44 3 47
Condition66%2/3
defined $session and $self->use_caching
!ll&&!rl&&r
0443
MC/DC50%1/2
defined $session and $self->use_caching
defined $session $self->use_caching
Truth table66%2/3
A and B
A= defined $sessionB= $self->use_caching
ABresult
0X0
100
111
132 44 return 0;
133 }
134 3 ▶ if( defined $session->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session->{$key}) 0 3 3
135 0 return(1);
136 }
137 3 $session->{$key} = 1;
138 3 return 0;
139 }
140  
141  
142 sub is_attr_defined {
143 6 ▶ my ( $self, @fields ) = @_;
Subroutine is_attr_defined: called CC=5 SCAR=19.4
Pod documented
144 6 my $a = $self->attributes;
145  
146 6 foreach my $field ( @fields ) {
147 6 ▶ if( ! defined $a->{$field}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $a->{$field} or $a->{$field} eq "" or $a->{$field} =~ /^\s+$/) 0 6 6
Condition33%2/6
not defined $a->{$field} or $a->{$field} eq "" or $a->{$field} =~ /^\s+$/
l!l&&r!l&&!r
006
not defined $a->{$field} or $a->{$field} eq ""
l!l&&r!l&&!r
006
MC/DC0%0/3
not defined $a->{$field} or $a->{$field} eq "" or $a->{$field} =~ /^\s+$/
not defined $a->{$field} $a->{$field} eq "" $a->{$field} =~ /^\s+$/
Truth table25%1/4
not(A) or B or C
A= not defined $a->{$field}B= $a->{$field} eq ""C= $a->{$field} =~ /^\s+$/
ABCresult
1XX1
01X1
0011
0000
148 || $a->{$field} eq ''
149 || $a->{$field} =~ /^\s+$/ ) {
150 0 return 0;
151 }
152 }
153  
154 6 return 1;
155 }
156  
157 21 __PACKAGE__->meta->make_immutable;
158  
159 21 1;
160  
161 __END__
162  
163 =pod
164  
165 =encoding UTF-8
166  
167 =head1 NAME
168  
169 Mail::MtPolicyd::Request - the request object
170  
171 =head1 VERSION
172  
173 version 2.01
174  
175 =head1 ATTRIBUTES
176  
177 =head2 attributes
178  
179 Contains an HashRef with all attributes of the request.
180  
181 To retrieve a single attribute the attr method could be used:
182  
183 $obj->attr('sender');
184  
185 =head2 session
186  
187 Contains a HashRef with all values stored in the session.
188  
189 mtpolicyd will persist the content of this HashRef across requests with the same instance_id.
190  
191 =head2 server
192  
193 Contains the Net::Server object of mtpolicyd.
194  
195 =head2 type
196  
197 The type of the request. Postfix will always use 'smtpd_access_policy'.
198  
199 =head2 use_caching
200  
201 Could be used to disable caching. Only used within the unit tests.
202  
203 =head1 METHODS
204  
205 =head2 dump_attr
206  
207 Returns an string to dump the content of a request.
208  
209 =head2 get($variable_name)
210  
211 Retrieve value of a session or request variable.
212  
213 The format for the variable name is
214  
215 (<scope>:)?<variable>
216  
217 If no scope is given it default to the request scope.
218  
219 Valid scopes are:
220  
221 =over
222  
223 =item session, s
224  
225 Session variables.
226  
227 =item request, r
228  
229 Request attributes.
230  
231 =back
232  
233 For example:
234  
235 $r->get('request:sender'); # retrieve sender from request
236 $r->get('r:sender'); # short format
237 $r->get('sender'); # scope defaults to request
238  
239 $r->get('session:user_policy'); # retrieve session variable user_policy
240 $r->get('s:user_policy'); # the same
241  
242 =head2 new_from_fh($fh)
243  
244 An object constructor for creating an request object with the content read
245 for the supplied filehandle $fh.
246  
247 Will die if am error ocours:
248  
249 =over
250  
251 =item error parsing request
252  
253 A line in the request could not be parsed.
254  
255 =item while reading request: <io-error>
256  
257 The filehandle had an error while reading the request.
258  
259 =item connection closed by peer
260  
261 Connection has been closed while reading the request.
262  
263 =item could not parse request
264  
265 The client did not send a complete request.
266  
267 =back
268  
269 =head2 do_cached( $key, $sub )
270  
271 This method will execute the function reference give in $sub and store
272 the return values in $key within the session.
273 If there is already a cached result stored within $key of the session
274 it will return the content instead of calling the reference again.
275  
276 Returns an Array with the return values of the function call.
277  
278 Example:
279  
280 my ( $ip_result, $info ) = $r->do_cached('rbl-'.$self->name.'-result',
281 sub { $self->_rbl->check( $ip ) } );
282  
283 =head2 is_already_done( $key )
284  
285 This function will raise an flag with name of $key within the session and return true if the
286 flag is already set. False otherwise.
287  
288 This could be used to prevent scores or headers from being applied a second time.
289  
290 Example:
291  
292 if( defined $self->score && ! $r->is_already_done('rbl-'.$self->name.'-score') ) {
293 $self->add_score($r, $self->name => $self->score);
294 }
295  
296 =head2 is_attr_defined
297  
298 Returns true if all given attribute names are defined and non-empty.
299  
300 =head1 AUTHOR
301  
302 Markus Benning <ich@markusbenning.de>
303  
304 =head1 COPYRIGHT AND LICENSE
305  
306 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
307  
308 This is free software, licensed under:
309  
310 The GNU General Public License, Version 2, June 1991
311  
312 =cut
« MtPolicyd/Client/Response.pm ↑ Summary MtPolicyd/Plugin/Stress.pm »