App/Request/ModPerl2.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 159 CC 8 SCAR 19.1
CC
8
Cov
0%
_preload
20.0
send_headers
12.0
r
6.0
CRAP 6.7
1 package Web::App::Request::ModPerl2;
2  
3 use Class::Easy;
4  
5 use Web::App::Request;
6 use base qw(Web::App::Request);
7  
8 return 1
9 unless $ENV{MOD_PERL};
10  
11 our $MOD_PERL = 2;
12 $MOD_PERL = $ENV{MOD_PERL_API_VERSION}
13 if $ENV{MOD_PERL_API_VERSION};
14  
15 use Apache2::Const -compile => qw(:common :http);
16  
17 use Apache2::RequestUtil;
18 use Apache2::RequestRec;
19  
20 # UNAVAILABLE FOR MAC OS X
21 # use Apache2::SizeLimit;
22 use Apache2::ServerUtil;
23  
24 # $Apache2::Size::MAX_PROCESS_SIZE = 256*1024; # 256MB
25 # $Apache2::Size::MIN_SHARE_SIZE = 128*1024; # 128MB
26 # $Apache2::Size::MAX_UNSHARED_SIZE = 160*1024; # 160MB
27  
28 # my $dir_config = Apache2::ServerUtil->server->dir_config;
29  
30 has 'http_code', is => 'rw', default => Apache2::Const::DONE;
31  
32 sub r {
33 my $r = Apache2::RequestUtil->request;
34
35 die unless defined $r;
36
37 return $r;
38 }
39  
40 sub rewrite_root {
41 my $r = shift;
42
43 $r->uri ('/index.html')
44 if $r->uri eq '/';
45 return Apache2::Const::DECLINED;
46 }
47  
48 sub _preload {
49 my $class = shift;
50 my $app = shift;
51
52 my $server = Apache2::ServerUtil->server;
53 my $location = '';
54
55 $server->add_config (['PerlTransHandler +Web::App::Request::ModPerl2::rewrite_root']);
56
57 my @ending = (
58 ' SetHandler modperl',
59 ' PerlResponseHandler Web::App->handle_request',
60 ' DefaultType text/html',
61 '</Location>',
62 );
63
64 my $screens = $app->config->screens;
65
66 my $base_uri = $screens->{'#base-uri'};
67
68 if ($base_uri and $base_uri ne '/') {
69 debug "preloading into $base_uri";
70 die "base-uri key in screens config must begins with '/'"
71 if $base_uri !~ /^\//;
72 my $location = "<Location $base_uri>";
73 $server->add_config([
74 $location,
75 @ending
76 ]);
77
78 } else {
79 debug "preloading into /";
80 my @screen_ids = grep {/^(\?|[^\#\/]+)$/} keys %$screens;
81 debug join ', ', @screen_ids;
82 foreach my $screen_id (@screen_ids) {
83 $screen_id = 'index.html'
84 if $screen_id eq '?';
85
86 debug "init screen /$screen_id";
87
88 my $location = "<Location /$screen_id>";
89 $server->add_config([
90 $location,
91 @ending
92 ]);
93 }
94 }
95
96 }
97  
98  
99 # this code called every request
100 sub _init {
101 my $self = shift;
102 my $app = shift;
103
104 my $r = r;
105
106 my $uri = $r->uri;
107 # my $path_info = $r->path_info;
108
109 my $screens = $app->config->screens;
110
111 my $base_uri = $screens->{'#base-uri'}; #($uri =~ /(.*)$path_info$/)[0];
112 $base_uri =~ s/\/$//; # we don't want double slashes
113
114 my $path_info = ($uri =~ /^$base_uri(.*)$/)[0];
115
116 debug "uri: $uri, p_i: $path_info, base_uri: $base_uri";
117
118 my $host = $self->incoming_headers->{'X-Forwarded-Host'} || $r->hostname;
119
120 $self->set_field_values (
121 host => $host,
122 uri => $uri,
123 base_uri => $base_uri,
124 path => $path_info,
125 unparsed_uri => $r->unparsed_uri,
126 );
127 }
128  
129 sub done_status {
130 Apache2::Const::OK;
131 }
132  
133 sub redirect_status {
134 Apache2::Const::REDIRECT;
135 }
136  
137  
138 sub test ($$) {
139 my $class = shift;
140 my $r = shift;
141
142 # my $r = Apache2::RequestUtil->request;
143 $r->content_type ('text/html');
144 #$r->headers_out
145
146 foreach (sort keys %ENV) {
147 $r->print ("$_ => $ENV{$_}<br/>\n");
148 }
149
150 $r->print ("HELLO!!!");
151
152 Apache2::Const::OK;
153 }
154  
155 sub r_method {
156 my $object = shift;
157 my $method = shift;
158
159 $object->$method (@_, r());
160 }
161  
162 sub send_headers {
163 my $self = shift;
164 my $headers = shift;
165
166 my $method = 'headers_out';
167 if ($headers->header ('Location')) {
168 debug "redirect detected";
169 $self->redirected (1);
170 $method = 'err_headers_out';
171 }
172
173 r->$method->clear;
174
175 foreach my $key (($headers->header_field_names)) {
176 my $val = $headers->header ($key);
177 if ($key =~ /content-type/i) {
178 r->content_type ($val);
179 } else {
180 # $key =~ s/\b(\w)/uc $1/ge;
181 r->$method->{$key} = $val;
182 }
183 }
184 }
185  
186 sub incoming_headers {
187 my $self = shift;
188
189 return r->headers_in;
190 }
191  
192 sub send_content {
193 my $self = shift;
194 my $content = shift;
195
196 debug "content output";
197
198 utf8::decode ($content);
199
200 r->print ($content);
201 }
202  
203 1;