Hector.pm

filter: Statementstmt 75.5% 114 / 151 Branchbran 47.5% 19 / 40 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 84.3% 27 / 32 Podpod 91.6% 11 / 12 totaltotal 68.9% 175 / 254 CC 25 SCAR 13.5
CC
25
Cov
67%
run
20.0
_getScriptName
6.0
_checkReferer
5.1
CRAP 3.8
« Hector/Session.pm ↑ Summary Hector/Log.pm »
1 #main framework object
2 package PSGI::Hector;
3  
4 =pod
5  
6 =head1 NAME
7  
8 PSGI::Hector - Very simple PSGI web framework
9  
10 =head1 SYNOPSIS
11  
12 my $app = App->init({
13 'responsePlugin' => 'Some::Class',
14 });
15 ###########################
16 ###########################
17 package App;
18 use PSGI::Hector;
19 use parent qw(PSGI::Hector);
20 ###########################
21 sub handleDefault{
22 #add code here for landing page
23 }
24  
25 =head1 DESCRIPTION
26  
27 All action subs are passed a L<PSGI::Hector> object as the only parameter, from this you should be able to reach
28 everything you need.
29  
30 =head1 METHODS
31  
32 =cut
33  
34 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 ▶use Class::Load qw(is_class_loaded load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶use parent qw(PSGI::Hector::Base PSGI::Hector::Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 ▶use PSGI::Hector::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 5 ▶use PSGI::Hector::Session; #for session management
Subroutine BEGIN: called CC=1 SCAR=0.0
41 5 ▶use PSGI::Hector::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use PSGI::Hector::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 our $VERSION = "2.0";
44 #########################################################
45  
46 =head2 init(\%options)
47  
48 my $options = {
49 'responsePlugin' => 'Some::Class',
50 'checkReferer' => 0,
51 'sessionClass' => 'Some::Class',
52 'requestClass' => 'Some::Class',
53 'sefUrls' => 0,
54 'debug' => 1
55 };
56 my $h = PSGI::Hector->init($options);
57
58 Factory class method, used in creating the application from the F<app.psgi> file. The return value
59 from this method can be used where ever $app would be used.
60  
61 This hash reference passed to the method contains any general options for the framework.
62  
63 =cut
64  
65 #########################################################
66 1 ▶sub init{
Subroutine init: called CC=1 SCAR=2.0
Pod documented
67 1 my($class, $options) = @_;
68 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 my $env = shift;
70 0 my $h = $class->new($options, $env);
71 0 $h->run(); #do this thing!
72 1 };
73 }
74 #########################################################
75  
76 =head2 new(\%options, \%env)
77  
78 my $options = {};
79 my $h = PSGI::Hector->new($options, $env);
80  
81 Constructor, requires a hash references of options to be passed and the PSGI environment.
82  
83 Usually this method is invoked from the init() class method.
84  
85 =cut
86  
87 #########################################################
88 8 ▶sub new{
Subroutine new: called CC=4 SCAR=14.0
Pod documented
89 8 my($class, $options, $env) = @_;
90 8 ▶ die("No response plugin option provided") unless $options->{'responsePlugin'}; #this option is mandatory
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"responsePlugin"} 0 8 8
91  
92 8 my $self = $class->SUPER::new();
93 8 $self->{'_env'} = $env;
94 8 $self->{'_options'} = $options;
95 8 my $requestClass = $self->__getFullClassName("Request");
96 8 ▶ if($self->getOption('requestClass')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getOption("requestClass")) 0 8 8
97 0 $requestClass = $self->getOption('requestClass');
98 }
99 8 ▶ if(!defined($self->getOption('debug'))){ #turn off debugging by default
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->getOption("debug")) 2 6 8
100 2 $self->_setOption("debug", 0);
101 }
102 8 $self->{'_request'} = $requestClass->new($env);
103 8 $self->{'_response'} = PSGI::Hector::Response->new($self, $self->getOption('responsePlugin')); #this could need access to a request object
104 8 $self->{'_log'} = PSGI::Hector::Log->new({
105 'debug' => $self->getOption('debug')
106 });
107 8 $self->_init(); #perform initial setup
108 8 $self;
109 }
110 #########################################################
111  
112 =pod
113  
114 =head2 getResponse()
115  
116 my $response = $h->getResponse();
117  
118 Returns the current instance of the response plugin object, previously defined in the constructor options.
119 See L<PSGI::Hector::Response> for more details.
120  
121 =cut
122  
123 ###########################################################
124 8 ▶sub getResponse{
Subroutine getResponse: called CC=1 SCAR=0.0
Pod documented
125 8 shift->{'_response'};
126 }
127 #########################################################
128  
129 =pod
130  
131 =head2 getSession()
132  
133 my $session = $h->getSession();
134  
135 Returns the current instance of the L<PSGI::Hector::Session> object.
136  
137 =cut
138  
139 ###########################################################
140 1 ▶sub getSession{
Subroutine getSession: called CC=3 SCAR=11.6
Pod documented
141 1 my $self = shift;
142 1 ▶ if(!$self->{'_session'}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_session"}) 1 0 1
143 1 my $sessionClass = $self->__getFullClassName("Session");
144 1 ▶ if($self->getOption('sessionClass')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getOption("sessionClass")) 0 1 1
145 0 $sessionClass = $self->getOption('sessionClass');
146 }
147 1 $self->{'_session'} = $sessionClass->new($self);
148 }
149 1 $self->{'_session'};
150 }
151 #########################################################
152  
153 =pod
154  
155 =head2 getRequest()
156  
157 my $request = $h->getRequest();
158  
159 Returns the current instance of the L<PSGI::Hector::Request> object.
160  
161 =cut
162  
163 ###########################################################
164 7 ▶sub getRequest{
Subroutine getRequest: called CC=2 SCAR=7.2
Pod documented
165 7 my $request = shift->{'_request'};
166 7 ▶ $request or die("No request object found");
Branch: 1 branch
BranchTrueFalseTotal
$request or die "No request object found" 7 0 7
167 }
168 #########################################################
169  
170 =pod
171  
172 =head2 getLog()
173  
174 my $logger = $h->getLog();
175  
176 returns an current instance of the L<PSGI::Hector::Log> object.
177  
178 =cut
179  
180 #########################################################
181 5 ▶sub getLog{
Subroutine getLog: called CC=1 SCAR=0.0
Pod documented
182 5 shift->{'_log'};
183 }
184 #########################################################
185  
186 =pod
187  
188 =head2 getAction()
189  
190 my $action = $h->getAction();
191  
192 Returns the curent action that the web application is performing. This is the current value of the "action"
193 request form field or query string item.
194  
195 If search engine friendly URLs are turned on the action will be determined from the last part of the script URL.
196  
197 =cut
198  
199 ###########################################################
200 1 ▶sub getAction{
Subroutine getAction: called CC=3 SCAR=11.7
Pod documented
201 1 my $self = shift;
202 1 my $action = "default";
203 1 my $request = $self->getRequest();
204 1 my $params = $request->getParameters();
205 1 ▶ if(defined($params->{'action'})){ #get action from query string or post string
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{'action'}) { } 0 1 1
206 0 $action = $params->{'action'};
207 }
208 else{ #do we have search engine friendly urls
209 1 my $sefAction = $self->_getSefAction();
210 1 ▶ if($sefAction){
Branch: 1 branch
BranchTrueFalseTotal
if ($sefAction) 0 1 1
211 0 $action = $sefAction;
212 }
213 }
214 1 $action;
215 }
216 #########################################################
217  
218 =pod
219  
220 =head2 getUrlForAction($action, $queryString)
221  
222 my $url = $h->getUrlForAction("someAction", "a=b&c=d");
223  
224 Returns the URL for the application with the given action and query string.
225  
226 =cut
227  
228 #########################################################
229 2 ▶sub getUrlForAction{
Subroutine getUrlForAction: called CC=2 SCAR=7.0
Pod documented
230 2 my($self, $action, $query) = @_;
231 2 my $url = "/" . $action;
232 2 ▶ if($query){ #add query string
Branch: 1 branch
BranchTrueFalseTotal
if ($query) 2 0 2
233 2 $url .= "?" . $query;
234 }
235 2 $url;
236 }
237 #########################################################
238  
239 =pod
240  
241 =head2 getFullUrlForAction($action, $queryString)
242  
243 my $url = $h->getFullUrlForAction("someAction", "a=b&c=d");
244  
245 Returns the Full URL for the application with the given action and query string and hostname.
246  
247 =cut
248  
249 #########################################################
250 1 ▶sub getFullUrlForAction{
Subroutine getFullUrlForAction: called CC=1 SCAR=0.0
Pod documented
251 1 my($self, $action, $query) = @_;
252 1 $self->getSiteUrl() . $self->getUrlForAction($action, $query);
253 }
254 #########################################################
255  
256 =pod
257  
258 =head2 run()
259  
260 $h->run();
261  
262 This methood is required for the web application to deal with the current request.
263 It should be called after any setup is done.
264  
265 If the response object decides that the response has not been modified then this
266 method will not run any action functions.
267  
268 The action sub run will be determined by first checking the actions hash if previously
269 given to the object then by checking if a method prefixed with "handle" exists in the
270 current class.
271  
272 =cut
273  
274 ###########################################################
275 0 ▶sub run{ #run the code for the given action
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
276 0 my $self = shift;
277 0 my $response = $self->getResponse();
278 0 ▶ if($response->code() != 304){ #need to do something
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 304) 0 0 0
279 0 $self->getLog()->log("Need to run action sub", 'debug');
280 0 my $action = $self->getAction();
281 0 $self->getLog()->log("Using action: '$action'", 'debug');
282 0 my $subName = "handle" . ucfirst($action); #add prefix for security
283 0 my $class = ref($self);
284 0 ▶ if($class->can($subName)){ #default action sub exists
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can($subName)) { } 0 0 0
285 0 eval{
286 0 $self->$subName();
287 };
288 0 ▶ if($@){ #problem with sub
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
289 0 $response->setError($@);
290 }
291 }
292 else{ #no code to execute
293 0 $response->code(404);
294 0 $response->message('Not Found');
295 0 $response->setError("No action found for: $action");
296 }
297 }
298 0 $response->display(); #display the output to the browser
299 }
300 ##########################################################
301  
302 =pod
303  
304 =head2 getOption("key")
305  
306 my $value = $h->getOption("debug");
307  
308 Returns the value of the configuration option given.
309  
310 =cut
311  
312 ##########################################################
313 43 ▶sub getOption{
Subroutine getOption: called CC=2 SCAR=6.9
Pod documented
314 43 my($self, $key) = @_;
315 43 my $value = undef;
316 43 ▶ if(defined($self->{'_options'}->{$key})){ #this config option has been set
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_options"}{$key}) 24 19 43
317 24 $value = $self->{'_options'}->{$key};
318 }
319 43 $value;
320 }
321 ##########################################################
322 18 ▶sub getEnv{
Subroutine getEnv: called CC=1 SCAR=0.0
Pod undocumented
323 18 shift->{'_env'};
324 }
325 ###########################################################
326 # Private methods
327 #########################################################
328 13 ▶sub __getFullClassName{
Subroutine __getFullClassName: called CC=2 SCAR=7.1
329 13 my($self, $name) = @_;
330 13 my $class = ref($self);
331 13 ▶ $class->__checkClass($class, $name) or die('Could not find full class name');
Branch: 1 branch
BranchTrueFalseTotal
$class->__checkClass($class, $name) or die "Could not find full class name" 13 0 13
332 }
333 #########################################################
334 29 ▶sub __checkClass{
Subroutine __checkClass: called CC=3 SCAR=11.0
335 29 my($class, $current, $name) = @_;
336 29 my $try = $current . "::" . $name;
337 29 ▶ return $try if is_class_loaded($try);
Branch: 1 branch
BranchTrueFalseTotal
if is_class_loaded($try) 13 16 29
338 16 eval{
339 16 load_class($try);
340 };
341 16 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 16 0 16
342 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
343 16 my @parents = @{$current . "::ISA"}; #get base classes
344 16 my @results = map { $class->__checkClass($_, $name) } @parents;
345 16 my @found = grep { defined($_) } @results;
346 16 shift(@found)
347 }
348 }
349 #########################################################
350 0 ▶sub __getActionDigest{
Subroutine __getActionDigest: not called CC=1 SCAR=6.9
351 0 my $self = shift;
352 0 my $sha1 = Digest::SHA1->new();
353 0 $sha1->add($self->getAction());
354 0 $sha1->hexdigest();
355 }
356 ###########################################################
357 1 ▶sub _getSefAction{
Subroutine _getSefAction: called CC=3 SCAR=12.3
358 1 my $self = shift;
359 1 my $action = undef;
360 1 my $env = $self->getEnv();
361 1 ▶ if(defined($env->{'PATH_INFO'}) && $env->{'PATH_INFO'} =~ m/\/(.+)$/){ #get the action from the last part of the url
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"PATH_INFO"} and $env->{"PATH_INFO"} =~ m[/(.+)$]) 0 1 1
Condition33%1/3
defined $env->{"PATH_INFO"} and $env->{"PATH_INFO"} =~ m[/(.+)$]
!ll&&!rl&&r
100
MC/DC0%0/2
defined $env->{"PATH_INFO"} and $env->{"PATH_INFO"} =~ m[/(.+)$]
defined $env->{"PATH_INFO"} $env->{"PATH_INFO"} =~ m[/(.+)$]
Truth table33%1/3
A and B
A= defined $env->{"PATH_INFO"}B= $env->{"PATH_INFO"} =~ m[/(.+)$]
ABresult
0X0
100
111
362 0 $action = $1;
363 }
364 1 $action;
365 }
366 ###########################################################
367 8 ▶sub _init{ #things to do when this object is created
Subroutine _init: called CC=3 SCAR=12.0
368 8 my $self = shift;
369 8 ▶ if(!defined($self->getOption('checkReferer')) || $self->getOption('checkReferer')){ #check the referer by default
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->getOption("checkReferer") or $self->getOption("checkReferer")) 8 0 8
Condition33%1/3
not defined $self->getOption("checkReferer") or $self->getOption("checkReferer")
l!l&&r!l&&!r
800
MC/DC0%0/2
not defined $self->getOption("checkReferer") or $self->getOption("checkReferer")
not defined $self->getOption("checkReferer") $self->getOption("checkReferer")
Truth table33%1/3
not(A) or B
A= not defined $self->getOption("checkReferer")B= $self->getOption("checkReferer")
ABresult
1X1
011
000
370 8 $self->_checkReferer(); #check this first
371 }
372 8 1;
373 }
374 ###########################################################
375 8 ▶sub _checkReferer{ #simple referer check for very basic security
Subroutine _checkReferer: called CC=4 SCAR=16.3
376 8 my $self = shift;
377 8 my $result = 0;
378 8 my $env = $self->getEnv();
379 8 my $host = $env->{'HTTP_HOST'};
380 8 ▶ if($host && $env->{'HTTP_REFERER'} && $env->{'HTTP_REFERER'} =~ m/^(http|https):\/\/$host/){ #simple check here
Branch: 1 branch
BranchTrueFalseTotal
if ($host and $env->{'HTTP_REFERER'} and $env->{'HTTP_REFERER'} =~ m[^(http|https)://$host]) { } 8 0 8
Condition33%2/6
$host and $env->{"HTTP_REFERER"} and $env->{"HTTP_REFERER"} =~ m[^(http|https)://$host]
!ll&&!rl&&r
008
$host and $env->{"HTTP_REFERER"}
!ll&&!rl&&r
008
MC/DC0%0/3
$host and $env->{"HTTP_REFERER"} and $env->{"HTTP_REFERER"} =~ m[^(http|https)://$host]
$host $env->{"HTTP_REFERER"} $env->{"HTTP_REFERER"} =~ m[^(http|https)://$host]
Truth table25%1/4
A and B and C
A= $hostB= $env->{"HTTP_REFERER"}C= $env->{"HTTP_REFERER"} =~ m[^(http|https)://$host]
ABCresult
0XX0
10X0
1100
1111
381 8 $result = 1;
382 }
383 else{
384 0 my $response = $self->getResponse();
385 0 $response->setError("Details where not sent from the correct web page");
386 }
387 8 $result;
388 }
389 ##########################################################
390 0 ▶sub _getActions{
Subroutine _getActions: not called CC=1 SCAR=6.9
391 0 shift->{'_actions'};
392 }
393 ###########################################################
394 2 ▶sub _setOption{
Subroutine _setOption: called CC=1 SCAR=0.0
395 2 my($self, $key, $value) = @_;
396 2 $self->{'_options'}->{$key} = $value;
397 2 1;
398 }
399 ##########################################################
400 0 ▶sub _getScriptName{ #returns the basename of the running script
Subroutine _getScriptName: not called CC=2 SCAR=17.9
401 0 my $self = shift;
402 0 my $env = $self->getEnv();
403 0 my $scriptName = $env->{'REQUEST_URI'};
404 0 ▶ die("Cant find scriptname, are you running a CGI") unless($scriptName);
Branch: 1 branch
BranchTrueFalseTotal
unless $scriptName 0 0 0
405 0 return basename($scriptName);
406 }
407 ###########################################################
408  
409 =pod
410  
411 =head1 CONFIGURATION SUMMARY
412  
413 The following list gives a summary of each Hector
414 configuration options.
415  
416 =head3 responsePlugin
417  
418 A scalar string consisting of the response class to use.
419  
420 See L<PSGI::Hector::Response::Base> for details on how to create your own response class, or
421 a list of response classes provided in this package.
422  
423 =head3 checkReferer
424  
425 Flag to indicate if referer checking should be performed. When enabled an
426 error will raised when the referer is not present or does not contain the server's
427 hostname.
428  
429 This option is enabled by default.
430  
431 =head3 sessionClass
432  
433 A scalar string consisting of the session class to use. Useful if you want to change the way
434 session are stored.
435  
436 Defaults to ref($self)::Session
437  
438 =head3 requestClass
439  
440 A scalar string consisting of the request class to use. Useful if you want to change the way
441 requests are handled.
442  
443 Defaults to ref($self)::Request
444  
445 =head3 sefUrls
446  
447 A boolean value indicating if search engine friendly URLs are to be used.
448  
449 =head3 debug
450  
451 A boolean value indicating if debug mode is enabled. This can then be used in output views or code to print extra debug.
452  
453 =head1 Notes
454  
455 To change the session prefix characters use the following code at the top of your script:
456  
457 $PSGI::Hector::Session::prefix = "ABC";
458
459 To change the session file save path use the following code at the top of your script:
460  
461 $PSGI::Hector::Session::path = "/var/tmp";
462
463 =head2 Reverse proxies
464  
465 To run your application behind a reverse proxy using apache, please use the following setup:
466  
467 <Location /psgi>
468 RequestHeader set X-Forwarded-Script-Name /psgi
469 RequestHeader set X-Traversal-Path /
470 ProxyPass http://localhost:8080/
471 ProxyPassReverse http://localhost:8080/
472 </Location>
473  
474 =head1 Author
475  
476 MacGyveR <dumb@cpan.org>
477  
478 Development questions, bug reports, and patches are welcome to the above address
479  
480 =head1 See Also
481  
482 =head1 Copyright
483  
484 Copyright (c) 2017 MacGyveR. All rights reserved.
485  
486 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
487  
488 =cut
489  
490 ###########################################################
491 5 return 1;
« Hector/Session.pm ↑ Summary Hector/Log.pm »