File Coverage

blib/lib/Protocol/SPDY/Compress.pm
Criterion Covered Total %
statement 38 38 100.0
branch 9 14 64.2
condition n/a
subroutine 9 9 100.0
pod 5 5 100.0
total 61 66 92.4


line stmt bran cond sub pod time code
1             package Protocol::SPDY::Compress;
2             {
3             $Protocol::SPDY::Compress::VERSION = '0.999_007';
4             }
5 3     3   10 use strict;
  3         4  
  3         126  
6 3     3   11 use warnings;
  3         4  
  3         90  
7              
8             =head1 NAME
9              
10             Protocol::SPDY::Compress - handle zlib compression/decompression
11              
12             =head1 VERSION
13              
14             version 0.999_007
15              
16             =head1 SYNOPSIS
17              
18             use Protocol::SPDY;
19              
20             =head1 DESCRIPTION
21              
22             Used internally. See L instead.
23              
24             =cut
25              
26 3     3   1552 use Compress::Raw::Zlib qw(Z_OK Z_SYNC_FLUSH WANT_GZIP_OR_ZLIB);
  3         13473  
  3         230  
27 3     3   17 use Protocol::SPDY::Constants ':all';
  3         4  
  3         1235  
28              
29             =head1 METHODS
30              
31             =cut
32              
33             =head2 new
34              
35             Instantiate - takes no parameters.
36              
37             =cut
38              
39 14     14 1 1530 sub new { my $class = shift; bless { }, $class }
  14         76  
40              
41             =head2 inflater
42              
43             Returns an inflater object, for decompressing data.
44              
45             =cut
46              
47             sub inflater {
48 20     20 1 23 my $self = shift;
49 20 100       66 return $self->{inflater} if $self->{inflater};
50 8         74 my ($d, $status) = Compress::Raw::Zlib::Inflate->new(
51             -WindowBits => WANT_GZIP_OR_ZLIB,
52             -Dictionary => ZLIB_DICTIONARY,
53             );
54 8 50       2125 die "Zlib failure: $status" unless $d;
55 8         24 $self->{inflater} = $d;
56             }
57              
58             =head2 deflater
59              
60             Returns a deflater object, for compressing data.
61              
62             =cut
63              
64             sub deflater {
65 18     18 1 21 my $self = shift;
66 18 100       55 return $self->{deflater} if $self->{deflater};
67 6         55 my ($d, $status) = Compress::Raw::Zlib::Deflate->new(
68             -WindowBits => 12,
69             -Dictionary => ZLIB_DICTIONARY,
70             );
71 6 50       2785 die "Zlib failure: $status" unless $d;
72 6         20 $self->{deflater} = $d;
73             }
74              
75             =head2 decompress
76              
77             Given a scalar containing bytes, this will return the decompressed
78             contents as a scalar, or raise an exception on failure.
79              
80             =cut
81              
82             sub decompress {
83 20     20 1 65 my $self = shift;
84 20         29 my $data = shift;
85 20         41 my $comp = $self->inflater;
86 20         317 my $status = $comp->inflate($data => \my $out);
87 20 50       66 die "Failed: $status" unless $status == Z_OK;
88 20         129 $out;
89             }
90              
91             =head2 compress
92              
93             Given a scalar containing bytes, this will return the compressed
94             contents as a scalar, or raise an exception on failure.
95              
96             =cut
97              
98             sub compress {
99 18     18 1 18 my $self = shift;
100 18         24 my $data = shift;
101 18         42 my $comp = $self->deflater;
102              
103 18         159 my $status = $comp->deflate($data => \my $start);
104 18 50       64 die "Failed: $status" unless $status == Z_OK;
105 18         139 $status = $comp->flush(\my $extra => Z_SYNC_FLUSH);
106 18 50       373 die "Failed: $status" unless $status == Z_OK;
107 18         95 return $start . $extra;
108             }
109              
110             1;
111              
112             __END__