Message.pm

filter: Statementstmt 87.6% 242 / 276 Branchbran 82.2% 97 / 118 Conditioncond 53.9% 48 / 89 MC/DCmcdc 30.6% 15 / 49 Subroutinesub 92.1% 47 / 51 Podpod 100% 18 / 18 totaltotal 77.7% 467 / 601 CC 90 SCAR 19.5
CC
90
Cov
80%
dejsonify
18.1
size
12.0
jsonify
10.0
CRAP 7.0
« Message/Queue/DQN.pm ↑ Summary Message/Generator.pm »
1 #+##############################################################################
2 # #
3 # File: Messaging/Message.pm #
4 # #
5 # Description: abstraction of a message #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package Messaging::Message;
14 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 our $VERSION = "1.7";
17 6 our $REVISION = sprintf("%d.%02d", q$Revision: 1.25 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 6 ▶use Encode qw(encode decode FB_CROAK LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use JSON qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use No::Worries::Export qw(export_control);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use Params::Validate qw(validate validate_pos :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #
31 # global variables
32 #
33  
34 our(
35 6 %_LoadedModule, # hash of successfully loaded modules
36 %_CompressionModule, # known compression modules
37 $_CompressionAlgos, # known compression algorithms
38 $_JSON, # JSON object
39 );
40  
41 6 %_CompressionModule = (
42 "lz4" => "LZ4",
43 "snappy" => "Snappy",
44 "zlib" => "Zlib",
45 );
46 6 $_CompressionAlgos = join("|", sort(keys(%_CompressionModule)));
47 6 $_JSON = JSON->new();
48  
49 #+++############################################################################
50 # #
51 # helper functions #
52 # #
53 #---############################################################################
54  
55 #
56 # make sure a module is loaded
57 #
58  
59 17 ▶sub _require ($) {
Subroutine _require: called CC=3 SCAR=11.6
60 17 my($module) = @_;
61  
62 17 ▶ return if $_LoadedModule{$module};
Branch: 1 branch
BranchTrueFalseTotal
if $Messaging::Message::_LoadedModule{$module} 13 4 17
63 4 eval("require $module"); ## no critic 'ProhibitStringyEval'
64 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 4 4
65 0 $@ =~ s/\s+at\s.+?\sline\s+\d+\.?$//;
66 0 dief("failed to load %s: %s", $module, $@);
67 } else {
68 4 $_LoadedModule{$module} = 1;
69 }
70 }
71  
72 #
73 # evaluate some code with fatal warnings
74 #
75  
76 180 ▶sub _eval ($&;$) {
Subroutine _eval: called CC=2 SCAR=7.0
77 180 my($what, $code, $arg) = @_;
78  
79 180 eval {
80 0 ▶ local $SIG{__WARN__} = sub { die($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
81 180 $code->($arg);
82 };
83 180 ▶ return unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 177 3 180
84 3 $@ =~ s/\s+at\s.+?\sline\s+\d+\.?$//;
85 3 dief("%s failed: %s", $what, $@);
86 }
87  
88 #
89 # helpers for body encoding and compression
90 #
91  
92 15 ▶sub _maybe_base64_encode ($) {
Subroutine _maybe_base64_encode: called CC=2 SCAR=7.0
93 15 my($object) = @_;
94  
95 15 ▶ return unless $object->{"body"} =~ /[^\t\n\r\x20-\x7e]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $object->{"body"} =~ /[^\t\n\r\x20-\x7e]/ 0 15 15
96 # only if it contains more than printable ASCII characters (plus \t \n \r)
97 15 ▶ _eval("Base64 encoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 15 $object->{"body"} = encode_base64($object->{"body"}, "");
99 15 });
100 15 $object->{"encoding"}{"base64"}++;
101 }
102  
103 3 ▶sub _maybe_utf8_encode ($) {
Subroutine _maybe_utf8_encode: called CC=2 SCAR=6.9
104 3 my($object) = @_;
105 3 my($tmp);
106  
107 3 ▶ _eval("UTF-8 encoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 3 $tmp = encode("UTF-8", $object->{"body"}, FB_CROAK|LEAVE_SRC);
109 3 });
110 3 ▶ return if $tmp eq $object->{"body"};
Branch: 1 branch
BranchTrueFalseTotal
if $tmp eq $object->{"body"} 2 1 3
111 1 $object->{"body"} = $tmp;
112 1 $object->{"encoding"}{"utf8"}++;
113 }
114  
115 6 ▶sub _do_compress ($$) {
Subroutine _do_compress: called CC=1 SCAR=0.0
116 6 my($object, $algo) = @_;
117 6 my($compress, $tmp);
118  
119 6 $compress = \&{"Compress::$_CompressionModule{$algo}::compress"};
120 6 ▶ _eval("$_CompressionModule{$algo} compression", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 6 $tmp = $compress->(\$object->{"body"});
122 6 });
123 6 $object->{"body"} = $tmp;
124 6 $object->{"encoding"}{$algo}++;
125 }
126  
127 #+++############################################################################
128 # #
129 # object oriented interface #
130 # #
131 #---############################################################################
132  
133 #
134 # normal constructor
135 #
136  
137 my %new_options = (
138 "header" => {
139 type => HASHREF,
140 callbacks => {
141 "hash of strings" =>
142 159 ▶ sub { grep(!defined($_)||ref($_), values(%{$_[0]})) == 0 },
Subroutine __ANON__: called CC=2 SCAR=7.1
Condition66%2/3
!defined($_) || ref $_
l!l&&r!l&&!r
01537
MC/DC50%1/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table66%2/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
143 },
144 6 optional => 1,
145 },
146 "body" => {
147 type => SCALAR,
148 optional => 1,
149 },
150 "body_ref" => {
151 type => SCALARREF,
152 optional => 1,
153 },
154 "text" => {
155 type => BOOLEAN,
156 optional => 1,
157 },
158 );
159  
160 224 ▶sub new : method {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
161 224 my($class, %option, $body, $self);
162  
163 224 $class = shift(@_);
164 224 ▶ %option = validate(@_, \%new_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 147 77 224
165 dief("new(): options body and body_ref are mutually exclusive")
166 221 ▶ if exists($option{"body"}) and exists($option{"body_ref"});
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"body"} and exists $option{"body_ref"} 1 220 221
Condition100%3/3
exists $option{"body"} and exists $option{"body_ref"}
!ll&&!rl&&r
200201
MC/DC100%2/2
exists $option{"body"} and exists $option{"body_ref"}
exists $option{"body"} exists $option{"body_ref"}
Truth table100%3/3
A and B
A= exists $option{"body"}B= exists $option{"body_ref"}
ABresult
0X0
100
111
167 # default message
168 220 $body = "";
169 220 $self = { "header" => {}, "body_ref" => \$body, "text" => 0 };
170 # handle options
171 220 ▶ $self->{"header"} = $option{"header"} if exists($option{"header"});
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"header"} 118 102 220
172 220 ▶ $self->{"body_ref"} = $option{"body_ref"} if exists($option{"body_ref"});
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"body_ref"} 121 99 220
173 220 ▶ $self->{"body_ref"} = \$option{"body"} if exists($option{"body"});
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{"body"} 20 200 220
174 220 ▶ $self->{"text"} = $option{"text"} ? 1 : 0 if exists($option{"text"});
Branch: 2 branches
BranchTrueFalseTotal
if exists $option{"text"} 134 86 220
$option{'text'} ? : 68 66 134
175 # so far so good!
176 220 bless($self, $class);
177 220 return($self);
178 }
179  
180 #
181 # normal accessors
182 #
183  
184 58 ▶sub header : method {
Subroutine header: called CC=2 SCAR=9.2
Pod documented
185 58 my($self);
186  
187 58 $self = shift(@_);
188 58 ▶ return($self->{"header"}) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 58 0 58
189 0 validate_pos(@_, $new_options{"header"});
190 0 $self->{"header"} = $_[0];
191 0 return($self);
192 }
193  
194 2 ▶sub body_ref : method {
Subroutine body_ref: called CC=5 SCAR=17.5
Pod documented
195 2 my($self);
196  
197 2 $self = shift(@_);
198 2 ▶ return($self->{"body_ref"}) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 1 1 2
199 1 ▶ validate_pos(@_, $new_options{"body_ref"})
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR" 0 1 1
Condition33%2/6
@_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR"
!ll&&!rl&&r
001
@_ == 1 and defined $_[0]
!ll&&!rl&&r
001
MC/DC0%0/3
@_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR"
@_ == 1 defined $_[0] ref $_[0] eq "SCALAR"
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq "SCALAR"
ABCresult
0XX0
10X0
1100
1111
200 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "SCALAR";
201 1 $self->{"body_ref"} = $_[0];
202 1 return($self);
203 }
204  
205 74 ▶sub text : method {
Subroutine text: called CC=6 SCAR=19.9
Pod documented
206 74 my($self);
207  
208 74 $self = shift(@_);
209 74 ▶ return($self->{"text"}) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 73 1 74
210 1 ▶ validate_pos(@_, $new_options{"text"})
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and !defined($_[0]) || ref $_[0] eq "" 0 1 1
Condition33%2/6
@_ == 1 and !defined($_[0]) || ref $_[0] eq ""
!ll&&!rl&&r
001
not defined $_[0] or ref $_[0] eq ""
l!l&&r!l&&!r
010
MC/DC0%0/3
@_ == 1 and !defined($_[0]) || ref $_[0] eq ""
@_ == 1 not defined $_[0] ref $_[0] eq ""
Truth table25%1/4
A and not(B) or C
A= @_ == 1B= not defined $_[0]C= ref $_[0] eq ""
ABCresult
0XX0
1000
11X1
1011
211 unless @_ == 1 and (not defined($_[0]) or ref($_[0]) eq "");
212 1 ▶ $self->{"text"} = $_[0] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 1 1
213 1 return($self);
214 }
215  
216 #
217 # extra accessors
218 #
219  
220 82 ▶sub header_field : method {
Subroutine header_field: called CC=8 SCAR=21.1
Pod documented
221 82 my($self);
222  
223 82 $self = shift(@_);
224 82 ▶ if (@_ >= 1 and defined($_[0]) and ref($_[0]) eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 1 and defined $_[0] and ref $_[0] eq "") 80 2 82
Condition83%5/6
@_ >= 1 and defined $_[0] and ref $_[0] eq ""
!ll&&!rl&&r
1180
@_ >= 1 and defined $_[0]
!ll&&!rl&&r
0181
MC/DC66%2/3
@_ >= 1 and defined $_[0] and ref $_[0] eq ""
@_ >= 1 defined $_[0] ref $_[0] eq ""
Truth table75%3/4
A and B and C
A= @_ >= 1B= defined $_[0]C= ref $_[0] eq ""
ABCresult
0XX0
10X0
1100
1111
225 80 ▶ return($self->{"header"}{$_[0]}) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 78 2 80
226 2 ▶ if (@_ == 2 and defined($_[1]) and ref($_[1]) eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and defined $_[1] and ref $_[1] eq "") 1 1 2
Condition50%3/6
@_ == 2 and defined $_[1] and ref $_[1] eq ""
!ll&&!rl&&r
011
@_ == 2 and defined $_[1]
!ll&&!rl&&r
002
MC/DC33%1/3
@_ == 2 and defined $_[1] and ref $_[1] eq ""
@_ == 2 defined $_[1] ref $_[1] eq ""
Truth table50%2/4
A and B and C
A= @_ == 2B= defined $_[1]C= ref $_[1] eq ""
ABCresult
0XX0
10X0
1100
1111
227 1 $self->{"header"}{$_[0]} = $_[1];
228 1 return($self);
229 }
230 }
231 # so far so bad :-(
232 3 validate_pos(@_, { type => SCALAR }, { type => SCALAR, optional => 1 });
233 }
234  
235 50 ▶sub body : method {
Subroutine body: called CC=5 SCAR=17.1
Pod documented
236 50 my($self, $body);
237  
238 50 $self = shift(@_);
239 50 ▶ return(${ $self->{"body_ref"} }) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 49 1 50
240 1 ▶ validate_pos(@_, $new_options{"body"})
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "" 0 1 1
Condition33%2/6
@_ == 1 and defined $_[0] and ref $_[0] eq ""
!ll&&!rl&&r
001
@_ == 1 and defined $_[0]
!ll&&!rl&&r
001
MC/DC0%0/3
@_ == 1 and defined $_[0] and ref $_[0] eq ""
@_ == 1 defined $_[0] ref $_[0] eq ""
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq ""
ABCresult
0XX0
10X0
1100
1111
241 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "";
242 1 $body = $_[0]; # copy
243 1 $self->{"body_ref"} = \$body;
244 1 return($self);
245 }
246  
247 #
248 # extra methods
249 #
250  
251 7 ▶sub copy : method {
Subroutine copy: called CC=2 SCAR=6.9
Pod documented
252 7 my($self, %header, $body, $copy);
253  
254 7 $self = shift(@_);
255 7 ▶ validate_pos(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 7 7
256 7 %header = %{ $self->{"header"} }; # copy
257 7 $body = ${ $self->{"body_ref"} }; # copy
258 $copy = {
259 "header" => \%header,
260 "body_ref" => \$body,
261 7 "text" => $self->{"text"},
262 };
263 7 bless($copy, ref($self));
264 7 return($copy);
265 }
266  
267 0 ▶sub size : method {
Subroutine size: not called CC=3 SCAR=24.8
Pod documented
268 0 my($self, $size, $key, $value);
269  
270 0 $self = shift(@_);
271 0 ▶ validate_pos(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
272 0 $size = 1 + length(${ $self->{"body_ref"} });
273 0 ▶ while (($key, $value) = each(%{ $self->{"header"} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"header"};}) 0 0 0
274 0 $size += 2 + length($key) + length($value);
275 }
276 0 return($size);
277 }
278  
279 #+++############################################################################
280 # #
281 # (de)jsonification #
282 # #
283 #---############################################################################
284  
285 #
286 # jsonify (= transform into a JSON object)
287 #
288  
289 6 my %jsonify_options = (
290 "compression" => {
291 type => SCALAR,
292 regex => qr/^($_CompressionAlgos)?!?$/o,
293 optional => 1,
294 },
295 );
296  
297 10 ▶sub _jsonify_text ($$$$$) {
Subroutine _jsonify_text: called CC=6 SCAR=21.5
298 10 my($self, $object, $algo, $force, $len) = @_;
299  
300 10 ▶ if ($algo and $force) {
Branch: 2 branches
BranchTrueFalseTotal
if ($algo and $force) { } 0 10 10
elsif ($algo and $len > 255) { } 3 7 10
Condition66%2/3
$algo and $force
!ll&&!rl&&r
730
MC/DC0%0/2
$algo and $force
$algo $force
Truth table66%2/3
A and B
A= $algoB= $force
ABresult
0X0
100
111
301 # always compress
302 0 _maybe_utf8_encode($object);
303 0 _do_compress($object, $algo);
304 0 _maybe_base64_encode($object);
305 ▶ } elsif ($algo and $len > 255) {
Condition66%2/3
$algo and $len > 255
!ll&&!rl&&r
703
MC/DC50%1/2
$algo and $len > 255
$algo $len > 255
Truth table66%2/3
A and B
A= $algoB= $len > 255
ABresult
0X0
100
111
306 # maybe compress
307 3 _maybe_utf8_encode($object);
308 3 _do_compress($object, $algo);
309 3 _maybe_base64_encode($object);
310 3 ▶ if (length($object->{"body"}) >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $object->{"body"} >= $len) 0 3 3
311 # not worth it
312 0 $object->{"body"} = ${ $self->{"body_ref"} };
313 0 delete($object->{"encoding"});
314 }
315 } else {
316 # do not compress
317 }
318 }
319  
320 12 ▶sub _jsonify_binary ($$$$$) {
Subroutine _jsonify_binary: called CC=7 SCAR=22.5
321 12 my($self, $object, $algo, $force, $len) = @_;
322  
323 12 ▶ if ($algo and $force) {
Branch: 2 branches
BranchTrueFalseTotal
if ($algo and $force) { } 0 12 12
elsif ($algo and $len > 255) { } 3 9 12
Condition66%2/3
$algo and $force
!ll&&!rl&&r
930
MC/DC0%0/2
$algo and $force
$algo $force
Truth table66%2/3
A and B
A= $algoB= $force
ABresult
0X0
100
111
324 # always compress
325 0 _do_compress($object, $algo);
326 0 _maybe_base64_encode($object);
327 ▶ } elsif ($algo and $len > 255) {
Condition66%2/3
$algo and $len > 255
!ll&&!rl&&r
903
MC/DC50%1/2
$algo and $len > 255
$algo $len > 255
Truth table66%2/3
A and B
A= $algoB= $len > 255
ABresult
0X0
100
111
328 # maybe compress
329 3 ▶ $len *= 4/3 if $object->{"body"} =~ /[^\t\n\r\x20-\x7e]/;
Branch: 1 branch
BranchTrueFalseTotal
if $object->{"body"} =~ /[^\t\n\r\x20-\x7e]/ 1 2 3
330 3 _do_compress($object, $algo);
331 3 _maybe_base64_encode($object);
332 3 ▶ if (length($object->{"body"}) >= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $object->{"body"} >= $len) 0 3 3
333 # not worth it
334 0 $object->{"body"} = ${ $self->{"body_ref"} };
335 0 delete($object->{"encoding"});
336 0 _maybe_base64_encode($object);
337 }
338 } else {
339 # do not compress
340 9 _maybe_base64_encode($object);
341 }
342 }
343  
344 38 ▶sub jsonify : method {
Subroutine jsonify: called CC=10 SCAR=23.0
Pod documented
345 38 my($self, %option, %object, $algo, $force, $len);
346  
347 38 $self = shift(@_);
348 38 ▶ %option = validate(@_, \%jsonify_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 32 38
349 38 ▶ if ($option{"compression"} and $option{"compression"} =~ /^(\w+)(!?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"compression"} and $option{"compression"} =~ /^(\w+)(!?)$/) 6 32 38
Condition66%2/3
$option{"compression"} and $option{"compression"} =~ /^(\w+)(!?)$/
!ll&&!rl&&r
3206
MC/DC50%1/2
$option{"compression"} and $option{"compression"} =~ /^(\w+)(!?)$/
$option{"compression"} $option{"compression"} =~ /^(\w+)(!?)$/
Truth table66%2/3
A and B
A= $option{"compression"}B= $option{"compression"} =~ /^(\w+)(!?)$/
ABresult
0X0
100
111
350 6 ($algo, $force) = ($1, $2);
351 }
352 # check compression availability
353 38 ▶ _require("Compress::$_CompressionModule{$algo}") if $algo;
Branch: 1 branch
BranchTrueFalseTotal
if $algo 6 32 38
354 # build the JSON object
355 38 ▶ $object{"text"} = JSON::true if $self->{"text"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"text"} 10 28 38
356 38 ▶ $object{"header"} = $self->{"header"} if keys(%{ $self->{"header"} });
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"header"};} 9 29 38
357 38 $len = length(${ $self->{"body_ref"} });
358 38 ▶ return(\%object) unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 16 22 38
359 22 $object{"body"} = ${ $self->{"body_ref"} };
360 # handle non-empty body
361 22 ▶ if ($self->{"text"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'text'}) { } 10 12 22
362 # text body
363 10 _jsonify_text($self, \%object, $algo, $force, $len);
364 } else {
365 # binary body
366 12 _jsonify_binary($self, \%object, $algo, $force, $len);
367 }
368 # set the encoding string
369 15 $object{"encoding"} = join("+", sort(keys(%{ $object{"encoding"} })))
370 22 ▶ if $object{"encoding"};
Branch: 1 branch
BranchTrueFalseTotal
if $object{"encoding"} 15 7 22
371 # so far so good!
372 22 return(\%object);
373 }
374  
375 #
376 # dejsonify (= alternate constructor using the JSON object)
377 #
378  
379 my %dejsonify_options = (
380 "header" => $new_options{"header"},
381 "body" => {
382 type => SCALAR,
383 optional => 1,
384 },
385 "text" => {
386 type => OBJECT,
387 callbacks => {
388 20 ▶ "JSON::is_bool" => sub { JSON::is_bool($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
389 },
390 6 optional => 1,
391 },
392 "encoding" => {
393 type => SCALAR,
394 optional => 1,
395 },
396 );
397  
398 74 ▶sub dejsonify : method {
Subroutine dejsonify: called CC=18 SCAR=28.9
Pod documented
399 74 my($class, $object, $encoding, $self, $tmp, $len, $uncompress);
400  
401 74 $class = shift(@_);
402 74 ▶ validate_pos(@_, { type => HASHREF })
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "HASH" 1 73 74
Condition50%3/6
@_ == 1 and defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
0173
@_ == 1 and defined $_[0]
!ll&&!rl&&r
0074
MC/DC33%1/3
@_ == 1 and defined $_[0] and ref $_[0] eq "HASH"
@_ == 1 defined $_[0] ref $_[0] eq "HASH"
Truth table50%2/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
403 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "HASH";
404 73 validate(@_, \%dejsonify_options);
405 70 $object = $_[0];
406 70 ▶ $encoding = $object->{"encoding"} || "";
Condition100%2/2
$object->{'encoding'} || ''
l!l
2743
MC/DC100%1/1
$object->{'encoding'} || ''
$object->{'encoding'}
Truth table100%2/2
A
A= $object->{'encoding'}
Aresult
11
00
407 70 ▶ dief("invalid encoding: %s", $encoding)
Branch: 1 branch
BranchTrueFalseTotal
unless $encoding eq "" or ("$encoding+") =~ /^((base64|utf8|$Messaging::Message::_CompressionAlgos)\+)+$/o 1 69 70
Condition100%3/3
$encoding eq "" or ("$encoding+") =~ /^((base64|utf8|$Messaging::Message::_CompressionAlgos)\+)+$/o
l!l&&r!l&&!r
43261
MC/DC100%2/2
$encoding eq "" or ("$encoding+") =~ /^((base64|utf8|$Messaging::Message::_CompressionAlgos)\+)+$/o
$encoding eq "" ("$encoding+") =~ /^((base64|utf8|$Messaging::Message::_CompressionAlgos)\+)+$/o
Truth table100%3/3
A or B
A= $encoding eq ""B= ("$encoding+") =~ /^((base64|utf8|$Messaging::Message::_CompressionAlgos)\+)+$/o
ABresult
1X1
011
000
408 unless $encoding eq ""
409 or "${encoding}+" =~ /^((base64|utf8|$_CompressionAlgos)\+)+$/o;
410 69 ▶ _require("Compress::$_CompressionModule{$1}")
Branch: 1 branch
BranchTrueFalseTotal
if $encoding =~ /($Messaging::Message::_CompressionAlgos)/o 9 60 69
411 if $encoding =~ /($_CompressionAlgos)/o;
412 # construct the message
413 69 $self = $class->new();
414 69 ▶ $self->{"text"} = 1 if $object->{"text"};
Branch: 1 branch
BranchTrueFalseTotal
if $object->{"text"} 16 53 69
415 $self->{"header"} = $object->{"header"}
416 69 ▶ if $object->{"header"} and keys(%{ $object->{"header"} });
Branch: 1 branch
BranchTrueFalseTotal
if $object->{"header"} and keys %{$$object{"header"};} 34 35 69
Condition100%3/3
$object->{"header"} and keys %{$$object{"header"};}
!ll&&!rl&&r
29634
MC/DC100%2/2
$object->{"header"} and keys %{$$object{"header"};}
$object->{"header"} keys %{$$object{"header"};}
Truth table100%3/3
A and B
A= $object->{"header"}B= keys %{$$object{"header"};}
ABresult
0X0
100
111
417 69 ▶ if (exists($object->{"body"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $object->{"body"}) 57 12 69
418 57 $tmp = $object->{"body"};
419 57 ▶ if ($encoding =~ /base64/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding =~ /base64/) 26 31 57
420 # body has been Base64 encoded, compute length to detect unexpected
421 # characters (this is because MIME::Base64 silently ignores them)
422 26 $len = length($tmp);
423 26 ▶ dief("invalid Base64 data: %s", $object->{"body"}) if $len % 4;
Branch: 1 branch
BranchTrueFalseTotal
if $len % 4 1 25 26
424 25 $len = $len * 3 / 4;
425 25 $len -= substr($tmp, -2) =~ tr/=/=/;
426 25 ▶ _eval("Base64 decoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
427 25 $tmp = decode_base64($tmp);
428 25 });
429 25 ▶ dief("invalid Base64 data: %s", $object->{"body"})
Branch: 1 branch
BranchTrueFalseTotal
unless $len == length $tmp 2 23 25
430 unless $len == length($tmp);
431 }
432 54 ▶ if ($encoding =~ /($_CompressionAlgos)/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding =~ /($Messaging::Message::_CompressionAlgos)/o) 9 45 54
433 # body has been compressed
434 9 $uncompress = \&{"Compress::$_CompressionModule{$1}::uncompress"};
435 9 ▶ _eval("$_CompressionModule{$1} decompression", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
436 9 $tmp = $uncompress->(\$tmp);
437 9 });
438 9 ▶ dief("invalid $_CompressionModule{$1} compressed data!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp 0 9 9
439 unless defined($tmp);
440 }
441 54 ▶ if ($encoding =~ /utf8/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding =~ /utf8/) 4 50 54
442 # body has been UTF-8 encoded
443 4 ▶ _eval("UTF-8 decoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
444 4 $tmp = decode("UTF-8", $tmp, FB_CROAK);
445 4 });
446 }
447 54 $self->{"body_ref"} = \$tmp;
448 }
449 # so far so good!
450 66 return($self);
451 }
452  
453 #+++############################################################################
454 # #
455 # (de)stringification #
456 # #
457 #---############################################################################
458  
459 #
460 # stringify (= transform into a text string)
461 #
462  
463 7 ▶sub stringify : method {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
464 7 my($self, $tmp);
465  
466 7 $self = shift(@_);
467 7 $tmp = $self->jsonify(@_);
468 7 ▶ _eval("JSON encoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
469 7 $tmp = $_JSON->encode($tmp);
470 7 });
471 7 return($tmp);
472 }
473  
474 18 ▶sub stringify_ref : method {
Subroutine stringify_ref: called CC=1 SCAR=0.0
Pod documented
475 18 my($self, $tmp);
476  
477 18 $self = shift(@_);
478 18 $tmp = $self->jsonify(@_);
479 18 ▶ _eval("JSON encoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
480 18 $tmp = $_JSON->encode($tmp);
481 18 });
482 18 return(\$tmp);
483 }
484  
485 #
486 # destringify (= alternate constructor using the stringified representation)
487 #
488  
489 36 ▶sub destringify : method {
Subroutine destringify: called CC=4 SCAR=15.2
Pod documented
490 36 my($class, $tmp);
491  
492 36 $class = shift(@_);
493 36 ▶ validate_pos(@_, { type => SCALAR })
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "" 0 36 36
Condition33%2/6
@_ == 1 and defined $_[0] and ref $_[0] eq ""
!ll&&!rl&&r
0036
@_ == 1 and defined $_[0]
!ll&&!rl&&r
0036
MC/DC0%0/3
@_ == 1 and defined $_[0] and ref $_[0] eq ""
@_ == 1 defined $_[0] ref $_[0] eq ""
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq ""
ABCresult
0XX0
10X0
1100
1111
494 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "";
495 36 ▶ _eval("JSON decoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
496 36 $tmp = $_JSON->decode(${ $_[0] });
497 36 }, \$_[0]);
498 34 return($class->dejsonify($tmp));
499 }
500  
501 33 ▶sub destringify_ref : method {
Subroutine destringify_ref: called CC=4 SCAR=15.2
Pod documented
502 33 my($class, $tmp);
503  
504 33 $class = shift(@_);
505 33 ▶ validate_pos(@_, { type => SCALARREF })
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR" 0 33 33
Condition33%2/6
@_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR"
!ll&&!rl&&r
0033
@_ == 1 and defined $_[0]
!ll&&!rl&&r
0033
MC/DC0%0/3
@_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR"
@_ == 1 defined $_[0] ref $_[0] eq "SCALAR"
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq "SCALAR"
ABCresult
0XX0
10X0
1100
1111
506 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "SCALAR";
507 33 ▶ _eval("JSON decoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
508 33 $tmp = $_JSON->decode(${ $_[0] });
509 33 }, $_[0]);
510 33 return($class->dejsonify($tmp));
511 }
512  
513 #+++############################################################################
514 # #
515 # (de)serialization #
516 # #
517 #---############################################################################
518  
519 #
520 # serialize (= transform into a binary string)
521 #
522  
523 18 ▶sub serialize : method {
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
524 18 my($self, $tmp);
525  
526 18 $self = shift(@_);
527 18 $tmp = $self->stringify_ref(@_);
528 18 ▶ _eval("UTF-8 encoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
529 18 $tmp = encode("UTF-8", ${ $tmp }, FB_CROAK);
530 18 });
531 18 return($tmp);
532 }
533  
534 0 ▶sub serialize_ref : method {
Subroutine serialize_ref: not called CC=1 SCAR=6.9
Pod documented
535 0 my($self, $tmp);
536  
537 0 $self = shift(@_);
538 0 $tmp = $self->stringify_ref(@_);
539 0 ▶ _eval("UTF-8 encoding", sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
540 0 $tmp = encode("UTF-8", ${ $tmp }, FB_CROAK);
541 0 });
542 0 return(\$tmp);
543 }
544  
545 #
546 # deserialize (= alternate constructor using the serialized representation)
547 #
548  
549 28 ▶sub deserialize : method {
Subroutine deserialize: called CC=5 SCAR=17.1
Pod documented
550 28 my($class, $tmp);
551  
552 28 $class = shift(@_);
553 28 ▶ validate_pos(@_, { type => SCALAR })
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "" 0 28 28
Condition33%2/6
@_ == 1 and defined $_[0] and ref $_[0] eq ""
!ll&&!rl&&r
0028
@_ == 1 and defined $_[0]
!ll&&!rl&&r
0028
MC/DC0%0/3
@_ == 1 and defined $_[0] and ref $_[0] eq ""
@_ == 1 defined $_[0] ref $_[0] eq ""
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq ""
ABCresult
0XX0
10X0
1100
1111
554 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "";
555 28 ▶ return($class->destringify($_[0])) unless $_[0] =~ /[^[:ascii:]]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /[^[:ascii:]]/ 24 4 28
556 4 ▶ _eval("UTF-8 decoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
557 4 $tmp = decode("UTF-8", ${ $_[0] }, FB_CROAK|LEAVE_SRC);
558 4 }, \$_[0]);
559 3 return($class->destringify($tmp));
560 }
561  
562 35 ▶sub deserialize_ref : method {
Subroutine deserialize_ref: called CC=5 SCAR=17.0
Pod documented
563 35 my($class, $tmp);
564  
565 35 $class = shift(@_);
566 35 ▶ validate_pos(@_, { type => SCALARREF })
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR" 0 35 35
Condition33%2/6
@_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR"
!ll&&!rl&&r
0035
@_ == 1 and defined $_[0]
!ll&&!rl&&r
0035
MC/DC0%0/3
@_ == 1 and defined $_[0] and ref $_[0] eq "SCALAR"
@_ == 1 defined $_[0] ref $_[0] eq "SCALAR"
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq "SCALAR"
ABCresult
0XX0
10X0
1100
1111
567 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "SCALAR";
568 35 ▶ return($class->destringify_ref($_[0])) unless ${ $_[0] } =~ /[^[:ascii:]]/;
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[0];} =~ /[^[:ascii:]]/ 33 2 35
569 2 ▶ _eval("UTF-8 decoding", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
570 2 $tmp = decode("UTF-8", ${ $_[0] }, FB_CROAK|LEAVE_SRC);
571 2 }, $_[0]);
572 2 return($class->destringify($tmp));
573 }
574  
575 #
576 # export control
577 #
578  
579 9 ▶sub import : method {
Subroutine import: called CC=1 SCAR=0.0
580 9 my($pkg, %exported);
581  
582 9 $pkg = shift(@_);
583 9 %exported = ("_require" => 1);
584 9 export_control(scalar(caller()), $pkg, \%exported, @_);
585 }
586  
587 6 1;
588  
589 __DATA__
« Message/Queue/DQN.pm ↑ Summary Message/Generator.pm »