From 214d99a14062ad4cdd207fd926e32ebf62b752e7 Mon Sep 17 00:00:00 2001
From: Jim Brandt <jbrandt@bestpractical.com>
Date: Fri, 22 Dec 2023 13:57:15 -0500
Subject: Convert other Mason templates to new headers template

27bd738eaf created a single method in Web.pm, CacheControlExpiresHeaders
to generate HTTP response headers, specifically those related to
caching instructions for browsers. That was applied to Helpers, but
wasn't used for regular RT pages.

Later, 915eb4b7d0 sought to fix a regression that resulted in
cache headers not being sent for static files returned via
Plack::Middleware::Static. That fix went to great lengths to
try to re-use functionality from CacheControlExpiresHeaders,
including moving all of the code to GetStaticHeaders. This
probably wasn't really needed since it's reasonable to allow
the special case static handler to send it's own one or two headers.
It also made the code confusing since dynamic pages in Mason
called CacheControlExpiresHeaders, which then called GetStaticHeaders
to get headers for responses that were not static.

This update gets all of the Mason web pages using the same code
for these headers. It leaves the current methods in place to continue
handling static files. That can likely be simplified and cleaned up
in a future commit.

Patch-Name: fix_browser_cache2.diff
Applied-Upstream: 5.0.6, commit:468f86bd3e82c3b5b5ef7087d416a7509d4b1abe
Origin: vendor
Forwarded: not-needed
---
 share/html/Helpers/Autocomplete/autohandler | 6 ++----
 share/html/Helpers/RightsInspector/Search   | 2 +-
 share/html/Helpers/autohandler              | 5 ++---
 t/web/helpers-http-cache-headers.t          | 4 ++--
 4 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/share/html/Helpers/Autocomplete/autohandler b/share/html/Helpers/Autocomplete/autohandler
index 63463278..33312ca1 100644
--- a/share/html/Helpers/Autocomplete/autohandler
+++ b/share/html/Helpers/Autocomplete/autohandler
@@ -46,8 +46,6 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 <%init>
-  RT::Interface::Web::CacheControlExpiresHeaders( Time => 2 * 60 );
-
-  $m->call_next;
+$m->comp('/Elements/HttpResponseHeaders', MaxAgeSeconds => 2 * 60);
+$m->call_next;
 </%init>
-
diff --git a/share/html/Helpers/RightsInspector/Search b/share/html/Helpers/RightsInspector/Search
index 387bcfc1..83b74926 100644
--- a/share/html/Helpers/RightsInspector/Search
+++ b/share/html/Helpers/RightsInspector/Search
@@ -49,7 +49,7 @@
 use RT::RightsInspector;
 my $results = RT::RightsInspector->Search(%ARGS);
 $r->content_type('application/json; charset=utf-8');
-RT::Interface::Web::CacheControlExpiresHeaders( Time => 'no-cache' );
+$m->comp('/Elements/HttpResponseHeaders');
 $m->out(JSON($results));
 $m->abort;
 </%INIT>
diff --git a/share/html/Helpers/autohandler b/share/html/Helpers/autohandler
index 6c74ee3f..3466485b 100644
--- a/share/html/Helpers/autohandler
+++ b/share/html/Helpers/autohandler
@@ -46,7 +46,6 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 <%init>
-  RT::Interface::Web::CacheControlExpiresHeaders( Time => 'no-cache' );
-
-  $m->call_next;
+$m->comp('/Elements/HttpResponseHeaders');
+$m->call_next;
 </%init>
diff --git a/t/web/helpers-http-cache-headers.t b/t/web/helpers-http-cache-headers.t
index 2e661a23..cb8799d1 100644
--- a/t/web/helpers-http-cache-headers.t
+++ b/t/web/helpers-http-cache-headers.t
@@ -73,8 +73,8 @@ diag "set up expected date headers";
       'Expires'       => 'Sun, 05 May 2013 15:28:19 GMT',
     },
     default      => {
-      'Cache-Control' => 'no-cache',
-      'Expires'       => 'Fri, 05 Apr 2013 15:28:19 GMT',
+      'Cache-Control' => 'no-cache, max-age=0',
+      'Expires'       => 'Fri, 05 Apr 2013 15:27:49 GMT',
     },
   };
 
