Catalyst::Plugin::Compress::Gzip - Gzip response


Catalyst-Plugin-Compress-Zlib documentation Contained in the Catalyst-Plugin-Compress-Zlib distribution.

Index


Code Index:

NAME

Top

Catalyst::Plugin::Compress::Gzip - Gzip response

SYNOPSIS

Top

    use Catalyst qw[Compress::Gzip];




DESCRIPTION

Top

Gzip compress response if client supports it.

METHODS

Top

finalize

SEE ALSO

Top

Catalyst.

AUTHOR

Top

Christian Hansen, ch@ngmedia.com

LICENSE

Top

This library is free software . You can redistribute it and/or modify it under the same terms as perl itself.


Catalyst-Plugin-Compress-Zlib documentation Contained in the Catalyst-Plugin-Compress-Zlib distribution.

package Catalyst::Plugin::Compress::Gzip;
use strict;
use warnings;
use MRO::Compat;

use Compress::Zlib ();

sub finalize {
    my $c = shift;

    if ( $c->response->content_encoding ) {
        return $c->next::method(@_);
    }

    unless ( $c->response->body ) {
        return $c->next::method(@_);
    }

    unless ( $c->response->status == 200 ) {
        return $c->next::method(@_);
    }

    unless ( $c->response->content_type =~ /^text|xml$|javascript$/ ) {
        return $c->next::method(@_);
    }

    my $accept = $c->request->header('Accept-Encoding') || '';

    unless ( index( $accept, "gzip" ) >= 0 ) {
        return $c->next::method(@_);
    }

   my $body = $c->response->body;
   eval { local $/; $body = <$body> } if ref $body;
   die "Response body is an unsupported kind of reference" if ref $body;

    $c->response->body( Compress::Zlib::memGzip( $body ) );
    $c->response->content_length( length( $c->response->body ) );
    $c->response->content_encoding('gzip');
    $c->response->headers->push_header( 'Vary', 'Accept-Encoding' );

    $c->next::method(@_);
}

1;

__END__