View | Details | Raw Unified | Return to bug 10016
Collapse All | Expand All

(-)a/C4/Output.pm (-5 / +15 lines)
Lines 255-261 sub pagination_bar { Link Here
255
255
256
=item output_with_http_headers
256
=item output_with_http_headers
257
257
258
   &output_with_http_headers($query, $cookie, $data, $content_type[, $status])
258
   &output_with_http_headers($query, $cookie, $data, $content_type[, $status[, $extra_options]])
259
259
260
Outputs $data with the appropriate HTTP headers,
260
Outputs $data with the appropriate HTTP headers,
261
the authentication cookie $cookie and a Content-Type specified in
261
the authentication cookie $cookie and a Content-Type specified in
Lines 267-278 $content_type is one of the following: 'html', 'js', 'json', 'xml', 'rss', or 'a Link Here
267
267
268
$status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
268
$status is an HTTP status message, like '403 Authentication Required'. It defaults to '200 OK'.
269
269
270
$extra_options is hashref.  If the key 'force_no_caching' is present and has
271
a true value, the HTTP headers include directives to force there to be no
272
caching whatsoever.
273
270
=cut
274
=cut
271
275
272
sub output_with_http_headers {
276
sub output_with_http_headers {
273
    my ( $query, $cookie, $data, $content_type, $status ) = @_;
277
    my ( $query, $cookie, $data, $content_type, $status, $extra_options ) = @_;
274
    $status ||= '200 OK';
278
    $status ||= '200 OK';
275
279
280
    $extra_options //= {};
281
276
    my %content_type_map = (
282
    my %content_type_map = (
277
        'html' => 'text/html',
283
        'html' => 'text/html',
278
        'js'   => 'text/javascript',
284
        'js'   => 'text/javascript',
Lines 285-297 sub output_with_http_headers { Link Here
285
    );
291
    );
286
292
287
    die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
293
    die "Unknown content type '$content_type'" if ( !defined( $content_type_map{$content_type} ) );
294
    my $cache_policy = 'no-cache';
295
    $cache_policy .= ', no-store, max-age=0' if $extra_options->{force_no_caching};
288
    my $options = {
296
    my $options = {
289
        type    => $content_type_map{$content_type},
297
        type    => $content_type_map{$content_type},
290
        status  => $status,
298
        status  => $status,
291
        charset => 'UTF-8',
299
        charset => 'UTF-8',
292
        Pragma          => 'no-cache',
300
        Pragma          => 'no-cache',
293
        'Cache-Control' => 'no-cache',
301
        'Cache-Control' => $cache_policy,
294
    };
302
    };
303
    $options->{expires} = 'now' if $extra_options->{force_no_caching};
304
295
    $options->{cookie} = $cookie if $cookie;
305
    $options->{cookie} = $cookie if $cookie;
296
    if ($content_type eq 'html') {  # guaranteed to be one of the content_type_map keys, else we'd have died
306
    if ($content_type eq 'html') {  # guaranteed to be one of the content_type_map keys, else we'd have died
297
        $options->{'Content-Style-Type' } = 'text/css';
307
        $options->{'Content-Style-Type' } = 'text/css';
Lines 308-315 sub output_with_http_headers { Link Here
308
}
318
}
309
319
310
sub output_html_with_http_headers {
320
sub output_html_with_http_headers {
311
    my ( $query, $cookie, $data, $status ) = @_;
321
    my ( $query, $cookie, $data, $status, $extra_options ) = @_;
312
    output_with_http_headers( $query, $cookie, $data, 'html', $status );
322
    output_with_http_headers( $query, $cookie, $data, 'html', $status, $extra_options );
313
}
323
}
314
324
315
325
(-)a/opac/sco/sco-main.pl (-1 / +1 lines)
Lines 268-271 $template->param( Link Here
268
    SCOUserCSS => C4::Context->preference('SCOUserCSS'),
268
    SCOUserCSS => C4::Context->preference('SCOUserCSS'),
269
);
269
);
270
270
271
output_html_with_http_headers $query, $cookie, $template->output;
271
output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
(-)a/t/Output.t (-2 / +21 lines)
Lines 3-10 Link Here
3
use strict;
3
use strict;
4
use warnings;
4
use warnings;
5
5
6
use Test::More tests => 1;
6
use Test::More tests => 5;
7
use CGI;
7
8
8
BEGIN {
9
BEGIN {
9
    use_ok('C4::Output');
10
    use_ok('C4::Output');
10
}
11
}
11
- 
12
13
my $query = CGI->new();
14
my $cookie;
15
my $output = 'foobarbaz';
16
17
{
18
    local *STDOUT;
19
    my $stdout;
20
    open STDOUT, '>', \$stdout;
21
    output_html_with_http_headers $query, $cookie, $output, undef, { force_no_caching => 1 };
22
    like($stdout, qr/Cache-control: no-cache, no-store, max-age=0/, 'force_no_caching sets Cache-control as desired');
23
    like($stdout, qr/Expires: /, 'force_no_caching sets an Expires header');
24
    $stdout = '';
25
    close STDOUT;
26
    open STDOUT, '>', \$stdout;
27
    output_html_with_http_headers $query, $cookie, $output, undef, undef;
28
    like($stdout, qr/Cache-control: no-cache[^,]/, 'not using force_no_caching sets Cache-control as desired');
29
    unlike($stdout, qr/Expires: /, 'force_no_caching does not set an Expires header');
30
}

Return to bug 10016