From 158124cf37ef8cf8d250d7d7d6b55c514a60a30b Mon Sep 17 00:00:00 2001 From: Patrick Donelan Date: Sun, 14 Mar 2010 20:41:22 -0400 Subject: [PATCH] mid-way commit --- etc/dev.localhost.localdomain.psgi | 26 +------- lib/Plack/Middleware/WebGUI.pm | 28 -------- lib/WebGUI.pm | 101 +++++++++++++++++++++++------ lib/WebGUI/Session.pm | 8 +-- lib/WebGUI/Session/Http.pm | 4 +- lib/WebGUI/URL/Content.pm | 4 ++ 6 files changed, 92 insertions(+), 79 deletions(-) delete mode 100644 lib/Plack/Middleware/WebGUI.pm diff --git a/etc/dev.localhost.localdomain.psgi b/etc/dev.localhost.localdomain.psgi index ff33e309b..144af28db 100644 --- a/etc/dev.localhost.localdomain.psgi +++ b/etc/dev.localhost.localdomain.psgi @@ -2,28 +2,6 @@ use Plack::Builder; use lib '/data/WebGUI/lib'; use WebGUI; -my $wg = WebGUI->new( - root => '/data/WebGUI', - config => 'dev.localhost.localdomain.conf', -); +my $wg = WebGUI->new( root => '/data/WebGUI', site => '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 => '^' . $wg->config->get('extrasURL') . '/', -# root => $wg->config->get('extrasPath'); -# -# # 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 => '^' . $wg->config->get('uploadsURL') . '/', -# # root => $wg->config->get('uploadsPath'); -# -# enable 'Plack::Middleware::Static', -# path => '^' . $wg->config->get('uploadsURL') . '/', -# root => $wg->config->get('uploadsPath'); - - sub { $wg->run(@_) }; -} +$wg->psgi_app; \ No newline at end of file diff --git a/lib/Plack/Middleware/WebGUI.pm b/lib/Plack/Middleware/WebGUI.pm deleted file mode 100644 index 748f531f0..000000000 --- a/lib/Plack/Middleware/WebGUI.pm +++ /dev/null @@ -1,28 +0,0 @@ -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; \ No newline at end of file diff --git a/lib/WebGUI.pm b/lib/WebGUI.pm index 75527b733..6fd8f6409 100644 --- a/lib/WebGUI.pm +++ b/lib/WebGUI.pm @@ -28,9 +28,12 @@ use WebGUI::User; use Moose; use Plack::Request; -has root => ( is => 'ro', required => 1 ); # WEBGUI_ROOT, e.g. /data/WebGUI -has config => ( is => 'ro', required => 1 ); # Site config, e.g. dev.localhost.localdomain.conf +has root => ( is => 'ro', isa => 'Str', required => 1 ); # e.g. /data/WebGUI +has site => ( is => 'ro', isa => 'Str', required => 1 ); # e.g. dev.localhost.localdomain.conf has session => ( is => 'rw', isa => 'WebGUI::Session' ); +has config => ( is => 'rw', isa => 'WebGUI::Config' ); + +use overload q(&{}) => sub { shift->psgi_app }, fallback => 1; =head1 NAME @@ -50,9 +53,32 @@ These subroutines are available from this package: =cut +around BUILDARGS => sub { + my $orig = shift; + my $class = shift; + + # Make constructor work as: + # WebGUI->new( $root, $site ) + # In addition to the more verbose: + # WebGUI->new( root => $root, site => $site ) + if (@_ eq 2) { + return $class->$orig(root => $_[0], site => $_[1] ); + } else { + return $class->$orig(@_); + } +}; + +sub BUILD { + my $self = shift; + + # Instantiate the WebGUI::Config object + my $config = WebGUI::Config->new( $self->root, $self->site ); + $self->config( $config ); +} + #------------------------------------------------------------------- -=head2 authen ( requestObject, [ user, pass, config ]) +=head2 authen ( requestObject, [ user, pass ]) HTTP Basic auth for WebGUI. @@ -68,14 +94,10 @@ The username to authenticate with. Will pull from the request object if not spec The password to authenticate with. Will pull from the request object if not specified. -=head3 config - -A reference to a WebGUI::Config object. One will be created if it isn't specified. - =cut sub authen { - my ($self, $request, $username, $password, $config) = @_; + my ($self, $request, $username, $password) = @_; # # set username and password if it's an auth handler # if ($username eq "") { @@ -89,13 +111,13 @@ sub authen { # } # } - $config ||= WebGUI::Config->new( $self->root, $self->config ); + my $config = $self->config; # determine session id my $sessionId = $request->cookies->{$config->getCookieName}; # Instantiate the session object - my $session = $self->session( WebGUI::Session->open($self->root, $self->config, $request, $sessionId) ); + my $session = $self->session( WebGUI::Session->open($self->root, $config, $request, $sessionId) ); my $log = $session->log; # $request->pnotes(wgSession => $session); # TODO - no more pnotes @@ -160,26 +182,63 @@ sub to_app { }; } +sub psgi_app { + my $self = shift; + return $self->{psgi_app} ||= $self->compile_psgi_app; +} + +sub compile_psgi_app { + my $self = shift; + + my $app = sub { + my $env = shift; + + my $request = Plack::Request->new( $env ); + my $response = $self->handle($request); + + return $response; + }; + + my $config = $self->config; + + # Extras + use Plack::Middleware::Static; + my $extrasURL = $config->get('extrasURL'); + my $extrasPath = $config->get('extrasPath'); + $app = Plack::Middleware::Static->wrap($app, + path => sub { s{^$extrasURL/}{} }, + root => "$extrasPath/", + ); + + # Uploads + my $uploadsURL = $config->get('uploadsURL'); + my $uploadsPath = $config->get('uploadsPath'); + $app = Plack::Middleware::Static->wrap($app, + path => sub { s{^$uploadsURL/}{} }, + root => "$uploadsPath/", + ); + + return $app; +} + #------------------------------------------------------------------- -=head2 run ( env ) +=head2 handle ( request ) Primary http init/response handler for WebGUI. This method decides whether to hand off the request to contentHandler() or uploadsHandler() -=head3 env +=head3 request -The PSGI environment hash +The Plack::Request object =cut -sub run { - my ($self, $env) = @_; +sub handle { + my ($self, $request) = @_; - my $request = Plack::Request->new( $env ); - my $config = WebGUI::Config->new( $self->root, $self->config ); - - my $matchUri = $request->uri; + my $config = $self->config; my $gateway = $config->get("gateway"); + my $matchUri = $request->uri; $matchUri =~ s{^$gateway}{/}; # handle basic auth @@ -187,12 +246,12 @@ sub run { if ($auth && $auth =~ m/^Basic/) { # machine oriented # Get username and password and hand over to authen $auth =~ s/Basic //; - $self->authen($request, split(":", MIME::Base64::decode_base64($auth), 2), $config); + $self->authen($request, split(":", MIME::Base64::decode_base64($auth), 2)); } else { # realm oriented # TODO - what to do here? Should we check response status after call to authen? # $request->push_handlers(PerlAuthenHandler => sub { return WebGUI::authen($request, undef, undef, $config)}); - $self->authen($request, undef, undef, $config); + $self->authen($request); } # url handlers diff --git a/lib/WebGUI/Session.pm b/lib/WebGUI/Session.pm index 169ebf7ef..1b8d8733f 100644 --- a/lib/WebGUI/Session.pm +++ b/lib/WebGUI/Session.pm @@ -434,7 +434,7 @@ The path to the WebGUI files. =head3 configFile -The filename of the config file that WebGUI should operate from. +The filename of the config file that WebGUI should operate from, or a WebGUI::Config object =head3 requestObject @@ -453,10 +453,10 @@ Uses simple session vars. See WebGUI::Session::Var::new() for more details. sub open { my $class = shift; my $webguiRoot = shift; - my $configFile = shift; + my $c = shift; my $request = shift; - my $config = WebGUI::Config->new($webguiRoot,$configFile); - my $self = {_config=>$config }; + my $config = ref $c ? $c : WebGUI::Config->new($webguiRoot,$c); + my $self = {_config=>$config }; # TODO - if we store reference here, should we weaken WebGUI->config? bless $self , $class; $self->{_request} = $request if defined $request; $self->{_response} = $request->new_response( 200 ) if defined $request; diff --git a/lib/WebGUI/Session/Http.pm b/lib/WebGUI/Session/Http.pm index fed19340e..2863460f6 100644 --- a/lib/WebGUI/Session/Http.pm +++ b/lib/WebGUI/Session/Http.pm @@ -278,7 +278,7 @@ sub sendHeader { # under these circumstances, don't allow caching if ($userId ne "1" || $cacheControl eq "none" || $self->session->setting->get("preventProxyCache")) { $response->header("Cache-Control" => "private, max-age=1"); - $request->no_cache(1); +# $request->no_cache(1); # TODO - re-enable this? } # in all other cases, set cache, but tell it to ask us every time so we don't mess with recently logged in users else { @@ -304,7 +304,7 @@ sub _sendMinimalHeader { my $response = $self->session->response; $response->content_type('text/html; charset=UTF-8'); $response->header('Cache-Control' => 'private'); - $response->no_cache(1); +# $response->no_cache(1); # TODO - re-enable this? $response->status($self->getStatus()); # $response->status_line($self->getStatus().' '.$self->getStatusDescription()); # TODO - re-enable return undef; diff --git a/lib/WebGUI/URL/Content.pm b/lib/WebGUI/URL/Content.pm index 3116d1807..6d870f5ae 100644 --- a/lib/WebGUI/URL/Content.pm +++ b/lib/WebGUI/URL/Content.pm @@ -77,6 +77,10 @@ sub handler { $session->errorHandler->error( $@ ); } else { + if (defined $output) { + $session->response->body($output); + return; + } if ($output eq "chunked") { if ($session->errorHandler->canShowDebug()) { $session->output->print($session->errorHandler->showDebug(),1);