Skip to content

Commit

Permalink
Item13592: bring in line with FastCGIEngineContrib
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDaum committed Jul 31, 2015
1 parent f33592d commit 3feac32
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 51 deletions.
43 changes: 23 additions & 20 deletions lib/FCGI/FoswikiWebDAV.pm
Expand Up @@ -18,7 +18,7 @@ use POSIX qw(:signal_h);
use HTTP::WebDAV ();
use HTTP::BasicAuth ();
use Foswiki ();
use Cwd ();
use Cwd ();

our $VERSION = '1.0.0';

Expand All @@ -29,16 +29,19 @@ sub new {
$this->{manager} ||= 'FCGI::FoswikiWebDAVProcManager';
$this->{nproc} ||= 1;
$this->{maxRequests} ||= 100;
$this->{sock} = 0;
$this->{hupRecieved} = 0;
$this->{removeStatusLine} = $args{removeStatusLine};
$this->{sock} = 0;
$this->{hupRecieved} = 0;
$this->{removeStatusLine} = $args{removeStatusLine};
$this->{maxRequests} = $args{max};
$this->{maxSize} = $args{size};
$this->{sizecheckNumRequests} = $args{check};

if ( defined $this->{pidfile} ) {
$this->{pidfile} =~ /^(.*)$/ and $this->{pidfile} = $1;
}

($this->{script}) = $0 =~ /^(.*)$/;
($this->{dir}) = Cwd::cwd() =~ /^(.*)$/;
( $this->{script} ) = $0 =~ /^(.*)$/;
( $this->{dir} ) = Cwd::cwd() =~ /^(.*)$/;

return $this;
}
Expand All @@ -51,21 +54,25 @@ sub run {
or die "Failed to create FastCGI socket: $!";
}

my $r = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $this->{sock},
my $r =
FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $this->{sock},
&FCGI::FAIL_ACCEPT_ON_INTR );
my $manager;

if ($this->{listen}) {
if ( $this->{listen} ) {

$this->fork() if $this->{detach};
eval "use " . $this->{manager} . "; 1";
unless ($@) {
$manager = $this->{manager}->new(
{
client => $this,
n_processes => $this->{nproc},
pid_fname => $this->{pidfile},
quiet => $this->{quiet}
client => $this,
n_processes => $this->{nproc},
pid_fname => $this->{pidfile},
max_size => $this->{maxRequests},
max_requests => $this->{maxSize},
sizecheck_num_requests => $this->{sizecheckNumRequests},
quiet => $this->{quiet}
}
);
$manager->pm_manage();
Expand Down Expand Up @@ -126,17 +133,14 @@ sub run {

# Send response
my $rs = $response->as_string();

# Apache FCGID doesn't support NPH and can't parse off the header
$rs =~ s/^HTTP.*?\n//s if $this->{removeStatusLine};
print $rs;

# check lifetime conditions
my $mtime = ( stat $localSiteCfg )[9];
$this->{maxRequests}--;
if ( $mtime > $lastMTime
|| $this->{hupRecieved}
|| $this->{maxRequests} == 0 )
{
if ( $mtime > $lastMTime || $this->{hupRecieved} ) {
$r->LastCall();
if ($manager) {
kill SIGHUP, $manager->pm_parameter('MANAGER_PID');
Expand Down Expand Up @@ -176,10 +180,9 @@ sub reExec {
my $perl = $Config::Config{perlpath};

chdir $this->{dir}
or die
"FCGI::FoswikiWebDAV::reExec(): Could not restore directory: $!";
or die "FCGI::FoswikiWebDAV::reExec(): Could not restore directory: $!";

exec $perl, '-wT', $this->{script}, map { /^(.*)$/; $1 } @ARGV
exec $perl, $this->{script}, map { /^(.*)$/; $1 } @ARGV
or die "FCGI::FoswikiWebDAV::reExec(): Could not exec(): $!";
}

Expand Down
18 changes: 9 additions & 9 deletions lib/FCGI/FoswikiWebDAVProcManager.pm
Expand Up @@ -4,8 +4,8 @@ package FCGI::FoswikiWebDAVProcManager;
use strict;
use warnings;

use FCGI::ProcManager;
our @ISA = qw( FCGI::ProcManager );
use FCGI::ProcManager::Constrained;
our @ISA = qw( FCGI::ProcManager::Constrained );

sub sig_manager {
my $this = shift;
Expand All @@ -16,27 +16,27 @@ sub sig_manager {
}

sub pm_die {
my ($this, $msg, $n) = @_;
my ( $this, $msg, $n ) = @_;

$msg ||= ''; # protect against error in FCGI.pm
$msg ||= ''; # protect against error in FCGI.pm

if ($this->{client}{hupRecieved}) {
if ( $this->{client}{hupRecieved} ) {
$this->{client}->reExec;
}
else {
$this->SUPER::pm_die($msg, $n);
$this->SUPER::pm_die( $msg, $n );
}
}

sub pm_notify {
my ($this, $msg) = @_;
my ( $this, $msg ) = @_;

return if $this->{quiet};
return if $this->{quiet};
$this->SUPER::pm_notify($msg);
}

sub pm_change_process_name {
my ($this,$name) = @_;
my ( $this, $name ) = @_;

$name =~ s/perl/foswiki-dav/g;
$0 = $name;
Expand Down
40 changes: 22 additions & 18 deletions lib/FCGI/VirtualHostingFoswikiWebDAV.pm
Expand Up @@ -4,29 +4,33 @@ package FCGI::VirtualHostingFoswikiWebDAV;
use strict;
use warnings;

use Foswiki ();
use Foswiki ();
use Foswiki::Contrib::VirtualHostingContrib::VirtualHost ();
use FCGI::FoswikiWebDAV ();
use FCGI::FoswikiWebDAV ();

our @ISA = ('FCGI::FoswikiWebDAV');

sub handleRequest {
my ($this, $request, $response, $auth_provider) = @_;

my $host = $request->header("host");

my $status;
Foswiki::Contrib::VirtualHostingContrib::VirtualHost->run_on(
$host,
sub {
# change the process name during the request
local $0 = sprintf("foswiki-virtualhost-webdav[%s%s]", $host, $request->uri());

$status = $this->SUPER::handleRequest($request, $response, $auth_provider);
}
);

return $status;
my ( $this, $request, $response, $auth_provider ) = @_;

my $host = $request->header("host");
$host =~ s/:.*$//; # strip off port

my $status;
Foswiki::Contrib::VirtualHostingContrib::VirtualHost->run_on(
$host,
sub {
# change the process name during the request
local $0 = sprintf( "foswiki-virtualhost-webdav[%s%s]",
$host, $request->uri() );

$status =
$this->SUPER::handleRequest( $request, $response,
$auth_provider );
}
);

return $status;
}

1;
Expand Down
6 changes: 4 additions & 2 deletions tools/foswiki-dav.init-script
Expand Up @@ -23,14 +23,16 @@ FOSWIKI_FCGI=webdav.fcgi
FOSWIKI_BIND=127.0.0.1:9001
FOSWIKI_CHILDREN=5
FOSWIKI_PIDFILE=/var/run/$NAME.pid
FOSWIKI_TRACE=0
FOSWIKI_FILESYSTEM=Foswiki

# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
. /etc/default/$NAME
fi

FOSWIKI_DAEMON=$FOSWIKI_ROOT/tools/$FOSWIKI_FCGI
FOSWIKI_DAEMON_OPTS="-n $FOSWIKI_CHILDREN -l $FOSWIKI_BIND -p $FOSWIKI_PIDFILE -d -q"
FOSWIKI_DAEMON_OPTS="-n $FOSWIKI_CHILDREN -l $FOSWIKI_BIND -p $FOSWIKI_PIDFILE -d -q -t $FOSWIKI_TRACE -fs $FOSWIKI_FILESYSTEM"

start() {
log_daemon_msg "Starting $DESC" $NAME
Expand Down Expand Up @@ -93,7 +95,7 @@ case "$1" in
status
;;
*)
echo "Usage: $NAME {start|stop|restart}"
echo "Usage: $NAME {start|stop|restart|reload|status}"
exit 1
;;
esac
14 changes: 13 additions & 1 deletion tools/virtualhosts-webdav.fcgi
Expand Up @@ -30,6 +30,7 @@ use HTTP::Response ();
use MIME::Base64 ();
use Getopt::Long ();
use Pod::Usage ();
use FCGI::VirtualHostingFoswikiWebDAV ();

my $detach = 0;
my $fs = 'Foswiki';
Expand All @@ -48,10 +49,16 @@ my $trace = 0;
my $removeStatusLine = 0;

my $isOkay;
my $max;
my $size;
my $check;

my %options = (
'listen|l=s' => \$listen,
'nproc|n=i' => \$nproc,
'max|x=i' => \$max,
'check|c=i' => \$check,
'size|s=i' => \$size,
'pidfile|p=s' => \$pidfile,
'manager|M=s' => \$manager,
'daemon|d' => \$detach,
Expand Down Expand Up @@ -99,7 +106,6 @@ if ( $fs eq 'PlainPlusAttrs' && ! -d $root ) {
}

# Get a daemon, and dispatch the request
use FCGI::VirtualHostingFoswikiWebDAV ();
my $daemon = FCGI::VirtualHostingFoswikiWebDAV->new(
listen => $listen,
nproc => $nproc,
Expand All @@ -108,6 +114,9 @@ my $daemon = FCGI::VirtualHostingFoswikiWebDAV->new(
detach => $detach,
quiet => $quiet,
removeStatusLine => $removeStatusLine,
max => $max,
size => $size,
check => $check,

filesys => 'Filesys::Virtual::' . $fs,
trace => $trace,
Expand Down Expand Up @@ -144,6 +153,9 @@ __DATA__
-n --nproc Number of backends to use, defaults to 1
-p --pidfile File used to write pid to
-M --manager FCGI manager class
-x --max Maximum requests served per server instance
-c --check Number of requests when to check the size of the server
-s --size Maximum memory size of a server before being recycled
-r --rsl Remove status line from responses; required by Apache
-d --daemon Detach from terminal and keeps running as a daemon
-q --quiet Disable notification messages
Expand Down
11 changes: 10 additions & 1 deletion tools/webdav.fcgi
Expand Up @@ -30,6 +30,7 @@ use HTTP::Response ();
use MIME::Base64 ();
use Getopt::Long ();
use Pod::Usage ();
use FCGI::FoswikiWebDAV ();

my $detach = 0;
my $fs = 'Foswiki';
Expand All @@ -47,10 +48,16 @@ my $trace = 0;
my $removeStatusLine = 0;

my $isOkay;
my $max;
my $size;
my $check;

my %options = (
'listen|l=s' => \$listen,
'nproc|n=i' => \$nproc,
'max|x=i' => \$max,
'check|c=i' => \$check,
'size|s=i' => \$size,
'pidfile|p=s' => \$pidfile,
'manager|M=s' => \$manager,
'daemon|d' => \$detach,
Expand Down Expand Up @@ -93,7 +100,6 @@ sub parseURL {
}

# Get a daemon, and dispatch the request
use FCGI::FoswikiWebDAV ();
my $daemon = FCGI::FoswikiWebDAV->new(
listen => $listen,
nproc => $nproc,
Expand All @@ -102,6 +108,9 @@ my $daemon = FCGI::FoswikiWebDAV->new(
detach => $detach,
quiet => $quiet,
removeStatusLine => $removeStatusLine,
max => $max,
size => $size,
check => $check,

filesys => 'Filesys::Virtual::' . $fs,
trace => $trace,
Expand Down

0 comments on commit 3feac32

Please sign in to comment.