File Coverage

blib/lib/Apache2/Controller/NonResponseBase.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Apache2::Controller::NonResponseBase;
2              
3             =head1 NAME
4              
5             Apache2::Controller::NonResponseBase - internal base class for
6             non-response handlers in Apache2::Controller framework
7              
8             =head1 VERSION
9              
10             Version 1.001.001
11              
12             =cut
13              
14 2     2   15 use version;
  2         4  
  2         15  
15             our $VERSION = version->new('1.001.001');
16              
17             =head1 SYNOPSIS
18              
19             This is an INTERNAL base class and you don't need to use it.
20              
21             package Apache2::Controller;
22             use base Apache2::Controller::NonResponseBase;
23              
24             # no need to define handler() or new()
25            
26             1;
27              
28             =head1 DESCRIPTION
29              
30             This factors out the common parts of handlers in the C
31             framework other than the main response handler. These non-response
32             handlers like Dispatch and Session do not need to create the
33             Apache2::Request object (I think...), so that is put off until
34             the Response phase.
35              
36             You should not use this module for anything that you're doing.
37              
38             Pre-response phase handlers do not handle errors in the same way
39             that Apache2::Controller does. If you get an error in a pre-response
40             phase, A2C cannot call your render class error() method, because
41             that stuff is not set up yet. Instead, it spits the error to
42             the error log, logs the reason for the response code, and
43             returns the response code. This should get Apache to quit
44             processing the chain of handlers... we'll see.
45              
46             =head1 METHODS
47              
48             =cut
49              
50 2     2   182 use strict;
  2         5  
  2         74  
51 2     2   11 use warnings FATAL => 'all';
  2         4  
  2         72  
52 2     2   11 use English '-no_match_vars';
  2         10  
  2         10  
53              
54 2     2   2461 use Log::Log4perl qw(:easy);
  2         58122  
  2         39  
55 2     2   2531 use YAML::Syck;
  2         4008  
  2         144  
56              
57 2     2   113810 use Apache2::RequestRec ();
  0            
  0            
58             use Apache2::RequestUtil ();
59             use Apache2::Log;
60             use Apache2::Const -compile => qw( :common :http :methods );
61              
62             use Apache2::Controller::X;
63             use Apache2::Controller::Const qw( @RANDCHARS $NOT_GOOD_CHARS );
64             use Apache2::Controller::Funk qw( log_bad_request_reason );
65              
66             =head2 handler
67              
68             handler() takes the request, creates an object using the
69             child class name, runs the process() method, and handles errors.
70              
71             =cut
72              
73             sub handler : method {
74             my ($class, $r) = @_;
75              
76             DEBUG("begin $class ->handler()");
77              
78             my ($handler, $status, $X) = ( );
79              
80             eval {
81             $handler = $class->new($r);
82             $status = $handler->process();
83             };
84             if ($X = Exception::Class->caught('Apache2::Controller::X')) {
85             $status = $X->status || Apache2::Const::SERVER_ERROR;
86             WARN("Caught an Apache2::Controller::X: $status");
87             WARN(ref($X).": $X\n".($X->dump ? Dump($X->dump) : '').$X->trace());
88             }
89             elsif ($X = $EVAL_ERROR) {
90             WARN("Caught an unknown error: $X");
91             $status = Apache2::Const::SERVER_ERROR;
92             }
93              
94             if ($status) {
95             DEBUG("Setting http-status to '$status'");
96             $r->status($status);
97             }
98              
99             if ($status && $status >= Apache2::Const::HTTP_BAD_REQUEST) {
100             DEBUG("logging bad request");
101             eval { log_bad_request_reason($r, $X); };
102             if (my $X = Exception::Class->caught('Apache2::Controller::X')) {
103             FATAL("Bad error logging bad request! '$X'\n".$X->trace);
104             }
105             elsif ($EVAL_ERROR) {
106             FATAL("Weird error logging bad request! '$EVAL_ERROR'");
107             }
108             }
109              
110             # Exception objects with non-error status were already WARN'ed.
111            
112             $status = Apache2::Const::OK if !defined $status;
113             DEBUG("returning '$status'");
114             return $status;
115             }
116              
117             =head2 new
118              
119             C creates an object of the child class and assigns the
120             C<< Apache2::RequestRec >> object to
121             C<< $self->{r} >>.
122              
123             If the parent class defines a method C, this will
124             be called at the end of object creation.
125              
126             Unlike L, the handler object of other handlers
127             that use this package as a base do not create, delegate to and subclass
128             the L object. They just keep the original
129             L object in
130             C<< $self->{r} >>.
131              
132             =cut
133              
134             my %can_init;
135              
136             sub new {
137             my ($class, $r) = @_;
138              
139             DEBUG("handler class is '$class', reqrec is '$r'");
140              
141             my $self = {
142             r => $r,
143             class => $class,
144             };
145             bless $self, $class;
146              
147             $can_init{$class} = $self->can('init') if !exists $can_init{$class};
148             $self->init() if $can_init{$class};
149              
150             return $self;
151             }
152              
153             1;
154              
155             =head1 SEE ALSO
156              
157             L
158              
159             L
160              
161             L
162              
163             L
164              
165             =head1 AUTHOR
166              
167             Mark Hedges, C<< >>
168              
169             =head1 COPYRIGHT & LICENSE
170              
171             Copyright 2008-2010 Mark Hedges, all rights reserved.
172              
173             This program is free software; you can redistribute it and/or modify it
174             under the same terms as Perl itself.
175              
176             This software is provided as-is, with no warranty
177             and no guarantee of fitness
178             for any particular purpose.
179