| 1 | package Web::App::Request::ModPerl; | ||
| 2 | |||
| 3 | use Class::Easy; | ||
| 4 | |||
| 5 | use base qw(Web::App::Request); | ||
| 6 | |||
| 7 | return 1 | ||
| 8 | unless $ENV{MOD_PERL}; | ||
| 9 | |||
| 10 | our $mod_perl_api = 1; | ||
| 11 | $mod_perl_api = $ENV{MOD_PERL_API_VERSION} | ||
| 12 | if exists $ENV{MOD_PERL_API_VERSION} and $ENV{MOD_PERL_API_VERSION} == 2; | ||
| 13 | |||
| 14 | my $mod_perl_config = [ | ||
| 15 | {}, | ||
| 16 | { | ||
| 17 | request => 'Apache', | ||
| 18 | server => 'Apache', | ||
| 19 | const => 'Apache::Constants', | ||
| 20 | strings => { | ||
| 21 | stage => '', | ||
| 22 | handler => 'perl-script', | ||
| 23 | } | ||
| 24 | |||
| 25 | }, | ||
| 26 | { | ||
| 27 | request => 'Apache2::RequestUtil', | ||
| 28 | # request_rec => 'Apache2::RequestRec', | ||
| 29 | server => 'Apache2::ServerUtil', | ||
| 30 | const => 'Apache2::Const', | ||
| 31 | strings => { | ||
| 32 | stage => 'Response', | ||
| 33 | handler => 'modperl', | ||
| 34 | } | ||
| 35 | } | ||
| 36 | ]; | ||
| 37 | |||
| 38 | my $mod_perl = $mod_perl_config->[$mod_perl_api]; | ||
| 39 | |||
| 40 | my $strings = delete $mod_perl->{strings}; | ||
| 41 | |||
| 42 | if ($mod_perl_api == 2) { | ||
| 43 | try_to_use_inc ('Apache2::RequestRec'); | ||
| 44 | *{Apache2::RequestRec::send_http_header} = sub {return}; | ||
| 45 | } | ||
| 46 | |||
| 47 | for my $k (keys %$mod_perl) { | ||
| 48 | next if $k eq 'sizelimit' and $^O eq 'darwin'; | ||
| 49 | try_to_use_inc ($mod_perl->{$k}); | ||
| 50 | } | ||
| 51 | |||
| 52 | my $const = $mod_perl->{const}; | ||
| 53 | |||
| 54 | if ($mod_perl_api == 2) { | ||
| 55 | $const->import (-compile => qw(:common :http)); | ||
| 56 | } else { | ||
| 57 | $const->import (qw(:common :http)); | ||
| 58 | } | ||
| 59 | |||
| 60 | # UNAVAILABLE FOR MAC OS X | ||
| 61 | # use Apache2::SizeLimit; | ||
| 62 | |||
| 63 | # $Apache2::Size::MAX_PROCESS_SIZE = 256*1024; # 256MB | ||
| 64 | # $Apache2::Size::MIN_SHARE_SIZE = 128*1024; # 128MB | ||
| 65 | # $Apache2::Size::MAX_UNSHARED_SIZE = 160*1024; # 160MB | ||
| 66 | |||
| 67 | # my $dir_config = Apache2::ServerUtil->server->dir_config; | ||
| 68 | |||
| 69 | has 'http_code', is => 'rw', default => $const->DONE; | ||
| 70 | |||
| 71 | sub r { | ||
| 72 | my $r = $mod_perl->{request}->request; | ||
| 73 | |||
| 74 | die unless defined $r; | ||
| 75 | |||
| 76 | return $r; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub server { | ||
| 80 | return $mod_perl->{server}->server; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub rewrite_root { | ||
| 84 | my $r = shift; | ||
| 85 | |||
| 86 | $r->uri ('/index.html') | ||
| 87 | if $r->uri eq '/'; | ||
| 88 | return $const->DECLINED; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _preload { | ||
| 92 | my $class = shift; | ||
| 93 | my $app = shift; | ||
| 94 | |||
| 95 | my $server = $class->server; | ||
| 96 | my $location = ''; | ||
| 97 | |||
| 98 | return if $mod_perl_api != 2; | ||
| 99 | |||
| 100 | $server->add_config (['PerlTransHandler +Web::App::Request::ModPerl::rewrite_root']); | ||
| 101 | |||
| 102 | my @ending = ( | ||
| 103 | " SetHandler $strings->{handler}", | ||
| 104 | " Perl$strings->{stage}Handler Web::App->handle_request", | ||
| 105 | ' DefaultType text/html', | ||
| 106 | '</Location>', | ||
| 107 | ); | ||
| 108 | |||
| 109 | my $screens = $app->config->screens; | ||
| 110 | |||
| 111 | my $base_uri = $screens->{'#base-uri'}; | ||
| 112 | |||
| 113 | if ($base_uri and $base_uri ne '/') { | ||
| 114 | debug "preloading into $base_uri"; | ||
| 115 | die "base-uri key in screens config must begins with '/'" | ||
| 116 | if $base_uri !~ /^\//; | ||
| 117 | my $location = "<Location $base_uri>"; | ||
| 118 | #warn join "\n", ($location, @ending); | ||
| 119 | $server->add_config([ | ||
| 120 | $location, | ||
| 121 | @ending | ||
| 122 | ]); | ||
| 123 | |||
| 124 | } else { | ||
| 125 | debug "preloading into /"; | ||
| 126 | my @screen_ids = grep {/^(\?|[^\#\/]+)$/} keys %$screens; | ||
| 127 | debug join ', ', @screen_ids; | ||
| 128 | foreach my $screen_id (@screen_ids) { | ||
| 129 | $screen_id = 'index.html' | ||
| 130 | if $screen_id eq '?'; | ||
| 131 | |||
| 132 | debug "init screen /$screen_id"; | ||
| 133 | |||
| 134 | my $location = "<Location /$screen_id>"; | ||
| 135 | #warn join "\n", ($location, @ending); | ||
| 136 | $server->add_config([ | ||
| 137 | $location, | ||
| 138 | @ending | ||
| 139 | ]); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | # this code called every request | ||
| 147 | sub _init { | ||
| 148 | my $self = shift; | ||
| 149 | my $app = shift; | ||
| 150 | |||
| 151 | my $r = r; | ||
| 152 | |||
| 153 | my $uri = $r->uri; | ||
| 154 | # my $path_info = $r->path_info; | ||
| 155 | |||
| 156 | my $screens = $app->config->screens; | ||
| 157 | |||
| 158 | my $base_uri = $screens->{'#base-uri'}; #($uri =~ /(.*)$path_info$/)[0]; | ||
| 159 | $base_uri =~ s/\/$//; # we don't want double slashes | ||
| 160 | |||
| 161 | my $path_info = ($uri =~ /^$base_uri(.*)$/)[0]; | ||
| 162 | |||
| 163 | debug "uri: $uri, p_i: $path_info, base_uri: $base_uri"; | ||
| 164 | |||
| 165 | my $host = $self->incoming_headers->{'X-Forwarded-Host'} || $r->hostname; | ||
| 166 | |||
| 167 | $self->set_field_values ( | ||
| 168 | host => $host, | ||
| 169 | uri => $uri, | ||
| 170 | base_uri => $base_uri, | ||
| 171 | path => $path_info, | ||
| 172 | ); | ||
| 173 | } | ||
| 174 | |||
| 175 | sub set_status { | ||
| 176 | my $self = shift; | ||
| 177 | my $code = shift; | ||
| 178 | if ($self->can('r')) { | ||
| 179 | if ($code == 200) { | ||
| 180 | debug "setting r->status->HTTP_OK"; | ||
| 181 | $self->r->status ($const->HTTP_OK); | ||
| 182 | } elsif ($code == 302) { | ||
| 183 | debug "setting r->status->HTTP_MOVED_TEMPORARILY"; | ||
| 184 | $self->r->status ($const->HTTP_MOVED_TEMPORARILY); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | sub done_status { | ||
| 190 | $const->DONE; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub redirect_status { | ||
| 194 | $const->REDIRECT; | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | sub r_method { | ||
| 199 | my $object = shift; | ||
| 200 | my $method = shift; | ||
| 201 | |||
| 202 | if ($mod_perl_api == 1) { | ||
| 203 | $object->$method (@_); | ||
| 204 | } elsif ($mod_perl_api == 2) { | ||
| 205 | $object->$method (@_, r()); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | sub send_headers { | ||
| 210 | my $self = shift; | ||
| 211 | my $headers = shift; | ||
| 212 | |||
| 213 | my $r = r; | ||
| 214 | |||
| 215 | my $method = 'headers_out'; | ||
| 216 | if ($headers->header ('Location')) { | ||
| 217 | debug "redirect detected"; | ||
| 218 | $self->redirected (1); | ||
| 219 | $method = 'err_headers_out'; | ||
| 220 | } | ||
| 221 | |||
| 222 | $r->$method->clear; | ||
| 223 | |||
| 224 | foreach my $key (($headers->header_field_names)) { | ||
| 225 | my $val = $headers->header ($key); | ||
| 226 | if ($key =~ /content-type/i) { | ||
| 227 | $r->content_type ($val); | ||
| 228 | } else { | ||
| 229 | # $key =~ s/\b(\w)/uc $1/ge; | ||
| 230 | $r->$method->{$key} = $val; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | $r->send_http_header; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub incoming_headers { | ||
| 238 | my $self = shift; | ||
| 239 | |||
| 240 | return r->headers_in; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub send_content { | ||
| 244 | my $self = shift; | ||
| 245 | my $content = shift; | ||
| 246 | |||
| 247 | debug "content output"; | ||
| 248 | |||
| 249 | utf8::decode ($content); | ||
| 250 | |||
| 251 | r->print ($content); | ||
| 252 | } | ||
| 253 | |||
| 254 | 1; |