| 1 | package QBit::WebInterface; | ||
| 2 | $QBit::WebInterface::VERSION = '0.033'; | ||
| 3 | use qbit; | ||
| 4 | |||
| 5 | use POSIX qw(strftime setlocale LC_TIME); | ||
| 6 | use URI::Escape qw(uri_escape_utf8); | ||
| 7 | |||
| 8 | use QBit::WebInterface::Routing; | ||
| 9 | use QBit::WebInterface::Response; | ||
| 10 | |||
| 11 | use Exception::WebInterface::Controller::CSRF; | ||
| 12 | use Exception::Request::UnknownMethod; | ||
| 13 | |||
| 14 | our %HTTP_STATUSES = ( | ||
| 15 | 200 => 'OK', | ||
| 16 | 201 => 'CREATED', | ||
| 17 | 202 => 'Accepted', | ||
| 18 | 203 => 'Partial Information', | ||
| 19 | 204 => 'No Response', | ||
| 20 | 301 => 'Moved', | ||
| 21 | 302 => 'Found', | ||
| 22 | 303 => 'Method', | ||
| 23 | 304 => 'Not Modified', | ||
| 24 | 400 => 'Bad request', | ||
| 25 | 401 => 'Unauthorized', | ||
| 26 | 402 => 'PaymentRequired', | ||
| 27 | 403 => 'Forbidden', | ||
| 28 | 404 => 'Not found', | ||
| 29 | 500 => 'Internal Error', | ||
| 30 | 501 => 'Not implemented', | ||
| 31 | 502 => 'Service temporarily overloaded', | ||
| 32 | 503 => 'Gateway timeout', | ||
| 33 | ); | ||
| 34 | |||
| 35 | sub request { | ||
| 36 | my ($self, $request) = @_; | ||
| 37 | |||
| 38 | return defined($request) ? $self->{'__REQUEST__'} = $request : $self->{'__REQUEST__'}; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub response { | ||
| 42 | my ($self, $response) = @_; | ||
| 43 | |||
| 44 | return defined($response) ? $self->{'__RESPONSE__'} = $response : $self->{'__RESPONSE__'}; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub routing { | ||
| 48 | my ($self, %opts) = @_; | ||
| 49 | |||
| 50 | $self->{'__ROUTING__'} = QBit::WebInterface::Routing->new(%opts); | ||
| 51 | |||
| 52 | return $self->{'__ROUTING__'}; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub get_cmds { | ||
| 56 | my ($self) = @_; | ||
| 57 | |||
| 58 | unless (exists($self->{'__ALL_CMDS__'})) { | ||
| 59 | my $cmds = {}; | ||
| 60 | my @cmd_with_route = (); | ||
| 61 | package_merge_isa_data( | ||
| 62 | ref($self), | ||
| 63 | $cmds, | ||
| 64 | sub { | ||
| 65 | my ($package, $res) = @_; | ||
| 66 | |||
| 67 | my $pkg_cmds = package_stash($package)->{'__CMDS__'} || {}; | ||
| 68 | foreach my $path (keys(%$pkg_cmds)) { | ||
| 69 | foreach my $cmd (keys(%{$pkg_cmds->{$path}})) { | ||
| 70 | $cmds->{$path}{$cmd} = $pkg_cmds->{$path}{$cmd}; | ||
| 71 | |||
| 72 | $self->{'__IMPORTED_CONTROLLERS__'}{$cmds->{$path}{$cmd}{'package'}} = TRUE; | ||
| 73 | |||
| 74 | if ($cmds->{$path}{$cmd}{'attributes'}{'URL'}) { | ||
| 75 | push( | ||
| 76 | @cmd_with_route, | ||
| 77 | { | ||
| 78 | path => $path, | ||
| 79 | cmd => $cmd, | ||
| 80 | route_params => $cmds->{$path}{$cmd}{'route_params'} | ||
| 81 | } | ||
| 82 | ); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | }, | ||
| 87 | __PACKAGE__ | ||
| 88 | ); | ||
| 89 | |||
| 90 | if (defined($self->{'__ROUTING__'})) { | ||
| 91 | my $package = $self->get_option('controller_class', 'QBit::WebInterface::Controller'); | ||
| 92 | |||
| 93 | unless ($self->{'__IMPORTED_CONTROLLERS__'}{$package}) { | ||
| 94 | require_class($package); | ||
| 95 | $package->import(app_pkg => ref($self)); | ||
| 96 | |||
| 97 | $self->{'__IMPORTED_CONTROLLERS__'}{$package} = TRUE; | ||
| 98 | } | ||
| 99 | |||
| 100 | $self->{'__ROUTING__'}->create_handler_cmds($package, $cmds); | ||
| 101 | } | ||
| 102 | |||
| 103 | if (@cmd_with_route) { | ||
| 104 | my $routing = $self->{'__ROUTING__'}; | ||
| 105 | |||
| 106 | unless (defined($routing)) { | ||
| 107 | $routing = $self->routing(); | ||
| 108 | } | ||
| 109 | |||
| 110 | foreach (@cmd_with_route) { | ||
| 111 | $routing->_generate_route(@{$_->{'route_params'}})->to(path => $_->{'path'}, cmd => $_->{'cmd'}); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | $self->{'__ALL_CMDS__'} = $cmds; | ||
| 116 | } | ||
| 117 | |||
| 118 | return $self->{'__ALL_CMDS__'}; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub build_response { | ||
| 122 | my ($self) = @_; | ||
| 123 | |||
| 124 | $self->pre_run(); | ||
| 125 | |||
| 126 | throw Exception gettext('No request object') unless $self->request; | ||
| 127 | $self->response(QBit::WebInterface::Response->new()); | ||
| 128 | |||
| 129 | try { | ||
| 130 | my $cmds = $self->get_cmds(); | ||
| 131 | my ($path, $cmd_name, %params) = $self->get_cmd(); | ||
| 132 | |||
| 133 | $cmd_name = $cmds->{$path}{'__DEFAULT__'}{'name'} if $cmd_name eq ''; | ||
| 134 | $cmd_name = '' unless defined($cmd_name); | ||
| 135 | |||
| 136 | $self->set_option(cur_cmd => $cmd_name); | ||
| 137 | $self->set_option(cur_cmdpath => $path); | ||
| 138 | |||
| 139 | if (exists($cmds->{$path}{$cmd_name})) { | ||
| 140 | my $cmd = $cmds->{$path}{$cmd_name}; | ||
| 141 | |||
| 142 | my $controller = $cmd->{'package'}->new( | ||
| 143 | app => $self, | ||
| 144 | path => $path, | ||
| 145 | attrs => $cmd->{'attributes'} | ||
| 146 | ); | ||
| 147 | |||
| 148 | $self->{'__BREAK_PROCESS__'} = 0; | ||
| 149 | $self->pre_cmd($controller); | ||
| 150 | |||
| 151 | unless ($self->{'__BREAK_PROCESS__'}) { | ||
| 152 | $controller->{'__BREAK_CMD__'} = FALSE; | ||
| 153 | $controller->pre_cmd() if $controller->can('pre_cmd'); | ||
| 154 | |||
| 155 | unless ($controller->{'__BREAK_CMD__'}) { | ||
| 156 | if ($cmd->{'attributes'}{'SAFE'}) { | ||
| 157 | throw Exception::WebInterface::Controller::CSRF gettext('CSRF has been detected') | ||
| 158 | unless $controller->check_anti_csrf_token($self->request->param(sign => ''), | ||
| 159 | url => $self->get_option('cur_cmdpath') . '/' . $self->get_option('cur_cmd')); | ||
| 160 | } | ||
| 161 | |||
| 162 | my @data = $cmd->{'sub'}->($controller, %params); | ||
| 163 | if (defined(my $method = $cmd->{'process_method'})) { | ||
| 164 | $controller->$method(@data); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | $self->post_cmd(); | ||
| 170 | } else { | ||
| 171 | $self->response->status(404); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | catch Exception::Denied with { | ||
| 175 | $self->response->status(403); | ||
| 176 | $self->response->data(undef); | ||
| 177 | } | ||
| 178 | catch Exception::Request::UnknownMethod with { | ||
| 179 | $self->response->status(400); | ||
| 180 | $self->response->data(undef); | ||
| 181 | } | ||
| 182 | catch { | ||
| 183 | ldump(@_); | ||
| 184 | $self->_catch_internal_server_error(@_); | ||
| 185 | }; | ||
| 186 | |||
| 187 | my $ua = $self->request->http_header('User-Agent'); | ||
| 188 | $self->response->headers->{'Pragma'} = ($ua =~ /MSIE/) ? 'public' : 'no-cache'; | ||
| 189 | |||
| 190 | $self->response->headers->{'Cache-Control'} = | ||
| 191 | ($ua =~ /MSIE/) | ||
| 192 | ? 'must-revalidate, post-check=0, pre-check=0' | ||
| 193 | : 'no-cache, no-store, max-age=0, must-revalidate'; | ||
| 194 | |||
| 195 | my $tm = time(); | ||
| 196 | my $zone = (strftime("%z", localtime($tm)) + 0) / 100; | ||
| 197 | my $loc = setlocale(LC_TIME); | ||
| 198 | setlocale(LC_TIME, 'en_US.UTF-8'); | ||
| 199 | my $GMT = strftime("%a, %d %b %Y %H:%M:%S GMT", localtime($tm - $zone * 3600)); | ||
| 200 | setlocale(LC_TIME, $loc); | ||
| 201 | |||
| 202 | $self->response->headers->{'Expires'} = $GMT; | ||
| 203 | |||
| 204 | $self->post_run(); | ||
| 205 | |||
| 206 | $self->response->timelog($self->timelog); | ||
| 207 | } | ||
| 208 | |||
| 209 | sub break { | ||
| 210 | my ($self, @data) = @_; | ||
| 211 | |||
| 212 | $self->{'__BREAK_PROCESS__'} = 1; | ||
| 213 | return @data; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub form_fields { } | ||
| 217 | |||
| 218 | sub pre_cmd { } | ||
| 219 | |||
| 220 | sub post_cmd { } | ||
| 221 | |||
| 222 | sub default_cmd {throw 'Abstract metod'} | ||
| 223 | |||
| 224 | sub get_cmd { | ||
| 225 | my ($self) = @_; | ||
| 226 | |||
| 227 | my ($path, $cmd, %params); | ||
| 228 | if (defined($self->{'__ROUTING__'})) { | ||
| 229 | my $route = $self->{'__ROUTING__'}->get_current_route($self); | ||
| 230 | |||
| 231 | $path = $route->{'path'} // ''; | ||
| 232 | $cmd = $route->{'cmd'} // ''; | ||
| 233 | |||
| 234 | %params = %{$route->{'args'} // {}}; | ||
| 235 | |||
| 236 | if (length($path) || length($cmd) || !$self->get_option('use_base_routing')) { | ||
| 237 | return ($path, $cmd, %params); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | if ($self->request->uri() =~ /^\/([^?\/]+)(?:\/([^\/?#]+))?/) { | ||
| 242 | ($path, $cmd) = ($1, $2); | ||
| 243 | } else { | ||
| 244 | ($path, $cmd, %params) = $self->default_cmd(); | ||
| 245 | } | ||
| 246 | |||
| 247 | $path = '' unless defined($path); | ||
| 248 | $cmd = '' unless defined($cmd); | ||
| 249 | |||
| 250 | return ($path, $cmd, %params); | ||
| 251 | } | ||
| 252 | |||
| 253 | sub make_cmd { | ||
| 254 | my ($self, $new_cmd, $new_path, @params) = @_; | ||
| 255 | |||
| 256 | my %vars = defined($params[0]) | ||
| 257 | && ref($params[0]) eq 'HASH' ? %{$params[0]} : @params; | ||
| 258 | |||
| 259 | my ($path, $cmd) = $self->get_cmd(); | ||
| 260 | |||
| 261 | $path = uri_escape_utf8($self->_get_new_path($new_path, $path)); | ||
| 262 | $cmd = uri_escape_utf8($self->_get_new_cmd($new_cmd, $cmd)); | ||
| 263 | |||
| 264 | return "/$path/$cmd" | ||
| 265 | . ( | ||
| 266 | %vars | ||
| 267 | ? '?' | ||
| 268 | . join( | ||
| 269 | $self->get_option('link_param_separator', '&'), | ||
| 270 | map {uri_escape_utf8($_) . '=' . uri_escape_utf8($vars{$_})} keys(%vars) | ||
| 271 | ) | ||
| 272 | : '' | ||
| 273 | ); | ||
| 274 | } | ||
| 275 | |||
| 276 | sub _get_new_cmd { | ||
| 277 | my ($self, $new_cmd, $cur_cmd) = @_; | ||
| 278 | |||
| 279 | $cur_cmd = '' unless defined($cur_cmd); | ||
| 280 | |||
| 281 | return defined($new_cmd) ? $new_cmd : $cur_cmd; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub _get_new_path { | ||
| 285 | my ($self, $new_path, $cur_path) = @_; | ||
| 286 | |||
| 287 | $cur_path = '' unless defined($cur_path); | ||
| 288 | |||
| 289 | return defined($new_path) && length($new_path) ? $new_path : $cur_path; | ||
| 290 | } | ||
| 291 | |||
| 292 | sub _escape_filename { | ||
| 293 | my ($self, $filename) = @_; | ||
| 294 | |||
| 295 | $filename =~ s{"}{\\"}g; | ||
| 296 | $filename =~ s{\r}{}g; | ||
| 297 | $filename =~ s{\n}{}g; | ||
| 298 | |||
| 299 | return $filename; | ||
| 300 | } | ||
| 301 | |||
| 302 | sub _catch_internal_server_error { | ||
| 303 | my ($self, $exception) = @_; | ||
| 304 | |||
| 305 | if (my $dir = $self->get_option('error_dump_dir')) { | ||
| 306 | require File::Path; | ||
| 307 | File::Path::make_path($dir); | ||
| 308 | writefile("$dir/dump_" . format_date(curdate(), '%Y%m%d_%H%M%S') . "${$}.html", | ||
| 309 | $self->_exception2html($exception)); | ||
| 310 | $self->response->status(500); | ||
| 311 | $self->response->data(undef); | ||
| 312 | } else { | ||
| 313 | $self->response->status(200); | ||
| 314 | if (($self->request->http_header('Accept') || '') =~ /(application\/json|text\/javascript)/) { | ||
| 315 | $self->response->content_type("$1; charset=UTF-8"); | ||
| 316 | $self->response->data(to_json({error => gettext('Internal Server Error: %s', $exception->message())})); | ||
| 317 | } else { | ||
| 318 | $self->response->data($self->_exception2html($exception)); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | sub _exception2html { | ||
| 324 | my ($self, $exception) = @_; | ||
| 325 | |||
| 326 | my $server = `hostname -f`; | ||
| 327 | chomp($server); | ||
| 328 | |||
| 329 | my $short_dumper = sub { | ||
| 330 | my ($data, $max_depth) = @_; | ||
| 331 | local $Data::Dumper::Maxdepth = $max_depth; | ||
| 332 | local $Data::Dumper::Varname = ''; | ||
| 333 | local $Data::Dumper::Sortkeys = TRUE; | ||
| 334 | my $dtext = Dumper($data); | ||
| 335 | |||
| 336 | $dtext =~ /^(\$\d+ = )/; | ||
| 337 | my $prefix_length = $1 ? length($1) : 0; | ||
| 338 | $dtext =~ s/^.{$prefix_length}//mg; | ||
| 339 | |||
| 340 | $dtext =~ s/\\x\{([a-f0-9]{2,})\}/chr(hex($1))/ge; | ||
| 341 | $dtext =~ s/;$//msg; | ||
| 342 | $dtext =~ s/\n$//msg; | ||
| 343 | utf8::decode($dtext); | ||
| 344 | |||
| 345 | return $dtext; | ||
| 346 | }; | ||
| 347 | |||
| 348 | my $html = | ||
| 349 | '<html>' | ||
| 350 | . '<head>' | ||
| 351 | . '<meta http-equiv="content-type" content="text/html; charset=UTF-8">' | ||
| 352 | . '<title>' | ||
| 353 | . gettext('Fatal error') | ||
| 354 | . '</title>' | ||
| 355 | . '</head>' | ||
| 356 | . '<body bgcolor="#FFFFFF" text="#000000">' | ||
| 357 | |||
| 358 | . '<div style="background-color: #CCFF99; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 359 | . gettext('Server') . ': ' | ||
| 360 | . html_encode($server) . '</h3>' | ||
| 361 | . '<strong>' | ||
| 362 | . gettext('Host') | ||
| 363 | . ':</strong> ' | ||
| 364 | . html_encode($self->request->http_header('host')) . '<br>' | ||
| 365 | . '<strong>' | ||
| 366 | . gettext('Date') | ||
| 367 | . ':</strong> ' | ||
| 368 | . html_encode(format_date(curdate(), '%c')) . '<br>' | ||
| 369 | . '</div>' | ||
| 370 | |||
| 371 | . '<div style="background-color: #FF7777; font-size: 110%; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 372 | . html_encode(ref($exception)) . '</h3>' | ||
| 373 | . '<h4><pre>' | ||
| 374 | . html_encode($exception->{'text'}) | ||
| 375 | . '</pre></h4>' | ||
| 376 | . '<strong>' | ||
| 377 | . gettext('Package') | ||
| 378 | . ':</strong> ' | ||
| 379 | . html_encode($exception->{'package'}) . '<br>' | ||
| 380 | . '<strong>' | ||
| 381 | . gettext('Filename') | ||
| 382 | . ':</strong> ' | ||
| 383 | . html_encode($exception->{'filename'}) . ' (' | ||
| 384 | . gettext('line') . ' ' | ||
| 385 | . html_encode($exception->{'line'}) . ')<br>' | ||
| 386 | . '</div>' | ||
| 387 | |||
| 388 | . '<div style="background-color: #EEAA77; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 389 | . gettext('Request') | ||
| 390 | . ':</h3>' | ||
| 391 | . '<table width="100%">' | ||
| 392 | . '<tr><th valign="top" align="right">' | ||
| 393 | . gettext('Method') | ||
| 394 | . '</th><td>' | ||
| 395 | . html_encode($self->request->method) | ||
| 396 | . '</td></tr>' | ||
| 397 | . '<tr><th valign="top" align="right">' | ||
| 398 | . gettext('URL') | ||
| 399 | . '</th><td>' | ||
| 400 | . html_encode($self->request->url) | ||
| 401 | . '</td></tr>' | ||
| 402 | . join( | ||
| 403 | '', | ||
| 404 | map { | ||
| 405 | '<tr><th valign="top" nowrap="nowrap" align="right">' | ||
| 406 | . html_encode($_->[1]) | ||
| 407 | . '</th><td>' | ||
| 408 | . html_encode($self->request->http_header($_->[0]) || '') | ||
| 409 | . '</td></tr>' | ||
| 410 | } ( | ||
| 411 | [referer => gettext('Referer')], | ||
| 412 | ['user-agent' => gettext('User agent')], | ||
| 413 | ['remote-addr' => gettext('Remote address')], | ||
| 414 | [accept => gettext('Accept')], | ||
| 415 | ['accept-encoding' => gettext('Accept encoding')], | ||
| 416 | ['accept-language' => gettext('Accept languages')], | ||
| 417 | [cookie => gettext('Cookie')] | ||
| 418 | ) | ||
| 419 | ) | ||
| 420 | . '</table>' | ||
| 421 | . '</div>' | ||
| 422 | |||
| 423 | . '<div style="background-color: #FFFACD; padding: 5px 10px; margin: 1px;">' | ||
| 424 | . '<h3>Backtrace:</h3>' | ||
| 425 | . join( | ||
| 426 | '', | ||
| 427 | map { | ||
| 428 | my $level = $_; | ||
| 429 | '<div style="font-family: monospace; margin-bottom: 0.5em;"><strong>' | ||
| 430 | . html_encode($_->{'subroutine'}) | ||
| 431 | . '</strong>(' | ||
| 432 | . '<pre style="margin: 0px 0px 0px 2em; padding: 0px;">' | ||
| 433 | . join("\n\n", map {html_encode($short_dumper->($_, 1)) . ","} @{$_->{'args'}}) | ||
| 434 | . '</pre>' . ') ' | ||
| 435 | . gettext('called at %s line %s', html_encode($_->{'filename'}), $_->{'line'}) | ||
| 436 | . '</div>'; | ||
| 437 | } @{$exception->{'callstack'}} | ||
| 438 | ) | ||
| 439 | . '</div>' | ||
| 440 | |||
| 441 | . '<div style="background-color: #CCCCCC; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 442 | . gettext('Server enviroment') | ||
| 443 | . ':</h3>' | ||
| 444 | . '<table border="1">' | ||
| 445 | . join('', | ||
| 446 | map {'<tr><th align="right">' . html_encode($_) . '</th><td>' . html_encode($ENV{$_} || '') . '</td></tr>'} | ||
| 447 | keys(%ENV)) | ||
| 448 | . '</table>' | ||
| 449 | . '</div>' | ||
| 450 | |||
| 451 | . '<div style="background-color: #EEEEEE; padding: 5px 10px; margin: 1px;">' . '<h3>' | ||
| 452 | . gettext('Application dump') | ||
| 453 | . ':</h3>' . '<pre>' | ||
| 454 | . html_encode($short_dumper->($self)) | ||
| 455 | . '</pre>' | ||
| 456 | . '</div>' | ||
| 457 | |||
| 458 | . '</body>' . '</html>'; | ||
| 459 | } | ||
| 460 | |||
| 461 | TRUE; | ||
| 462 | |||
| 463 | __END__ |