diff --git a/lib/WebGUI.pm b/lib/WebGUI.pm index 7ceb2ba6d..75527b733 100644 --- a/lib/WebGUI.pm +++ b/lib/WebGUI.pm @@ -25,11 +25,11 @@ use WebGUI::Config; use WebGUI::Pluggable; use WebGUI::Session; use WebGUI::User; -use Any::Moose; +use Moose; use Plack::Request; -has root => ( is => 'ro', required => 1 ); # WEBGUI_ROOT -has config => ( is => 'ro', required => 1 ); # WEBGUI_CONFIG +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 session => ( is => 'rw', isa => 'WebGUI::Session' ); =head1 NAME @@ -77,8 +77,6 @@ A reference to a WebGUI::Config object. One will be created if it isn't specifie sub authen { my ($self, $request, $username, $password, $config) = @_; - my $response = $request->new_response( 200 ); - # # set username and password if it's an auth handler # if ($username eq "") { # if ($request->auth_type eq "Basic") { @@ -103,7 +101,7 @@ sub authen { if (defined $sessionId && $session->user->isRegistered) { # got a session id passed in or from a cookie $log->info("BASIC AUTH: using cookie"); - $response->status( 200 ); # OK; + $session->response->status( 200 ); # OK return; } # TODO - put this back in once we figure out get_basic_auth_pw @@ -120,7 +118,7 @@ sub authen { my $auth = eval { WebGUI::Pluggable::instanciate("WebGUI::Auth::".$authMethod, "new", [ $session, $authMethod ] ) }; if ($@) { # got an error $log->error($@); - $response->status( 500 ); # SERVER_ERROR + $session->response->status( 500 ); # SERVER_ERROR return; } elsif ($auth->authenticate($username, $password)) { # lets try to authenticate @@ -133,21 +131,35 @@ sub authen { } $session->{_var} = WebGUI::Session::Var->new($session, $sessionId); $session->user({user=>$user}); - $response->status( 200 ); # OK + $session->response->status( 200 ); # OK return; } } } $log->security($username." failed to login using HTTP Basic Authentication"); $request->note_basic_auth_failure; - $response->status( 401 ); # HTTP_UNAUTHORIZED; + $session->response->status( 401 ); # HTTP_UNAUTHORIZED return; } $log->info("BASIC AUTH: skipping"); - $response->status( 401 ); # HTTP_UNAUTHORIZED; + $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; + }; +} + #------------------------------------------------------------------- =head2 run ( env ) @@ -164,7 +176,6 @@ sub run { my ($self, $env) = @_; my $request = Plack::Request->new( $env ); - my $response = $request->new_response( 200 ); my $config = WebGUI::Config->new( $self->root, $self->config ); my $matchUri = $request->uri; @@ -190,6 +201,7 @@ sub run { # set $repsonse->body (e.g. so they can set it to IO) -- they no longer return $output my $error = ""; my $gotMatch = 0; + 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")}) { diff --git a/lib/WebGUI/Session.pm b/lib/WebGUI/Session.pm index c10ec4ede..169ebf7ef 100644 --- a/lib/WebGUI/Session.pm +++ b/lib/WebGUI/Session.pm @@ -70,6 +70,7 @@ B It is important to distinguish the difference between a WebGUI session $session->os $session->privilege $session->request + $session->response $session->scratch $session->setting $session->stow @@ -457,7 +458,8 @@ sub open { my $config = WebGUI::Config->new($webguiRoot,$configFile); my $self = {_config=>$config }; bless $self , $class; - $self->{_request} = $request if (defined $request); + $self->{_request} = $request if defined $request; + $self->{_response} = $request->new_response( 200 ) if defined $request; my $sessionId = shift || $self->http->getCookies->{$config->getCookieName} || $self->id->generate; $sessionId = $self->id->generate unless $self->id->valid($sessionId); my $noFuss = shift; @@ -541,7 +543,7 @@ sub quick { =head2 request ( ) -Returns the Plack::Request object, or undef if it doesn't exist. +Returns the L object, or undef if it doesn't exist. =cut @@ -552,6 +554,19 @@ sub request { #------------------------------------------------------------------- +=head2 response ( ) + +Returns the L object, or undef if it doesn't exist. + +=cut + +sub response { + my $self = shift; + return $self->{_response}; +} + +#------------------------------------------------------------------- + =head2 scratch ( ) Returns a WebGUI::Session::Scratch object. diff --git a/lib/WebGUI/Session/Http.pm b/lib/WebGUI/Session/Http.pm index 2a141797a..fed19340e 100644 --- a/lib/WebGUI/Session/Http.pm +++ b/lib/WebGUI/Session/Http.pm @@ -258,7 +258,7 @@ sub sendHeader { return undef if ($self->{_http}{noHeader}); return $self->_sendMinimalHeader unless defined $self->session->db(1); - my ($request, $datetime, $config, $var) = $self->session->quick(qw(request datetime config var)); + my ($request, $response, $datetime, $config, $var) = $self->session->quick(qw(request response datetime config var)); return undef unless $request; my $userId = $var->get("userId"); @@ -269,44 +269,44 @@ sub sendHeader { $self->setNoHeader(1); my %params; if ($self->isRedirect()) { - $request->new_response->header(Location => $self->getRedirectLocation); - $request->new_response->status($self->getStatus); + $response->header(Location => $self->getRedirectLocation); + $response->status($self->getStatus); } else { $request->content_type($self->getMimeType); my $cacheControl = $self->getCacheControl; my $date = ($userId eq "1") ? $datetime->epochToHttp($self->getLastModified) : $datetime->epochToHttp; # under these circumstances, don't allow caching if ($userId ne "1" || $cacheControl eq "none" || $self->session->setting->get("preventProxyCache")) { - $request->new_response->header("Cache-Control" => "private, max-age=1"); + $response->header("Cache-Control" => "private, max-age=1"); $request->no_cache(1); } # 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 { - $request->new_response->header( 'Last-Modified' => $date); - $request->new_response->header( 'Cache-Control' => "must-revalidate, max-age=" . $cacheControl ); + $response->header( 'Last-Modified' => $date); + $response->header( 'Cache-Control' => "must-revalidate, max-age=" . $cacheControl ); # do an extra incantation if the HTTP protocol is really old if ($request->protocol =~ /(\d\.\d)/ && $1 < 1.1) { my $date = $datetime->epochToHttp(time() + $cacheControl); - $request->new_response->header( 'Expires' => $date ); + $response->header( 'Expires' => $date ); } } if ($self->getFilename) { - $request->new_response->headers( 'Content-Disposition' => qq{attachment; filename="}.$self->getFilename().'"'); + $response->headers( 'Content-Disposition' => qq{attachment; filename="}.$self->getFilename().'"'); } - $request->new_response->status($self->getStatus()); -# $request->new_response->status_line($self->getStatus().' '.$self->getStatusDescription()); # TODO - re-enable + $response->status($self->getStatus()); +# $response->status_line($self->getStatus().' '.$self->getStatusDescription()); # TODO - re-enable } return undef; } sub _sendMinimalHeader { my $self = shift; - my $request = $self->session->request; - $request->content_type('text/html; charset=UTF-8'); - $request->new_response->header('Cache-Control' => 'private'); - $request->no_cache(1); - $request->response->status($self->getStatus()); -# $request->response->status_line($self->getStatus().' '.$self->getStatusDescription()); # TODO - re-enable + my $response = $self->session->response; + $response->content_type('text/html; charset=UTF-8'); + $response->header('Cache-Control' => 'private'); + $response->no_cache(1); + $response->status($self->getStatus()); +# $response->status_line($self->getStatus().' '.$self->getStatusDescription()); # TODO - re-enable return undef; } @@ -375,14 +375,12 @@ sub setCookie { my $domain = shift; $ttl = (defined $ttl ? $ttl : '+10y'); - if ($self->session->request) { - $self->session->request->new_response->cookies->{$name} = { - value => $value, - path => '/', - expires => $ttl ne 'session' ? $ttl : undef, - domain => $domain, - }; - } + $self->session->response->cookies->{$name} = { + value => $value, + path => '/', + expires => $ttl ne 'session' ? $ttl : undef, + domain => $domain, + }; } diff --git a/lib/WebGUI/Session/Output.pm b/lib/WebGUI/Session/Output.pm index a0bbcb884..84424690a 100644 --- a/lib/WebGUI/Session/Output.pm +++ b/lib/WebGUI/Session/Output.pm @@ -95,16 +95,18 @@ sub print { print $handle $content; } elsif ($self->session->request) { - # TODO - take away this hack - if (ref $self->session->request->body eq 'ARRAY') { - push @{$self->session->request->body}, $content; - } else { - if ($self->session->request->logger) { - $self->session->request->logger->({ level => 'warn', message => "dropping content: $content" }); - } else { - warn "dropping content: $content"; - } - } + # TODO - put in IO bound delayed response + warn "content: $content"; +# $self->session->request->body([]) unless $self->session->request->body(); +# if (ref $self->session->request->body eq 'ARRAY') { +# push @{$self->session->request->body}, $content; +# } else { +# if ($self->session->request->logger) { +# $self->session->request->logger->({ level => 'warn', message => "dropping content: $content" }); +# } else { +# warn "dropping content";#: $content"; +# } +# } # $self->session->request->print($content); } else {