QBit/WebInterface/Routing.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 213 SCAR 63.1
CC
23
Cov
0%
build_response
420.0
exception_handling
12.0
import
6.0
CRAP 552.0
1 package QBit::WebInterface::Routing;
2 $QBit::WebInterface::Routing::VERSION = '0.008';
3 use POSIX qw(strftime setlocale LC_TIME);
4  
5 use qbit;
6  
7 use QBit::WebInterface::Routing::Routes;
8 use QBit::WebInterface::Response;
9  
10 use base qw(QBit::WebInterface QBit::Application);
11  
12 sub import {
13 my ($package, %opts) = @_;
14  
15 $package->SUPER::import(%opts);
16  
17 my $package_wi = caller(0);
18  
19 die gettext('Use only in QBit::WebInterface and QBit::Application descendant')
20 unless $package_wi->isa('QBit::WebInterface')
21 && $package_wi->isa('QBit::Application');
22  
23 {
24 no strict 'refs';
25  
26 *{"${package_wi}::build_response"} = \&build_response;
27 *{"${package_wi}::routing"} = \&routing;
28 *{"${package_wi}::exception_handling"} = \&exception_handling;
29 }
30 }
31  
32 sub routing {
33 my ($self, %opts) = @_;
34  
35 $self->{'__ROUTING__'} = QBit::WebInterface::Routing::Routes->new(%opts);
36  
37 return $self->{'__ROUTING__'};
38 }
39  
40 sub build_response {
41 my ($self) = @_;
42  
43 $self->pre_run();
44  
45 throw gettext('No request object') unless $self->request;
46 $self->response(QBit::WebInterface::Response->new());
47  
48 my $cmds = $self->get_cmds();
49  
50 my $path = '';
51 my $cmd = '';
52 my %params = ();
53  
54 if ($self->{'__ROUTING__'}) {
55 my $route = $self->{'__ROUTING__'}->get_current_route($self);
56  
57 if (exists($route->{'handler'})) {
58 $path = '__HANDLER_PATH__';
59 $cmd = '__HANDLER_CMD__';
60  
61 my $package = $self->get_option('controller_class', 'QBit::WebInterface::Controller');
62  
63 my $imported = $self->{'__IMPORTED__'}{$package};
64  
65 foreach my $p (keys(%$cmds)) {
66 last if $imported;
67  
68 foreach my $c (keys(%{$cmds->{$p}})) {
69 if ($cmds->{$p}{$c}{'package'} eq $package) {
70 $imported = TRUE;
71  
72 last;
73 }
74 }
75 }
76  
77 unless ($imported) {
78 my $req_package = $package . '.pm';
79 $req_package =~ s/::/\//g;
80  
81 require $req_package;
82  
83 $package->import();
84  
85 $self->{'__IMPORTED__'}{$package} = TRUE;
86 }
87  
88 $cmds->{$path}{$cmd} = {
89 'package' => $package,
90 'sub' => $route->{'handler'},
91 'type' => $route->{'type'} // 'CMD',
92 'process_method' => $route->{'process_method'},
93 'attrs' => {map {$_ => TRUE} @{$route->{'attrs'} // []}}
94 };
95 } else {
96 $path = $route->{'path'} // '';
97 $cmd = $route->{'cmd'} // '';
98 }
99  
100 %params = %{$route->{'args'} // {}};
101 }
102  
103 if ($self->get_option('use_base_routing') && !(length($path) || length($cmd))) {
104 ($path, $cmd) = $self->get_cmd();
105  
106 $cmd = $cmds->{$path}{'__DEFAULT__'}{'name'} if $cmd eq '';
107 $cmd = '' unless defined($cmd);
108 }
109  
110 $self->set_option(cur_cmd => $cmd);
111 $self->set_option(cur_cmdpath => $path);
112  
113 if ($self->{'__EXCEPTION_IN_ROUTING__'}) {
114 #nothing do...
115 } elsif (exists($cmds->{$path}{$cmd})) {
116 try {
117 my $cmd = $cmds->{$path}{$cmd};
118  
119 my $controller = $cmd->{'package'}->new(
120 app => $self,
121 path => $path,
122 attrs => $cmd->{'attrs'}
123 );
124  
125 $self->{'__BREAK_PROCESS__'} = 0;
126 $self->pre_cmd($controller);
127  
128 unless ($self->{'__BREAK_PROCESS__'}) {
129 $controller->{'__BREAK_CMD__'} = FALSE;
130 $controller->pre_cmd() if $controller->can('pre_cmd');
131  
132 unless ($controller->{'__BREAK_CMD__'}) {
133 if ($controller->attrs()->{'SAFE'}) {
134 throw Exception::WebInterface::Controller::CSRF gettext('CSRF has been detected')
135 unless $controller->check_anti_csrf_token(
136 $self->request->param(sign => $params{'sign'} // ''),
137 url => $self->get_option('cur_cmdpath') . '/' . $self->get_option('cur_cmd'));
138 }
139  
140 my @data = $cmd->{'sub'}($controller, %params);
141 if (defined(my $method = $cmd->{'process_method'})) {
142 $controller->$method(@data);
143 }
144 }
145 }
146  
147 $self->post_cmd();
148 }
149 catch Exception::Denied with {
150 $self->response->status(403);
151 $self->response->data(undef);
152 }
153 catch Exception::Request::UnknownMethod with {
154 $self->response->status(400);
155 $self->response->data(undef);
156 }
157 catch {
158 $self->exception_handling(shift);
159 };
160 } else {
161 $self->response->status(404);
162 }
163  
164 my $ua = $self->request->http_header('User-Agent');
165 $self->response->headers->{'Pragma'} = ($ua =~ /MSIE/) ? 'public' : 'no-cache';
166  
167 $self->response->headers->{'Cache-Control'} =
168 ($ua =~ /MSIE/)
169 ? 'must-revalidate, post-check=0, pre-check=0'
170 : 'no-cache, no-store, max-age=0, must-revalidate';
171  
172 my $tm = time();
173 my $zone = (strftime("%z", localtime($tm)) + 0) / 100;
174 my $loc = setlocale(LC_TIME);
175 setlocale(LC_TIME, 'en_US.UTF-8');
176 my $GMT = strftime("%a, %d %b %Y %H:%M:%S GMT", localtime($tm - $zone * 3600));
177 setlocale(LC_TIME, $loc);
178  
179 $self->response->headers->{'Expires'} = $GMT;
180  
181 $self->post_run();
182  
183 $self->response->timelog($self->timelog);
184 }
185  
186 sub exception_handling {
187 my ($self, $exception) = @_;
188  
189 if (my $dir = $self->get_option('error_dump_dir')) {
190 require File::Path;
191 File::Path::make_path($dir);
192 writefile("$dir/dump_" . format_date(curdate(), '%Y%m%d_%H%M%S') . "${$}.html",
193 $self->_exception2html($exception));
194 $self->response->status(500);
195 $self->response->data(undef);
196 } else {
197 if (($self->request->http_header('Accept') || '') =~ /(application\/json|text\/javascript)/) {
198 $self->response->content_type("$1; charset=UTF-8");
199 $self->response->data(to_json({error => $exception->message()}));
200 } else {
201 $self->response->data($self->_exception2html($exception));
202 }
203 }
204 }
205  
206 TRUE;
207  
208 __END__