Demo site loads

This commit is contained in:
Patrick Donelan 2010-03-14 22:09:57 -04:00
parent 158124cf37
commit d858c6e1ab
8 changed files with 99 additions and 227 deletions

View file

@ -1,7 +1,4 @@
use Plack::Builder;
use lib '/data/WebGUI/lib'; use lib '/data/WebGUI/lib';
use WebGUI; use WebGUI;
my $wg = WebGUI->new( root => '/data/WebGUI', site => 'dev.localhost.localdomain.conf' ); my $app = WebGUI->new( root => '/data/WebGUI', site => 'dev.localhost.localdomain.conf' )->psgi_app;
$wg->psgi_app;

View file

@ -76,112 +76,6 @@ sub BUILD {
$self->config( $config ); $self->config( $config );
} }
#-------------------------------------------------------------------
=head2 authen ( requestObject, [ user, pass ])
HTTP Basic auth for WebGUI.
=head3 requestObject
The Plack::Request object instantiated from the PSGI env hash
=head3 user
The username to authenticate with. Will pull from the request object if not specified.
=head3 pass
The password to authenticate with. Will pull from the request object if not specified.
=cut
sub authen {
my ($self, $request, $username, $password) = @_;
# # set username and password if it's an auth handler
# if ($username eq "") {
# if ($request->auth_type eq "Basic") {
## ($status, $password) = $request->get_basic_auth_pw; # TODO - don't think this is supported by Plack::Request
# $username = $request->user;
# }
# else {
# $response->status( 401 ); # HTTP_UNAUTHORIZED;
# return;
# }
# }
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, $config, $request, $sessionId) );
my $log = $session->log;
# $request->pnotes(wgSession => $session); # TODO - no more pnotes
if (defined $sessionId && $session->user->isRegistered) { # got a session id passed in or from a cookie
$log->info("BASIC AUTH: using cookie");
$session->response->status( 200 ); # OK
return;
}
# TODO - put this back in once we figure out get_basic_auth_pw
# elsif ($status != 200) { # prompt the user for their username and password
# $log->info("BASIC AUTH: prompt for user/pass");
# return $status;
# }
elsif (defined $username && $username ne "") { # no session cookie, let's try to do basic auth
$log->info("BASIC AUTH: using user/pass");
my $user = WebGUI::User->newByUsername($session, $username);
if (defined $user) {
my $authMethod = $user->authMethod;
if ($authMethod) { # we have an auth method, let's try to instantiate
my $auth = eval { WebGUI::Pluggable::instanciate("WebGUI::Auth::".$authMethod, "new", [ $session, $authMethod ] ) };
if ($@) { # got an error
$log->error($@);
$session->response->status( 500 ); # SERVER_ERROR
return;
}
elsif ($auth->authenticate($username, $password)) { # lets try to authenticate
$log->info("BASIC AUTH: authenticated successfully");
$sessionId = $session->db->quickScalar("select sessionId from userSession where userId=?",[$user->userId]);
unless (defined $sessionId) { # no existing session found
$log->info("BASIC AUTH: creating new session");
$sessionId = $session->id->generate;
$auth->_logLogin($user->userId, "success (HTTP Basic)");
}
$session->{_var} = WebGUI::Session::Var->new($session, $sessionId);
$session->user({user=>$user});
$session->response->status( 200 ); # OK
return;
}
}
}
$log->security($username." failed to login using HTTP Basic Authentication");
$request->note_basic_auth_failure;
$session->response->status( 401 ); # HTTP_UNAUTHORIZED
return;
}
$log->info("BASIC AUTH: skipping");
$session->response->status( 401 ); # HTTP_UNAUTHORIZED
return;
}
sub to_app {
my ( $self, $env ) = @_;
# immediately starts the response and stream the content
return sub {
my $respond = shift;
my $writer = $respond->( [ 200, [ 'Content-Type', 'application/json' ] ] );
# IO bound delayed response
$writer->write( "hi there\n" );
$writer->close;
};
}
sub psgi_app { sub psgi_app {
my $self = shift; my $self = shift;
return $self->{psgi_app} ||= $self->compile_psgi_app; return $self->{psgi_app} ||= $self->compile_psgi_app;
@ -189,13 +83,11 @@ sub psgi_app {
sub compile_psgi_app { sub compile_psgi_app {
my $self = shift; my $self = shift;
my $app = sub { my $app = sub {
my $env = shift; my $env = shift;
my $request = Plack::Request->new($env); # This could also be WebGUI::Request
my $request = Plack::Request->new( $env ); my $response = $self->dispatch($request);
my $response = $self->handle($request);
return $response; return $response;
}; };
@ -215,79 +107,64 @@ sub compile_psgi_app {
my $uploadsPath = $config->get('uploadsPath'); my $uploadsPath = $config->get('uploadsPath');
$app = Plack::Middleware::Static->wrap($app, $app = Plack::Middleware::Static->wrap($app,
path => sub { s{^$uploadsURL/}{} }, path => sub { s{^$uploadsURL/}{} },
root => "$uploadsPath/", root => "$uploadsPath/",
); );
return $app; return $app;
} }
#------------------------------------------------------------------- sub dispatch {
my ( $self, $request ) = @_;
=head2 handle ( request )
Primary http init/response handler for WebGUI. This method decides whether to hand off the request to contentHandler() or uploadsHandler()
=head3 request
The Plack::Request object
=cut
sub handle {
my ($self, $request) = @_;
my $config = $self->config; my $config = $self->config;
my $gateway = $config->get("gateway");
my $matchUri = $request->uri; # determine session id
$matchUri =~ s{^$gateway}{/}; my $sessionId = $request->cookies->{$config->getCookieName};
# handle basic auth # Instantiate the session object
my $auth = $request->header('Authorization'); my $session = $self->session( WebGUI::Session->open($self->root, $config, $request, $sessionId) );
if ($auth && $auth =~ m/^Basic/) { # machine oriented
# Get username and password and hand over to authen for my $handler (@{$config->get("contentHandlers")}) {
$auth =~ s/Basic //; my $output = eval { WebGUI::Pluggable::run($handler, "handler", [ $session ] )};
$self->authen($request, split(":", MIME::Base64::decode_base64($auth), 2)); if ( my $e = WebGUI::Error->caught ) {
} $session->errorHandler->error($e->package.":".$e->line." - ".$e->error);
else { # realm oriented $session->errorHandler->debug($e->package.":".$e->line." - ".$e->trace);
# 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)}); elsif ( $@ ) {
$self->authen($request); $session->errorHandler->error( $@ );
} }
else {
# url handlers # We decide what to do next depending on what the contentHandler returned
# TODO - rip out urlHandler API - convert all to middleware
# all remaining url handlers (probably just Asset which might get converted to something else) should # "chunked" or "empty" means it took care of its own output needs
# set $repsonse->body (e.g. so they can set it to IO) -- they no longer return $output if (defined $output && ( $output eq "chunked" || $output eq "empty" )) {
my $error = ""; if ($session->errorHandler->canShowDebug()) {
my $gotMatch = 0; $session->output->print($session->errorHandler->showDebug(),1);
my $response = $self->session->response; }
# TODO - would now be a time to fix the WEBGUI_FATAL label black magic?
WEBGUI_FATAL: foreach my $handler (@{$config->get("urlHandlers")}) {
my ($regex) = keys %{$handler};
if ($matchUri =~ m{$regex}i) {
eval { WebGUI::Pluggable::run($handler->{$regex}, "handler", [$request, $self->session]) };
if ($@) {
$error = $@;
last; last;
} }
else { # non-empty output should be used as the response body
# Record that at least one url handler ran successfully elsif (defined $output && $output ne "") {
$gotMatch = 1; # Auto-set the headers
$session->http->sendHeader; # TODO: should be renamed setHeader
# But only return response if body was set
if (defined $response->body ) { # or maybe get a smarter way for url handlers to flag success - b/c this may break delayed IO # Use contentHandler's return value as the output
return $response->finalize; $session->output->print($output);
} if ($session->errorHandler->canShowDebug()) {
$session->output->print($session->errorHandler->showDebug(),1);
}
last;
}
# Keep processing for success codes
elsif ($session->http->getStatus < 200 || $session->http->getStatus > 299) {
$session->http->sendHeader;
last;
} }
} }
}
if ( !$gotMatch ) {
# can't handle the url due to error or misconfiguration
$response->body( "This server is unable to handle the url '".$request->uri."' that you requested. ".$error );
} }
return $response->finalize; $session->close;
return $session->response->finalize;
} }
1; 1;

View file

@ -589,7 +589,7 @@ sub process {
} }
# Return a JSONinfied version of vars if JSON is the only requested content type. # Return a JSONinfied version of vars if JSON is the only requested content type.
if ( defined $session->request && $session->request->headers_in->{Accept} eq 'application/json' ) { if ( defined $session->request && $session->request->header('Accept') eq 'application/json' ) {
$session->http->setMimeType( 'application/json' ); $session->http->setMimeType( 'application/json' );
return to_json( $vars ); return to_json( $vars );
} }

View file

@ -20,8 +20,6 @@ use Time::HiRes;
use WebGUI::Asset; use WebGUI::Asset;
use WebGUI::PassiveAnalytics::Logging; use WebGUI::PassiveAnalytics::Logging;
use Apache2::Const -compile => qw(OK);
=head1 NAME =head1 NAME
Package WebGUI::Content::MyHandler Package WebGUI::Content::MyHandler
@ -131,7 +129,7 @@ sub handler {
my $oldContentType = $request->content_type($ct); my $oldContentType = $request->content_type($ct);
if ($request->sendfile($filename) ) { if ($request->sendfile($filename) ) {
$session->close; $session->close;
return Apache2::Const::OK; return; # TODO - what should we return to indicate streaming?
} }
else { else {
$request->content_type($oldContentType); $request->content_type($oldContentType);

View file

@ -96,8 +96,7 @@ sub print {
} }
elsif ($self->session->request) { elsif ($self->session->request) {
# TODO - put in IO bound delayed response # TODO - put in IO bound delayed response
warn "content: $content"; $self->session->response->body( $self->session->response->body() . $content );
# $self->session->request->body([]) unless $self->session->request->body();
# if (ref $self->session->request->body eq 'ARRAY') { # if (ref $self->session->request->body eq 'ARRAY') {
# push @{$self->session->request->body}, $content; # push @{$self->session->request->body}, $content;
# } else { # } else {

View file

@ -322,7 +322,7 @@ sub getRequestedUrl {
my $self = shift; my $self = shift;
return undef unless ($self->session->request); return undef unless ($self->session->request);
unless ($self->{_requestedUrl}) { unless ($self->{_requestedUrl}) {
$self->{_requestedUrl} = $self->session->request->uri; $self->{_requestedUrl} = $self->session->request->path_info; # TODO - is path_info right?
my $gateway = $self->session->config->get("gateway"); my $gateway = $self->session->config->get("gateway");
$self->{_requestedUrl} =~ s/^$gateway([^?]*)\??.*$/$1/; $self->{_requestedUrl} =~ s/^$gateway([^?]*)\??.*$/$1/;
} }

View file

@ -15,6 +15,7 @@ package WebGUI::URL::Content;
=cut =cut
use strict; use strict;
use Apache2::Const -compile => qw(OK DECLINED);
use WebGUI::Affiliate; use WebGUI::Affiliate;
use WebGUI::Exception; use WebGUI::Exception;
use WebGUI::Pluggable; use WebGUI::Pluggable;
@ -41,7 +42,7 @@ These subroutines are available from this package:
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 handler ( request, session ) =head2 handler ( request, server, config )
The Apache request handler for this package. The Apache request handler for this package.
@ -60,55 +61,55 @@ to the user, instead of displaying the Page Not Found page.
=cut =cut
sub handler { sub handler {
my ($request, $session) = @_; my ($request, $server, $config) = @_;
my $config = $session->config; $request->push_handlers(PerlResponseHandler => sub {
# my $session = $request->pnotes('wgSession'); # TODO - no more pnotes my $session = $request->pnotes('wgSession');
# unless (defined $session) { unless (defined $session) {
# TODO - fix this - server is gone $session = WebGUI::Session->open($server->dir_config('WebguiRoot'), $config->getFilename, $request, $server);
# $session = WebGUI::Session->open($server->dir_config('WebguiRoot'), $config->getFilename, $request, $server);
# }
WEBGUI_FATAL: foreach my $handler (@{$config->get("contentHandlers")}) {
my $output = eval { WebGUI::Pluggable::run($handler, "handler", [ $session ] )};
if ( my $e = WebGUI::Error->caught ) {
$session->errorHandler->error($e->package.":".$e->line." - ".$e->error);
$session->errorHandler->debug($e->package.":".$e->line." - ".$e->trace);
} }
elsif ( $@ ) { WEBGUI_FATAL: foreach my $handler (@{$config->get("contentHandlers")}) {
$session->errorHandler->error( $@ ); my $output = eval { WebGUI::Pluggable::run($handler, "handler", [ $session ] )};
} if ( my $e = WebGUI::Error->caught ) {
else { $session->errorHandler->error($e->package.":".$e->line." - ".$e->error);
if (defined $output) { $session->errorHandler->debug($e->package.":".$e->line." - ".$e->trace);
$session->response->body($output);
return;
} }
if ($output eq "chunked") { elsif ( $@ ) {
if ($session->errorHandler->canShowDebug()) { $session->errorHandler->error( $@ );
$session->output->print($session->errorHandler->showDebug(),1); }
else {
if ($output eq "chunked") {
if ($session->errorHandler->canShowDebug()) {
$session->output->print($session->errorHandler->showDebug(),1);
}
last;
} }
last; if ($output eq "empty") {
} if ($session->errorHandler->canShowDebug()) {
if ($output eq "empty") { $session->output->print($session->errorHandler->showDebug(),1);
if ($session->errorHandler->canShowDebug()) { }
$session->output->print($session->errorHandler->showDebug(),1); last;
} }
last; elsif (defined $output && $output ne "") {
} $session->http->sendHeader;
elsif (defined $output && $output ne "") { $session->output->print($output);
$session->http->sendHeader; if ($session->errorHandler->canShowDebug()) {
$session->output->print($output); $session->output->print($session->errorHandler->showDebug(),1);
if ($session->errorHandler->canShowDebug()) { }
$session->output->print($session->errorHandler->showDebug(),1); last;
}
# Keep processing for success codes
elsif ($session->http->getStatus < 200 || $session->http->getStatus > 299) {
$session->http->sendHeader;
last;
} }
last;
}
# Keep processing for success codes
elsif ($session->http->getStatus < 200 || $session->http->getStatus > 299) {
$session->http->sendHeader;
last;
} }
} }
} $session->close;
$session->close; return Apache2::Const::OK;
});
$request->push_handlers(PerlMapToStorageHandler => sub { return Apache2::Const::OK });
$request->push_handlers(PerlTransHandler => sub { return Apache2::Const::OK });
return Apache2::Const::OK;
} }
1; 1;

View file

@ -49,7 +49,7 @@ ok($output =~ m/true/, "process() - conditionals");
ok($output =~ m/\b(?:XY){5}\b/, "process() - loops"); ok($output =~ m/\b(?:XY){5}\b/, "process() - loops");
# See if template listens the Accept header # See if template listens the Accept header
$session->request->headers_in->{Accept} = 'application/json'; $session->request->header('Accept' => 'application/json');
my $json = $template->process(\%var); my $json = $template->process(\%var);
my $andNowItsAPerlHashRef = eval { from_json( $json ) }; my $andNowItsAPerlHashRef = eval { from_json( $json ) };