Skip to content

Commit

Permalink
Merge branch 'plebgui' into plebgui-7.10
Browse files Browse the repository at this point in the history
Conflicts:
	lib/WebGUI.pm
  • Loading branch information
martink committed Jul 13, 2012
2 parents 9ba3022 + eb24698 commit b1bb15e
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 13 deletions.
27 changes: 27 additions & 0 deletions apache.conf
@@ -0,0 +1,27 @@
<VirtualHost *:80>
PerlOptions +Parent
PerlSwitches -I/data/WebGUI/lib

# CGI
#AddHandler cgi-script cgi
#ScriptAlias / /data/WebGUI/etc/dev.localhost.localdomain.cgi/
#<Directory /data/WebGUI/etc>
# Options +ExecCGI
#</Directory>

# Apache2
#SetHandler perl-script
#PerlHandler Plack::Server::Apache2
#PerlSetVar psgi_app /data/WebGUI/etc/dev.localhost.localdomain.psgi

# FastCGI
FastCgiServer /data/WebGUI/etc/dev.localhost.localdomain.fcgi
ScriptAlias / /data/WebGUI/etc/dev.localhost.localdomain.fcgi/

# mod_psgi
#<Location />
# SetHandler psgi
# PSGIApp /data/WebGUI/etc/dev.localhost.localdomain.psgi
#</Location>

</VirtualHost>
5 changes: 5 additions & 0 deletions etc/dev.localhost.localdomain.cgi
@@ -0,0 +1,5 @@
#!/usr/bin/perl
use Plack::Server::CGI;

my $app = Plack::Util::load_psgi("/data/WebGUI/etc/dev.localhost.localdomain.psgi");
Plack::Server::CGI->new->run($app);
5 changes: 5 additions & 0 deletions etc/dev.localhost.localdomain.fcgi
@@ -0,0 +1,5 @@
#!/usr/bin/perl
use Plack::Server::FCGI;

my $app = Plack::Util::load_psgi("/data/WebGUI/etc/dev.localhost.localdomain.psgi");
Plack::Server::FCGI->new->run($app);
7 changes: 7 additions & 0 deletions etc/dev.localhost.localdomain.perlbal
@@ -0,0 +1,7 @@
LOAD PSGI
CREATE SERVICE psgi
SET role = web_server
SET listen = 127.0.0.1:80
SET plugins = psgi
PSGI_APP = dev.localhost.localdomain.psgi
ENABLE psgi
25 changes: 25 additions & 0 deletions etc/dev.localhost.localdomain.psgi
@@ -0,0 +1,25 @@
use Plack::Builder;
use lib '/data/WebGUI/lib';
use WebGUI;
WebGUI->init( root => '/data/WebGUI', config => 'dev.localhost.localdomain.conf' );

builder {

# Handle /extras via Plack::Middleware::Static
# (or Plack::Middleware::WebGUI could do this for us by looking up extrasPath and extrasURL in site.conf)
enable 'Plack::Middleware::Static',
path => qr{^/extras/},
root => '/data/WebGUI/www';

# Handle /uploads via Plack::Middleware::WGAccess (including .wgaccess)
# (or Plack::Middleware::WebGUI could do this for us by looking up uploadsPath and uploadsURL in site.conf)
#enable 'Plack::Middleware::WGAccess',
# path => qr{^/uploads/},
# root => '/data/domains/dev.localhost.localdomain/public';

enable 'Plack::Middleware::Static',
path => qr{^/uploads/},
root => '/data/domains/dev.localhost.localdomain/public';

sub { WebGUI::handle_psgi(shift) };
}
96 changes: 96 additions & 0 deletions lib/Plack/Middleware/WGAccess.pm
@@ -0,0 +1,96 @@
package Plack::Middleware::WGAccess;
use strict;
use warnings;
use base qw/Plack::Middleware::Static/;
use Path::Class 'dir';

=head1 NAME
Plack::Middleware::WGAccess
=head1 DESCRIPTION
Plack Middleware that delivers static files with .wgaccess awareness
=cut

sub _handle_static {
my($self, $env) = @_;

#######################################
# Copied from Plack::Middleware::Static::_handle_static

my $path_match = $self->path or return;

if ($env->{PATH_INFO} =~ m!\.\.[/\\]!) {
return $self->return_403;
}

my $path = do {
my $matched;
local $_ = $env->{PATH_INFO};
if (ref $path_match eq 'CODE') {
$matched = $path_match->($_);
} else {
$matched = $_ =~ $path_match;
}
return unless $matched;
$_;
} or return;

my $docroot = dir($self->root || ".");
my $file = $docroot->file(File::Spec::Unix->splitpath($path));
my $realpath = Cwd::realpath($file->absolute->stringify);

# Is the requested path within the root?
if ($realpath && !$docroot->subsumes($realpath)) {
return $self->return_403;
}

# Does the file actually exist?
if (!$realpath || !-f $file) {
return $self->return_404;
}

# If the requested file present but lacking the permission to read it?
if (!-r $file) {
return $self->return_403;
}

###############################
# Copied from WebGUI::URL::Uploads
my $wgaccess = File::Spec::Unix->catfile($file->dir, '.wgaccess');
if (-e $wgaccess) {
my $fileContents;
open(my $FILE, "<", $wgaccess);
while (my $line = <$FILE>) {
$fileContents .= $line;
}
close($FILE);
my @privs = split("\n", $fileContents);
unless ($privs[1] eq "7" || $privs[1] eq "1") {

# Construct request,server,config in the usual way
require WebGUI::Session::Plack;
my $request = WebGUI::Session::Plack->new( env => $env );
my $server = $request->server;

my $session = $request->pnotes('wgSession');
unless (defined $session) {
$session = WebGUI::Session->open($server->dir_config('WebguiRoot'), $request->dir_config('WebguiConfig'), $request, $server);
}
my $hasPrivs = ($session->var->get("userId") eq $privs[0] || $session->user->isInGroup($privs[1]) || $session->user->isInGroup($privs[2]));
$session->close();
if ($hasPrivs) {
return $self->SUPER::_handle_static($env); # serve statically
}
else {
return $self->return_403;
}
}
} else {
return $self->SUPER::_handle_static($env); # serve statically
}
}

1;
28 changes: 28 additions & 0 deletions lib/Plack/Middleware/WebGUI.pm
@@ -0,0 +1,28 @@
package Plack::Middleware::WebGUI;
use strict;
use warnings;
use base qw/Plack::Middleware/;

__PACKAGE__->mk_accessors('root', 'config');

=head1 NAME
Plack::Middleware::WebGUI
=head1 DESCRIPTION
Plack Middleware that populates $env
=cut

sub call {
my $self = shift;
my $env = shift;

$env->{'wg.WEBGUI_ROOT'} = $self->root;
$env->{'wg.WEBGUI_CONFIG'} = $self->config;

$self->app->($env);
}

1;
59 changes: 48 additions & 11 deletions lib/WebGUI.pm
Expand Up @@ -20,7 +20,7 @@ our $STATUS = 'stable';
=cut

use strict;
use Apache2::Access ();
use Apache2::Access ();
use Apache2::Const -compile => qw(OK DECLINED HTTP_UNAUTHORIZED SERVER_ERROR);
use Apache2::Request;
use Apache2::RequestIO;
Expand Down Expand Up @@ -83,9 +83,14 @@ A reference to a WebGUI::Session object.

sub authen {
my ($request, $username, $password, $session) = @_;
$request = Apache2::Request->new($request);
my $log = $session->log;
my $server = Apache2::ServerUtil->server;
my $server;
if ($request->isa('WebGUI::Session::Plack')) {
$server = $request->server;
} else {
$request = Apache2::Request->new($request);
$server = Apache2::ServerUtil->server; #instantiate the server api
}
my $status = Apache2::Const::OK;

# set username and password if it's an auth handler
Expand Down Expand Up @@ -148,11 +153,18 @@ The Apache2::RequestRec object passed in by Apache's mod_perl.
=cut

sub handler {
my $request = shift; #start with apache request object
$request = Apache2::Request->new($request);
my $configFile = shift || $request->dir_config('WebguiConfig'); #either we got a config file, or we'll build it from the request object's settings
my $server = Apache2::ServerUtil->server; #instantiate the server api
my $config = WebGUI::Config->new($server->dir_config('WebguiRoot'), $configFile); #instantiate the config object
my $request = shift; # either apache request object or PSGI env hash
my ($server, $config);
if ($request->isa('WebGUI::Session::Plack')) {
$server = $request->server;
$config = WebGUI->config; # use our cached version
} else {
$request = Apache2::Request->new($request);
$server = Apache2::ServerUtil->server; #instantiate the server api
my $configFile = shift || $request->dir_config('WebguiConfig'); #either we got a config file, or we'll build it from the request object's settings
$config = WebGUI::Config->new($server->dir_config('WebguiRoot'), $configFile); #instantiate the config object
}

my $error = "";
my $matchUri = $request->uri;
my $gateway = $config->get("gateway");
Expand All @@ -177,18 +189,43 @@ sub handler {
}
}
return Apache2::Const::DECLINED if ($gotMatch);

# can't handle the url due to error or misconfiguration
$request->push_handlers(PerlResponseHandler => sub {
$request->push_handlers(PerlResponseHandler => sub {
print "This server is unable to handle the url '".$request->uri."' that you requested. ".$error;
return Apache2::Const::OK;
} );
$request->push_handlers(PerlTransHandler => sub { return Apache2::Const::OK });
return Apache2::Const::DECLINED;
return Apache2::Const::DECLINED;
}



sub handle_psgi {
my $env = shift;
require WebGUI::Session::Plack;
my $plack = WebGUI::Session::Plack->new( env => $env );

# returns something like Apache2::Const::OK, which we ignore
my $ret = handler($plack);

# let Plack::Response do its thing
return $plack->finalize;
}

# Experimental speed boost
my ($root, $config_file, $config);
sub init {
my $class = shift;
my %opts = @_;
$root = $opts{root};
$config_file = $opts{config};
$config = WebGUI::Config->new($root, $config_file);
warn 'INIT';
}
sub config { $config }
sub root { $root }
sub config_file { $config_file }

1;

16 changes: 14 additions & 2 deletions lib/WebGUI/Session.pm
Expand Up @@ -452,10 +452,22 @@ sub open {
my $configFile = shift;
my $request = shift;
my $server = shift;
my $config = WebGUI::Config->new($webguiRoot,$configFile);
my $config = WebGUI->config || WebGUI::Config->new($webguiRoot,$configFile);
my $self = {_config=>$config, _server=>$server};
bless $self , $class;
$self->{_request} = $request if (defined $request);

# $self->{_request} = $request if (defined $request);
if ($request) {
if ($request->isa('WebGUI::Session::Plack')) {
# Use our WebGUI::Session::Plack object that is supposed to do everything Apache2::* can
$self->{_request} = $request;
} else {
# Use WebGUI::Session::Request to wrap Apache2::* calls
require WebGUI::Session::Request;
$self->{_request} = WebGUI::Session::Request->new( r => $request, session => $self );
}
}

my $sessionId = shift || $self->http->getCookies->{$config->getCookieName} || $self->id->generate;
$sessionId = $self->id->generate unless $self->id->valid($sessionId);
my $noFuss = shift;
Expand Down
14 changes: 14 additions & 0 deletions lib/WebGUI/Session/Http.pm
Expand Up @@ -93,6 +93,10 @@ Retrieves the cookies from the HTTP header and returns a hash reference containi
sub getCookies {
my $self = shift;
if ($self->session->request) {
if ($self->session->request->isa('WebGUI::Session::Plack')) {
return $self->session->request->{request}->cookies;
}

# Have to require this instead of using it otherwise it causes problems for command-line scripts on some platforms (namely Windows)
require APR::Request::Apache2;
my $jarHashRef = eval { APR::Request::Apache2->handle($self->session->request)->jar(); };
Expand Down Expand Up @@ -414,6 +418,16 @@ sub setCookie {
$ttl = (defined $ttl ? $ttl : '+10y');

if ($self->session->request) {
if ( $self->session->request->isa('WebGUI::Session::Plack') ) {
$self->session->request->{response}->cookies->{$name} = {
value => $value,
path => '/',
expires => $ttl ne 'session' ? $ttl : undef,
domain => $domain,
};
}
return;

require Apache2::Cookie;
my $cookie = Apache2::Cookie->new($self->session->request,
-name=>$name,
Expand Down

0 comments on commit b1bb15e

Please sign in to comment.