Move ifModifiedSince into WebGUI::Session::Request

This commit is contained in:
Colin Kuskie 2010-11-22 09:53:03 -08:00
parent 995b04e7de
commit e7fcf33a4d
5 changed files with 40 additions and 62 deletions

View file

@ -323,3 +323,8 @@ NEW: $session->response->location($url);
OLD: $session->http->getRedirectLocation();
NEW: $session->response->location();
ifModifiedSince was moved from WebGUI::Session::Http to WebGUI::Session::Request.
OLD: $session->http->ifModifiedSince;
NEW: $session->request->ifModifiedSince;

View file

@ -69,7 +69,7 @@ sub dispatch {
WebGUI::PassiveAnalytics::Logging::log($session, $asset);
# display from cache if page hasn't been modified.
if ($session->user->isVisitor
&& !$session->http->ifModifiedSince($asset->getContentLastModified, $session->setting->get('maxCacheTimeout'))) {
&& !$session->request->ifModifiedSince($asset->getContentLastModified, $session->setting->get('maxCacheTimeout'))) {
$session->response->status("304");
$session->http->sendHeader;
return "chunked";

View file

@ -121,40 +121,6 @@ sub getStreamedFile {
}
#-------------------------------------------------------------------
=head2 ifModifiedSince ( epoch [, maxCacheTimeout] )
Returns 1 if the epoch is greater than the modified date check.
=head3 epoch
The date that the requested content was last modified in epoch format.
=head3 maxCacheTimeout
A modifier to the epoch, that allows us to set a maximum timeout where content will appear to
have changed and a new page request will be allowed to be processed.
=cut
sub ifModifiedSince {
my $self = shift;
my $epoch = shift;
my $maxCacheTimeout = shift;
my $modified = $self->session->request->header('If-Modified-Since');
return 1 if ($modified eq "");
$modified = HTTP::Date::str2time($modified);
##Implement a step function that increments the epoch time in integer multiples of
##the maximum cache time. Used to handle the case where layouts containing macros
##(like assetproxied Navigations) can be periodically updated.
if ($maxCacheTimeout) {
my $delta = time() - $epoch;
$epoch += $delta - ($delta % $maxCacheTimeout);
}
return ($epoch > $modified);
}
#-------------------------------------------------------------------
=head2 isRedirect ( )

View file

@ -76,6 +76,40 @@ sub callerIsSearchSite {
#-------------------------------------------------------------------
=head2 ifModifiedSince ( epoch [, maxCacheTimeout] )
Returns 1 if the epoch is greater than the modified date check.
=head3 epoch
The date that the requested content was last modified in epoch format.
=head3 maxCacheTimeout
A modifier to the epoch, that allows us to set a maximum timeout where content will appear to
have changed and a new page request will be allowed to be processed.
=cut
sub ifModifiedSince {
my $self = shift;
my $epoch = shift;
my $maxCacheTimeout = shift;
my $modified = $self->header('If-Modified-Since');
return 1 if ($modified eq "");
$modified = HTTP::Date::str2time($modified);
##Implement a step function that increments the epoch time in integer multiples of
##the maximum cache time. Used to handle the case where layouts containing macros
##(like assetproxied Navigations) can be periodically updated.
if ($maxCacheTimeout) {
my $delta = time() - $epoch;
$epoch += $delta - ($delta % $maxCacheTimeout);
}
return ($epoch > $modified);
}
#-------------------------------------------------------------------
=head2 new_response ()
Creates a new L<WebGUI::Session::Response> object.

View file

@ -385,33 +385,6 @@ is($http->sendHeader, undef, 'sendHeader returns undef when no request object is
}
####################################################
#
# ifModifiedSince
#
####################################################
##Clear request object to run a new set of requests
{
##A new, clean session
my $http_request = HTTP::Request::Common::GET('http://'.$session->config->get('sitename')->[0]);
$http_request->header('If-Modified-Since' => '');
my $session = WebGUI::Test->newSession('nocleanup', $http_request);
my $guard = WebGUI::Test->addToCleanup($session);
ok $session->http->ifModifiedSince(0), 'ifModifiedSince: empty header always returns true';
}
{
my $http_request = HTTP::Request::Common::GET('http://'.$session->config->get('sitename')->[0]);
$http_request->header('If-Modified-Since' => $session->datetime->epochToHttp(WebGUI::Test->webguiBirthday));
my $session = WebGUI::Test->newSession('nocleanup', $http_request);
my $guard = WebGUI::Test->cleanupGuard($session);
ok $session->http->ifModifiedSince(WebGUI::Test->webguiBirthday + 5), '... epoch check, true';
ok !$session->http->ifModifiedSince(WebGUI::Test->webguiBirthday - 5), '... epoch check, false';
ok $session->http->ifModifiedSince(WebGUI::Test->webguiBirthday - 5, 3600), '... epoch check, made true by maxCacheTimeout';
}
done_testing;
####################################################