| 1 | package PMLTQ::Suggest::Server; | ||
| 2 | our $AUTHORITY = 'cpan:MATY'; | ||
| 3 | $PMLTQ::Suggest::Server::VERSION = '0.0.3'; | ||
| 4 | use base qw(HTTP::Server::Simple::CGI); | ||
| 5 | use URI; | ||
| 6 | use URI::file; | ||
| 7 | |||
| 8 | use PMLTQ::Suggest::Utils; | ||
| 9 | use PMLTQ::Suggest; | ||
| 10 | |||
| 11 | our $permitted_paths_re = '^(?:)/'; | ||
| 12 | |||
| 13 | sub run { | ||
| 14 | my $self = shift; | ||
| 15 | if ($self->prefork) { | ||
| 16 | $self->SUPER::run(@_, host => $self->host, ipv => 4, max_servers => $self->prefork); | ||
| 17 | } else { | ||
| 18 | $self->SUPER::run(@_); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | sub handle_request { | ||
| 23 | my ($self, $cgi) = @_; | ||
| 24 | eval { | ||
| 25 | my $path = $cgi->path_info(); | ||
| 26 | if ($path eq '/') { | ||
| 27 | servePMLTQ($self,$cgi); | ||
| 28 | } else { | ||
| 29 | notFound($cgi); | ||
| 30 | } | ||
| 31 | }; | ||
| 32 | |||
| 33 | serverError($cgi, $@) if ($@); | ||
| 34 | } | ||
| 35 | |||
| 36 | # Maximum number of servers to prefork | ||
| 37 | sub prefork { | ||
| 38 | my $self = shift; | ||
| 39 | $self->{prefork_child} = $_[0] if scalar @_ > 0; | ||
| 40 | return $self->{prefork_child}; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub net_server { return $_[0]->prefork ? 'Net::Server::PreForkSimple' : undef; } | ||
| 44 | |||
| 45 | sub notFound { | ||
| 46 | my ($cgi)=@_; | ||
| 47 | print "HTTP/1.0 404 Not found\r\n"; | ||
| 48 | print $cgi->header, | ||
| 49 | $cgi->start_html('Not found'), | ||
| 50 | $cgi->h1('Not found'), | ||
| 51 | $cgi->end_html; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub serverError { | ||
| 55 | my ($cgi, $error)=(@_,''); | ||
| 56 | $error =~ tr/\n/ /; | ||
| 57 | $error =~ s/ at .*$//; | ||
| 58 | $error =~ s/\.\.\.propagated.*$//; | ||
| 59 | print STDERR '['.localtime()."] $error\n"; | ||
| 60 | $error =~ s/'\/[^']*\/([^\/']*)'/'$1'/; | ||
| 61 | |||
| 62 | print "HTTP/1.0 500 Internal server error\r\n"; | ||
| 63 | 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 | my ($self,$cgi) = @_; # Net::HTTPServer::Request object | ||
| 73 | my @names = split(/,/,$cgi->param('r')||''); | ||
| 74 | my $paths = $cgi->param('p'); | ||
| 75 | my @paths = $paths ? split(/\|/, $paths) : (); | ||
| 76 | unless (@paths) { | ||
| 77 | print STDERR '['.localtime()."] No path!\n"; | ||
| 78 | return notFound($cgi); | ||
| 79 | } | ||
| 80 | my @positions; | ||
| 81 | foreach my $p (@paths) { | ||
| 82 | my ($path, $goto)=PMLTQ::Suggest::Utils::parse_file_suffix($p); | ||
| 83 | $path = URI->new($path)->canonical->as_string; | ||
| 84 | if ($path=~m{/\.\./} or $path !~ $permitted_paths_re) { | ||
| 85 | print STDERR '['.localtime()."] Path $path not permitted\n"; | ||
| 86 | return notFound($cgi); | ||
| 87 | } elsif (!$goto) { | ||
| 88 | print STDERR '['.localtime()."] Path $p does not contain an address\n"; | ||
| 89 | return notFound($cgi); | ||
| 90 | } | ||
| 91 | push @positions, [$path,$goto]; | ||
| 92 | } | ||
| 93 | my $pmltq; | ||
| 94 | eval { | ||
| 95 | $pmltq = PMLTQ::Suggest::make_pmltq( | ||
| 96 | \@positions, | ||
| 97 | (@names ? (reserved_names => {map {$_=>1} @names}) : ()), | ||
| 98 | verbose => 1 | ||
| 99 | ); | ||
| 100 | }; | ||
| 101 | if (!defined $pmltq) { | ||
| 102 | $@ = "Empty query! Possible error - unable to find node in file" unless $@; | ||
| 103 | die; # send error upper | ||
| 104 | } else { | ||
| 105 | print STDERR '['.localtime()."] Serving PMLTQ for $paths: $pmltq\n"; | ||
| 106 | binmode(select()); | ||
| 107 | Encode::_utf8_off($pmltq); | ||
| 108 | print "HTTP/1.0 200 OK\r\n"; | ||
| 109 | print $cgi->header(-type => 'text/plain', | ||
| 110 | -charset => 'UTF-8', | ||
| 111 | # -Content_length => ((stat($fh))[7]), | ||
| 112 | ); | ||
| 113 | print $pmltq; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; |