blib/lib/PMLTQ/Suggest/Server.pm

filter: Statementstmt 25.3% 19 / 75 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 41.6% 5 / 12 Podpod 42.8% 3 / 7 totaltotal 21.4% 27 / 126 CC 16 SCAR 40.4
CC
16
Cov
18%
servePMLTQ
132.0
handle_request
12.0
run
6.0
CRAP 56.8
« blib/lib/PMLTQ/Suggest/Utils.pm ↑ Summary Build »
1 package PMLTQ::Suggest::Server;
2 1 our $AUTHORITY = 'cpan:MATY';
3 1 $PMLTQ::Suggest::Server::VERSION = '0.0.1';
4 1 ▶use base qw(HTTP::Server::Simple::CGI);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use PMLTQ::Suggest::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PMLTQ::Suggest;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $permitted_paths_re = '^(?:)/';
12  
13 sub run {
14 0 ▶ my $self = shift;
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
15 0 ▶ if ($self->prefork) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prefork) { } 0 0 0
16 0 $self->SUPER::run(@_, host => $self->host, ipv => 4, max_servers => $self->prefork);
17 } else {
18 0 $self->SUPER::run(@_);
19 }
20 }
21  
22 sub handle_request {
23 0 ▶ my ($self, $cgi) = @_;
Subroutine handle_request: not called CC=3 SCAR=24.8
Pod documented
24 0 eval {
25 0 my $path = $cgi->path_info();
26 0 ▶ if ($path eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '/') { } 0 0 0
27 0 servePMLTQ($self,$cgi);
28 } else {
29 0 notFound($cgi);
30 }
31 };
32  
33 0 ▶ serverError($cgi, $@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
34 }
35  
36 # Maximum number of servers to prefork
37 sub prefork {
38 0 ▶ my $self = shift;
Subroutine prefork: not called CC=2 SCAR=17.9
Pod undocumented
39 0 ▶ $self->{prefork_child} = $_[0] if scalar @_ > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
40 0 return $self->{prefork_child};
41 }
42  
43 0 ▶sub net_server { return $_[0]->prefork ? 'Net::Server::PreForkSimple' : undef; }
Subroutine net_server: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->prefork ? : 0 0 0
44  
45 sub notFound {
46 0 ▶ my ($cgi)=@_;
Subroutine notFound: not called CC=1 SCAR=6.9
Pod undocumented
47 0 print "HTTP/1.0 404 Not found\r\n";
48 0 print $cgi->header,
49 $cgi->start_html('Not found'),
50 $cgi->h1('Not found'),
51 $cgi->end_html;
52 }
53  
54 sub serverError {
55 0 ▶ my ($cgi, $error)=(@_,'');
Subroutine serverError: not called CC=1 SCAR=6.9
Pod undocumented
56 0 $error =~ tr/\n/ /;
57 0 $error =~ s/ at .*$//;
58 0 $error =~ s/\.\.\.propagated.*$//;
59 0 print STDERR '['.localtime()."] $error\n";
60 0 $error =~ s/'\/[^']*\/([^\/']*)'/'$1'/;
61  
62 0 print "HTTP/1.0 500 Internal server error\r\n";
63 0 print $cgi->header,
64 $cgi->start_html('Internal server error'),
65 $cgi->h1('Error occurred while processing request!'),
66 "\r\n",
67 $cgi->p($error),
68 $cgi->end_html;
69 }
70  
71 sub servePMLTQ {
72 0 ▶ my ($self,$cgi) = @_; # Net::HTTPServer::Request object
Subroutine servePMLTQ: not called CC=11 SCAR=48.8
Pod undocumented
73 0 ▶ my @names = split(/,/,$cgi->param('r')||'');
Condition0%0/2
$cgi->param('r') || ''
l!l
00
MC/DC0%0/1
$cgi->param('r') || ''
$cgi->param('r')
Truth table0%0/2
A
A= $cgi->param('r')
Aresult
11
00
74 0 my $paths = $cgi->param('p');
75 0 ▶ my @paths = $paths ? split(/\|/, $paths) : ();
Branch: 1 branch
BranchTrueFalseTotal
$paths ? : 0 0 0
76 0 ▶ unless (@paths) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paths) 0 0 0
77 0 print STDERR '['.localtime()."] No path!\n";
78 0 return notFound($cgi);
79 }
80 0 my @positions;
81 0 foreach my $p (@paths) {
82 0 my ($path, $goto)=PMLTQ::Suggest::Utils::parse_file_suffix($p);
83 0 $path = URI->new($path)->canonical->as_string;
84 0 ▶ if ($path=~m{/\.\./} or $path !~ $permitted_paths_re) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ m[/\.\./] or not $path =~ /$permitted_paths_re/) { } 0 0 0
elsif (not $goto) { } 0 0 0
Condition0%0/3
$path =~ m[/\.\./] or not $path =~ /$permitted_paths_re/
l!l&&r!l&&!r
000
MC/DC0%0/2
$path =~ m[/\.\./] or not $path =~ /$permitted_paths_re/
$path =~ m[/\.\./] not $path =~ /$permitted_paths_re/
Truth table0%0/3
A or not(B)
A= $path =~ m[/\.\./]B= not $path =~ /$permitted_paths_re/
ABresult
1X1
011
000
85 0 print STDERR '['.localtime()."] Path $path not permitted\n";
86 0 return notFound($cgi);
87 } elsif (!$goto) {
88 0 print STDERR '['.localtime()."] Path $p does not contain an address\n";
89 0 return notFound($cgi);
90 }
91 0 push @positions, [$path,$goto];
92 }
93 0 my $pmltq;
94 0 eval {
95 $pmltq = PMLTQ::Suggest::make_pmltq(
96 \@positions,
97 0 ▶ (@names ? (reserved_names => {map {$_=>1} @names}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 0 0 0
98 verbose => 1
99 );
100 };
101 0 ▶ if (!defined $pmltq) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pmltq) { } 0 0 0
102 0 ▶ $@ = "Empty query! Possible error - unable to find node in file" unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
103 0 die; # send error upper
104 } else {
105 0 print STDERR '['.localtime()."] Serving PMLTQ for $paths: $pmltq\n";
106 0 binmode(select());
107 0 Encode::_utf8_off($pmltq);
108 0 print "HTTP/1.0 200 OK\r\n";
109 0 print $cgi->header(-type => 'text/plain',
110 -charset => 'UTF-8',
111 # -Content_length => ((stat($fh))[7]),
112 );
113 0 print $pmltq;
114 }
115 }
116  
117 1 1;
« blib/lib/PMLTQ/Suggest/Utils.pm ↑ Summary Build »