Controller/Funk.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 95.0% 38 / 40 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Controller/PerChildInit.pm ↑ Summary Controller.pm »
1 package Apache2::Controller::Funk;
2  
3 =head1 NAME
4  
5 Apache2::Controller::Funk
6  
7 =head1 VERSION
8  
9 Version 1.001.001
10  
11 =cut
12  
13 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 our $VERSION = version->new('1.001.001');
15  
16 =head1 SYNOPSIS
17  
18 $bool = controller_allows_method($class, $method);
19  
20 check_allowed_method($class, $method); # throws NOT_FOUND exception
21  
22 =head1 DESCRPTION
23  
24 Useful routines for both Apache2::Controller and Apache2::Controller::Dispatch
25 objects to run. Results and whether to 'require' are cached in this package's
26 namespace across requests, optimizing efficiency per mod_perl2 child, and are
27 queried futher using 'exists', which is very fast.
28  
29 =cut
30  
31 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 ▶use Log::Log4perl qw( :easy );
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 2 ▶use Apache2::Controller::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 0 ▶use Apache2::Const -compile => qw( NOT_FOUND );
Subroutine BEGIN: called CC=1 SCAR=2.6
43  
44 our @EXPORT_OK = qw(
45 controller_allows_method
46 check_allowed_method
47 log_bad_request_reason
48 default_consumer_secret
49 );
50  
51 # this was dumb... this should really be a directive.
52 # or it should let whatever pipe downstream do whatever it needs to.
53 Readonly my $ACCESS_LOG_REASON_LENGTH => 512;
54  
55 =head1 IMPORTABLE FUNCTIONS
56  
57 =head2 controller_allows_method
58  
59 $bool = controller_allows_method($class, $method); # controller_allows_method()
60  
61 Ask if method name is returned by C<< allowed_methods() >>
62 in the given controller package.
63  
64 Only two 'exists' calls are required for each query after caching the
65 first result for this child.
66  
67 =cut
68  
69 my %allowed_methods = ( );
70 sub controller_allows_method {
71 my ($class, $method) = @_;
72  
73 a2cx "class undefined" if !defined $class;
74 a2cx "method undefined" if !defined $method;
75 DEBUG(sub{
76 "checking class '$class', method '$method', allowed is:\n"
77 .Dump(\%allowed_methods)
78 });
79  
80 # check that the method is allowed.
81 # make sure the selected method is allowed in the controller class
82  
83 if (!exists $allowed_methods{$class}) {
84  
85 eval "require $class;";
86 a2cx "cannot require $class: $EVAL_ERROR" if $EVAL_ERROR;
87  
88 my $isa_a2c;
89 eval "\$isa_a2c = $class->isa('Apache2::Controller');";
90 a2cx "$class is not an Apache2::Controller" unless $isa_a2c;
91  
92 a2cx "$class knows no allowed_methods()"
93 unless $class->can('allowed_methods');
94  
95 my @allowed_methods = $class->allowed_methods();
96  
97 DEBUG("allowed_methods: (@allowed_methods)");
98 $allowed_methods{$class} = { map {($_=>1)} @allowed_methods };
99 DEBUG(sub{Dump(\%allowed_methods)});
100 }
101 return exists $allowed_methods{$class}{$method};
102 }
103  
104 =head2 check_allowed_method
105  
106 check_allowed_method($method, $class); # check_allowed_method()
107  
108 Throw a NOT_FOUND exception if the method is not an allowed method
109 in the C<< allowed_methods() >> list in the controller package.
110  
111 =cut
112  
113 sub check_allowed_method {
114 my ($class, $method) = @_;
115 a2cx "class undefined" if !defined $class;
116 a2cx "method undefined" if !defined $method;
117 DEBUG("checking class '$class', method '$method'");
118  
119 if (!controller_allows_method($class, $method)) {
120 DEBUG("Method $method not allowed in $class.");
121 a2cx message => "Method $method not allowed from $class.",
122 status => Apache2::Const::NOT_FOUND;
123 }
124 return;
125 }
126  
127 =head2 log_bad_request_reason( )
128  
129 log_bad_request_reason( $r, $X );
130  
131 Call $r->log_reason( $msg, $r->uri() ) where $msg is a truncated
132 version of $X in case $X is too long.
133  
134 =cut
135  
136 sub log_bad_request_reason {
137 my ($r, $X) = @_;
138 a2cx 'usage: log_bad_request_reason($r, $X)'
139 if !$r || !ref($r) || !$r->can('log_reason') || !$X;
140
141 my $x_text = "$X";
142 my $reason = $ACCESS_LOG_REASON_LENGTH < length $x_text
143 ? substr($x_text, 0, $ACCESS_LOG_REASON_LENGTH)
144 : $x_text;
145 $r->log_reason( $reason, $r->uri() );
146 return;
147 }
148  
149 1;
« Controller/PerChildInit.pm ↑ Summary Controller.pm »