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

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 98 CC 8 SCAR 21.5
CC
8
Cov
0%
request_body_override
20.0
initialize
12.0
cleanup
12.0
CRAP 8.6
1 package Drogo::Server::Nginx;
2 use strict;
3 use IO::File;
4  
5 =head1 NAME
6  
7 Drogo::Server::Nginx - Directly interface with Nginx using --with-http_perl_module
8  
9 =head1 METHODS
10  
11 Implements all methods in 'nginx' module provided with Nginx's --with-http_perl_module option.
12  
13 =cut
14  
15 use base qw(
16 Drogo::Server
17 nginx
18 );
19  
20 my %request_data;
21  
22 sub initialize
23 {
24 my ($class, $obj) = @_;
25  
26 bless($obj, __PACKAGE__);
27  
28 %request_data = ();
29  
30 my $ip_header = $obj->variable('proxy_ip_header');
31  
32 if ($ip_header)
33 {
34 $request_data{remote_addr} = $obj->header_in($ip_header);
35 }
36 elsif (my $remote_addr = $obj->variable('remote_addr'))
37 {
38 $request_data{remote_addr} = $remote_addr;
39 }
40 else
41 {
42 $request_data{remote_addr} = $obj->SUPER::remote_addr;
43 }
44  
45 return $obj;
46 }
47  
48 sub remote_addr { $request_data{remote_addr} }
49  
50 sub server_return
51 {
52 my ($self, $what) = @_;
53  
54 return $what;
55 }
56  
57 sub cleanup
58 {
59 my $self = shift;
60  
61 # cleanup magical request file
62 unlink($self->request_body_file)
63 if $self->request_body_file;
64  
65 if ($request_data{tmp_file})
66 {
67 eval { $request_data{input_fh}->close };
68 unlink($request_data{tmp_file});
69 }
70  
71 %request_data = ();
72 }
73  
74 sub tmpfilename { join('-', 'drogongxp', $$, time) }
75  
76 =head2 input
77  
78 Returns a filestream to the input.
79  
80 =cut
81  
82 sub input { $request_data{input_fh} }
83  
84 my $request_body_method;
85  
86 # This is a hack since Nginx's has_request_body returns a new
87 # 'nginx' object, opposed to a Drogo::Server::Nginx object
88  
89 sub process_request_method ($&)
90 {
91 my $self = shift; #nginx object, not this server object
92 $request_body_method = shift;
93  
94 return $self->has_request_body(\&request_body_override);
95 }
96  
97 sub request_body { $request_data{request_body} }
98  
99 sub request_body_override
100 {
101 my $self = shift; #nginx object, not this server object
102 __PACKAGE__->initialize($self); # rebless as this object
103  
104 my $tmpdir = $self->variable('tmpdir') || '/tmp';
105  
106 if ($self->request_body_file)
107 {
108 my $input = '';
109  
110 $request_data{input_fh} = IO::File->new('< ' . $self->request_body_file);
111 $request_data{input_fh}->read($input, $self->post_limit)
112 if $request_data{input_fh};
113 $request_data{request_body} = $input;
114 }
115  
116 unless ($request_data{input_fh})
117 {
118 $request_data{tmp_file} = $tmpdir . '/' . tmpfilename();
119  
120 my $wfh = IO::File->new('> ' . $request_data{tmp_file});
121 $wfh->print($self->SUPER::request_body);
122 $wfh->close;
123  
124 $request_data{input_fh} = IO::File->new('< ' . $request_data{tmp_file});
125  
126 my $input = '';
127 $request_data{input_fh}->read($input, $self->post_limit);
128 $request_data{request_body} = $input;
129 }
130  
131  
132 {
133 no strict 'refs';
134 return &$request_body_method($self);
135 }
136 }
137  
138 =head1 AUTHORS
139  
140 Bizowie <http://bizowie.com>
141  
142 =head1 COPYRIGHT AND LICENSE
143  
144 Copyright (C) 2013 Bizowie
145  
146 This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
147  
148 =cut
149  
150 1;
151