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