| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 27 | 0 | servePMLTQ($self,$cgi); | |||||||||||||||||||||||||||||||
| 28 | } else { | ||||||||||||||||||||||||||||||||
| 29 | 0 | notFound($cgi); | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | }; | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | serverError($cgi, $@) if ($@); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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') || ''
MC/DC0%0/1
$cgi->param('r') || ''
$cgi->param('r')
Truth table0%0/2
A
A= $cgi->param('r')
| |||||||||||||||||||||||||||||||||
| 74 | 0 | my $paths = $cgi->param('p'); | |||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my @paths = $paths ? split(/\|/, $paths) : (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | unless (@paths) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$path =~ m[/\.\./] or not $path =~ /$permitted_paths_re/
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/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 98 | verbose => 1 | ||||||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if (!defined $pmltq) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $@ = "Empty query! Possible error - unable to find node in file" unless $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||