2009-08-24 23:01:58 +04:00
|
|
|
#!/usr/bin/perl
|
2013-07-19 18:59:12 +04:00
|
|
|
# Standalone HTTP server for running Bugzilla based on HTTP::Server::Simple and Net::Server
|
|
|
|
# USAGE: perl HTTPServerSimple.pl --conf_file bugzilla.conf
|
|
|
|
# See bugzilla.conf sample in the end of this file
|
2009-08-24 23:01:58 +04:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
2013-07-19 18:59:12 +04:00
|
|
|
BEGIN
|
|
|
|
{
|
2009-08-24 23:01:58 +04:00
|
|
|
require File::Basename;
|
|
|
|
chdir(File::Basename::dirname($0));
|
|
|
|
}
|
|
|
|
|
|
|
|
use lib qw(.);
|
|
|
|
use CGI ();
|
2013-07-19 18:59:12 +04:00
|
|
|
CGI->compile(qw(:cgi -no_xhtml -oldstyle_urls :private_tempfiles :unique_headers SERVER_PUSH :push));
|
2009-08-24 23:01:58 +04:00
|
|
|
$CGI::USE_PARAM_SEMICOLONS = 0;
|
|
|
|
|
2013-07-19 18:59:12 +04:00
|
|
|
# Fake exit() function to only terminate current request
|
|
|
|
my $in_eval = 0;
|
|
|
|
*CORE::GLOBAL::exit = sub
|
|
|
|
{
|
|
|
|
if ($in_eval)
|
|
|
|
{
|
|
|
|
die bless { rc => shift }, 'Bugzilla::HTTPServerSimple::FakeExit';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CORE::exit(@_);
|
|
|
|
}
|
|
|
|
};
|
2009-08-26 22:03:52 +04:00
|
|
|
$SIG{INT} = sub { warn "Terminating"; CORE::exit(); };
|
2013-07-19 18:59:12 +04:00
|
|
|
|
|
|
|
# Create and run
|
|
|
|
my $server = Bugzilla::HTTPServerSimple->new($ARGV[0]);
|
2009-08-24 23:01:58 +04:00
|
|
|
$server->run();
|
|
|
|
|
2013-07-19 18:59:12 +04:00
|
|
|
# HTTP::Server::Simple subclass (the real server)
|
2009-08-24 23:01:58 +04:00
|
|
|
package Bugzilla::HTTPServerSimple;
|
|
|
|
|
2009-08-26 22:03:52 +04:00
|
|
|
use Bugzilla;
|
2009-08-25 18:29:48 +04:00
|
|
|
use Time::HiRes qw(gettimeofday tv_interval);
|
2009-08-24 23:01:58 +04:00
|
|
|
use IO::SendFile qw(sendfile);
|
2013-07-19 18:59:12 +04:00
|
|
|
use POSIX qw(strftime);
|
|
|
|
use LWP::MediaTypes qw(guess_media_type);
|
|
|
|
|
2009-08-24 23:01:58 +04:00
|
|
|
use base qw(HTTP::Server::Simple::CGI);
|
|
|
|
|
2013-07-19 18:59:12 +04:00
|
|
|
# Code cache
|
2009-08-24 23:01:58 +04:00
|
|
|
my %subs = ();
|
|
|
|
|
2013-07-19 18:59:12 +04:00
|
|
|
sub new
|
|
|
|
{
|
|
|
|
my ($class, $conf) = @_;
|
|
|
|
my $self = HTTP::Server::Simple::new($class);
|
|
|
|
$self->{_config} = Bugzilla::NetServerConfigParser->_read_conf($conf);
|
|
|
|
$self->{_config_hash} = {};
|
|
|
|
for (my $i = 0; $i < @{$self->{_config}}; $i += 2)
|
|
|
|
{
|
|
|
|
$self->{_config_hash}->{$self->{_config}->[$i]} ||= $self->{_config}->[$i+1];
|
|
|
|
if ($self->{_config}->[$i] eq 'port')
|
|
|
|
{
|
|
|
|
# Remove first 'port' option (workaround hardcode from HTTP::Server::Simple)
|
|
|
|
splice @{$self->{_config}}, $i, 2;
|
|
|
|
$i -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
$self->SUPER::run(@{$self->{_config}});
|
|
|
|
}
|
|
|
|
|
|
|
|
sub port
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{_config_hash}->{port};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub net_server
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{_config_hash}->{class};
|
|
|
|
}
|
|
|
|
|
2009-08-24 23:01:58 +04:00
|
|
|
sub handle_request
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my ($cgi) = @_;
|
2013-07-19 18:59:12 +04:00
|
|
|
# Set non-parsed-headers CGI mode
|
2009-08-24 23:01:58 +04:00
|
|
|
$cgi->nph(1);
|
|
|
|
$CGI::USE_PARAM_SEMICOLONS = 0;
|
2013-07-19 18:59:12 +04:00
|
|
|
# Determine SCRIPT_FILENAME
|
2009-08-24 23:01:58 +04:00
|
|
|
my $script = $ENV{SCRIPT_FILENAME};
|
|
|
|
$ENV{REQUEST_URI} =~ s!^/*bugs\d*/*!/!iso;
|
|
|
|
unless ($script)
|
|
|
|
{
|
|
|
|
($script) = $ENV{REQUEST_URI} =~ m!/+([^\?\#]*)!so;
|
|
|
|
}
|
|
|
|
$script ||= 'index.cgi';
|
2013-07-19 18:59:12 +04:00
|
|
|
# Serve static files (should be done by nginx, but we support it for completeness)
|
2009-08-24 23:01:58 +04:00
|
|
|
my $fd;
|
|
|
|
$script =~ s!^/*!!so;
|
|
|
|
if (($script !~ /\.cgi$/iso || $script =~ /\//so) && open $fd, '<', $script)
|
|
|
|
{
|
2013-07-19 18:59:12 +04:00
|
|
|
print "HTTP/1.0 200 OK\r\n";
|
|
|
|
print $cgi->header(-type => guess_media_type($script), -Content_length => -s $script);
|
2009-08-24 23:01:58 +04:00
|
|
|
sendfile(fileno(STDOUT), fileno($fd), 0, -s $script);
|
|
|
|
close $fd;
|
2013-07-19 18:59:12 +04:00
|
|
|
print STDERR strftime("[%Y-%m-%d %H:%M:%S]", localtime)." Served $script via sendfile()\n";
|
2009-08-24 23:01:58 +04:00
|
|
|
return 200;
|
|
|
|
}
|
2013-07-19 18:59:12 +04:00
|
|
|
binmode STDOUT, ':utf8';
|
2009-08-26 22:03:52 +04:00
|
|
|
$Bugzilla::_request_cache = {};
|
2013-07-19 18:59:12 +04:00
|
|
|
# Use require() instead of sub caching under NYTProf profiler for more correct reports
|
2009-08-26 22:03:52 +04:00
|
|
|
if ($ENV{NYTPROF} && $INC{'Devel/NYTProf.pm'})
|
|
|
|
{
|
|
|
|
my $start = [gettimeofday];
|
|
|
|
delete $INC{$script};
|
|
|
|
require $script;
|
|
|
|
my $elapsed = tv_interval($start) * 1000;
|
2013-07-19 18:59:12 +04:00
|
|
|
print STDERR strftime("[%Y-%m-%d %H:%M:%S]", localtime)." Served $script via require() in $elapsed ms\n";
|
2009-08-26 22:03:52 +04:00
|
|
|
return 200;
|
|
|
|
}
|
2013-07-19 18:59:12 +04:00
|
|
|
# Simple "FastCGI" implementation - cache *.cgi in subs
|
2009-08-24 23:01:58 +04:00
|
|
|
if (!$subs{$script})
|
|
|
|
{
|
|
|
|
my $content;
|
|
|
|
if (open $fd, "<$script")
|
|
|
|
{
|
|
|
|
local $/ = undef;
|
|
|
|
$content = <$fd>;
|
|
|
|
close $fd;
|
|
|
|
}
|
|
|
|
if ($content)
|
|
|
|
{
|
|
|
|
$subs{$script} = eval "sub { $content }";
|
|
|
|
if ($@)
|
|
|
|
{
|
|
|
|
warn "Error while loading $script:\n$@";
|
|
|
|
return 500;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 18:59:12 +04:00
|
|
|
# Run cached sub
|
2009-08-24 23:01:58 +04:00
|
|
|
if ($subs{$script})
|
|
|
|
{
|
2009-08-25 18:29:48 +04:00
|
|
|
my $start = [gettimeofday];
|
2013-07-19 18:59:12 +04:00
|
|
|
$in_eval = 1;
|
2009-08-24 23:01:58 +04:00
|
|
|
eval { &{$subs{$script}}(); };
|
2013-07-19 18:59:12 +04:00
|
|
|
$in_eval = 0;
|
2009-08-25 20:19:15 +04:00
|
|
|
if ($@ && (!ref($@) || ref($@) ne 'Bugzilla::HTTPServerSimple::FakeExit'))
|
2009-08-24 23:01:58 +04:00
|
|
|
{
|
|
|
|
warn "Error while running $script:\n$@";
|
|
|
|
}
|
2009-08-25 18:29:48 +04:00
|
|
|
my $elapsed = tv_interval($start) * 1000;
|
2013-07-19 18:59:12 +04:00
|
|
|
print STDERR strftime("[%Y-%m-%d %H:%M:%S]", localtime)." Served $script in $elapsed ms\n";
|
2009-08-24 23:01:58 +04:00
|
|
|
}
|
|
|
|
return 404;
|
|
|
|
}
|
|
|
|
|
2013-07-19 18:59:12 +04:00
|
|
|
# Override bad HTTP::Server::Simple::parse_headers implementation with a good one
|
2009-08-26 22:03:52 +04:00
|
|
|
sub parse_headers
|
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
my @headers;
|
|
|
|
my $chunk;
|
|
|
|
while ($chunk = <STDIN>)
|
|
|
|
{
|
|
|
|
$chunk =~ s/[\r\l\n\s]+$//so;
|
2013-07-19 18:59:12 +04:00
|
|
|
if ($chunk =~ /^([^()<>\@,;:\\"\/\[\]?={} \t]+):\s*(.*)/i)
|
|
|
|
{
|
2009-08-26 22:03:52 +04:00
|
|
|
push @headers, $1 => $2;
|
|
|
|
}
|
|
|
|
last if $chunk =~ /^$/so;
|
|
|
|
}
|
|
|
|
return \@headers;
|
|
|
|
}
|
|
|
|
|
2013-07-19 18:59:12 +04:00
|
|
|
# Net::Server fake subclass used to call _read_conf()
|
|
|
|
package Bugzilla::NetServerConfigParser;
|
|
|
|
|
|
|
|
use base 'Net::Server';
|
|
|
|
|
|
|
|
# _read_conf() could call fatal() in case of parse error
|
|
|
|
sub fatal
|
|
|
|
{
|
|
|
|
shift;
|
|
|
|
die @_;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Fake "exit" exception class
|
2009-08-25 20:19:15 +04:00
|
|
|
package Bugzilla::HTTPServerSimple::FakeExit;
|
|
|
|
|
2009-08-24 23:01:58 +04:00
|
|
|
1;
|
|
|
|
__END__
|