URL handlers are now completely replaced by Middleware

This commit is contained in:
Patrick Donelan 2010-04-14 16:25:10 -04:00
parent 9b4e67b828
commit 30a2c09a36
11 changed files with 53 additions and 414 deletions

View file

@ -45,6 +45,7 @@ sub call {
} catch {
# We don't have a logger object, so for now just warn() the error
warn "Unable to instantiate WebGUI::Session - $_";
return; # make sure $session assignment is undef
};
if ( !$session ) {
@ -85,7 +86,11 @@ sub call {
# Close the Session
$env->{'webgui.session'}->close();
#memory_cycle_ok( $env->{'webgui.session'} );
delete $env->{'webgui.session'};
#use Test::Memory::Cycle;
#memory_cycle_ok( $env );
}
);
}

View file

@ -0,0 +1,34 @@
package WebGUI::Middleware::Snoop;
use strict;
use parent qw(Plack::Middleware);
=head1 NAME
WebGUI::Middleware::Snoop - sample middleware port of WebGUI::URL::Snoop
=head1 DESCRIPTION
This is PSGI middleware for WebGUI.
It was ported from L<WebGUI::URL::Snoop>, back when we still had URL handlers.
L<WebGUI::URL::Snoop> described itself as "A URL handler that should never be called."
You might find this middleware useful as a template for creating other simple classes.
=cut
sub call {
my $self = shift;
my $env = shift;
my $path = $env->{PATH_INFO};
if ($path =~ qr{^/abcdefghijklmnopqrstuvwxyz$}) {
my $snoop = q|<html><head><title>Snoopy</title></head><body><div style="width: 600px; padding: 200px;">&#87;&#104;&#121;&#32;&#119;&#111;&#117;&#108;&#100;&#32;&#121;&#111;&#117;&#32;&#116;&#121;&#112;&#101;&#32;&#105;&#110;&#32;&#116;&#104;&#105;&#115;&#32;&#85;&#82;&#76;&#63;&#32;&#82;&#101;&#97;&#108;&#108;&#121;&#46;&#32;&#87;&#104;&#97;&#116;&#32;&#119;&#101;&#114;&#101;&#32;&#121;&#111;&#117;&#32;&#101;&#120;&#112;&#101;&#99;&#116;&#105;&#110;&#103;&#32;&#116;&#111;&#32;&#115;&#101;&#101;&#32;&#104;&#101;&#114;&#101;&#63;&#32;&#89;&#111;&#117;&#32;&#114;&#101;&#97;&#108;&#108;&#121;&#32;&#110;&#101;&#101;&#100;&#32;&#116;&#111;&#32;&#103;&#101;&#116;&#32;&#97;&#32;&#108;&#105;&#102;&#101;&#46;&#32;&#65;&#114;&#101;&#32;&#121;&#111;&#117;&#32;&#115;&#116;&#105;&#108;&#108;&#32;&#104;&#101;&#114;&#101;&#63;&#32;&#83;&#101;&#114;&#105;&#111;&#117;&#115;&#108;&#121;&#44;&#32;&#121;&#111;&#117;&#32;&#110;&#101;&#101;&#100;&#32;&#116;&#111;&#32;&#103;&#111;&#32;&#100;&#111;&#32;&#115;&#111;&#109;&#101;&#116;&#104;&#105;&#110;&#103;&#32;&#101;&#108;&#115;&#101;&#46;&#32;&#73;&#32;&#116;&#104;&#105;&#110;&#107;&#32;&#121;&#111;&#117;&#114;&#32;&#98;&#111;&#115;&#115;&#32;&#105;&#115;&#32;&#99;&#97;&#108;&#108;&#105;&#110;&#103;&#46;</div></body></html>|;
return [ 200, [ 'Content-Type' => 'text/html' ], [ $snoop ] ];
} else {
return $self->app->($env);
}
}
1;

View file

@ -1,65 +0,0 @@
package WebGUI::URL::Credits;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use Apache2::Const -compile => qw(OK DECLINED);
use APR::Finfo ();
use APR::Const -compile => qw(FINFO_NORM);
use WebGUI::Session;
=head1 NAME
Package WebGUI::URL::Credits
=head1 DESCRIPTION
A URL handler that displays the credits file.
=head1 SYNOPSIS
use WebGUI::URL::Credits;
my $status = WebGUI::URL::Credits::handler($r, $s, $config);
=head1 SUBROUTINES
These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 handler ( request, server, config )
The Apache request handler for this package.
=cut
sub handler {
my ($request, $server, $config) = @_;
my $filename = $config->getWebguiRoot."/docs/credits.txt";
$request->push_handlers(PerlResponseHandler => sub {
$request->content_type('text/plain');
$request->sendfile($filename);
return Apache2::Const::OK;
});
$request->push_handlers(PerlTransHandler => sub { return Apache2::Const::OK });
$request->push_handlers(PerlMapToStorageHandler => sub { return Apache2::Const::OK });
return Apache2::Const::OK;
}
1;

View file

@ -1,59 +0,0 @@
package WebGUI::URL::PassThru;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use Apache2::Const -compile => qw(OK DECLINED DIR_MAGIC_TYPE);
=head1 NAME
Package WebGUI::URL::PassThru
=head1 DESCRIPTION
A URL handler that just passes the URLs back to Apache.
=head1 SYNOPSIS
use WebGUI::URL::PassThru;
my $status = WebGUI::URL::PassThru::handler($r, $s, $config);
=head1 SUBROUTINES
These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 handler ( request, server, config )
=cut
sub handler {
my ($request, $server, $config) = @_;
if ($request->handler eq 'perl-script' && # Handler is Perl
-d $request->filename && # Filename requested is a directory
$request->is_initial_req) # and this is the initial request
{
$request->handler(Apache2::Const::DIR_MAGIC_TYPE); # Hand off to mod_dir
return Apache2::Const::OK;
}
return Apache2::Const::OK;
}
1;

View file

@ -1,61 +0,0 @@
package WebGUI::URL::Snoop;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use Apache2::Const -compile => qw(OK DECLINED);
use WebGUI::Session;
=head1 NAME
Package WebGUI::URL::Snoop
=head1 DESCRIPTION
A URL handler that should never be called.
=head1 SYNOPSIS
use WebGUI::URL::Snoop;
my $status = WebGUI::URL::Snoop::handler($r, $configFile);
=head1 SUBROUTINES
These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 handler ( request, configFile )
The Apache request handler for this package.
=cut
sub handler {
my ($request, $server, $config) = @_;
$request->content_type("text/html");
$request->push_handlers(PerlResponseHandler => sub {
$request->print(q|<html><head><title>Snoopy</title></head><body><div style="width: 600px; padding: 200px;">&#87;&#104;&#121;&#32;&#119;&#111;&#117;&#108;&#100;&#32;&#121;&#111;&#117;&#32;&#116;&#121;&#112;&#101;&#32;&#105;&#110;&#32;&#116;&#104;&#105;&#115;&#32;&#85;&#82;&#76;&#63;&#32;&#82;&#101;&#97;&#108;&#108;&#121;&#46;&#32;&#87;&#104;&#97;&#116;&#32;&#119;&#101;&#114;&#101;&#32;&#121;&#111;&#117;&#32;&#101;&#120;&#112;&#101;&#99;&#116;&#105;&#110;&#103;&#32;&#116;&#111;&#32;&#115;&#101;&#101;&#32;&#104;&#101;&#114;&#101;&#63;&#32;&#89;&#111;&#117;&#32;&#114;&#101;&#97;&#108;&#108;&#121;&#32;&#110;&#101;&#101;&#100;&#32;&#116;&#111;&#32;&#103;&#101;&#116;&#32;&#97;&#32;&#108;&#105;&#102;&#101;&#46;&#32;&#65;&#114;&#101;&#32;&#121;&#111;&#117;&#32;&#115;&#116;&#105;&#108;&#108;&#32;&#104;&#101;&#114;&#101;&#63;&#32;&#83;&#101;&#114;&#105;&#111;&#117;&#115;&#108;&#121;&#44;&#32;&#121;&#111;&#117;&#32;&#110;&#101;&#101;&#100;&#32;&#116;&#111;&#32;&#103;&#111;&#32;&#100;&#111;&#32;&#115;&#111;&#109;&#101;&#116;&#104;&#105;&#110;&#103;&#32;&#101;&#108;&#115;&#101;&#46;&#32;&#73;&#32;&#116;&#104;&#105;&#110;&#107;&#32;&#121;&#111;&#117;&#114;&#32;&#98;&#111;&#115;&#115;&#32;&#105;&#115;&#32;&#99;&#97;&#108;&#108;&#105;&#110;&#103;&#46;</div></body></html>|);
return Apache2::Const::OK;
} );
$request->push_handlers(PerlTransHandler => sub { return Apache2::Const::OK });
return Apache2::Const::OK;
}
1;

View file

@ -1,54 +0,0 @@
package WebGUI::URL::Unauthorized;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use Apache2::Const -compile => qw(AUTH_REQUIRED);
=head1 NAME
Package WebGUI::URL::Unauthorized
=head1 DESCRIPTION
A URL handler that deals with requests where the user cannot access what they requested.
=head1 SYNOPSIS
use WebGUI::URL::Unauthorized;
my $status = WebGUI::URL::Unauthorized::handler($r, $s, $config);
=head1 SUBROUTINES
These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 handler ( request, server, config )
The Apache request handler for this package.
=cut
sub handler {
my ($request, $server, $config) = @_;
return Apache2::Const::AUTH_REQUIRED;
}
1;

View file

@ -1,88 +0,0 @@
package WebGUI::URL::Uploads;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use Apache2::Const -compile => qw(OK DECLINED NOT_FOUND AUTH_REQUIRED);
use WebGUI::Session;
=head1 NAME
Package WebGUI::URL::Uploads;
=head1 DESCRIPTION
A URL handler that handles privileges for uploaded files.
=head1 SYNOPSIS
use WebGUI::URL::Uploads;
my $status = WebGUI::URL::Uploads::handler($r, $s, $config);
=head1 SUBROUTINES
These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 handler ( request, server, config )
The Apache request handler for this package.
=cut
sub handler {
my ($request, $server, $config) = @_;
$request->push_handlers(PerlAccessHandler => sub {
if (-e $request->filename) {
my $path = $request->filename;
$path =~ s/^(\/.*\/).*$/$1/;
if (-e $path.".wgaccess") {
my $fileContents;
open(my $FILE, "<" ,$path.".wgaccess");
while (my $line = <$FILE>) {
$fileContents .= $line;
}
close($FILE);
my @privs = split("\n", $fileContents);
unless ($privs[1] eq "7" || $privs[1] eq "1") {
my $session = $request->pnotes('wgSession');
unless (defined $session) {
# $session = WebGUI::Session->open($server->dir_config('WebguiRoot'), $config->getFilename, $request);
}
my $hasPrivs = ($session->var->get("userId") eq $privs[0] || $session->user->isInGroup($privs[1]) || $session->user->isInGroup($privs[2]));
$session->close();
if ($hasPrivs) {
return Apache2::Const::OK;
}
else {
return Apache2::Const::AUTH_REQUIRED;
}
}
}
return Apache2::Const::OK;
}
else {
return Apache2::Const::NOT_FOUND;
}
} );
return Apache2::Const::OK;
}
1;

View file

@ -1,55 +0,0 @@
package WebGUI::URL::MyHandler;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use Apache2::Const -compile => qw(OK DECLINED NOT_FOUND);
=head1 NAME
Package WebGUI::URL::MyHandler
=head1 DESCRIPTION
A URL handler that does whatever I tell it to do.
=head1 SYNOPSIS
use WebGUI::URL::MyHandler;
my $status = WebGUI::URL::MyHandler::handler($r, $configFile);
=head1 SUBROUTINES
These subroutines are available from this package:
=cut
#-------------------------------------------------------------------
=head2 handler ( request, server, config )
The Apache request handler for this package.
=cut
sub handler {
my ($request, $server, $config) = @_;
# ...
return Apache2::Const::OK;
}
1;
#vim:ft=perl