Refactored Request/Response into WebGUI::Session::

This commit is contained in:
Patrick Donelan 2010-04-08 21:30:55 -04:00
parent 1ad2f0cfd7
commit b7e7d5b936
5 changed files with 117 additions and 90 deletions

View file

@ -1,33 +0,0 @@
package WebGUI::Request;
=head2 DESCRIPTION
The WebGUI server response object. See L<Plack::Response>
=cut
use strict;
use parent qw(Plack::Request);
use Plack::Util::Accessor qw(session);
use WebGUI::Response;
=head1 METHODS
=head2 new_response ()
Creates a new L<WebGUI::Response> object.
N.B. A L<WebGUI::Response> object is automatically created when L<WebGUI::Session>
is instantiated, so in most cases you will not need to call this method.
See L<WebGUI::Session/response>
=cut
sub new_response {
my $self = shift;
my $response = WebGUI::Response->new(@_);
$response->session($self->session);
return $response;
}
1;

View file

@ -29,6 +29,7 @@ use WebGUI::Session::Id;
use WebGUI::Session::Os;
use WebGUI::Session::Output;
use WebGUI::Session::Privilege;
use WebGUI::Session::Request;
use WebGUI::Session::Scratch;
use WebGUI::Session::Setting;
use WebGUI::Session::Stow;
@ -424,7 +425,7 @@ sub log {
#-------------------------------------------------------------------
=head2 open ( webguiRoot, configFile [, requestObject, sessionId, noFuss ] )
=head2 open ( webguiRoot, configFile [, env, sessionId, noFuss ] )
Constructor. Opens a closed ( or new ) WebGUI session.
@ -436,13 +437,14 @@ The path to the WebGUI files.
The filename of the config file that WebGUI should operate from, or a WebGUI::Config object
=head3 requestObject
=head3 env
The Plack::Request object. If this session is being instanciated from the web, this is required.
The L<PSGI> env hash. If this session is being instanciated from the web, this is required.
=head3 sessionId
Optionally retrieve a specific session id. Normally this is set by a cookie in the user's browser.
If you have a L<PSGI> env hash, you might find the sessionId at: $env->{'psgix.session'}->id
=head3 noFuss
@ -451,21 +453,29 @@ Uses simple session vars. See WebGUI::Session::Var::new() for more details.
=cut
sub open {
my $class = shift;
my $webguiRoot = shift;
my $c = shift;
my $request = shift;
my ($class, $webguiRoot, $c, $env, $sessionId, $noFuss) = @_;
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;
if (defined $request) {
$request->session($self); # hello circular reference
$self->{_request} = $request;
bless $self, $class;
if ($env) {
my $request = WebGUI::Session::Request->new($env);
$self->{_request} = $request;
$self->{_response} = $request->new_response( 200 );
# TODO: it might be nice to set a default Content-type here, but we can't until Assets can override it again
# $self->{_response} = $request->new_response( 200 );#, [ 'Content-type' => 'text/html; charset=UTF-8' ] );
# Use the WebGUI::Session::Request object to look up the sessionId from cookies, if it
# wasn't given explicitly
$sessionId ||= $request->cookies->{$config->getCookieName};
}
my $sessionId = shift || $request->cookies->{$config->getCookieName} || $self->id->generate;
$sessionId = $self->id->generate unless $self->id->valid($sessionId);
my $noFuss = shift;
# If the sessionId is still unset or is invalid, generate a new one
if (!$sessionId || !$self->id->valid($sessionId)) {
$sessionId = $self->id->generate;
}
$self->{_var} = WebGUI::Session::Var->new($self,$sessionId, $noFuss);
return $self;
}

View file

@ -0,0 +1,40 @@
package WebGUI::Session::Request;
use strict;
use parent qw(Plack::Request);
use WebGUI::Session::Response;
=head1 SYNOPSIS
my $session = WebGUI::Session->open(...);
my $request = $session->request;
=head1 DESCRIPTION
WebGUI's PSGI request utility class. Sub-classes L<Plack::Request>.
An instance of this object is created automatically when the L<WebGUI::Session>
is created.
=head1 METHODS
=head2 new_response ()
Creates a new L<WebGUI::Session::Response> object.
N.B. A L<WebGUI::Session::Response> object is automatically created when L<WebGUI::Session>
is instantiated, so in most cases you will not need to call this method.
See L<WebGUI::Session/response>
=cut
sub new_response {
my $self = shift;
return WebGUI::Session::Response->new(@_);
}
# This is only temporary
sub TRACE {
shift->env->{'psgi.errors'}->print(join '', @_, "\n");
}
1;

View file

@ -1,12 +1,20 @@
package WebGUI::Response;
package WebGUI::Session::Response;
use strict;
use parent qw(Plack::Response);
use Plack::Util::Accessor qw(session streaming writer streamer);
=head2 DESCRIPTION
=head1 SYNOPSIS
The WebGUI server response object. See of L<Plack::Response>
my $session = WebGUI::Session->open(...);
my $response = $session->response;
=head1 DESCRIPTION
WebGUI's PSGI response utility class. Sub-classes L<Plack::Response>.
An instance of this object is created automatically when the L<WebGUI::Session>
is created.
=cut