blib/lib/Lemonldap/NG/Common/PSGI.pm

filter: Statementstmt 40.7% 88 / 216 Branchbran 30.3% 20 / 66 Conditioncond 21.4% 18 / 84 MC/DCmcdc 6.8% 3 / 44 Subroutinesub 41.6% 15 / 36 Podpod 45.4% 10 / 22 totaltotal 32.9% 154 / 468 CC 72 SCAR 33.6
CC
72
Cov
34%
sendError
110.0
sendHtml
72.0
getLanguage
42.0
CRAP 28.7
« blib/lib/Lemonldap/NG/Common/Notifications/XML.pm ↑ Summary blib/lib/Lemonldap/NG/Common/MessageBroker/Web.pm »
1 package Lemonldap::NG::Common::PSGI;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Lemonldap::NG::Common::PSGI::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Lemonldap::NG::Common::PSGI::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '2.23.0';
11  
12 5 our $_json = JSON->new->allow_nonref;
13  
14 # PROPERTIES
15  
16 5 has error => ( is => 'rw', default => '' );
17 5 has languages => ( is => 'rw', isa => 'Str', default => 'en' );
18 5 has logLevel => ( is => 'rw', isa => 'Str', default => 'info' );
19 5 has staticPrefix => ( is => 'rw', isa => 'Str' );
20 5 has instanceName => ( is => 'rw', isa => 'Str', default => '' );
21 5 has customCSS => ( is => 'rw', isa => 'Str', default => '' );
22 5 has customPortalUrl => ( is => 'rw', isa => 'Str', default => '' );
23 5 has templateDir => ( is => 'rw', isa => 'Str|ArrayRef' );
24 5 has links => ( is => 'rw', isa => 'ArrayRef' );
25 5 has menuLinks => ( is => 'rw', isa => 'ArrayRef' );
26 5 has logger => ( is => 'rw' );
27 5 has userLogger => ( is => 'rw' );
28 5 has _auditLogger => ( is => 'rw' );
29  
30 # portal only stores the "default" portal URL, in case a request
31 # is not available in the current context to compute the dynamic portal URL
32 5 has portal => ( is => 'rw', isa => 'Str' );
33  
34 # INITIALIZATION
35  
36 7 ▶sub init {
Subroutine init: called CC=4 SCAR=14.5
Pod undocumented
37 7 my ( $self, $args ) = @_;
38 7 ▶ unless ( ref $args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args) 0 7 7
39 0 $self->error('init argument must be a hashref');
40 0 return 0;
41 }
42 7 foreach my $k ( keys %$args ) {
43 25 ▶ $self->{$k} = $args->{$k} unless ( $k eq 'logger' );
Branch: 1 branch
BranchTrueFalseTotal
unless $k eq "logger" 19 6 25
44 }
45 7 $self->_initLogger($args);
46  
47 6 return 1;
48 }
49  
50 7 ▶sub _initLogger {
Subroutine _initLogger: called CC=15 SCAR=29.0
51 7 my ( $self, $args ) = @_;
52  
53 my $logger =
54 $args->{logger}
55 || $ENV{LLNG_DEFAULTLOGGER}
56 7 ▶ || 'Lemonldap::NG::Common::Logger::Std';
Condition80%4/5
$args->{'logger'} || $ENV{'LLNG_DEFAULTLOGGER'} || 'Lemonldap::NG::Common::Logger::Std'
l!l
61
$args->{"logger"} or $ENV{"LLNG_DEFAULTLOGGER"}
l!l&&r!l&&!r
601
MC/DC50%1/2
$args->{'logger'} || $ENV{'LLNG_DEFAULTLOGGER'} || 'Lemonldap::NG::Common::Logger::Std'
$args->{"logger"} $ENV{"LLNG_DEFAULTLOGGER"}
Truth table66%2/3
A or B
A= $args->{"logger"}B= $ENV{"LLNG_DEFAULTLOGGER"}
ABresult
1X1
011
000
57 7 ▶ unless ( ref $self->logger ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->logger) 7 0 7
58 7 eval "require $logger";
59 7 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 6 7
60 6 my $err;
61 6 ▶ unless ( $self->{logLevel} =~ /^(?:debug|info|notice|warn|error)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"logLevel"} =~ /^(?:debug|info|notice|warn|error)$/) 0 6 6
62 $err =
63 'Bad logLevel value \''
64 . $self->{logLevel}
65 0 . "', switching to 'info'";
66 0 $self->{logLevel} = 'info';
67 }
68 6 $self->logger( $logger->new($self) );
69 6 ▶ $self->logger->error($err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 6 6
70 }
71  
72 6 ▶ unless ( ref $self->userLogger ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->userLogger) 6 0 6
73 6 ▶ $logger = $ENV{LLNG_USERLOGGER} || $args->{userLogger} || $logger;
Condition66%4/6
$ENV{'LLNG_USERLOGGER'} || $args->{'userLogger'} || $logger
l!l&&r!l&&!r
510
$ENV{"LLNG_USERLOGGER"} or $args->{"userLogger"}
l!l&&r!l&&!r
051
MC/DC0%0/3
$ENV{'LLNG_USERLOGGER'} || $args->{'userLogger'} || $logger
$ENV{"LLNG_USERLOGGER"} $args->{"userLogger"} $logger
Truth table50%2/4
A or B || C
A= $ENV{"LLNG_USERLOGGER"}B= $args->{"userLogger"}C= $logger
ABCresult
1XX1
01X1
0011
0000
74 6 eval "require $logger";
75 6 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
76 6 require Lemonldap::NG::Common::Logger::_Duplicate;
77 6 $self->userLogger(
78 Lemonldap::NG::Common::Logger::_Duplicate->new(
79 $self,
80 user => 1,
81 logger => $logger,
82 dup => $self->logger
83 )
84 );
85 }
86  
87 6 ▶ unless ( ref $self->_auditLogger ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->_auditLogger) 6 0 6
88 $logger =
89 $ENV{LLNG_AUDITLOGGER}
90 || $args->{auditLogger}
91 6 ▶ || "Lemonldap::NG::Common::AuditLogger::UserLoggerCompat";
Condition80%4/5
$ENV{'LLNG_AUDITLOGGER'} || $args->{'auditLogger'} || 'Lemonldap::NG::Common::AuditLogger::UserLoggerCompat'
l!l
42
$ENV{"LLNG_AUDITLOGGER"} or $args->{"auditLogger"}
l!l&&r!l&&!r
042
MC/DC50%1/2
$ENV{'LLNG_AUDITLOGGER'} || $args->{'auditLogger'} || 'Lemonldap::NG::Common::AuditLogger::UserLoggerCompat'
$ENV{"LLNG_AUDITLOGGER"} $args->{"auditLogger"}
Truth table66%2/3
A or B
A= $ENV{"LLNG_AUDITLOGGER"}B= $args->{"auditLogger"}
ABresult
1X1
011
000
92 6 eval "require $logger";
93 6 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
94 6 $self->_auditLogger( $logger->new($self) );
95 }
96 }
97  
98 0 ▶sub getLanguage {
Subroutine getLanguage: not called CC=6 SCAR=37.4
Pod undocumented
99 0 my ( $self, $req ) = @_;
100  
101 # Get the list of allowed languages from config
102 0 my @langArray = split( qr/[ ,]+/, $self->languages );
103  
104 # If the language in the cookie is found in allowed languages, use it
105 0 my $cookie_lang = $req->cookies->{llnglanguage};
106 0 ▶ if ( $cookie_lang and grep { $_ eq $cookie_lang } @langArray ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie_lang and grep {$_ eq $cookie_lang;} @langArray) 0 0 0
Condition0%0/3
$cookie_lang and grep {$_ eq $cookie_lang;} @langArray
!ll&&!rl&&r
000
MC/DC0%0/2
$cookie_lang and grep {$_ eq $cookie_lang;} @langArray
$cookie_lang grep {$_ eq $cookie_lang;} @langArray
Truth table0%0/3
A and B
A= $cookie_langB= grep {$_ eq $cookie_lang;} @langArray
ABresult
0X0
100
111
107 0 return $cookie_lang;
108 }
109  
110 # Get ordered list of preferred languages from Accept-Language header
111 0 my @preferred_req_language = $self->_getLanguageListFromHeader($req);
112  
113 # Go through preferred languages and pick the first allowed one
114 0 for my $req_lang (@preferred_req_language) {
115 0 ▶ if ( grep { lc($_) eq lc($req_lang) } @langArray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {lc $_ eq lc $req_lang;} @langArray) 0 0 0
116 0 return $req_lang;
117 }
118 }
119  
120 # fallback: pick the first allowed language, default to english
121 0 ▶ return ( $langArray[0] || 'en' );
Condition0%0/2
$langArray[0] || 'en'
l!l
00
MC/DC0%0/1
$langArray[0] || 'en'
$langArray[0]
Truth table0%0/2
A
A= $langArray[0]
Aresult
11
00
122 }
123  
124 0 ▶sub _getLanguageListFromHeader {
Subroutine _getLanguageListFromHeader: not called CC=4 SCAR=30.0
125 0 my ( $self, $req ) = @_;
126  
127 # Extract [2 letter code, priority] from list of Accept-Language values
128 my @req_languages_and_prio = map {
129 0 my ( $lang, $prio ) = m/^(\w+)(?:.*q=(\d\.[\d+]))?/;
130 0 ▶ $prio ||= 1;
Condition0%0/2
$prio ||= 1
l!l
00
MC/DC0%0/1
$prio ||= 1
$prio
Truth table0%0/2
A
A= $prio
Aresult
11
00
131  
132 # Skip invalid entries
133 0 ▶ $lang ? ( [ $lang, $prio ] ) : ()
Branch: 1 branch
BranchTrueFalseTotal
$lang ? : 0 0 0
Condition0%0/2
$req->languages || ''
l!l
00
MC/DC0%0/1
$req->languages || ''
$req->languages
Truth table0%0/2
A
A= $req->languages
Aresult
11
00
134 }
135 split( qr/\s*,\s*/, $req->languages || "" );
136  
137 # Sort by priority (perl sort is stable by default)
138 @req_languages_and_prio =
139 0 sort { $b->[1] <=> $a->[1] } @req_languages_and_prio;
140  
141 # Keep only language code
142 0 my @preferred_req_language = map { $_->[0] } @req_languages_and_prio;
143  
144 0 return @preferred_req_language;
145 }
146  
147 16 ▶sub auditLog {
Subroutine auditLog: called CC=1 SCAR=0.0
Pod undocumented
148 16 my ( $self, $req, %info ) = @_;
149 16 $self->_auditLogger->log( $req, %info );
150 }
151  
152 # RUNNING METHODS
153  
154 ## @method void lmLog(string mess, string level)
155 # Log subroutine. Print on STDERR messages if it exceeds `logLevel` value
156 # @param $mess Text to log
157 # @param $level Level (debug|info|notice|warn|error)
158 0 ▶sub lmLog {
Subroutine lmLog: not called CC=1 SCAR=6.9
Pod documented
159 0 my ( $self, $msg, $level ) = @_;
160 0 return $self->logger->$level($msg);
161 }
162  
163 ##@method void userLog(string mess, string level)
164 # Log user actions on Apache logs or syslog.
165 # @param $mess string to log
166 # @param $level level of log message
167 0 ▶sub userLog {
Subroutine userLog: not called CC=1 SCAR=6.9
Pod documented
168 0 my ( $self, $msg, $level ) = @_;
169 0 return $self->userLogger->$level($msg);
170 }
171  
172 ##@method void userInfo(string mess)
173 # Log non important user actions. Alias for userLog() with facility "info".
174 # @param $mess string to log
175 0 ▶sub userInfo {
Subroutine userInfo: not called CC=1 SCAR=6.9
Pod documented
176 0 my ( $self, $msg ) = @_;
177 0 return $self->userLogger->info($msg);
178 }
179  
180 ##@method void userNotice(string mess)
181 # Log user actions like access and logout. Alias for userLog() with facility
182 # "notice".
183 # @param $mess string to log
184 0 ▶sub userNotice {
Subroutine userNotice: not called CC=1 SCAR=6.9
Pod documented
185 0 my ( $self, $msg ) = @_;
186 0 return $self->userLogger->notice($msg);
187 }
188  
189 ##@method void userWarn(string mess)
190 # Log user errors like "bad password". Alias for userLog() with facility
191 # "warn".
192 # @param $mess string to log
193 0 ▶sub userWarn {
Subroutine userWarn: not called CC=1 SCAR=6.9
Pod undocumented
194 0 my ( $self, $msg ) = @_;
195 0 return $self->userLogger->warn($msg);
196 }
197  
198 ##@method void userError(string mess)
199 # Log user errors like "try to change password without token". Alias for
200 # userLog() with facility "error".
201 # @param $mess string to log
202 0 ▶sub userError {
Subroutine userError: not called CC=1 SCAR=6.9
Pod documented
203 0 my ( $self, $msg ) = @_;
204 0 return $self->userLogger->error($msg);
205 }
206  
207 # Responses methods
208 0 ▶sub sendJSONresponse {
Subroutine sendJSONresponse: not called CC=6 SCAR=37.4
Pod documented
209 0 my ( $self, $req, $j, %args ) = @_;
210 0 ▶ $args{code} ||= 200;
Condition0%0/2
$args{"code"} ||= 200
l!l
00
MC/DC0%0/1
$args{"code"} ||= 200
$args{"code"}
Truth table0%0/2
A
A= $args{"code"}
Aresult
11
00
211 0 ▶ $args{headers} ||= [ $req->spliceHdrs ];
Condition0%0/2
$args{"headers"} ||= [$req->spliceHdrs]
l!l
00
MC/DC0%0/1
$args{"headers"} ||= [$req->spliceHdrs]
$args{"headers"}
Truth table0%0/2
A
A= $args{"headers"}
Aresult
11
00
212 0 my $type = 'application/json; charset=utf-8';
213 0 ▶ if ( ref $j ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $j) 0 0 0
214 0 eval {
215 0 ▶ if ( $args{pretty} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'pretty'}) { } 0 0 0
216  
217 # This avoids changing the settings of the $_json reference
218 0 $j = to_json(
219 $j,
220 {
221 allow_nonref => 1,
222 pretty => 1,
223 canonical => 1
224 }
225 );
226 }
227 else {
228 0 $j = $_json->encode($j);
229 }
230 };
231 0 ▶ return $self->sendError( $req, $@ ) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
232 }
233 0 utf8::downgrade($j);
234 return $self->_buildPsgiResponse(
235 $args{code},
236 [
237 'Content-Type' => $type,
238 'Content-Length' => length($j),
239 0 @{ $args{headers} }
240 ],
241 [$j]
242 );
243 }
244  
245 0 ▶sub sendError {
Subroutine sendError: not called CC=10 SCAR=47.0
Pod documented
246 0 my ( $self, $req, $err, $code ) = @_;
247 0 ▶ $err ||= $req->error || '';
Condition0%0/5
$err ||= $req->error || ""
l!l&&r!l&&!r
000
$req->error || ''
l!l
00
MC/DC0%0/2
$err ||= $req->error || ""
$err $req->error
Truth table0%0/3
A ||= B
A= $errB= $req->error
ABresult
1X1
011
000
248 0 ▶ $code ||= 500;
Condition0%0/2
$code ||= 500
l!l
00
MC/DC0%0/1
$code ||= 500
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
249 0 ▶ $self->lmLog( "Error $code: $err", $code > 499 ? 'error' : 'notice' );
Branch: 1 branch
BranchTrueFalseTotal
$code > 499 ? : 0 0 0
250  
251 # SOAP responses
252 0 ▶ if ( $req->env->{HTTP_SOAPACTION} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($req->env->{'HTTP_SOAPACTION'}) { } 0 0 0
elsif (($req->accept // '') =~ /json/) { } 0 0 0
253 0 my $s = '<soapenv:Body>
254 <soapenv:Fault>
255 <Faultcode>soapenv:Client</Faultcode>
256 <Faultstring>' . $err . '.</Faultstring>
257 <Detail>
258 <Key>Fred</Key>
259 </Detail>
260 </soapenv:Fault>
261 </soapenv:Body>';
262 0 return $self->_buildPsgiResponse(
263 $code,
264 [
265 'Content-Type' => 'application/xml; charset=utf-8',
266 'Content-Length' => length($s),
267 $req->spliceHdrs,
268 ],
269 [$s]
270 );
271 }
272  
273 # Handle Ajax responses
274 ▶ elsif ( ( $req->accept // "" ) =~ /json/ ) {
Condition0%0/2
$req->accept // ""
l!l
00
MC/DC0%0/1
$req->accept // ""
$req->accept
Truth table0%0/2
A
A= $req->accept
Aresult
11
00
275 0 return $self->sendJSONresponse( $req, { error => $err },
276 code => $code );
277 }
278  
279 # Default response: HTML
280 else {
281 0 ▶ my $title = eval { HTTP::Status::status_message($code) } || 'Error';
Condition0%0/2
eval { do { &HTTP::Status::status_message($code) } } || 'Error'
l!l
00
MC/DC0%0/1
eval { do { &HTTP::Status::status_message($code) } } || 'Error'
eval { do { &HTTP::Status::status_message($code) } }
Truth table0%0/2
A
A= eval { do { &HTTP::Status::status_message($code) } }
Aresult
11
00
282 0 ▶ print STDERR $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
283  
284 # TODO: this should probably use a template instead
285 0 my $s = "<html><head><title>$title</title>
286 <style>
287 body{background:#000;color:#fff;padding:10px 50px;font-family:sans-serif;}a{text-decoration:none;color:#fff;}h1{text-align:center;}
288 </style>
289 </head>
290 <body>
291 <h1>$title</h1>
292 <p>$err</p>
293 <center><a href=\"https://lemonldap-ng.org\">LemonLDAP::NG</a></center>
294 </body>
295 </html>";
296 0 return $self->sendRawHtml( $req, $s, code => $code );
297 }
298 }
299  
300 0 ▶sub sendRawHtml {
Subroutine sendRawHtml: not called CC=3 SCAR=24.8
Pod undocumented
301 0 my ( $self, $req, $s, %args ) = @_;
302 0 ▶ my $code = $args{code} || 200;
Condition0%0/2
$args{'code'} || 200
l!l
00
MC/DC0%0/1
$args{'code'} || 200
$args{'code'}
Truth table0%0/2
A
A= $args{'code'}
Aresult
11
00
303 0 ▶ my $headers = $args{headers} || [ $req->spliceHdrs ];
Condition0%0/2
$args{'headers'} || [$req->spliceHdrs]
l!l
00
MC/DC0%0/1
$args{'headers'} || [$req->spliceHdrs]
$args{'headers'}
Truth table0%0/2
A
A= $args{'headers'}
Aresult
11
00
304 return $self->_buildPsgiResponse(
305 $code,
306 [
307 'Content-Type' => 'text/html; charset=utf-8',
308 'Content-Length' => length($s),
309 0 @{$headers},
310 ],
311 [$s]
312 );
313 }
314  
315 0 ▶sub abort {
Subroutine abort: not called CC=1 SCAR=6.9
Pod documented
316 0 my ( $self, $err ) = @_;
317 0 eval { $self->logger->error($err) };
318 return $self->psgiAdapter(
319 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
320 0 $self->sendError( $_[0], $err, 500 );
321 }
322 0 );
323 }
324  
325 0 ▶sub _mustBeDefined {
Subroutine _mustBeDefined: not called CC=2 SCAR=17.9
326 0 my $name = ( caller(1) )[3];
327 0 $name =~ s/^.*:://;
328 0 my $call = ( caller(1) )[0];
329 0 ▶ my $ref = ref( $_[0] ) || $call;
Condition0%0/3
ref $_[0] || $call
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $call
ref $_[0] $call
Truth table0%0/3
A || B
A= ref $_[0]B= $call
ABresult
1X1
011
000
330 0 die "$name() method must be implemented (probably in $ref)";
331 }
332  
333 0 ▶sub handler { _mustBeDefined(@_) }
Subroutine handler: not called CC=1 SCAR=6.9
Pod undocumented
334  
335 0 ▶sub sendJs {
Subroutine sendJs: not called CC=4 SCAR=30.0
Pod undocumented
336 0 my ( $self, $req ) = @_;
337 0 my $sp = $self->staticPrefix;
338 0 $sp =~ s/\/*$/\//;
339 0 ▶ my $sc = $req->script_name // "";
Condition0%0/2
$req->script_name // ""
l!l
00
MC/DC0%0/1
$req->script_name // ""
$req->script_name
Truth table0%0/2
A
A= $req->script_name
Aresult
11
00
340  
341 0 ▶ my $portal = $req->can('portal') ? $req->portal : $self->portal;
Branch: 1 branch
BranchTrueFalseTotal
$req->can('portal') ? : 0 0 0
342  
343 # Javascript scriptname is assumed by our JS code to end with /
344 0 $sc =~ s#/*$#/#;
345 0 my $s =
346 sprintf 'var staticPrefix="%s";'
347 . 'var scriptname="%s";'
348 . 'var availableLanguages="%s".split(/[,;] */);'
349 . 'var portal="%s";', $sp, $sc, $self->languages,
350 $portal;
351 0 ▶ $s .= $self->javascript($req) if ( $self->can('javascript') );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("javascript") 0 0 0
352 0 return $self->_buildPsgiResponse(
353 200,
354 [
355 'Content-Type' => 'application/javascript',
356 'Content-Length' => length($s),
357 'Cache-Control' => 'public,max-age=2592000',
358 ],
359 [$s]
360 );
361 }
362  
363 0 ▶sub sendTextResponse {
Subroutine sendTextResponse: not called CC=2 SCAR=17.9
Pod undocumented
364 0 my ( $self, $req, $text, %args ) = @_;
365  
366 0 my $data = $text;
367  
368 #my $data = Encode::encode( "UTF-8", $text );
369  
370 0 ▶ $args{type} ||= "text/plain; charset=utf-8";
Condition0%0/2
$args{"type"} ||= "text/plain; charset=utf-8"
l!l
00
MC/DC0%0/1
$args{"type"} ||= "text/plain; charset=utf-8"
$args{"type"}
Truth table0%0/2
A
A= $args{"type"}
Aresult
11
00
371  
372 0 return $self->sendBinaryResponse( $req, $data, %args );
373 }
374  
375 0 ▶sub sendBinaryResponse {
Subroutine sendBinaryResponse: not called CC=4 SCAR=30.0
Pod undocumented
376 0 my ( $self, $req, $data, %args ) = @_;
377 0 ▶ $args{code} ||= 200;
Condition0%0/2
$args{"code"} ||= 200
l!l
00
MC/DC0%0/1
$args{"code"} ||= 200
$args{"code"}
Truth table0%0/2
A
A= $args{"code"}
Aresult
11
00
378 0 ▶ $args{type} ||= "application/octet-stream";
Condition0%0/2
$args{"type"} ||= "application/octet-stream"
l!l
00
MC/DC0%0/1
$args{"type"} ||= "application/octet-stream"
$args{"type"}
Truth table0%0/2
A
A= $args{"type"}
Aresult
11
00
379 0 ▶ my $headers = $args{headers} || [ $req->spliceHdrs ];
Condition0%0/2
$args{'headers'} || [$req->spliceHdrs]
l!l
00
MC/DC0%0/1
$args{'headers'} || [$req->spliceHdrs]
$args{'headers'}
Truth table0%0/2
A
A= $args{'headers'}
Aresult
11
00
380  
381 return $self->_buildPsgiResponse(
382 $args{code},
383 [
384 'Content-Type' => $args{type},
385 0 'Content-Length' => length($data),
386 @$headers,
387 ],
388 [$data]
389 );
390 }
391  
392 0 ▶sub sendHtml {
Subroutine sendHtml: not called CC=8 SCAR=42.8
Pod documented
393 0 my ( $self, $req, $template, %args ) = @_;
394 0 my $sp = $self->staticPrefix;
395 0 $sp =~ s/\/*$/\//;
396 0 ▶ my $sc = $req->script_name // "";
Condition0%0/2
$req->script_name // ""
l!l
00
MC/DC0%0/1
$req->script_name // ""
$req->script_name
Truth table0%0/2
A
A= $req->script_name
Aresult
11
00
397  
398 # SCRIPTNAME is assumed by our templates to end with /
399 0 $sc =~ s#/*$#/#;
400 0 ▶ $args{code} ||= 200;
Condition0%0/2
$args{"code"} ||= 200
l!l
00
MC/DC0%0/1
$args{"code"} ||= 200
$args{"code"}
Truth table0%0/2
A
A= $args{"code"}
Aresult
11
00
401 0 ▶ $args{headers} ||= [ $req->spliceHdrs ];
Condition0%0/2
$args{"headers"} ||= [$req->spliceHdrs]
l!l
00
MC/DC0%0/1
$args{"headers"} ||= [$req->spliceHdrs]
$args{"headers"}
Truth table0%0/2
A
A= $args{"headers"}
Aresult
11
00
402 0 my $htpl;
403 0 eval {
404 0 $self->logger->debug("Starting HTML generation using $template");
405 0 require HTML::Template;
406 $htpl = HTML::Template->new(
407 filename => $template . ".tpl",
408 0 ▶ path => ( $args{templateDir} // $self->templateDir ),
Condition0%0/3
$args{"templateDir"} // $self->templateDir
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"templateDir"} // $self->templateDir
$args{"templateDir"} $self->templateDir
Truth table0%0/3
A // B
A= $args{"templateDir"}B= $self->templateDir
ABresult
1X1
011
000
409 search_path_on_include => 1,
410 die_on_bad_params => 0,
411 die_on_missing_include => 1,
412 cache => 0,
413 global_vars => 1,
414 loop_context_vars => 1,
415 );
416  
417 # TODO: replace app
418 # TODO: warn if STATICPREFIX does not end with '/'
419 $htpl->param(
420 STATIC_PREFIX => $sp,
421 INSTANCE_NAME => $self->instanceName,
422 CUSTOM_CSS => $self->customCSS,
423 SCRIPTNAME => $sc,
424 ( $self->can('tplParams') ? ( $self->tplParams($req) ) : () ),
425 (
426 $args{params}
427 0 ▶ ? %{ $args{params} }
Branch: 2 branches
BranchTrueFalseTotal
$self->can('tplParams') ? : 0 0 0
$args{'params'} ? : 0 0 0
428 : ()
429 ),
430 );
431 };
432 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
433 0 $self->logger->error("Unable to render template $template.tpl: $@");
434 0 return $self->sendError( $req,
435 "Unable to render template $template.tpl", 500 );
436 }
437  
438 # Set headers
439 0 my $hdrs = [ 'Content-Type' => 'text/html', @{ $args{headers} } ];
440 0 $self->logger->debug("Sending $template");
441 0 return $self->_buildPsgiResponse( $args{code}, $hdrs, [ $htpl->output() ] );
442 }
443  
444 ###############
445 # Main method #
446 ###############
447  
448 6 ▶sub run {
Subroutine run: called CC=4 SCAR=13.9
Pod documented
449 6 my ( $self, $args ) = @_;
450 6 ▶ $args //= {};
Condition100%2/2
$args //= {}
l!l
15
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
451 6 ▶ unless ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 1 5 6
452 1 $self = $self->new($args);
453 1 ▶ $self->init($args) or $self->logger->error('Initialization failed');
Branch: 1 branch
BranchTrueFalseTotal
$self->init($args) or $self->logger->error("Initialization failed") 1 0 1
454 }
455 6 return $self->_run;
456 }
457  
458 6 ▶sub _run {
Subroutine _run: called CC=1 SCAR=0.0
459 6 my $self = shift;
460 return $self->psgiAdapter(
461 8 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
462 8 $self->handler( $_[0] );
463 }
464 6 );
465 }
466  
467 # This method returns a sub that takes a Lemonldap::NG::Common::PSGI::Request
468 # object and returns a PSGI response into a proper PSGI method.
469 6 ▶sub psgiAdapter {
Subroutine psgiAdapter: called CC=1 SCAR=0.0
Pod undocumented
470 6 my ( $self, $sub ) = @_;
471 8 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
472 8 my $env = shift;
473 8 my $req = Lemonldap::NG::Common::PSGI::Request->new($env);
474 8 return $self->logAndRun( $req, $sub );
475 }
476 6 }
477  
478 # This method sets up LemonLDAP::NG logging for the current request
479 8 ▶sub logAndRun {
Subroutine logAndRun: called CC=10 SCAR=28.8
Pod undocumented
480 8 my ( $self, $req, $sub ) = @_;
481  
482 # register the request object to the logging system
483 8 ▶ if ( ref( $self->logger ) and $self->logger->can('setRequestObj') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->logger and $self->logger->can("setRequestObj")) 0 8 8
Condition33%1/3
ref $self->logger and $self->logger->can("setRequestObj")
!ll&&!rl&&r
080
MC/DC0%0/2
ref $self->logger and $self->logger->can("setRequestObj")
ref $self->logger $self->logger->can("setRequestObj")
Truth table33%1/3
A and B
A= ref $self->loggerB= $self->logger->can("setRequestObj")
ABresult
0X0
100
111
484 0 $self->logger->setRequestObj($req);
485 }
486 8 ▶ if ( ref( $self->userLogger ) and $self->userLogger->can('setRequestObj') )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->userLogger and $self->userLogger->can("setRequestObj")) 8 0 8
Condition33%1/3
ref $self->userLogger and $self->userLogger->can("setRequestObj")
!ll&&!rl&&r
008
MC/DC0%0/2
ref $self->userLogger and $self->userLogger->can("setRequestObj")
ref $self->userLogger $self->userLogger->can("setRequestObj")
Truth table33%1/3
A and B
A= ref $self->userLoggerB= $self->userLogger->can("setRequestObj")
ABresult
0X0
100
111
487 {
488 8 $self->userLogger->setRequestObj($req);
489 }
490  
491 8 ▶ if ( ref $self->logger ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->logger) 8 0 8
492 8 $self->logger->info( "New request "
493 . ref($self) . " "
494 . $req->method . " "
495 . $req->request_uri );
496 }
497 8 my $res = $sub->($req);
498  
499 # Clear the logging system before the next request
500 8 ▶ if ( ref( $self->logger ) and $self->logger->can('clearRequestObj') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->logger and $self->logger->can("clearRequestObj")) 0 8 8
Condition33%1/3
ref $self->logger and $self->logger->can("clearRequestObj")
!ll&&!rl&&r
080
MC/DC0%0/2
ref $self->logger and $self->logger->can("clearRequestObj")
ref $self->logger $self->logger->can("clearRequestObj")
Truth table33%1/3
A and B
A= ref $self->loggerB= $self->logger->can("clearRequestObj")
ABresult
0X0
100
111
501 0 $self->logger->clearRequestObj($req);
502 }
503 8 ▶ if ( ref( $self->userLogger )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->userLogger and $self->userLogger->can("clearRequestObj")) 8 0 8
Condition33%1/3
ref $self->userLogger and $self->userLogger->can("clearRequestObj")
!ll&&!rl&&r
008
MC/DC0%0/2
ref $self->userLogger and $self->userLogger->can("clearRequestObj")
ref $self->userLogger $self->userLogger->can("clearRequestObj")
Truth table33%1/3
A and B
A= ref $self->userLoggerB= $self->userLogger->can("clearRequestObj")
ABresult
0X0
100
111
504 and $self->userLogger->can('clearRequestObj') )
505 {
506 8 $self->userLogger->clearRequestObj($req);
507 }
508  
509 8 return $res;
510 }
511  
512 0 ▶sub sendRedirection {
Subroutine sendRedirection: not called CC=4 SCAR=30.0
Pod undocumented
513 0 my ( $self, $req, $destination, %args ) = @_;
514  
515 0 ▶ $args{code} ||= 302;
Condition0%0/2
$args{"code"} ||= 302
l!l
00
MC/DC0%0/1
$args{"code"} ||= 302
$args{"code"}
Truth table0%0/2
A
A= $args{"code"}
Aresult
11
00
516 0 ▶ $args{headers} ||= [ $req->spliceHdrs ];
Condition0%0/2
$args{"headers"} ||= [$req->spliceHdrs]
l!l
00
MC/DC0%0/1
$args{"headers"} ||= [$req->spliceHdrs]
$args{"headers"}
Truth table0%0/2
A
A= $args{"headers"}
Aresult
11
00
517  
518 return $self->_buildPsgiResponse(
519 $args{code},
520 [
521 ( $destination ? ( Location => $destination ) : () ),
522 0 ▶ @{ $args{headers} }
Branch: 1 branch
BranchTrueFalseTotal
$destination ? : 0 0 0
523 ],
524 []
525 );
526 }
527  
528 0 ▶sub _buildPsgiResponse {
Subroutine _buildPsgiResponse: not called CC=1 SCAR=6.9
529 0 my ( $self, $code, $header, $data ) = @_;
530 0 return [ $code, $header, $data ];
531 }
532  
533 5 1;
534 __END__
« blib/lib/Lemonldap/NG/Common/Notifications/XML.pm ↑ Summary blib/lib/Lemonldap/NG/Common/MessageBroker/Web.pm »