FakeApache/Request.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« FakeApache/Log.pm ↑ Summary FakeApache.pm »
1 package Plack::App::FakeApache::Request;
2 $Plack::App::FakeApache::Request::VERSION = '0.1';
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use MooX::HandlesVia;
6 use Types::Standard qw/HashRef Any InstanceOf Bool Str Maybe/;
7 use APR::Pool;
8 use APR::Table;
9  
10 use HTTP::Status qw(:is);
11  
12 use Plack::Request;
13 use Plack::Response;
14 use Plack::App::File;
15  
16 use Plack::App::FakeApache::Connection;
17 use Plack::App::FakeApache::Log;
18 use Plack::App::FakeApache::Server;
19 use Cwd qw(cwd);
20 use URI;
21  
22 my $NS = "plack.app.fakeapache";
23  
24 # Plack related attributes:
25 has env => (
26 is => 'ro',
27 isa => HashRef,
28 required => 1,
29 );
30  
31 has plack_request => (
32 is => 'lazy',
33 isa => InstanceOf['Plack::Request'],
34 handles => {
35 method => 'method',
36 unparsed_uri => 'request_uri',
37 user => 'user',
38 },
39 );
40  
41 has plack_response => (
42 is => 'lazy',
43 isa => InstanceOf['Plack::Response'],
44 handles => {
45 set_content_length => 'content_length',
46 content_type => 'content_type',
47 content_encoding => 'content_encoding',
48 status => 'status',
49 },
50 );
51  
52 has log => (
53 is => 'rw',
54 default => sub { Plack::App::FakeApache::Log->new() },
55 handles => [ qw(log_error log_reason warn) ],
56 );
57  
58 has server => (
59 is => 'rw',
60 default => sub { Plack::App::FakeApache::Server->new() },
61 );
62  
63 # Apache related attributes
64 has _apr_pool => (
65 is => 'lazy',
66 isa => InstanceOf['APR::Pool'],
67 );
68  
69 has headers_in => (
70 is => 'lazy',
71 isa => InstanceOf['APR::Table'],
72 );
73  
74 has headers_out => (
75 is => 'lazy',
76 isa => InstanceOf['APR::Table'],
77 );
78  
79 has err_headers_out => (
80 is => 'lazy',
81 isa => InstanceOf['APR::Table'],
82 );
83  
84 has _subprocess_env => (
85 is => 'lazy',
86 isa => InstanceOf['APR::Table'],
87 );
88  
89  
90 has dir_config => (
91 is => 'bare',
92 isa => HashRef,
93 default => sub { {} },
94 handles_via => 'Hash',
95 handles => { dir_config => 'accessor' },
96 );
97  
98  
99 has location => (
100 is => 'rw',
101 isa => Str,
102 default => '/',
103 );
104  
105 has filename => (
106 is => 'rw',
107 isa => Maybe[Str],
108 );
109  
110 has root => (
111 is => 'rw',
112 isa => Str,
113 default => cwd(),
114 );
115  
116 has is_initial_req => (
117 is => 'lazy',
118 isa => Bool,
119 default => 1,
120 );
121  
122 has auth_type => (
123 is => 'ro',
124 isa => Str,
125 );
126  
127 has auth_name => (
128 is => 'ro',
129 isa => Str,
130 );
131  
132 # builders
133 sub _build_plack_request { return Plack::Request->new( shift->env ) }
134 sub _build_plack_response { return Plack::Response->new( 200, {}, [] ) }
135 sub _build__apr_pool { return APR::Pool->new() }
136 sub _build_headers_out { return APR::Table::make( shift->_apr_pool, 64 ) }
137 sub _build_err_headers_out{ return APR::Table::make( shift->_apr_pool, 64 ) }
138  
139 sub _build__subprocess_env {
140 my $self = shift;
141 my $env = $self->env;
142 my $table = APR::Table::make( $self->_apr_pool, 64 );
143  
144 $table->add( $_ => $env->{$_} ) for grep { /^[_A-Z]+$/ } keys %$env;
145  
146 return $table;
147 }
148  
149 sub _build_headers_in {
150 my $self = shift;
151 my $table = APR::Table::make( $self->_apr_pool, 64 );
152  
153 $self->plack_request->headers->scan( sub {
154 $table->add( @_ );
155 } );
156  
157 return $table;
158 }
159  
160 sub _build_filename {
161 my $self = shift;
162  
163 my $paf = Plack::App::File->new(
164 root => $self->root
165 );
166 my ($file, $path) = $paf->locate_file( $self->env );
167  
168 return undef if ref $file; # some sort of error
169 return $file;
170 }
171  
172 # Plack methods
173 sub finalize {
174 my $self = shift;
175 my $response = $self->plack_response;
176  
177 $self->headers_out->do( sub { $response->header( @_ ); 1 } ) if is_success( $self->status() );
178 $self->err_headers_out->do( sub { $response->header( @_ ); 1 } );
179  
180 return $response->finalize;
181 };
182  
183 # Apache methods
184  
185 sub args {
186 my $self = shift;
187 return $self->plack_request->env->{QUERY_STRING};
188 }
189  
190 sub hostname {
191 my $self = shift;
192  
193 return $self->env->{SERVER_NAME};
194 }
195  
196 sub subprocess_env {
197 my $self = shift;
198  
199 if (@_ == 1) {
200 return $self->_subprocess_env->get( @_ );
201 }
202  
203 if (@_ == 2) {
204 return $self->_subprocess_env->set( @_ );
205 }
206  
207 if (defined wantarray) {
208 return $self->_subprocess_env;
209 }
210  
211 $self->_subprocess_env->do( sub { $ENV{ $_[0] } = $_[1]; 1 } );
212 return;
213 }
214  
215 sub document_root {
216 my $self = shift;
217 return $self->root;
218 }
219  
220 sub pnotes {
221 my $self = shift;
222 my $key = shift;
223 my $old = $self->env->{$NS.'.pnotes'}->{$key};
224  
225 if (@_) {
226 $self->env->{$NS.'.pnotes'}->{$key} = shift;
227 }
228  
229 return $old;
230 }
231  
232 sub notes {
233 my $self = shift;
234 my $key = shift;
235 my $old = $self->env->{$NS.'.notes'}->{$key};
236  
237 if (@_) {
238 $self->env->{$NS.'.notes'}->{$key} = "".shift;
239 }
240  
241 return $old;
242 }
243  
244 # this is strictly mocking Apache::Connection, and only partially
245 sub connection {
246 my $self = shift;
247  
248 return Plack::App::FakeApache::Connection->new(
249 remote_ip => $self->plack_request->address,
250 log => $self->log,
251 );
252 }
253  
254 sub read {
255 my $self = shift;
256 my ($buffer, $length, $offset) = @_; # ... but use $_[0] for buffer
257  
258 my $request = $self->plack_request;
259  
260 # Is this needed? Intrudes on a Plack::Request private method...
261 unless ($request->env->{'psgix.input.buffered'}) {
262 $request->_parse_request_body;
263  
264 # Sets psgix.input.buffered and rewinds.
265 }
266  
267 my $fh = $request->input
268 or return 0;
269  
270 return $fh->read($_[0], $length, $offset);
271 }
272  
273 sub print {
274 my $self = shift;
275  
276 my $length = 0;
277 for (@_) {
278 $self->_add_content($_);
279 $length += length;
280 }
281  
282 return $length;
283 }
284  
285 sub write {
286 my ($self, $buffer, $length, $offset) = @_;
287  
288 if (defined $length && $length == -1) {
289 $self->_add_content($buffer);
290 return length $buffer;
291 }
292  
293 my $output = substr $buffer, $offset // 0, $length // length $buffer;
294  
295 $self->_add_content($output);
296 return length $output;
297 }
298  
299 sub _add_content {
300 my $self = shift;
301  
302 push @{ $self->plack_response->body }, @_;
303 }
304  
305 sub rflush {
306 1;
307 }
308  
309 sub uri {
310 my $self = shift;
311 return $self->plack_request->uri->path;
312 }
313  
314 sub construct_url {
315 my $self = shift;
316 my $path = shift;
317 my $uri = URI->new($self->plack_request->uri);
318 $uri->path($path) if $path;
319 return $uri->as_string;
320 }
321  
322 1;
« FakeApache/Log.pm ↑ Summary FakeApache.pm »