[Rt-commit] rt branch, 4.0/helpers-http-cache-headers, created. rt-4.0.8-306-g5e4f209

Todd Wade todd at bestpractical.com
Thu Apr 4 11:38:39 EDT 2013


The branch, 4.0/helpers-http-cache-headers has been created
        at  5e4f20939598e753239ccd3cd9d5b13001375ec2 (commit)

- Log -----------------------------------------------------------------
commit 5e4f20939598e753239ccd3cd9d5b13001375ec2
Author: Todd Wade <todd at bestpractical.com>
Date:   Fri Dec 28 16:53:07 2012 -0500

    set cache related headers on helper endpoints
    
    * 1-2 min cache for share/html/Helpers/Autocomplete/*
    * no cache for share/html/Helpers/*

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 3295c0d..4d64c95 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -871,6 +871,36 @@ sub Redirect {
     $HTML::Mason::Commands::m->abort;
 }
 
+=head2 CacheControlExpiresHeaders
+
+set both Cache-Control and Expires http headers
+
+=cut
+
+sub CacheControlExpiresHeaders {
+    my %args = @_;
+
+    if ( ! defined $args{Time} ) {
+        $args{Time} = 0;
+    } elsif ( $args{Time} eq 'no-cache' ) {
+        $args{Time} = 0;
+    } elsif ( $args{Time} eq 'forever' ) {
+        $args{Time} = 30 * 24 * 60 * 60;
+    }
+
+    my $CacheControl = $args{Time}
+        ? sprintf "max-age=%d, private", $args{Time}
+        : 'no-cache'
+    ;
+    $HTML::Mason::Commands::r->headers_out->{'Cache-Control'} = $CacheControl;
+
+    my $expires = RT::Date->new(RT->SystemUser);
+    $expires->SetToNow;
+    $expires->AddSeconds( $args{Time} ) if $args{Time};
+
+    $HTML::Mason::Commands::r->headers_out->{'Expires'} = $expires->RFC2616;
+}
+
 =head2 StaticFileHeaders 
 
 Send the browser a few headers to try to get it to (somewhat agressively)
diff --git a/share/html/Helpers/Autocomplete/autohandler b/share/html/Helpers/Autocomplete/autohandler
new file mode 100644
index 0000000..73d9966
--- /dev/null
+++ b/share/html/Helpers/Autocomplete/autohandler
@@ -0,0 +1,53 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%#
+%# COPYRIGHT:
+%#
+%# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
+%#                                          <sales at bestpractical.com>
+%#
+%# (Except where explicitly superseded by other copyright notices)
+%#
+%#
+%# LICENSE:
+%#
+%# This work is made available to you under the terms of Version 2 of
+%# the GNU General Public License. A copy of that license should have
+%# been provided with this software, but in any event can be snarfed
+%# from www.gnu.org.
+%#
+%# This work is distributed in the hope that it will be useful, but
+%# WITHOUT ANY WARRANTY; without even the implied warranty of
+%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+%# General Public License for more details.
+%#
+%# You should have received a copy of the GNU General Public License
+%# along with this program; if not, write to the Free Software
+%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+%# 02110-1301 or visit their web page on the internet at
+%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+%#
+%#
+%# CONTRIBUTION SUBMISSION POLICY:
+%#
+%# (The following paragraph is not intended to limit the rights granted
+%# to you to modify and distribute this software under the terms of
+%# the GNU General Public License and is only of importance to you if
+%# you choose to contribute your changes and enhancements to the
+%# community by submitting them to Best Practical Solutions, LLC.)
+%#
+%# By intentionally submitting any modifications, corrections or
+%# derivatives to this work, or any other work intended for use with
+%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+%# you are the copyright holder for those contributions and you grant
+%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+%# royalty-free, perpetual, license to use, copy, create derivative
+%# works based on those contributions, and sublicense and distribute
+%# those contributions and any derivatives thereof.
+%#
+%# END BPS TAGGED BLOCK }}}
+<%init>
+  RT::Interface::Web::CacheControlExpiresHeaders( Time => 2 * 60 );
+
+  $m->call_next;
+</%init>
+
diff --git a/share/html/Helpers/autohandler b/share/html/Helpers/autohandler
new file mode 100644
index 0000000..5bd9f55
--- /dev/null
+++ b/share/html/Helpers/autohandler
@@ -0,0 +1,52 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%#
+%# COPYRIGHT:
+%#
+%# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
+%#                                          <sales at bestpractical.com>
+%#
+%# (Except where explicitly superseded by other copyright notices)
+%#
+%#
+%# LICENSE:
+%#
+%# This work is made available to you under the terms of Version 2 of
+%# the GNU General Public License. A copy of that license should have
+%# been provided with this software, but in any event can be snarfed
+%# from www.gnu.org.
+%#
+%# This work is distributed in the hope that it will be useful, but
+%# WITHOUT ANY WARRANTY; without even the implied warranty of
+%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+%# General Public License for more details.
+%#
+%# You should have received a copy of the GNU General Public License
+%# along with this program; if not, write to the Free Software
+%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+%# 02110-1301 or visit their web page on the internet at
+%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+%#
+%#
+%# CONTRIBUTION SUBMISSION POLICY:
+%#
+%# (The following paragraph is not intended to limit the rights granted
+%# to you to modify and distribute this software under the terms of
+%# the GNU General Public License and is only of importance to you if
+%# you choose to contribute your changes and enhancements to the
+%# community by submitting them to Best Practical Solutions, LLC.)
+%#
+%# By intentionally submitting any modifications, corrections or
+%# derivatives to this work, or any other work intended for use with
+%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+%# you are the copyright holder for those contributions and you grant
+%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+%# royalty-free, perpetual, license to use, copy, create derivative
+%# works based on those contributions, and sublicense and distribute
+%# those contributions and any derivatives thereof.
+%#
+%# END BPS TAGGED BLOCK }}}
+<%init>
+  RT::Interface::Web::CacheControlExpiresHeaders();
+
+  $m->call_next;
+</%init>
diff --git a/t/web/helpers-http-cache-headers.t b/t/web/helpers-http-cache-headers.t
new file mode 100644
index 0000000..74ca7b9
--- /dev/null
+++ b/t/web/helpers-http-cache-headers.t
@@ -0,0 +1,93 @@
+use strict;
+use warnings;
+use utf8;
+
+# trs: I'd write a quick t/web/caching-headers.t file which loops the available
+#      endpoints checking for the right headers.
+
+use File::Find;
+
+BEGIN {
+    # Ensure that the test and server processes use the same fixed time.
+    use constant TIME => time();
+    use Test::MockTime 'set_fixed_time';
+    set_fixed_time(TIME);
+
+    use RT::Test
+        tests   => undef,
+        config  => "use Test::MockTime 'set_fixed_time'; set_fixed_time(".TIME.");";
+}
+
+my ($base, $m) = RT::Test->started_ok;
+ok $m->login, 'logged in';
+
+my $docroot = join '/', qw(share html);
+
+# find endpoints to loop over
+my @endpoints;
+find({
+  wanted => sub {
+    if ( -f $_ && $_ !~ m|autohandler$| ) {
+      ( my $endpoint = $_ ) =~ s|^$docroot||;
+      push @endpoints, $endpoint;
+    }
+  },
+  no_chdir => 1,
+} => join '/', $docroot => 'Helpers');
+
+my $ticket_id;
+diag "create a ticket via the API";
+{
+    my $ticket = RT::Ticket->new( RT->SystemUser );
+    my ($id, $txn, $msg) = $ticket->Create(
+        Queue => 'General',
+        Subject => 'test ticket',
+    );
+    ok $id, 'created a ticket #'. $id or diag "error: $msg";
+    is $ticket->Subject, 'test ticket', 'correct subject';
+    $ticket_id = $id;
+}
+
+
+my $expected;
+diag "set up expected date headers";
+{
+  my $autocomplete = RT::Date->new(RT->SystemUser);
+  $autocomplete->SetToNow;
+  $autocomplete->AddSeconds(2*60); 
+
+  my $default = RT::Date->new(RT->SystemUser);
+  $default->SetToNow;
+
+  # expected headers
+  $expected = {
+    Autocomplete => {
+      'Cache-Control' => 'max-age=120, private',
+      'Expires'       => $autocomplete->RFC2616,
+    },
+    default      => {
+      'Cache-Control' => 'no-cache',
+      'Expires'       => $default->RFC2616,
+    },
+  };
+}
+
+foreach my $endpoint ( @endpoints ) {
+  $m->get_ok( $endpoint . "?id=${ticket_id}&Status=open&Requestor=root" );
+
+  my $header_key = $endpoint =~ m|Autocomplete| ? 'Autocomplete' : 'default';
+  my $headers = $expected->{$header_key};
+
+  is(
+      $m->res->header('Cache-Control') => $headers->{'Cache-Control'},
+      'got expected Cache-Control header'
+  );
+
+  is(
+    $m->res->header('Expires') => $headers->{'Expires'},
+    'got expected Expires header'
+  );
+}
+
+undef $m;
+done_testing;

-----------------------------------------------------------------------


More information about the Rt-commit mailing list