App/Request.pm

filter: Statementstmt 17.5% 38 / 216 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 20.6% 6 / 29 Podpod 4.3% 1 / 23 totaltotal 11.9% 45 / 378 CC 50 SCAR 40.5
CC
50
Cov
12%
handle
140.0
check_params
132.0
get_through
56.0
CRAP 57.3
« App/Config.pm ↑ Summary App/Lib/IO.pm »
1 package Web::App::Request;
2 # $Id: Request.pm,v 1.13 2009/03/29 10:01:05 apla Exp $
3  
4 1 ▶use Class::Easy::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Encode qw/encode decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Web::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use CGI::Minimal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 has 'params', is => 'rw';
13 1 has 'screen', is => 'rw';
14 1 has 'processors', is => 'rw';
15 1 has 'presentation', is => 'rw';
16 1 has 'headers_sent', is => 'rw';
17  
18 1 has 'data_available';
19 1 has 'path_info', is => 'rw';
20 1 has 'base_uri', is => 'rw';
21  
22 1 has 'dir_info', is => 'rw';
23 1 has 'file_name', is => 'rw';
24 1 has 'file_extension', is => 'rw';
25  
26 1 has 'redirected', is => 'rw', default => 0;
27  
28 1 has 'unparsed_uri', is => 'rw';
29 1 has 'path', is => 'rw';
30  
31 1 has 'uri', is => 'rw';
32 1 has 'host', is => 'rw';
33  
34 1 has 'error_count', is => 'rw';
35  
36 1 has 'type', is => 'rw'; # CGI or XHR
37  
38 1 has 'screen_matches', is => 'rw';
39  
40 0 ▶sub preload {
Subroutine preload: not called CC=3 SCAR=24.8
Pod undocumented
41 0 my $class = shift;
42 0 my $app = shift;
43
44 0 my $pack = &detect_package;
45
46 0 ▶ try_to_use ($pack) || die;
Branch: 1 branch
BranchTrueFalseTotal
try_to_use($pack) or die 0 0 0
47
48 0 ▶ $pack->_preload ($app)
Branch: 1 branch
BranchTrueFalseTotal
if $pack->can("_preload") 0 0 0
49 if $pack->can ('_preload');
50
51 }
52  
53 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
54 0 my $class = shift;
55 0 my $app = shift;
56
57 0 my $request = {
58 processors => [],
59 presenter => {},
60 };
61
62 0 debug ">>>>>>>>>>>>>>>> request handling <<<<<<<<<<<<<<<<";
63
64 0 my $pack = &detect_package;
65
66 0 ▶ try_to_use ($pack) || die;
Branch: 1 branch
BranchTrueFalseTotal
try_to_use($pack) or die 0 0 0
67
68 0 bless $request, $pack;
69
70 0 ▶ $request->_init ($app)
Branch: 1 branch
BranchTrueFalseTotal
if $request->can("_init") 0 0 0
71 if $request->can ('_init');
72
73 0 return $request;
74 }
75  
76 0 ▶sub process {
Subroutine process: not called CC=6 SCAR=37.4
Pod undocumented
77 0 my $self = shift;
78
79 0 my $app = Web::App::Core->instance;
80
81 0 my $response = Web::App::Response->new;
82
83 0 my $screen_class = $app->screen_class;
84
85 0 my $screen = $screen_class->for_request ($self);
86
87 0 ▶ unless (defined $screen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $screen) 0 0 0
88 0 $screen = $screen_class->for_code (404);
89 0 ▶ unless (defined $screen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $screen) 0 0 0
90 # we must have check for main screen during Screen init procedure
91 # if not, this error appears on most error screens
92 0 $screen = $screen_class->main_screen;
93 }
94 }
95
96 0 ▶ if ($screen->auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($screen->auth) 0 0 0
97 0 my $session = Web::App::Session->new ($self);
98 0 ▶ $screen = $screen_class->for_code (403)
Branch: 1 branch
BranchTrueFalseTotal
unless $session->authorized($screen) 0 0 0
99 unless $session->authorized ($screen);
100
101 0 return $self->present_and_transmit;
102 }
103
104 0 my $commands = $screen->commands;
105
106 # TODO: coroutines
107
108 0 my $get_through = "get_through_parallel";
109 0 if (1 || $app->config->{no_coro}) {
110 0 $get_through = "get_through";
111 }
112
113 # real request state change occurs if codes
114 # 302/303, 401, 403, 404, 5xx received
115 0 my $http_code = $self->$get_through ($screen->commands);
116
117 0 ▶ if ($http_code >= 300) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_code >= 300) 0 0 0
118 # sometimes error screens have additional processing
119 0 $screen = $screen_class->for_code ($response->http_code);
120
121 # if we have error even when processing error screen, god bless america
122 0 $self->$get_through ($screen->commands);
123 }
124
125 0 $self->present_and_transmit;
126 }
127  
128 0 ▶sub get_through { # screen queue
Subroutine get_through: not called CC=7 SCAR=40.3
Pod undocumented
129 0 my $self = shift;
130 0 my $queue = shift;
131
132 0 my $response_data = $self->response->data;
133
134 0 my $processing = $queue;
135
136 0 ▶ if (ref $queue->[0] ne 'ARRAY') { # we assume command objects
Branch: 1 branch
BranchTrueFalseTotal
if (ref $queue->[0] ne "ARRAY") 0 0 0
137 0 $processing = [$queue];
138 }
139
140 0 foreach my $processing_queue (@$processing) {
141 0 foreach my $command (@$processing_queue) {
142 0 my ($http_code, $data) = ($command->run);
143 0 ▶ $response_data->{$command->response_slot} = $data
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 0 0
144 if defined $data;
145
146 0 ▶ return $http_code
Branch: 1 branch
BranchTrueFalseTotal
if $http_code != 200 and $command->important 0 0 0
Condition0%0/3
$http_code != 200 and $command->important
!ll&&!rl&&r
000
MC/DC0%0/2
$http_code != 200 and $command->important
$http_code != 200 $command->important
Truth table0%0/3
A and B
A= $http_code != 200B= $command->important
ABresult
0X0
100
111
147 if $http_code != 200 and $command->important;
148 }
149 }
150
151 0 return 200;
152 }
153  
154 0 ▶sub present_and_transmit {
Subroutine present_and_transmit: not called CC=1 SCAR=6.9
Pod undocumented
155 0 my $self = shift;
156
157 0 my $document = $self->presentation ($self->response);
158
159 0 $self->transmit ($document);
160 }
161  
162 0 ▶sub detect_package {
Subroutine detect_package: not called CC=3 SCAR=24.8
Pod undocumented
163 0 my $pack = 'Web::App::Request';
164 # environment is our way to check for available request modes
165 0 ▶ if (exists $ENV{MOD_PERL}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{'MOD_PERL'}) { } 0 0 0
elsif (exists $ENV{'QUERY_STRING'}) { } 0 0 0
166 0 $pack = 'Web::App::Request::ModPerl';
167 } elsif (exists $ENV{QUERY_STRING}) {
168 0 $pack = 'Web::App::Request::CGI';
169 } else {
170 0 critical "unknown request type";
171 }
172
173 0 return $pack;
174 }
175  
176 0 ▶sub done_status {
Subroutine done_status: not called CC=1 SCAR=6.9
Pod undocumented
177 0 1;
178 }
179  
180 0 ▶sub redirect_status {
Subroutine redirect_status: not called CC=1 SCAR=6.9
Pod undocumented
181 0 1;
182 }
183  
184 0 ▶sub param {
Subroutine param: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my $self = shift;
186
187 0 return $self->params->param (@_);
188 }
189  
190 0 ▶sub check_params {
Subroutine check_params: not called CC=11 SCAR=48.8
Pod undocumented
191 0 my $class = shift;
192 0 my $app = shift;
193 0 my $params = shift;
194  
195 # этот метод проверяет данные в Apache::Request и переносит их
196 # в данные для презентации. если данные не соответсвуют описанию,
197 # то стек процессоров должен быть очищен.
198
199 0 my $self = $app->request;
200
201 0 my $fields = $self->screen->params;
202
203 # use Data::Dumper;
204 # die Dumper $self->screen_config
205 # unless scalar keys %$fields;
206
207 0 foreach my $field (@$fields) {
208
209 # warn "processing field: $field_name\n";
210
211 0 my $required = $field->{'required'};
212 0 my $type = $field->{'type'};
213 0 my $name = $field->{'name'};
214
215 0 my $regexp = $self->get_regexp_for_type ($type);
216
217 0 ▶ if ($required and (
Branch: 1 branch
BranchTrueFalseTotal
if ($required and !exists $self->params->{$name} || ref $self->params->{$name} ne "ARRAY") 0 0 0
Condition0%0/6
$required and !exists $self->params->{$name} || ref $self->params->{$name} ne "ARRAY"
!ll&&!rl&&r
000
not exists $self->params->{$name} or ref $self->params->{$name} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/3
$required and !exists $self->params->{$name} || ref $self->params->{$name} ne "ARRAY"
$required not exists $self->params->{$name} ref $self->params->{$name} ne "ARRAY"
Truth table0%0/4
A and not(B) or C
A= $requiredB= not exists $self->params->{$name}C= ref $self->params->{$name} ne "ARRAY"
ABCresult
0XX0
1000
11X1
1011
218 not exists ($self->params->{$name})
219 or ref $self->params->{$name} ne 'ARRAY'
220 )) {
221 0 $self->empty_param ($field);
222 }
223
224 0 my $values = $self->params->{$name};
225 0 foreach my $counter (0 .. $#$values) {
226
227 0 my $value = $values->[$counter];
228
229 0 local $1;
230
231 0 ▶ if (!$value or $value !~ /$regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value and $value =~ /$regexp/) 0 0 0
Condition0%0/3
$value and $value =~ /$regexp/
!ll&&!rl&&r
000
MC/DC0%0/2
$value and $value =~ /$regexp/
$value $value =~ /$regexp/
Truth table0%0/3
A and B
A= $valueB= $value =~ /$regexp/
ABresult
0X0
100
111
232 0 $self->invalid_param ($field);
233 0 last;
234 }
235
236 0 $values->[$counter] = $1;
237
238 0 ▶ last unless defined $field->{'multi'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field->{"multi"} 0 0 0
239 }
240
241 } # foreach
242
243 $app->clear_process_queue
244 0 ▶ if $self->error_count > 0 and !$params->{'no-queue-cleanup'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->error_count > 0 and not $params->{"no-queue-cleanup"} 0 0 0
Condition0%0/3
$self->error_count > 0 and not $params->{"no-queue-cleanup"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->error_count > 0 and not $params->{"no-queue-cleanup"}
$self->error_count > 0 not $params->{"no-queue-cleanup"}
Truth table0%0/3
A and not(B)
A= $self->error_count > 0B= not $params->{"no-queue-cleanup"}
ABresult
0X0
100
111
245
246 #warn Dumper ($fields), "\n"
247 # if $error_count > 0;
248
249 0 return;
250
251 } # check_form_values
252  
253  
254 0 ▶sub next_processor {
Subroutine next_processor: not called CC=1 SCAR=6.9
Pod undocumented
255 0 my $self = shift;
256
257 0 my $processor = shift @{$self->{processors}};
258 0 return $processor;
259 }
260  
261 0 ▶sub cgi {
Subroutine cgi: not called CC=1 SCAR=6.9
Pod undocumented
262 0 shift->{'params'};
263 }
264  
265 # любые значения формы должны быть разобраны следующим образом
266 # тогда можно вывести два списка: поля, блокирующие прохождение формы
267 # и поля, которые попросту не будут учтены при ее использовании
268 # необходимость пропущенное значение неправильное значение
269 # необходимо absent, blocker blocker
270 # по желанию absent invalid
271  
272 0 ▶sub invalid_param {
Subroutine invalid_param: not called CC=1 SCAR=6.9
Pod undocumented
273 0 my $self = shift;
274 0 $self->param_error (shift, 'WRONG');
275 }
276  
277 0 ▶sub empty_param {
Subroutine empty_param: not called CC=1 SCAR=6.9
Pod undocumented
278 0 my $self = shift;
279  
280 0 $self->param_error (shift, 'EMPTY');
281 }
282  
283 0 ▶sub duplicate_param {
Subroutine duplicate_param: not called CC=1 SCAR=6.9
Pod undocumented
284 0 my $self = shift;
285 0 $self->param_error (shift, 'DUPLICATE');
286
287 }
288  
289 0 ▶sub param_error {
Subroutine param_error: not called CC=2 SCAR=17.9
Pod undocumented
290 0 my $self = shift;
291 0 my $field = shift;
292 0 my $code = shift;
293
294 0 my $app = Web::App->app;
295
296 0 my $var = $app->var;
297
298 0 $self->{error_count} ++;
299
300 $var->{errors}->{$field->{name}} = [
301 0 ▶ $field->{required} ? 'required' : 'optional',
Branch: 1 branch
BranchTrueFalseTotal
$field->{'required'} ? : 0 0 0
302 $code
303 ];
304
305 }
306  
307 0 ▶sub add_error_reason {
Subroutine add_error_reason: not called CC=1 SCAR=6.9
Pod undocumented
308 0 my $self = shift;
309 0 my $code = shift;
310 0 my $details = shift;
311
312 0 my $app = Web::App->instance;
313 0 push @{$app->request->params->{'error-reasons'}}, $code;
314 0 log_error ("'$code': $details");
315 }
316  
317 0 ▶sub get_regexp_for_type {
Subroutine get_regexp_for_type: not called CC=5 SCAR=34.0
Pod undocumented
318 0 my $self = shift;
319 0 my $type = shift;
320
321 0 ▶ critical "can't get type from undefined or empty string"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $type or $type eq "" 0 0 0
Condition0%0/3
not defined $type or $type eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $type or $type eq ""
not defined $type $type eq ""
Truth table0%0/3
not(A) or B
A= not defined $typeB= $type eq ""
ABresult
1X1
011
000
322 if not defined $type or $type eq '';
323
324 0 ▶ if ($type =~ /^regexp:(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /^regexp:(.*)$/) { } 0 0 0
elsif ($type eq 'email') { } 0 0 0
325 0 return qr/^($1)$/;
326 } elsif ($type eq 'email') {
327 0 return qr/^([\040-\176]+\@[-A-Za-z0-9.]+\.[A-Za-z]+)$/;
328 } else {
329 0 critical "'$type' is not known";
330 }
331 }
332  
333 0 ▶sub status {
Subroutine status: not called CC=1 SCAR=6.9
Pod undocumented
334 0 return 1;
335 }
336  
337 0 ▶sub CGI::Minimal::fix_params {
Subroutine fix_params: not called CC=6 SCAR=37.4
Pod undocumented
338 0 my $query = shift;
339
340 0 foreach my $form_field (($query->param)) {
341 0 my @values = ();
342 0 ▶ next if scalar grep {$_ ne ''} ($query->param_filename ($form_field));
Branch: 1 branch
BranchTrueFalseTotal
if scalar grep({$_ ne "";} $query->param_filename($form_field)) 0 0 0
343 0 foreach my $raw_value (($query->param ($form_field))) {
344 # try to decode
345 0 my $decoded;
346 0 local $@;
347 0 eval {$decoded = decode_utf8 ($raw_value)};
348 0 ▶ if (defined $decoded and not $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decoded and not $@) 0 0 0
Condition0%0/3
defined $decoded and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
defined $decoded and not $@
defined $decoded not $@
Truth table0%0/3
A and not(B)
A= defined $decodedB= not $@
ABresult
0X0
100
111
349 0 push @values, $decoded;
350 0 next;
351 }
352 0 push @values, $raw_value;
353 }
354 0 $query->param ({$form_field => \@values});
355 0 $query->{$form_field} = \@values;
356 }
357
358 }
359  
360 0 ▶sub handle {
Subroutine handle: not called CC=13 SCAR=49.4
Pod undocumented
361 0 my $self = shift;
362 0 my $app = shift;
363
364 0 my $config = $app->config;
365
366 0 my $path = $self->path;
367
368 0 $path =~ s/^\///;
369 0 debug "request path_info is: '$path'";
370
371 0 my ($screen, $path_info, $matches) = $config->screen_from_request ($path);
372
373 0 ▶ $path_info =~ s/\/\//\//sg
Branch: 1 branch
BranchTrueFalseTotal
if defined $path_info 0 0 0
374 if defined $path_info;
375  
376 0 my ($dir_info, $file_name, $file_extension);
377 0 ▶ ($dir_info, $file_name, $file_extension) = ($path_info =~ /^(?:(.*)\/+)?([^\/]+)\.([^\.\/]+)$/s)
Branch: 1 branch
BranchTrueFalseTotal
if defined $path_info 0 0 0
378 if defined $path_info;
379
380 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
381 0 debug "path_info '$path_info', dir_info '$dir_info', file_name '$file_name', file_extension '$file_extension'";
382 1 ▶ use warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
383
384 0 my $screen_name = $screen->id;
385
386 0 ▶ $screen_name ne ''
Branch: 2 branches
BranchTrueFalseTotal
$screen_name ne '' ? : 0 0 0
scalar @$matches ? : 0 0 0
387 ? debug 'this is request for screen: ' . $screen_name . (
388 scalar @$matches
389 ? ' [' . join (', ', @$matches) . ']'
390 : ''
391 )
392 : debug 'this is request for main system screen with empty id';
393
394 0 my $t = timer ('CGI::Minimal init');
395
396 0 CGI::Minimal::reset_globals;
397 # CGI::Minimal::allow_hybrid_post_get (1);
398 0 CGI::Minimal::max_read_size ($screen->request->{'max-size'});
399 0 my $query = CGI::Minimal->new;
400  
401 0 $t->lap ('fixups');
402
403 0 $query->fix_params;
404
405 0 ▶ if (scalar $query->param) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $query->param) 0 0 0
406 0 $self->{data_available} = 1;
407 }
408
409 # now screen id defined. we must check for default values
410 0 foreach my $screen_param (@{$screen->params}) {
411 $query->{$screen_param->{'name'}} = $screen_param->{'default'}
412 if not exists $query->{$screen_param->{'name'}}
413 0 ▶ and $#{$screen_param->{'default'}} > -1;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $$query{$screen_param->{"name"}} and $#{$$screen_param{"default"};} > -1 0 0 0
Condition0%0/3
not exists $$query{$screen_param->{"name"}} and $#{$$screen_param{"default"};} > -1
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $$query{$screen_param->{"name"}} and $#{$$screen_param{"default"};} > -1
not exists $$query{$screen_param->{"name"}} $#{$$screen_param{"default"};} > -1
Truth table0%0/3
not(A) and B
A= not exists $$query{$screen_param->{"name"}}B= $#{$$screen_param{"default"};} > -1
ABresult
0X0
100
111
414 }
415
416 0 $t->lap ('field values from Class::Easy');
417
418 0 $self->set_field_values (
419 dir_info => $dir_info,
420 file_name => $file_name,
421 file_extension => $file_extension,
422 path_info => $path_info,
423 screen => $screen,
424 params => $query,
425 screen_matches => $matches
426 );
427
428 0 $t->end;
429
430 0 $self->type ('CGI');
431
432 0 ▶ return unless $self->can ('incoming_headers');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("incoming_headers") 0 0 0
433
434 0 my $headers = $self->incoming_headers;
435
436 0 ▶ return unless $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless $headers 0 0 0
437
438 0 my $xhr_header = 'X-Request-Type';
439 0 ▶ return unless $headers->{$xhr_header} and $headers->{$xhr_header} eq 'XHR';
Branch: 1 branch
BranchTrueFalseTotal
unless $headers->{$xhr_header} and $headers->{$xhr_header} eq "XHR" 0 0 0
Condition0%0/3
$headers->{$xhr_header} and $headers->{$xhr_header} eq "XHR"
!ll&&!rl&&r
000
MC/DC0%0/2
$headers->{$xhr_header} and $headers->{$xhr_header} eq "XHR"
$headers->{$xhr_header} $headers->{$xhr_header} eq "XHR"
Truth table0%0/3
A and B
A= $headers->{$xhr_header}B= $headers->{$xhr_header} eq "XHR"
ABresult
0X0
100
111
440
441 0 debug "XHR request detected";
442
443 0 $self->type ('XHR');
444
445 }
446  
447 0 ▶sub send_content {
Subroutine send_content: not called CC=1 SCAR=6.9
Pod undocumented
448 0 my $self = shift;
449 0 my $content = shift;
450
451 0 debug "content output";
452
453 # fix for "Wide characters to print"
454 0 binmode STDOUT, ":utf8";
455 0 utf8::decode ($content);
456 0 $| = 1;
457
458 0 print $content;
459 }
460  
461 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
462 0 my $self = shift;
463
464 0 return {%$self};
465 }
466  
467 1 1;
« App/Config.pm ↑ Summary App/Lib/IO.pm »