| 1 | package QBit::WebInterface; | ||
| 2 | $QBit::WebInterface::VERSION = '0.031'; | ||
| 3 | use POSIX qw(strftime setlocale LC_TIME); | ||
| 4 | |||
| 5 | use qbit; | ||
| 6 | |||
| 7 | use QBit::WebInterface::Response; | ||
| 8 | |||
| 9 | use Exception::WebInterface::Controller::CSRF; | ||
| 10 | use Exception::Request::UnknownMethod; | ||
| 11 | |||
| 12 | sub request { | ||
| 13 | my ($self, $request) = @_; | ||
| 14 | |||
| 15 | return defined($request) ? $self->{'__REQUEST__'} = $request : $self->{'__REQUEST__'}; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub response { | ||
| 19 | my ($self, $response) = @_; | ||
| 20 | |||
| 21 | return defined($response) ? $self->{'__RESPONSE__'} = $response : $self->{'__RESPONSE__'}; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub get_cmds { | ||
| 25 | my ($self) = @_; | ||
| 26 | |||
| 27 | my $cmds = {}; | ||
| 28 | |||
| 29 | package_merge_isa_data( | ||
| 30 | ref($self), | ||
| 31 | $cmds, | ||
| 32 | sub { | ||
| 33 | my ($package, $res) = @_; | ||
| 34 | |||
| 35 | my $pkg_cmds = package_stash($package)->{'__CMDS__'} || {}; | ||
| 36 | foreach my $path (keys(%$pkg_cmds)) { | ||
| 37 | foreach my $cmd (keys(%{$pkg_cmds->{$path}})) { | ||
| 38 | $cmds->{$path}{$cmd} = $pkg_cmds->{$path}{$cmd}; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | }, | ||
| 42 | __PACKAGE__ | ||
| 43 | ); | ||
| 44 | |||
| 45 | return $cmds; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub build_response { | ||
| 49 | my ($self) = @_; | ||
| 50 | |||
| 51 | $self->pre_run(); | ||
| 52 | |||
| 53 | throw gettext('No request object') unless $self->request; | ||
| 54 | $self->response(QBit::WebInterface::Response->new()); | ||
| 55 | |||
| 56 | my $cmds = $self->get_cmds(); | ||
| 57 | my ($path, $cmd) = $self->get_cmd(); | ||
| 58 | |||
| 59 | $cmd = $cmds->{$path}{'__DEFAULT__'}{'name'} if $cmd eq ''; | ||
| 60 | $cmd = '' unless defined($cmd); | ||
| 61 | |||
| 62 | $self->set_option(cur_cmd => $cmd); | ||
| 63 | $self->set_option(cur_cmdpath => $path); | ||
| 64 | |||
| 65 | if (exists($cmds->{$path}{$cmd})) { | ||
| 66 | try { | ||
| 67 | my $cmd = $cmds->{$path}{$cmd}; | ||
| 68 | |||
| 69 | my $controller = $cmd->{'package'}->new( | ||
| 70 | app => $self, | ||
| 71 | path => $path, | ||
| 72 | attrs => $cmd->{'attrs'} | ||
| 73 | ); | ||
| 74 | |||
| 75 | $self->{'__BREAK_PROCESS__'} = 0; | ||
| 76 | $self->pre_cmd($controller); | ||
| 77 | |||
| 78 | unless ($self->{'__BREAK_PROCESS__'}) { | ||
| 79 | $controller->{'__BREAK_CMD__'} = FALSE; | ||
| 80 | $controller->pre_cmd() if $controller->can('pre_cmd'); | ||
| 81 | |||
| 82 | unless ($controller->{'__BREAK_CMD__'}) { | ||
| 83 | if ($controller->attrs()->{'SAFE'}) { | ||
| 84 | throw Exception::WebInterface::Controller::CSRF gettext('CSRF has been detected') | ||
| 85 | unless $controller->check_anti_csrf_token($self->request->param(sign => ''), | ||
| 86 | url => $self->get_option('cur_cmdpath') . '/' . $self->get_option('cur_cmd')); | ||
| 87 | } | ||
| 88 | |||
| 89 | my @data = $cmd->{'sub'}($controller); | ||
| 90 | if (defined(my $method = $cmd->{'process_method'})) { | ||
| 91 | $controller->$method(@data); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | $self->post_cmd(); | ||
| 97 | } | ||
| 98 | catch Exception::Denied with { | ||
| 99 | $self->response->status(403); | ||
| 100 | $self->response->data(undef); | ||
| 101 | } | ||
| 102 | catch Exception::Request::UnknownMethod with { | ||
| 103 | $self->response->status(400); | ||
| 104 | $self->response->data(undef); | ||
| 105 | } | ||
| 106 | catch { | ||
| 107 | $self->_catch_internal_server_error(@_); | ||
| 108 | }; | ||
| 109 | } else { | ||
| 110 | $self->response->status(404); | ||
| 111 | } | ||
| 112 | |||
| 113 | my $ua = $self->request->http_header('User-Agent'); | ||
| 114 | $self->response->headers->{'Pragma'} = ($ua =~ /MSIE/) ? 'public' : 'no-cache'; | ||
| 115 | |||
| 116 | $self->response->headers->{'Cache-Control'} = | ||
| 117 | ($ua =~ /MSIE/) | ||
| 118 | ? 'must-revalidate, post-check=0, pre-check=0' | ||
| 119 | : 'no-cache, no-store, max-age=0, must-revalidate'; | ||
| 120 | |||
| 121 | my $tm = time(); | ||
| 122 | my $zone = (strftime("%z", localtime($tm)) + 0) / 100; | ||
| 123 | my $loc = setlocale(LC_TIME); | ||
| 124 | setlocale(LC_TIME, 'en_US.UTF-8'); | ||
| 125 | my $GMT = strftime("%a, %d %b %Y %H:%M:%S GMT", localtime($tm - $zone * 3600)); | ||
| 126 | setlocale(LC_TIME, $loc); | ||
| 127 | |||
| 128 | $self->response->headers->{'Expires'} = $GMT; | ||
| 129 | |||
| 130 | $self->post_run(); | ||
| 131 | |||
| 132 | $self->response->timelog($self->timelog); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub break { | ||
| 136 | my ($self, @data) = @_; | ||
| 137 | |||
| 138 | $self->{'__BREAK_PROCESS__'} = 1; | ||
| 139 | return @data; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub form_fields { } | ||
| 143 | |||
| 144 | sub pre_cmd { } | ||
| 145 | |||
| 146 | sub post_cmd { } | ||
| 147 | |||
| 148 | sub default_cmd {throw 'Abstract metod'} | ||
| 149 | |||
| 150 | sub get_cmd {throw 'Abstract metod'} | ||
| 151 | |||
| 152 | sub make_cmd {throw 'Abstract metod'} | ||
| 153 | |||
| 154 | sub _get_new_cmd { | ||
| 155 | my ($self, $new_cmd, $cur_cmd) = @_; | ||
| 156 | |||
| 157 | $cur_cmd = '' unless defined($cur_cmd); | ||
| 158 | |||
| 159 | return defined($new_cmd) ? $new_cmd : $cur_cmd; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _get_new_path { | ||
| 163 | my ($self, $new_path, $cur_path) = @_; | ||
| 164 | |||
| 165 | $cur_path = '' unless defined($cur_path); | ||
| 166 | |||
| 167 | return defined($new_path) && length($new_path) ? $new_path : $cur_path; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub _escape_filename { | ||
| 171 | my ($self, $filename) = @_; | ||
| 172 | |||
| 173 | $filename =~ s{"}{\\"}g; | ||
| 174 | $filename =~ s{\r}{}g; | ||
| 175 | $filename =~ s{\n}{}g; | ||
| 176 | |||
| 177 | return $filename; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _catch_internal_server_error { | ||
| 181 | my ($self, $exception) = @_; | ||
| 182 | |||
| 183 | if (my $dir = $self->get_option('error_dump_dir')) { | ||
| 184 | require File::Path; | ||
| 185 | File::Path::make_path($dir); | ||
| 186 | writefile("$dir/dump_" . format_date(curdate(), '%Y%m%d_%H%M%S') . "${$}.html", | ||
| 187 | $self->_exception2html($exception)); | ||
| 188 | $self->response->status(500); | ||
| 189 | $self->response->data(undef); | ||
| 190 | } else { | ||
| 191 | $self->response->status(200); | ||
| 192 | if (($self->request->http_header('Accept') || '') =~ /(application\/json|text\/javascript)/) { | ||
| 193 | $self->response->content_type("$1; charset=UTF-8"); | ||
| 194 | $self->response->data(to_json({error => gettext('Internal Server Error: %s', $exception->message())})); | ||
| 195 | } else { | ||
| 196 | $self->response->data($self->_exception2html($exception)); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | sub _exception2html { | ||
| 202 | my ($self, $exception) = @_; | ||
| 203 | |||
| 204 | my $server = `hostname -f`; | ||
| 205 | chomp($server); | ||
| 206 | |||
| 207 | my $short_dumper = sub { | ||
| 208 | my ($data, $max_depth) = @_; | ||
| 209 | local $Data::Dumper::Maxdepth = $max_depth; | ||
| 210 | local $Data::Dumper::Varname = ''; | ||
| 211 | local $Data::Dumper::Sortkeys = TRUE; | ||
| 212 | my $dtext = Dumper($data); | ||
| 213 | |||
| 214 | $dtext =~ /^(\$\d+ = )/; | ||
| 215 | my $prefix_length = $1 ? length($1) : 0; | ||
| 216 | $dtext =~ s/^.{$prefix_length}//mg; | ||
| 217 | |||
| 218 | $dtext =~ s/\\x\{([a-f0-9]{2,})\}/chr(hex($1))/ge; | ||
| 219 | $dtext =~ s/;$//msg; | ||
| 220 | $dtext =~ s/\n$//msg; | ||
| 221 | utf8::decode($dtext); | ||
| 222 | |||
| 223 | return $dtext; | ||
| 224 | }; | ||
| 225 | |||
| 226 | my $html = | ||
| 227 | '<html>' | ||
| 228 | . '<head>' | ||
| 229 | . '<meta http-equiv="content-type" content="text/html; charset=UTF-8">' | ||
| 230 | . '<title>' | ||
| 231 | . gettext('Fatal error') | ||
| 232 | . '</title>' | ||
| 233 | . '</head>' | ||
| 234 | . '<body bgcolor="#FFFFFF" text="#000000">' | ||
| 235 | |||
| 236 | . '<div style="background-color: #CCFF99; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 237 | . gettext('Server') . ': ' | ||
| 238 | . html_encode($server) . '</h3>' | ||
| 239 | . '<strong>' | ||
| 240 | . gettext('Host') | ||
| 241 | . ':</strong> ' | ||
| 242 | . html_encode($self->request->http_header('host')) . '<br>' | ||
| 243 | . '<strong>' | ||
| 244 | . gettext('Date') | ||
| 245 | . ':</strong> ' | ||
| 246 | . html_encode(format_date(curdate(), '%c')) . '<br>' | ||
| 247 | . '</div>' | ||
| 248 | |||
| 249 | . '<div style="background-color: #FF7777; font-size: 110%; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 250 | . html_encode(ref($exception)) . '</h3>' | ||
| 251 | . '<h4><pre>' | ||
| 252 | . html_encode($exception->{'text'}) | ||
| 253 | . '</pre></h4>' | ||
| 254 | . '<strong>' | ||
| 255 | . gettext('Package') | ||
| 256 | . ':</strong> ' | ||
| 257 | . html_encode($exception->{'package'}) . '<br>' | ||
| 258 | . '<strong>' | ||
| 259 | . gettext('Filename') | ||
| 260 | . ':</strong> ' | ||
| 261 | . html_encode($exception->{'filename'}) . ' (' | ||
| 262 | . gettext('line') . ' ' | ||
| 263 | . html_encode($exception->{'line'}) . ')<br>' | ||
| 264 | . '</div>' | ||
| 265 | |||
| 266 | . '<div style="background-color: #EEAA77; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 267 | . gettext('Request') | ||
| 268 | . ':</h3>' | ||
| 269 | . '<table width="100%">' | ||
| 270 | . '<tr><th valign="top" align="right">' | ||
| 271 | . gettext('Method') | ||
| 272 | . '</th><td>' | ||
| 273 | . html_encode($self->request->method) | ||
| 274 | . '</td></tr>' | ||
| 275 | . '<tr><th valign="top" align="right">' | ||
| 276 | . gettext('URL') | ||
| 277 | . '</th><td>' | ||
| 278 | . html_encode($self->request->url) | ||
| 279 | . '</td></tr>' | ||
| 280 | . join( | ||
| 281 | '', | ||
| 282 | map { | ||
| 283 | '<tr><th valign="top" nowrap="nowrap" align="right">' | ||
| 284 | . html_encode($_->[1]) | ||
| 285 | . '</th><td>' | ||
| 286 | . html_encode($self->request->http_header($_->[0]) || '') | ||
| 287 | . '</td></tr>' | ||
| 288 | } ( | ||
| 289 | [referer => gettext('Referer')], | ||
| 290 | ['user-agent' => gettext('User agent')], | ||
| 291 | ['remote-addr' => gettext('Remote address')], | ||
| 292 | [accept => gettext('Accept')], | ||
| 293 | ['accept-encoding' => gettext('Accept encoding')], | ||
| 294 | ['accept-language' => gettext('Accept languages')], | ||
| 295 | [cookie => gettext('Cookie')] | ||
| 296 | ) | ||
| 297 | ) | ||
| 298 | . '</table>' | ||
| 299 | . '</div>' | ||
| 300 | |||
| 301 | . '<div style="background-color: #FFFACD; padding: 5px 10px; margin: 1px;">' | ||
| 302 | . '<h3>Backtrace:</h3>' | ||
| 303 | . join( | ||
| 304 | '', | ||
| 305 | map { | ||
| 306 | my $level = $_; | ||
| 307 | '<div style="font-family: monospace; margin-bottom: 0.5em;"><strong>' | ||
| 308 | . html_encode($_->{'subroutine'}) | ||
| 309 | . '</strong>(' | ||
| 310 | . '<pre style="margin: 0px 0px 0px 2em; padding: 0px;">' | ||
| 311 | . join("\n\n", map {html_encode($short_dumper->($_, 1)) . ","} @{$_->{'args'}}) | ||
| 312 | . '</pre>' . ') ' | ||
| 313 | . gettext('called at %s line %s', html_encode($_->{'filename'}), $_->{'line'}) | ||
| 314 | . '</div>'; | ||
| 315 | } @{$exception->{'callstack'}} | ||
| 316 | ) | ||
| 317 | . '</div>' | ||
| 318 | |||
| 319 | . '<div style="background-color: #CCCCCC; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 320 | . gettext('Server enviroment') | ||
| 321 | . ':</h3>' | ||
| 322 | . '<table border="1">' | ||
| 323 | . join('', | ||
| 324 | map {'<tr><th align="right">' . html_encode($_) . '</th><td>' . html_encode($ENV{$_} || '') . '</td></tr>'} | ||
| 325 | keys(%ENV)) | ||
| 326 | . '</table>' | ||
| 327 | . '</div>' | ||
| 328 | |||
| 329 | . '<div style="background-color: #EEEEEE; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 330 | . gettext('Application dump') | ||
| 331 | . ':</h3>' . '<pre>' | ||
| 332 | . html_encode($short_dumper->($self)) | ||
| 333 | . '</pre>' | ||
| 334 | . '</div>' | ||
| 335 | |||
| 336 | . '</body>' . '</html>'; | ||
| 337 | } | ||
| 338 | |||
| 339 | TRUE; | ||
| 340 | |||
| 341 | __END__ |