From e7fcf33a4d457f29ad7be2d0c0412683f7e238e4 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 22 Nov 2010 09:53:03 -0800 Subject: [PATCH] Move ifModifiedSince into WebGUI::Session::Request --- docs/migration.txt | 5 +++++ lib/WebGUI/Content/Asset.pm | 2 +- lib/WebGUI/Session/Http.pm | 34 ---------------------------------- lib/WebGUI/Session/Request.pm | 34 ++++++++++++++++++++++++++++++++++ t/Session/Http.t | 27 --------------------------- 5 files changed, 40 insertions(+), 62 deletions(-) diff --git a/docs/migration.txt b/docs/migration.txt index 85d80ed0c..34613b4de 100644 --- a/docs/migration.txt +++ b/docs/migration.txt @@ -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; diff --git a/lib/WebGUI/Content/Asset.pm b/lib/WebGUI/Content/Asset.pm index 078acf4dd..b876a022b 100644 --- a/lib/WebGUI/Content/Asset.pm +++ b/lib/WebGUI/Content/Asset.pm @@ -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"; diff --git a/lib/WebGUI/Session/Http.pm b/lib/WebGUI/Session/Http.pm index 9cf3892ca..e54d6c2bb 100644 --- a/lib/WebGUI/Session/Http.pm +++ b/lib/WebGUI/Session/Http.pm @@ -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 ( ) diff --git a/lib/WebGUI/Session/Request.pm b/lib/WebGUI/Session/Request.pm index 947cfea2f..2f8defb70 100644 --- a/lib/WebGUI/Session/Request.pm +++ b/lib/WebGUI/Session/Request.pm @@ -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 object. diff --git a/t/Session/Http.t b/t/Session/Http.t index c44b4ecab..c067dd72d 100644 --- a/t/Session/Http.t +++ b/t/Session/Http.t @@ -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; ####################################################