From e8c073b9df4c61076c897adb5f45a0683131f98f Mon Sep 17 00:00:00 2001 From: JT Smith Date: Tue, 18 Apr 2006 14:22:42 +0000 Subject: [PATCH] some http header changes --- docs/changelog/6.x.x.txt | 1 + lib/WebGUI.pm | 8 +- lib/WebGUI/Asset.pm | 2 +- lib/WebGUI/Asset/Event.pm | 1 + lib/WebGUI/Asset/Post/Thread.pm | 3 +- lib/WebGUI/Asset/Snippet.pm | 8 +- lib/WebGUI/Asset/Wobject.pm | 2 +- lib/WebGUI/Asset/Wobject/Article.pm | 13 +++ lib/WebGUI/Asset/Wobject/Collaboration.pm | 3 +- lib/WebGUI/Asset/Wobject/EventsCalendar.pm | 14 +++ lib/WebGUI/Asset/Wobject/Folder.pm | 14 +++ lib/WebGUI/Asset/Wobject/Layout.pm | 2 +- lib/WebGUI/Asset/Wobject/Matrix.pm | 14 +++ lib/WebGUI/Asset/Wobject/MessageBoard.pm | 14 +++ lib/WebGUI/Asset/Wobject/MultiSearch.pm | 14 +++ lib/WebGUI/Asset/Wobject/Product.pm | 14 +++ lib/WebGUI/Asset/Wobject/SQLReport.pm | 17 +++- lib/WebGUI/Asset/Wobject/SyndicatedContent.pm | 14 +++ lib/WebGUI/AssetExportHtml.pm | 4 +- lib/WebGUI/Operation/WebGUI.pm | 2 +- lib/WebGUI/Session/ErrorHandler.pm | 2 +- lib/WebGUI/Session/Http.pm | 92 ++++++++++++------- lib/WebGUI/Session/Style.pm | 2 +- 23 files changed, 206 insertions(+), 54 deletions(-) diff --git a/docs/changelog/6.x.x.txt b/docs/changelog/6.x.x.txt index a24ce86e0..5b15fdbeb 100644 --- a/docs/changelog/6.x.x.txt +++ b/docs/changelog/6.x.x.txt @@ -97,6 +97,7 @@ tabbed view to make scanning for content easier. - Help for forms now shows the fields that you should see with your UI level. There is a link to show all fields. + - added [ 732011 ] http header cache setting - fix [ 1410577 ] WebGUI::Session not included - fix a bug where a link was provided to become or delete non-existant users. - fix bugs with the in-memory session caching of user and group memberships diff --git a/lib/WebGUI.pm b/lib/WebGUI.pm index f15235e19..48b9ab370 100644 --- a/lib/WebGUI.pm +++ b/lib/WebGUI.pm @@ -84,7 +84,7 @@ sub contentHandler { my $session = WebGUI::Session->open($s->dir_config('WebguiRoot'),$r->dir_config('WebguiConfig'),$r, $s); if ($session->env->get("HTTP_X_MOZ") eq "prefetch") { # browser prefetch is a bad thing $session->http->setStatus("403","We don't allow prefetch, because it increases bandwidth, hurts stats, and can break web sites."); - $session->http->getHeader; + $session->http->sendHeader; } elsif ($session->setting->get("specialState") eq "upgrading") { upgrading($session); } elsif ($session->setting->get("specialState") eq "init") { @@ -104,7 +104,7 @@ sub contentHandler { $output = page($session); } $session->http->setCookie("wgSession",$session->var->{_var}{sessionId}) unless $session->var->{_var}{sessionId} eq $session->http->getCookies->{"wgSession"}; - $session->http->getHeader(); + $session->http->sendHeader(); unless ($session->http->isRedirect()) { $session->output->print($output); if ($session->errorHandler->canShowDebug()) { @@ -255,7 +255,7 @@ The current WebGUI::Session object. sub setup { my $session = shift; require WebGUI::Operation::WebGUI; - $session->http->getHeader; + $session->http->sendHeader; $session->output->print(WebGUI::Operation::WebGUI::www_setup($session)); } @@ -346,7 +346,7 @@ The current WebGUI::Session object. sub upgrading { my $session = shift; - $session->http->getHeader; + $session->http->sendHeader; open(FILE,"<".$session->config->getWebguiRoot."/docs/maintenance.html"); while () { $session->output->print($_); diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index 2f29e2842..c7fa019c9 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -1949,7 +1949,7 @@ sub www_manageAssets { my $out = $self->getAdminConsole->render($self->getAdminConsole->render("~~~")); my ($head, $foot) = split("~~~",$out); $self->session->style->sent(1); - $self->session->http->getHeader; + $self->session->http->sendHeader; $self->session->output->print($head); $self->session->output->print('
Manage | Search
',1); if ($self->session->scratch->get("manageAssetsSearchToggle")) { diff --git a/lib/WebGUI/Asset/Event.pm b/lib/WebGUI/Asset/Event.pm index 4992c8fff..7bfb09ef1 100644 --- a/lib/WebGUI/Asset/Event.pm +++ b/lib/WebGUI/Asset/Event.pm @@ -319,6 +319,7 @@ sub www_edit { sub www_view { my $self = shift; return $self->session->privilege->insufficient() unless ($self->canView); + $self->session->http->setCacheControl($self->get("visitorCacheTimeout")) if ($self->session->user->userId eq "1"); return $self->session->style->process($self->view,$self->getParent->getValue("styleTemplateId")); } diff --git a/lib/WebGUI/Asset/Post/Thread.pm b/lib/WebGUI/Asset/Post/Thread.pm index bfcc4d866..5748e2ba0 100644 --- a/lib/WebGUI/Asset/Post/Thread.pm +++ b/lib/WebGUI/Asset/Post/Thread.pm @@ -1021,7 +1021,8 @@ sub www_view { return $self->session->privilege->noAccess() unless $self->canView; my $check = $self->checkView; return $check if (defined $check); - $self->session->http->getHeader; + $self->session->http->setCacheControl($self->get("visitorCacheTimeout")) if ($self->session->user->userId eq "1"); + $self->session->http->sendHeader; $self->prepareView; my $style = $self->getParent->processStyle("~~~"); my ($head, $foot) = split("~~~",$style); diff --git a/lib/WebGUI/Asset/Snippet.pm b/lib/WebGUI/Asset/Snippet.pm index c565698fa..4700279e9 100644 --- a/lib/WebGUI/Asset/Snippet.pm +++ b/lib/WebGUI/Asset/Snippet.pm @@ -18,7 +18,6 @@ use strict; use WebGUI::Asset; use WebGUI::Asset::Template; use WebGUI::Macro; -use HTTP::Date; our @ISA = qw(WebGUI::Asset); @@ -203,12 +202,7 @@ sub www_view { my $self = shift; my $mimeType=$self->getValue('mimeType'); $self->session->http->setMimeType($mimeType || 'text/html'); - my $request = $self->session->request; - if (defined $request && $request->protocol =~ /(\d\.\d)/ && $1 >= 1.1){ - $request->header_out('Cache-Control', "max-age=" . $self->get("cacheTimeout")); - } elsif (defined $request) { - $request->header_out('Expires', HTTP::Date::time2str(time + $self->get("cacheTimeout"))); - } + $self->session->http->setCacheControl($self->get("cacheTimeout")); return $self->view(1); } diff --git a/lib/WebGUI/Asset/Wobject.pm b/lib/WebGUI/Asset/Wobject.pm index 830485fc8..fa94c1fc7 100644 --- a/lib/WebGUI/Asset/Wobject.pm +++ b/lib/WebGUI/Asset/Wobject.pm @@ -537,7 +537,7 @@ sub www_view { my $self = shift; my $check = $self->checkView; return $check if (defined $check); - $self->session->http->getHeader; + $self->session->http->sendHeader; $self->prepareView; my $style = $self->processStyle("~~~"); my ($head, $foot) = split("~~~",$style); diff --git a/lib/WebGUI/Asset/Wobject/Article.pm b/lib/WebGUI/Asset/Wobject/Article.pm index 710536856..c78db6a8a 100644 --- a/lib/WebGUI/Asset/Wobject/Article.pm +++ b/lib/WebGUI/Asset/Wobject/Article.pm @@ -371,6 +371,19 @@ sub www_deleteFile { return $self->www_edit; } +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("cacheTimeout")); + $self->SUPER::www_view(@_); +} 1; diff --git a/lib/WebGUI/Asset/Wobject/Collaboration.pm b/lib/WebGUI/Asset/Wobject/Collaboration.pm index 4f8c5efc6..09c9c23c9 100644 --- a/lib/WebGUI/Asset/Wobject/Collaboration.pm +++ b/lib/WebGUI/Asset/Wobject/Collaboration.pm @@ -1179,7 +1179,8 @@ sub _xml_encode { sub www_view { my $self = shift; my $disableCache = ($self->session->form->process("sortBy") ne ""); - return $self->SUPER::www_view($disableCache); + $self->session->http->setCacheControl($self->get("visitorCacheTimeout")) if ($self->session->user->userId eq "1" && !$disableCache); + return $self->SUPER::www_view(@_); } diff --git a/lib/WebGUI/Asset/Wobject/EventsCalendar.pm b/lib/WebGUI/Asset/Wobject/EventsCalendar.pm index 0d291ccdd..5d43995b0 100644 --- a/lib/WebGUI/Asset/Wobject/EventsCalendar.pm +++ b/lib/WebGUI/Asset/Wobject/EventsCalendar.pm @@ -509,6 +509,20 @@ sub view { } +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("visitorCacheTimeout")) if ($self->session->user->userId eq "1"); + $self->SUPER::www_view(@_); +} + 1; diff --git a/lib/WebGUI/Asset/Wobject/Folder.pm b/lib/WebGUI/Asset/Wobject/Folder.pm index 113067887..30a3c53b8 100644 --- a/lib/WebGUI/Asset/Wobject/Folder.pm +++ b/lib/WebGUI/Asset/Wobject/Folder.pm @@ -219,6 +219,20 @@ sub view { } +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("visitorCacheTimeout")) if ($self->session->user->userId eq "1"); + $self->SUPER::www_view(@_); +} + 1; diff --git a/lib/WebGUI/Asset/Wobject/Layout.pm b/lib/WebGUI/Asset/Wobject/Layout.pm index e355d2858..eb66b1033 100644 --- a/lib/WebGUI/Asset/Wobject/Layout.pm +++ b/lib/WebGUI/Asset/Wobject/Layout.pm @@ -286,7 +286,7 @@ sub www_view { my $ad = $adSpace->displayImpression if (defined $adSpace); $out =~ s/\Q$code/$ad/ges; } - $self->session->http->getHeader; + $self->session->http->sendHeader; $self->session->output->print($out, 1); return "chunked"; } diff --git a/lib/WebGUI/Asset/Wobject/Matrix.pm b/lib/WebGUI/Asset/Wobject/Matrix.pm index 92647b3f5..6ad95012c 100644 --- a/lib/WebGUI/Asset/Wobject/Matrix.pm +++ b/lib/WebGUI/Asset/Wobject/Matrix.pm @@ -1075,6 +1075,20 @@ sub view { return $out; } +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("visitorCacheTimeout")) if ($self->session->user->userId eq "1"); + $self->SUPER::www_view(@_); +} + #------------------------------------------------------------------- sub www_viewDetail { my $self = shift; diff --git a/lib/WebGUI/Asset/Wobject/MessageBoard.pm b/lib/WebGUI/Asset/Wobject/MessageBoard.pm index 22a58e707..931aeebb3 100644 --- a/lib/WebGUI/Asset/Wobject/MessageBoard.pm +++ b/lib/WebGUI/Asset/Wobject/MessageBoard.pm @@ -163,6 +163,20 @@ sub view { return $out; } +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("visitorCacheTimeout")) if ($self->session->user->userId eq "1"); + $self->SUPER::www_view(@_); +} + 1; diff --git a/lib/WebGUI/Asset/Wobject/MultiSearch.pm b/lib/WebGUI/Asset/Wobject/MultiSearch.pm index 973b1acfe..e31e1581e 100644 --- a/lib/WebGUI/Asset/Wobject/MultiSearch.pm +++ b/lib/WebGUI/Asset/Wobject/MultiSearch.pm @@ -140,4 +140,18 @@ sub view { } +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("cacheTimeout")); + $self->SUPER::www_view(@_); +} + 1; diff --git a/lib/WebGUI/Asset/Wobject/Product.pm b/lib/WebGUI/Asset/Wobject/Product.pm index 8f2ca01d0..63b9377c6 100644 --- a/lib/WebGUI/Asset/Wobject/Product.pm +++ b/lib/WebGUI/Asset/Wobject/Product.pm @@ -913,5 +913,19 @@ sub view { return $out; } +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("cacheTimeout")); + $self->SUPER::www_view(@_); +} + 1; diff --git a/lib/WebGUI/Asset/Wobject/SQLReport.pm b/lib/WebGUI/Asset/Wobject/SQLReport.pm index f335b09f0..ed23ec518 100644 --- a/lib/WebGUI/Asset/Wobject/SQLReport.pm +++ b/lib/WebGUI/Asset/Wobject/SQLReport.pm @@ -326,7 +326,7 @@ sub view { #use Data::Dumper; return '
'.Dumper($var).'
'; my $out = $self->processTemplate($var,undef,$self->{_viewTemplate}); if (!$self->session->var->isAdminOn && $self->get("cacheTimeout") > 10) { - WebGUI::Cache->new($self->session,"view_".$self->getId)->set($out,$self->get("visitorCacheTimeout")); + WebGUI::Cache->new($self->session,"view_".$self->getId)->set($out,$self->get("cacheTimeout")); } return $out; } @@ -491,5 +491,20 @@ sub _processQuery { } return \%var; } + +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("cacheTimeout")); + $self->SUPER::www_view(@_); +} + 1; diff --git a/lib/WebGUI/Asset/Wobject/SyndicatedContent.pm b/lib/WebGUI/Asset/Wobject/SyndicatedContent.pm index fca07594f..43dbbc373 100644 --- a/lib/WebGUI/Asset/Wobject/SyndicatedContent.pm +++ b/lib/WebGUI/Asset/Wobject/SyndicatedContent.pm @@ -619,6 +619,20 @@ sub _createRSSURLs{ } +#------------------------------------------------------------------- + +=head2 www_view () + +See WebGUI::Asset::Wobject::www_view() for details. + +=cut + +sub www_view { + my $self = shift; + $self->session->http->setCacheControl($self->get("cacheTimeout")); + $self->SUPER::www_view(@_); +} + #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ =head2 www_viewRSS090() diff --git a/lib/WebGUI/AssetExportHtml.pm b/lib/WebGUI/AssetExportHtml.pm index f9234b268..b582452ec 100644 --- a/lib/WebGUI/AssetExportHtml.pm +++ b/lib/WebGUI/AssetExportHtml.pm @@ -196,11 +196,11 @@ sub www_exportGenerate { return $self->session->privilege->insufficient() unless ($self->session->user->isInGroup(13)); # This routine is called in an IFRAME and prints status output directly to the browser. $|++; # Unbuffered data output - $self->session->output->print($self->session->http->getHeader()); + $self->session->http->sendHeader; my $startTime =$self->session->datetime->time(); my $error = $self->checkExportPath(); if ($error) { - $self->session->output->print($error); + $self->session->output->print($error,1); return; } my $i18n = WebGUI::International->new($self->session, 'Asset'); diff --git a/lib/WebGUI/Operation/WebGUI.pm b/lib/WebGUI/Operation/WebGUI.pm index 11d9ad9d3..30dc04c25 100644 --- a/lib/WebGUI/Operation/WebGUI.pm +++ b/lib/WebGUI/Operation/WebGUI.pm @@ -107,7 +107,7 @@ sub www_setup { $session->setting->set('companyURL',$session->form->url("companyURL")); $session->setting->set('companyEmail',$session->form->email("companyEmail")); $session->http->setRedirect($session->url->gateway()); - $session->http->getHeader; + $session->http->sendHeader; return ""; } else { $output .= 'Admin Account'; diff --git a/lib/WebGUI/Session/ErrorHandler.pm b/lib/WebGUI/Session/ErrorHandler.pm index 8d6e7d387..ea3b0d14c 100644 --- a/lib/WebGUI/Session/ErrorHandler.pm +++ b/lib/WebGUI/Session/ErrorHandler.pm @@ -190,7 +190,7 @@ sub fatal { Apache2::RequestUtil->request->content_type('text/html') if ($self->session->request); $self->getLogger->fatal($message); $self->getLogger->debug("Stack trace for FATAL ".$message."\n".$self->getStackTrace()); - $self->session->http->getHeader if ($self->session->request); + $self->session->http->sendHeader if ($self->session->request); unless ($self->canShowDebug()) { #NOTE: You can't internationalize this because with some types of errors that would cause an infinite loop. $self->session->output->print("

Problem With Request

diff --git a/lib/WebGUI/Session/Http.pm b/lib/WebGUI/Session/Http.pm index ee5d04a44..7ff37fe55 100644 --- a/lib/WebGUI/Session/Http.pm +++ b/lib/WebGUI/Session/Http.pm @@ -18,6 +18,7 @@ package WebGUI::Session::Http; use strict; use Apache2::Cookie; use APR::Request::Apache2; +use HTTP::Date; =head1 NAME @@ -33,8 +34,9 @@ This package allows the manipulation of HTTP protocol information. my $http = WebGUI::Session::Http->new($session); + $http->sendHeader(); + $cookies = $http->getCookies(); - $header = $http->getHeader(); $mimetype = $http->getMimeType(); $code = $http->getStatus(); $boolean = $http->isRedirect(); @@ -87,37 +89,6 @@ sub getCookies { } -#------------------------------------------------------------------- - -=head2 getHeader ( ) - -Generates an HTTP header. - -=cut - -sub getHeader { - my $self = shift; - return undef if ($self->{_http}{noHeader}); - return undef unless $self->session->request; - my %params; - if ($self->isRedirect()) { - $self->session->request->headers_out->set(Location => $self->{_http}{location}); - $self->session->request->status(301); - } else { - $self->session->request->content_type($self->{_http}{mimetype} || "text/html"); - if ($self->session->setting->get("preventProxyCache")) { - $self->session->request->headers_out->set(Expires => "-1d"); - } - if ($self->{_http}{filename}) { - $self->session->request->headers_out->set('Content-Disposition' => qq!attachment; filename="$self->{_http}{filename}"!); - } - } - #$params{"-cookie"} = $self->{_http}{cookie}; - $self->session->request->status_line($self->getStatus().' '.$self->{_http}{statusDescription}); - return; -} - - #------------------------------------------------------------------- =head2 getMimeType ( ) @@ -180,6 +151,36 @@ sub new { } +#------------------------------------------------------------------- + +=head2 sendHeader ( ) + +Generates and sends HTTP headers. + +=cut + +sub sendHeader { + my $self = shift; + return undef if ($self->{_http}{noHeader}); + return undef unless $self->session->request; + my %params; + if ($self->isRedirect()) { + $self->session->request->headers_out->set(Location => $self->{_http}{location}); + $self->session->request->status(301); + } else { + $self->session->request->content_type($self->{_http}{mimetype} || "text/html"); + if ($self->session->setting->get("preventProxyCache")) { + $self->session->request->headers_out->set(Expires => "-1d"); + } + if ($self->{_http}{filename}) { + $self->session->request->headers_out->set('Content-Disposition' => qq!attachment; filename="$self->{_http}{filename}"!); + } + } + $self->session->request->status_line($self->getStatus().' '.$self->{_http}{statusDescription}); + return; +} + + #------------------------------------------------------------------- =head2 session ( ) @@ -195,6 +196,33 @@ sub session { #------------------------------------------------------------------- +=head2 setCacheControl ( timeout ) + +Sets the cache control headers. + +=head3 timeout + +Either the number of seconds until the cache expires, or the word "none" to disable cache completely for this request. + +=cut + +sub setCacheControl { + my $self = shift; + my $timeout = shift; + my $request = $self->session->request; + if (defined $request) { + if ($timeout eq "none" || $self->session->setting->get("preventProxyCache")) { + $self->session->request->no_cache(1); + } elsif ($request->protocol =~ /(\d\.\d)/ && $1 >= 1.1){ + $request->header_out('Cache-Control', "max-age=" . $timeout); + } else { + $request->header_out('Expires', HTTP::Date::time2str(time + $self->get("cacheTimeout"))); + } + } +} + +#------------------------------------------------------------------- + =head2 setCookie ( name, value [ , timeToLive ] ) Sends a cookie to the browser. diff --git a/lib/WebGUI/Session/Style.pm b/lib/WebGUI/Session/Style.pm index adaca7cf4..74236ca92 100644 --- a/lib/WebGUI/Session/Style.pm +++ b/lib/WebGUI/Session/Style.pm @@ -187,7 +187,7 @@ if ($self->session->user->isInGroup(2)) { '; - $self->session->request->no_cache(1) if ($self->session->request); + $self->session->http->setCacheControl("none"); } my $style = WebGUI::Asset::Template->new($self->session,$templateId); my $output;