blib/lib/Drogo/Server/PSGI.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 213 CC 13 SCAR 16.8
CC
13
Cov
0%
initialize
12.0
process_request_method
12.0
send_http_header
12.0
CRAP 5.4
1 package Drogo::Server::PSGI;
2 use strict;
3 use URI::Escape;
4 use PSGI;
5 use IO::File;
6  
7 use base 'Drogo::Server';
8  
9 my %SERVER_VARIABLES;
10  
11 =head1 NAME
12  
13 Drogo::Server::PSGI - Implement a Drogo PSGI application.
14  
15 =head1 METHODS
16  
17 =head3 new
18  
19 Create a new server instance.
20  
21 Parameters:
22 respond => response object,
23  
24 Example app.psgi file.
25  
26 my $app = sub {
27 my $env = shift;
28  
29 return sub {
30 my $respond = shift;
31  
32 # create new server object
33 my $server = Drogo::Server::PSGI->new( env => $env, respond => $respond );
34  
35 # set default application package
36 $server->variable( app_package => 'dev' );
37  
38 # do something with server...
39 Example::App->handler( server => $server );
40 }
41 };
42  
43  
44 =cut
45  
46 sub new
47 {
48 my ($class, %params) = @_;
49  
50 my $self = { %params, output => '' };
51  
52 %SERVER_VARIABLES = ( );
53  
54 my ($path, $args) = split(/\?/, $self->{env}{REQUEST_URI});
55 $self->{uri} = $path;
56 $self->{args} = $args || '';
57 $self->{request_method} = $self->{env}{REQUEST_METHOD};
58 $self->{remote_addr} = $self->{env}{REMOTE_ADDR};
59  
60 # set proper headers_in
61 for my $env_key (keys %{$self->{env}})
62 {
63 if ($env_key =~ /^HTTP_(.*)$/)
64 {
65 my $header = lc($1);
66 $self->{headers_in}{$header} = $self->{env}{$env_key};
67  
68 # map dashes too
69 $header =~ s/_/-/g;
70 $self->{headers_in}{$header} = $self->{env}{$env_key};
71 }
72 }
73  
74 $self->{respond} = $params{respond};
75  
76 # content type needs manually set
77 $self->{headers_in}{'content-type'} =
78 $self->{env}{CONTENT_TYPE};
79  
80 bless($self);
81  
82 return $self;
83 }
84  
85 =head3 initialize
86  
87 Initializes Drogo instance.
88  
89 =cut
90  
91 sub initialize
92 {
93 my $self = shift;
94 my $ip_header = $self->variable('proxy_ip_header');
95  
96 if ($ip_header)
97 {
98 $self->{remote_addr} = $self->header_in($ip_header);
99 }
100 elsif (my $remote_addr = $self->variable('remote_addr'))
101 {
102 $self->{remote_addr} = $remote_addr;
103 }
104 else
105 {
106 $self->{remote_addr} = $self->{env}{REMOTE_ADDR};
107 }
108 }
109  
110 sub tmpfilename { join('-', 'drogopsgip', $$, time) }
111  
112 =head3 input
113  
114 Returns input stream.
115  
116 =cut
117  
118 sub input { shift->{input_fh} }
119  
120 =head3 process_request_method
121  
122 Processes a post.
123  
124 =cut
125  
126 sub process_request_method
127 {
128 my ($self, $coderef) = @_;
129  
130 return unless $self->{request_method} eq 'POST';
131  
132 # copy post data to temporary file
133 my $input = $self->{env}{'psgi.input'};
134 my $tmpdir = $self->variable('tmpdir') || '/tmp';
135  
136 # PSGI's Apache gateway lacks seek.
137 if ($input->can('seek'))
138 {
139 $self->{input_fh} = $input;
140 }
141 else
142 {
143 $self->{tmp_file} = $tmpdir . '/' . tmpfilename();
144 my $fh = IO::File->new('> ' . $self->{tmp_file});
145  
146 my $buffer;
147 $fh->print($buffer) while($input->read($buffer, 1024));
148 $fh->close;
149  
150 $self->{input_fh} = IO::File->new('< ' . $self->{tmp_file});
151 }
152  
153 my $input = '';
154 $self->{input_fh}->read($input, $self->post_limit);
155  
156 $self->{request_body} = $input;
157  
158 &$coderef($self);
159  
160 return 1;
161 }
162  
163 =head3 cleanup
164  
165 Cleanup processing.
166  
167 =cut
168  
169 sub cleanup
170 {
171 my $self = shift;
172  
173 if ($self->{tmp_file})
174 {
175 eval { $self->{input_fh}->close };
176 unlink($self->{tmp_file});
177 }
178 }
179  
180 =head3 variable(key => $value)
181  
182 Returns a persistant server variable.
183  
184 Key without value returns variable.
185  
186 These include variables set by the server configuration, as "user variables" in nginx.
187  
188 =cut
189  
190 sub variable
191 {
192 my ($self, $key, $value) = @_;
193  
194 if ($value)
195 {
196 $SERVER_VARIABLES{$key} = $value;
197 }
198 else
199 {
200 return $SERVER_VARIABLES{$key};
201 }
202 }
203  
204 =head3 uri
205  
206 Returns the uri.
207  
208 =cut
209  
210 sub uri { shift->{uri} }
211  
212 =head3 args
213  
214 Returns string of arguments.
215  
216 =cut
217  
218 sub args { shift->{args} }
219  
220 =head3 request_body
221  
222 Returns the request body (used for posts)
223  
224 =cut
225  
226 sub request_body { shift->{request_body} }
227  
228 =head3 request_method
229  
230 Returns the request method (GET or POST)
231  
232 =cut
233  
234 sub request_method { shift->{request_method} || 'GET' }
235  
236 =head3 remote_addr
237  
238 Returns remote address.
239  
240 =cut
241  
242 sub remote_addr
243 {
244 my $self = shift;
245  
246 return $self->{remote_addr} || '127.0.0.1';
247 }
248  
249 =head3 has_request_body
250  
251 Used by nginx for request body processing.
252  
253 This function is only called when the request method is a post,
254 in an effort to reduce processing time.
255  
256 =cut
257  
258 sub has_request_body { }
259  
260 =head3 header_in
261  
262 Returns a request header.
263  
264 =cut
265  
266 sub header_in
267 {
268 my ($self, $what) = @_;
269  
270 return $self->{headers_in}{lc($what)};
271 }
272  
273 =head3 header_out
274  
275 Sets a header out.
276  
277 =cut
278  
279 sub header_out
280 {
281 my ($self, $header, $value) = @_;
282  
283 return $self->{headers_out}{$header} = $value;
284 }
285  
286 =head3 send_http_header
287  
288 Send the http header.
289  
290 =cut
291  
292 sub send_http_header
293 {
294 my ($self, $header) = @_;
295  
296 if ($self->{writer})
297 {
298 die 'PSGI: respond already called';
299 }
300 elsif ($self->{respond})
301 {
302 $self->{writer} =
303 $self->{respond}->([ ($self->status || 200), [
304 'Content-Type' => $header,
305 %{$self->{headers_out} || {}}
306 ]]);
307 }
308 else
309 {
310 $self->{http_header} = $header;
311 }
312 }
313  
314 =head3 $self->status(...)
315  
316 Set output status... (200, 404, etc...)
317 If no argument given, returns status.
318  
319 =cut
320  
321 sub status
322 {
323 my ($self, $status) = @_;
324  
325 if ($status)
326 {
327 $self->{status} = $status;
328 }
329 else
330 {
331 return $self->{status};
332 }
333 }
334  
335 =head3 print
336  
337 Print stuff to the http stream.
338  
339 =cut
340  
341 sub print {
342 my ($self, $line) = @_;
343  
344 if ($self->{writer})
345 {
346 $self->{writer}->write($line);
347 }
348 else
349 {
350 $self->{output} .= $line;
351 }
352 }
353  
354 sub rflush { }
355  
356 =head3 sleep
357  
358 Sleeps (used by nginx), not needed for other server implementations.
359  
360 =cut
361  
362 sub sleep
363 {
364 my $self = shift;
365 sleep(shift);
366 }
367  
368 =head3 header_only
369  
370 Returns true of only the header was requested.
371  
372 =cut
373  
374 sub header_only { 0 }
375  
376 sub server_returns_object { 1 }
377  
378 =head3 unescape
379  
380 Unescape an encoded uri.
381  
382 =cut
383  
384 sub unescape
385 {
386 my ($self, $string) = @_;
387  
388 return uri_unescape($string);
389 }
390  
391 =head3 server_return
392  
393 This function defines what is returned to the server at the end of a dispatch.
394 For nginx, this will be a status code, but in this test implementation we're
395 returning the actual server object itself, so we can evaluate it while testing
396  
397 =cut
398  
399 sub server_return
400 {
401 my ($self, $what) = @_;
402  
403 my $rstatus = $self->{status} || 200;
404 my $content_type = $self->{http_header};
405  
406 if ($self->{writer})
407 {
408 $self->{writer}->close;
409 }
410 else
411 {
412 return [
413 $rstatus,
414 [
415 'Content-Type' => $content_type,
416 %{$self->{headers_out} || {}}
417 ],
418 [ $self->{output} ],
419 ];
420 }
421 }
422  
423 =head1 AUTHORS
424  
425 Bizowie <http://bizowie.com>
426  
427 =head1 COPYRIGHT AND LICENSE
428  
429 Copyright (C) 2013 Bizowie
430  
431 This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
432  
433 =cut
434  
435 1;
436