FakeApache.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.4% 42 / 44 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« FakeApache/Request.pm ↑ Summary
1 package Plack::App::FakeApache;
2 $Plack::App::FakeApache::VERSION = '0.1';
3 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use attributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Plack::Util::Accessor qw( authen_handler authz_handler response_handler handler dir_config root logger request_args request_class without_cgi);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use parent qw( Plack::Component );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶use Apache2::Const qw(OK DECLINED HTTP_OK HTTP_UNAUTHORIZED HTTP_NOT_FOUND);
Subroutine BEGIN: called CC=1 SCAR=2.6
17  
18 sub _get_phase_handlers
19 {
20 my $self = shift;
21 my $phase = shift;
22 my $accessor = $phase.'_handler';
23 my $handlers = $self->$accessor or return;
24 return @{$handlers};
25 }
26  
27 # RUN_FIRST
28 # Run until a handler returns something other than DECLINED...
29 sub _run_first
30 {
31 my $self = shift;
32 my $phase = shift;
33 my $fake_req = shift;
34 my $fallback_status = shift;
35  
36  
37 # Mangle env to cope with certain kinds of CGI brain damage.
38 if ( $self->with_cgi) {
39 require CGI::Emulate::PSGI;
40 my $env = $fake_req->plack_request->env;
41 local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($env));
42 # and stdin!
43 local *STDIN = $env->{'psgi.input'};
44 }
45  
46 my $status = OK;
47 foreach my $handler ($self->_get_phase_handlers($phase)) {
48 try {
49 $status = $handler->($fake_req);
50 }
51 catch {
52 die $_ unless $_ =~ /^EXIT 0$/;
53 };
54 last if $status != DECLINED;
55 }
56 return (defined($status) and $status != DECLINED) ? $status : $fallback_status; # mod_perl seems to do this if all handlers decline
57 }
58  
59 sub call {
60 my ($self, $env) = @_;
61  
62 my %args = (
63 env => $env,
64 dir_config => $self->dir_config || {},
65 request_class => $self->request_class || 'Plack::App::FakeApache::Request',
66 %{$self->request_args || {}}
67 );
68  
69 $args{root} = $self->root if defined $self->root;
70  
71 if ( $self->logger ) {
72 my $logger = $self->logger;
73 $args{log} = $logger if blessed($logger) and !$logger->isa('IO::Handle');
74 $args{log} ||= Plack::FakeApache::Log->new( logger => sub { print $logger @_ } );
75 }
76 my $request_class = $args{request_class};
77 my $fake_req = $request_class->new(%args);
78  
79 my $status = $self->_run_handlers($fake_req);
80  
81 $fake_req->status($status == OK ? HTTP_OK : $status);
82 return $fake_req->finalize;
83 }
84  
85 sub _run_handlers {
86 my $self = shift;
87 my $fake_req = shift;
88 my $status;
89  
90 # TODO: More request phases here...
91  
92 $status = $self->_run_first('authen', $fake_req, HTTP_UNAUTHORIZED);
93 return $status if $status != OK;
94  
95 $status = $self->_run_first('authz', $fake_req, HTTP_UNAUTHORIZED);
96 return $status if $status != OK;
97  
98 # we wrap the call to $handler->( ... ) in tie statements so
99 # prints, etc are caught and sent to the right place
100 tie *STDOUT, "Plack::App::FakeApache::Tie", $fake_req;
101 $status = $self->_run_first('response', $fake_req, HTTP_NOT_FOUND);
102 untie *STDOUT;
103 return $status if $status != OK;
104  
105 # TODO: More request phases here...
106  
107 return OK;
108 }
109  
110 sub prepare_app {
111 my $self = shift;
112 my $request_class = $self->request_class || 'Plack::App::FakeApache::Request';
113 load $request_class;
114  
115 $self->response_handler($self->response_handler || $self->handler);
116  
117 foreach my $accessor ( qw(authen_handler authz_handler response_handler) ) {
118 my $handlers = $self->$accessor or next;
119 my @handlers = ref($handlers) eq 'ARRAY' ? @{$handlers} : ($handlers);
120 @handlers = map({ $self->_massage_handler($_) } @handlers);
121 $self->$accessor([ @handlers ]);
122 }
123  
124 carp "handler or response_handler not defined" unless $self->response_handler;
125  
126 # Workaround for mod_perl handlers doing CGI->new($r). CGI doesn't
127 # know our fake request class, so we hijack CGI->new() and explicitly
128 # pass the request query string instead...
129 if ($self->with_cgi) {
130 my $new = CGI->can('new');
131 no warnings qw(redefine);
132 *CGI::new = sub {
133 my $request_class = $self->request_class || 'Plack::App::FakeApache::Request';
134 if (blessed($_[1]) and $_[1]->isa($request_class)) {
135 return $new->(CGI => $_[1]->env->{QUERY_STRING} || $_[1]->plack_request->content);
136 }
137 return $new->(@_);
138 };
139 }
140 else { # we probably want CGI.pm to complain it's being called if we get here
141 my $new = CGI->can('new');
142 no warnings qw/redefine/;
143 *CGI::new = sub {
144 warn "CALLING CGI->new\n";
145 return $new(@_);
146 };
147 }
148 return;
149 }
150  
151 sub _massage_handler
152 {
153 my $self = shift;
154 my $handler = shift;
155 my ($class, $method);
156 if ( blessed $handler ) {
157 $handler = sub { $handler->handler( @_ ) };
158 } elsif ( my ($class, $method) = $handler =~ m/(.+)->(.+)/ ) {
159 Plack::Util::load_class( $class );
160 $handler = sub { $class->$method( @_ ) };
161 } else {
162 my $class = $handler;
163 Plack::Util::load_class( $class );
164 my $method = eval { $class->can("handler") };
165 if ( grep { $_ eq 'method' } attributes::get($method) ) {
166 $handler = sub { $class->handler( @_ ) };
167 } else {
168 $handler = $method;
169 }
170 }
171 return $handler;
172 }
173  
174 sub with_cgi {
175 return ! $_[0]->without_cgi
176 }
177  
178 package Plack::App::FakeApache::Tie;
179 $Plack::App::FakeApache::Tie::VERSION = '0.1';
180 sub TIEHANDLE {
181 my $class = shift;
182 my $r = shift;
183 return bless \$r, $class;
184 }
185  
186 sub PRINT { my $r = ${ shift() }; $r->print(@_) }
187 sub WRITE { my $r = ${ shift() }; $r->write(@_) }
188  
189 1;
190  
191 __END__
« FakeApache/Request.pm ↑ Summary