lib/PAGI/Middleware/WebSocket/Compression.pm

filter: Statementstmt 63.6% 63 / 99 Branchbran 21.4% 9 / 42 Conditioncond 32.0% 8 / 25 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 48.1% 93 / 193 CC 34 SCAR 39.3
CC
34
Cov
48%
__ANON__
102.0
__ANON__
90.0
__ANON__
8.6
CRAP 50.8
« blib/lib/PAGI/App/SSE/Pubsub.pm ↑ Summary blib/lib/PAGI/App/WebSocket/Echo.pm »
1 package PAGI::Middleware::WebSocket::Compression;
2 2 $PAGI::Middleware::WebSocket::Compression::VERSION = '0.002000';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 PAGI::Middleware::WebSocket::Compression - WebSocket per-message compression
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::Middleware::Builder;
15  
16 my $app = builder {
17 enable 'WebSocket::Compression',
18 level => 6,
19 min_size => 128;
20 $my_app;
21 };
22  
23 =head1 DESCRIPTION
24  
25 PAGI::Middleware::WebSocket::Compression implements per-message deflate
26 compression (RFC 7692) for WebSocket connections. It negotiates the
27 permessage-deflate extension and transparently compresses/decompresses
28 messages.
29  
30 =head1 CONFIGURATION
31  
32 =over 4
33  
34 =item * level (default: 6)
35  
36 Compression level (1-9). Higher = better compression, slower.
37  
38 =item * min_size (default: 128)
39  
40 Minimum message size to compress. Messages smaller than this are sent
41 uncompressed.
42  
43 =item * server_no_context_takeover (default: 0)
44  
45 If true, don't use context takeover for server-to-client messages.
46  
47 =item * client_no_context_takeover (default: 0)
48  
49 If true, request client to not use context takeover.
50  
51 =back
52  
53 =cut
54  
55 sub _init {
56 3 ▶ my ($self, $config) = @_;
Subroutine _init: called CC=5 SCAR=16.3
57  
58 3 ▶ $self->{level} = $config->{level} // 6;
Condition100%2/2
$config->{"level"} // 6
l!l
12
MC/DC100%1/1
$config->{"level"} // 6
$config->{"level"}
Truth table100%2/2
A
A= $config->{"level"}
Aresult
11
00
59 3 ▶ $self->{min_size} = $config->{min_size} // 128;
Condition100%2/2
$config->{"min_size"} // 128
l!l
12
MC/DC100%1/1
$config->{"min_size"} // 128
$config->{"min_size"}
Truth table100%2/2
A
A= $config->{"min_size"}
Aresult
11
00
60 3 ▶ $self->{server_no_context_takeover} = $config->{server_no_context_takeover} // 0;
Condition50%1/2
$config->{"server_no_context_takeover"} // 0
l!l
03
MC/DC0%0/1
$config->{"server_no_context_takeover"} // 0
$config->{"server_no_context_takeover"}
Truth table50%1/2
A
A= $config->{"server_no_context_takeover"}
Aresult
11
00
61 3 ▶ $self->{client_no_context_takeover} = $config->{client_no_context_takeover} // 0;
Condition50%1/2
$config->{"client_no_context_takeover"} // 0
l!l
03
MC/DC0%0/1
$config->{"client_no_context_takeover"} // 0
$config->{"client_no_context_takeover"}
Truth table50%1/2
A
A= $config->{"client_no_context_takeover"}
Aresult
11
00
62 }
63  
64 sub wrap {
65 3 ▶ my ($self, $app) = @_;
Subroutine wrap: called CC=1 SCAR=2.4
Pod documented
66  
67 3 ▶ return async sub {
Subroutine __ANON__: called CC=4 SCAR=21.5
68 3 my ($scope, $receive, $send) = @_;
69 # Only apply to WebSocket connections
70 3 ▶ if ($scope->{type} ne 'websocket') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "websocket") 1 2 3
71 1 await $app->($scope, $receive, $send);
72 1 return;
73 }
74  
75 # Check if client offered permessage-deflate
76 2 my $extensions = $self->_parse_extensions($scope);
77 2 my $has_deflate = exists $extensions->{'permessage-deflate'};
78  
79 2 ▶ unless ($has_deflate) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_deflate) 1 1 2
80 # No compression support, pass through
81 1 await $app->($scope, $receive, $send);
82 1 return;
83 }
84  
85 # Try to load Compress::Raw::Zlib
86 1 my $have_zlib = eval { require Compress::Raw::Zlib; 1 };
87 1 ▶ unless ($have_zlib) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_zlib) 0 1 1
88 0 await $app->($scope, $receive, $send);
89 0 return;
90 }
91  
92 # Create compression/decompression streams
93 1 my ($deflator, $inflator);
94 1 my $compression_active = 0;
95  
96 my $init_streams = sub {
97 my ($d_status, $deflate) = Compress::Raw::Zlib::Deflate->new(
98 -Level => $self->{level},
99 1 ▶ -WindowBits => -15, # Raw deflate
Subroutine __ANON__: called CC=3 SCAR=12.0
100 -AppendOutput => 1,
101 );
102 1 my ($i_status, $inflate) = Compress::Raw::Zlib::Inflate->new(
103 -WindowBits => -15,
104 -AppendOutput => 1,
105 );
106  
107 1 ▶ return ($deflate, $inflate) if $d_status == Compress::Raw::Zlib::Z_OK()
Branch: 1 branch
BranchTrueFalseTotal
if $d_status == Compress::Raw::Zlib::Z_OK() and $i_status == Compress::Raw::Zlib::Z_OK() 0 1 1
Condition33%1/3
$d_status == Compress::Raw::Zlib::Z_OK() and $i_status == Compress::Raw::Zlib::Z_OK()
!ll&&!rl&&r
100
MC/DC0%0/2
$d_status == Compress::Raw::Zlib::Z_OK() and $i_status == Compress::Raw::Zlib::Z_OK()
$d_status == Compress::Raw::Zlib::Z_OK() $i_status == Compress::Raw::Zlib::Z_OK()
Truth table33%1/3
A and B
A= $d_status == Compress::Raw::Zlib::Z_OK()B= $i_status == Compress::Raw::Zlib::Z_OK()
ABresult
0X0
100
111
108 && $i_status == Compress::Raw::Zlib::Z_OK();
109 1 return;
110 1 };
111  
112 # Wrap send to handle compression
113 1 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=13 SCAR=46.3
114 1 my ($event) = @_;
115 1 ▶ if ($event->{type} eq 'websocket.accept') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "websocket.accept") 1 0 1
116 # Initialize compression streams
117 1 ($deflator, $inflator) = $init_streams->();
118 1 $compression_active = defined $deflator;
119  
120 1 ▶ if ($compression_active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compression_active) 0 1 1
121 # Add extension to accept response
122 0 my @extensions = ('permessage-deflate');
123 push @extensions, 'server_no_context_takeover'
124 0 ▶ if $self->{server_no_context_takeover};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"server_no_context_takeover"} 0 0 0
125 push @extensions, 'client_no_context_takeover'
126 0 ▶ if $self->{client_no_context_takeover};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"client_no_context_takeover"} 0 0 0
127  
128 0 $event = {
129 %$event,
130 extensions => join('; ', @extensions),
131 };
132 }
133 1 await $send->($event);
134 1 return;
135 }
136  
137 0 ▶ if ($event->{type} eq 'websocket.send' && $compression_active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "websocket.send" and $compression_active) 0 0 0
Condition0%0/3
$event->{"type"} eq "websocket.send" and $compression_active
!ll&&!rl&&r
000
MC/DC0%0/2
$event->{"type"} eq "websocket.send" and $compression_active
$event->{"type"} eq "websocket.send" $compression_active
Truth table0%0/3
A and B
A= $event->{"type"} eq "websocket.send"B= $compression_active
ABresult
0X0
100
111
138 0 my $text = $event->{text};
139 0 my $bytes = $event->{bytes};
140 0 ▶ my $data = defined $text ? $text : $bytes;
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 0 0 0
141  
142 # Only compress if above min_size
143 0 ▶ if (defined $data && length($data) >= $self->{min_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and length $data >= $self->{"min_size"}) 0 0 0
Condition0%0/3
defined $data and length $data >= $self->{"min_size"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and length $data >= $self->{"min_size"}
defined $data length $data >= $self->{"min_size"}
Truth table0%0/3
A and B
A= defined $dataB= length $data >= $self->{"min_size"}
ABresult
0X0
100
111
144 0 my $compressed = '';
145 0 my $status = $deflator->deflate($data, $compressed);
146 0 $status = $deflator->flush($compressed, Compress::Raw::Zlib::Z_SYNC_FLUSH());
147  
148 0 ▶ if ($status == Compress::Raw::Zlib::Z_OK()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == Compress::Raw::Zlib::Z_OK()) 0 0 0
149 # Remove trailing 0x00 0x00 0xff 0xff
150 0 $compressed =~ s/\x00\x00\xff\xff$//;
151  
152 0 ▶ $event = {
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 0 0 0
153 %$event,
154 (defined $text ? (text => undef) : ()),
155 bytes => $compressed,
156 compressed => 1,
157 };
158  
159 # Reset context if no takeover
160 0 ▶ if ($self->{server_no_context_takeover}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"server_no_context_takeover"}) 0 0 0
161 0 ($deflator, $inflator) = $init_streams->();
162 }
163 }
164 }
165 }
166  
167 0 await $send->($event);
168 1 };
169  
170 # Wrap receive to handle decompression
171 0 ▶ my $wrapped_receive = async sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
172 0 my $event = await $receive->();
173  
174 0 ▶ if ($event->{type} eq 'websocket.receive' && $compression_active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "websocket.receive" and $compression_active) 0 0 0
Condition0%0/3
$event->{"type"} eq "websocket.receive" and $compression_active
!ll&&!rl&&r
000
MC/DC0%0/2
$event->{"type"} eq "websocket.receive" and $compression_active
$event->{"type"} eq "websocket.receive" $compression_active
Truth table0%0/3
A and B
A= $event->{"type"} eq "websocket.receive"B= $compression_active
ABresult
0X0
100
111
175 0 ▶ if ($event->{compressed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"compressed"}) 0 0 0
176 0 ▶ my $data = $event->{bytes} // $event->{text};
Condition0%0/3
$event->{"bytes"} // $event->{"text"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$event->{"bytes"} // $event->{"text"}
$event->{"bytes"} $event->{"text"}
Truth table0%0/3
A // B
A= $event->{"bytes"}B= $event->{"text"}
ABresult
1X1
011
000
177 0 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 0 0 0
178 # Add trailer bytes
179 0 $data .= "\x00\x00\xff\xff";
180  
181 0 my $decompressed = '';
182 0 my $status = $inflator->inflate($data, $decompressed);
183  
184 0 ▶ if ($status == Compress::Raw::Zlib::Z_OK()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == Compress::Raw::Zlib::Z_OK()) 0 0 0
185 0 ▶ if (defined $event->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $event->{'text'}) { } 0 0 0
186 0 $event = { %$event, text => $decompressed, bytes => undef };
187 } else {
188 0 $event = { %$event, bytes => $decompressed };
189 }
190  
191 # Reset context if no takeover
192 0 ▶ if ($self->{client_no_context_takeover}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client_no_context_takeover"}) 0 0 0
193 0 ($deflator, $inflator) = $init_streams->();
194 }
195 }
196 }
197 }
198 }
199  
200 0 return $event;
201 1 };
202  
203 # Add compression info to scope
204 my $new_scope = {
205 %$scope,
206 'pagi.websocket.compression' => {
207 level => $self->{level},
208 min_size => $self->{min_size},
209 1 available => $has_deflate,
210 },
211 };
212  
213 1 await $app->($new_scope, $wrapped_receive, $wrapped_send);
214 3 };
215 }
216  
217 sub _parse_extensions {
218 2 ▶ my ($self, $scope) = @_;
Subroutine _parse_extensions: called CC=5 SCAR=16.3
219  
220 2 my %extensions;
221  
222 2 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
20
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
223 1 ▶ next unless lc($h->[0]) eq 'sec-websocket-extensions';
Branch: 1 branch
BranchTrueFalseTotal
unless lc $h->[0] eq "sec-websocket-extensions" 0 1 1
224  
225 1 for my $ext (split /\s*,\s*/, $h->[1]) {
226 1 my ($name, @params) = split /\s*;\s*/, $ext;
227 1 $extensions{$name} = \@params;
228 }
229 }
230  
231 2 return \%extensions;
232 }
233  
234 2 1;
235  
236 __END__
« blib/lib/PAGI/App/SSE/Pubsub.pm ↑ Summary blib/lib/PAGI/App/WebSocket/Echo.pm »