Lite/Message.pm

filter: Statementstmt 99.4% 172 / 173 Branchbran 87.5% 63 / 72 Conditioncond 70.8% 17 / 24 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 100% 23 / 23 Podpod 92.8% 13 / 14 totaltotal 92.2% 297 / 322 CC 48 SCAR 17.6
CC
48
Cov
94%
new
14.0
next_frame
7.0
_decode_mime
5.1
CRAP 5.8
« Lite/ServerSession.pm ↑ Summary Lite/ClientSession.pm »
1 # $Id: Message.pm,v 1.9 2004/04/22 20:45:32 davidb Exp $
2 #
3 # Copyright (C) 2003 Verisign, Inc.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 # USA
19  
20 package Net::BEEP::Lite::Message;
21  
22 =head1 NAME
23  
24 Net::BEEP::Lite::Message
25  
26 =head1 SYNOPSIS
27  
28 use Net::BEEP::Lite::Message;
29  
30 my $message = Net::BEEP::Lite::Message->new
31 ( Frame => $frame );
32  
33 $message->add_frame($next_frame);
34  
35 my $message2 = new Net::BEEP::Lite::Message
36 ( Type => 'MSG',
37 Channel => 3,
38 Content => $content,
39 ContentType => 'application/xml' );
40  
41 for my $frame ($message2->next_frame($seqno, $max_size)) {
42 # ... send the frame
43 }
44  
45 =head1 DESCRIPTON
46  
47 This class represents a BEEP message, the basic unit of data transport
48 at the user level. It contains both a reference to the session that
49 it was received on (or will be sent by), and content. It contains
50 methods to construct and deconstruct the message into frames, the
51 actual base unit of transport.
52  
53 This class is expected to be used in user code by both clients and
54 servers.
55  
56 =cut
57  
58 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 5 ▶use Net::BEEP::Lite::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 =head1 CONSTRUCTOR
66  
67 =over 4
68  
69 =item new( I<ARGS> )
70  
71 This is the main constructor. It takes a named parameter list as its
72 argument. The following parameters are recognized:
73  
74 =over 4
75  
76 =item Session
77  
78 A reference to the session that the message was received by or will be sent by.
79  
80 =item Type
81  
82 The message type (e.g., "MSG", "RPY", "ERR", etc.)
83  
84 =item Msgno
85  
86 The message number. This is generally fetched from the session, or,
87 for replies, from the message being replied to. This should only be
88 set for replies. 'MSG's should be set by the session on sending it.
89  
90 =item Channel
91  
92 The channel number.
93  
94 =item Payload
95  
96 The message payload (including the MIME header(s)). Either this or
97 "Content" and "ContentType" MUST be supplied.
98  
99 =item Content
100  
101 The message content (not including the MIME headers).
102  
103 =item ContentType
104  
105 The message content type. This will be added as a MIME header when
106 forming the payload. If not supplied, the default content type is
107 'application/octet-stream'.
108  
109 =item ContentEncoding
110  
111 The content encoding. This will be added as a MIME header when
112 forming the payload, if supplied.
113  
114 =item Frame
115  
116 A frame to form the basis (or entire) message. Generally, this is
117 supplied on its own.
118  
119 =item Debug
120  
121 Emit debug messages.
122  
123 =back
124  
125 =back
126  
127 =cut
128  
129 22 ▶sub new {
Subroutine new: called CC=14 SCAR=26.4
Pod documented
130 22 my $this = shift;
131 22 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0220
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
132 22 my %args = @_;
133  
134 22 my $self = {};
135 22 bless $self, $class;
136  
137 # ANSNO is only set for ANS message, but we would like it to be a
138 # defined hash element in either case.
139 22 $self->{ansno} = undef;
140  
141 22 $self->{debug} = 0;
142 22 $self->{trace} = 0;
143 # this is used by next_frame()
144 22 $self->{frame_offset} = 0;
145 22 $self->{generated_first_frame} = 0;
146  
147 22 $self->{payload} = $self->{content} = "";
148  
149 22 for (keys %args) {
150 76 my $val = $args{$_};
151  
152 76 ▶ /^Type$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Type$/i and do { $self->type(uc $val); next } 18 58 76
153 18 $self->type(uc $val);
154 18 next;
155 };
156 58 ▶ /^Msgno$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Msgno$/i and do { $self->msgno($val); next } 7 51 58
157 7 $self->msgno($val);
158 7 next;
159 };
160 51 ▶ /^Ansno$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Ansno$/i and do { $self->ansno($val); next } 1 50 51
161 1 $self->ansno($val);
162 1 next;
163 };
164 50 ▶ /^Channel$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Channel$/i and do { $self->{"channel_number"} = $val; next } 18 32 50
165 18 $self->{channel_number} = $val;
166 18 next;
167 };
168 32 ▶ /^Payload$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Payload$/i and do { $self->{"payload"} = $val; next } 7 25 32
169 7 $self->{payload} = $val;
170 7 next;
171 };
172 25 ▶ /^Content$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Content$/i and do { $self->{"content"} = $val; next } 10 15 25
173 10 $self->{content} = $val;
174 10 next;
175 };
176 15 ▶ /^Content.?Type$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Content.?Type$/i and do { $self->{"content_type"} = $val; next } 7 8 15
177 7 $self->{content_type} = $val;
178 7 next;
179 };
180 8 ▶ /^Content.?Encoding$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Content.?Encoding$/i and do { $self->{"content_encoding"} = $val; next } 1 7 8
181 1 $self->{content_encoding} = $val;
182 1 next;
183 };
184 7 ▶ /^Frame$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Frame$/i and do { $self->{"type"} = $val->type; $self->{"msgno"} = $val->msgno; $self->{"ansno"} = $val->ansno; $self->{"channel_number"} = $val->channel_number; $self->{"payload"} = $val->payload; next } 3 4 7
185 3 $self->{type} = $val->type();
186 3 $self->{msgno} = $val->msgno();
187 3 $self->{ansno} = $val->ansno();
188 3 $self->{channel_number} = $val->channel_number();
189 3 $self->{payload} = $val->payload();
190 3 next;
191 };
192 4 ▶ /^Debug$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Debug$/i and do { $self->{"debug"} = $val; next } 2 2 4
193 2 $self->{debug} = $val;
194 2 next;
195 };
196 2 ▶ /^Trace$/i and do {
Branch: 1 branch
BranchTrueFalseTotal
/^Trace$/i and do { $self->{"trace"} = $val; next } 2 0 2
197 2 $self->{trace} = $val;
198 2 next;
199 };
200 }
201  
202 22 $self;
203 }
204  
205 =head1 METHODS
206  
207 =over 4
208  
209 =item type([$val])
210  
211 Returns the type of the message (e.g., "MSG", "RPY", etc.). Updates
212 the type to $val if provided.
213  
214 =cut
215  
216 55 ▶sub type {
Subroutine type: called CC=2 SCAR=6.9
Pod documented
217 55 my $self = shift;
218 55 my $val = shift;
219  
220 55 ▶ $self->{type} = $val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 19 36 55
221 55 $self->{type};
222 }
223  
224 =item msgno([$val])
225  
226 Returns (or sets) the message number of the message.
227  
228 =cut
229  
230 27 ▶sub msgno {
Subroutine msgno: called CC=2 SCAR=6.9
Pod documented
231 27 my $self = shift;
232 27 my $val = shift;
233  
234 27 ▶ $self->{msgno} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 8 19 27
235 27 $self->{msgno};
236 }
237  
238  
239 9 ▶sub ansno {
Subroutine ansno: called CC=2 SCAR=6.9
Pod undocumented
240 9 my $self = shift;
241 9 my $val = shift;
242  
243 9 ▶ $self->{ansno} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 1 8 9
244 9 $self->{ansno};
245 }
246  
247 =item size()
248  
249 Returns the size of the payload of the message.
250  
251 =cut
252  
253 4 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
254 4 my $self = shift;
255  
256 4 length($self->payload());
257 }
258  
259 =item channel_number([$va])
260  
261 Returns or sets the channel number of the message.
262  
263 =cut
264  
265 19 ▶sub channel_number {
Subroutine channel_number: called CC=2 SCAR=7.0
Pod documented
266 19 my $self = shift;
267 19 my $val = shift;
268  
269 19 ▶ $self->{channel_number} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 19 19
270 19 $self->{channel_number};
271 }
272  
273 =item payload()
274  
275 Returns the payload of the message, forming it from the content,
276 content type, and content encoding, if necessary.
277  
278 =cut
279  
280 23 ▶sub payload {
Subroutine payload: called CC=1 SCAR=0.0
Pod documented
281 23 my $self = shift;
282  
283 23 $self->_content_payload_transfer();
284 23 $self->{payload};
285 }
286  
287 =item content_type()
288  
289 Returns the content type of the message (either set or parsed from the
290 payload).
291  
292 =cut
293  
294 4 ▶sub content_type {
Subroutine content_type: called CC=2 SCAR=6.9
Pod documented
295 4 my $self = shift;
296 4 $self->_content_payload_transfer();
297  
298 4 ▶ $self->{content_type} || 'application/octet-stream';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"content_type"} or "application/octet-stream" 3 1 4
299 }
300  
301 =item content_encoding()
302  
303 Returns the content encoding of the message (if one where set or
304 detected from the payload).
305  
306 =cut
307  
308 2 ▶sub content_encoding {
Subroutine content_encoding: called CC=2 SCAR=6.9
Pod documented
309 2 my $self = shift;
310 2 $self->_content_payload_transfer();
311  
312 2 ▶ $self->{content_encoding} || 'binary';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"content_encoding"} or "binary" 1 1 2
313 }
314  
315 =item content()
316  
317 Returns the content of the message (the payload minus MIME headers).
318 It calculates the content from the payload, if necessary.
319  
320 =cut
321  
322 9 ▶sub content {
Subroutine content: called CC=1 SCAR=0.0
Pod documented
323 9 my $self = shift;
324 9 $self->_content_payload_transfer();
325  
326 9 $self->{content};
327 }
328  
329 =item _content_payload_transfer()
330  
331 This will force the translation between content and payload.
332 Currently this can only be done once, but then again, this class
333 doesn't support changing either of them through the API. If you do
334 so, be sure to set the other to undef so that this routine will work.
335  
336 =cut
337  
338 38 ▶sub _content_payload_transfer {
Subroutine _content_payload_transfer: called CC=5 SCAR=16.1
339 38 my $self = shift;
340  
341 38 ▶ if (! $self->{content} and $self->{payload}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->{'content'} and $self->{'payload'}) { } 4 34 38
elsif (not $self->{'payload'} and $self->{'content'}) { } 10 45 55
Condition100%3/3
not $self->{"content"} and $self->{"payload"}
!ll&&!rl&&r
3134
MC/DC100%2/2
not $self->{"content"} and $self->{"payload"}
not $self->{"content"} $self->{"payload"}
Truth table100%3/3
not(A) and B
A= not $self->{"content"}B= $self->{"payload"}
ABresult
0X0
100
111
342 4 $self->_decode_mime();
343 }
344 ▶ elsif (! $self->{payload} and $self->{content}) {
Condition100%3/3
not $self->{"payload"} and $self->{"content"}
!ll&&!rl&&r
21310
MC/DC100%2/2
not $self->{"payload"} and $self->{"content"}
not $self->{"payload"} $self->{"content"}
Truth table100%3/3
not(A) and B
A= not $self->{"payload"}B= $self->{"content"}
ABresult
0X0
100
111
345 10 $self->_encode_mime();
346 }
347 }
348  
349 =item _decode_mime()
350  
351 Parse the payload into content, content type, and content encoding.
352 This is normally called automatically.
353  
354 =cut
355  
356 4 ▶sub _decode_mime {
Subroutine _decode_mime: called CC=5 SCAR=16.2
357 4 my $self = shift;
358  
359 4 my $payload = $self->{payload};
360  
361 4 my ($content, @headers) = _decode_mime_entity($payload);
362  
363 4 $self->{content} = $content;
364  
365 4 for my $header (@headers) {
366 2 ▶ next if not $header =~ /^(\S+):\s*(\S.*$)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $header =~ /^(\S+):\s*(\S.*$)/ 0 2 2
367 2 ▶ if ($1 eq 'Content-Type') {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq 'Content-Type') { } 1 1 2
elsif ($1 eq 'Content-Transfer-Encoding') { } 1 0 1
368 1 $self->{content_type} = $2;
369 } elsif ($1 eq 'Content-Transfer-Encoding') {
370 1 $self->{content_encoding} = $2;
371 }
372 }
373 }
374  
375 =item _encode_mime()
376  
377 Calculate the payload from the set content, content type, and content
378 encoding. This is normally called automatically.
379  
380 =cut
381  
382 10 ▶sub _encode_mime {
Subroutine _encode_mime: called CC=5 SCAR=16.1
383 10 my $self = shift;
384  
385 10 my @headers;
386 10 my $ct = $self->{content_type};
387 10 ▶ if ($ct and $ct ne 'application/octet-stream') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct and $ct ne "application/octet-stream") 7 3 10
Condition66%2/3
$ct and $ct ne "application/octet-stream"
!ll&&!rl&&r
307
MC/DC50%1/2
$ct and $ct ne "application/octet-stream"
$ct $ct ne "application/octet-stream"
Truth table66%2/3
A and B
A= $ctB= $ct ne "application/octet-stream"
ABresult
0X0
100
111
388 7 push @headers, "Content-Type: $ct";
389 }
390 10 my $ce = $self->{content_encoding};
391 10 ▶ if ($ce and $ce ne "binary") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ce and $ce ne "binary") 1 9 10
Condition66%2/3
$ce and $ce ne "binary"
!ll&&!rl&&r
901
MC/DC50%1/2
$ce and $ce ne "binary"
$ce $ce ne "binary"
Truth table66%2/3
A and B
A= $ceB= $ce ne "binary"
ABresult
0X0
100
111
392 1 push @headers, "Content-Transfer-Encoding: $ce";
393 }
394  
395 10 my $payload = _encode_mime_entity($self->{content}, @headers);
396  
397 10 $self->{payload} = $payload;
398 }
399  
400 =item add_frame($frame)
401  
402 Add a frame to an existing message. This is used to assemble a
403 message from multiple frames. For now, this method doesn't really
404 check that the additional frames really belong to the message.
405  
406 =cut
407  
408 3 ▶sub add_frame {
Subroutine add_frame: called CC=4 SCAR=15.5
Pod documented
409 3 my $self = shift;
410 3 my $frame = shift;
411 # TODO: check to see if this frame matches the message.
412  
413 3 ▶ if (!$self->{payload} and $self->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"payload"} and $self->{"content"}) 0 3 3
Condition33%1/3
not $self->{"payload"} and $self->{"content"}
!ll&&!rl&&r
300
MC/DC0%0/2
not $self->{"payload"} and $self->{"content"}
not $self->{"payload"} $self->{"content"}
Truth table33%1/3
not(A) and B
A= not $self->{"payload"}B= $self->{"content"}
ABresult
0X0
100
111
414 0 $self->payload(); # force the payload to be constructed.
415 }
416 # we want to force the content to be constructed from the payload
417 # after this.
418 3 $self->{content} = undef;
419  
420 3 my ($content, @headers) = _decode_mime_entity($frame->payload());
421 3 ▶ $self->{payload} .= $content if $content;
Branch: 1 branch
BranchTrueFalseTotal
if $content 3 0 3
422 }
423  
424 =item has_more_frames()
425  
426 Return true if there are more frames to be generated from this message.
427  
428 =cut
429  
430 4 ▶sub has_more_frames {
Subroutine has_more_frames: called CC=3 SCAR=11.0
Pod documented
431 4 my $self = shift;
432  
433 4 ▶ return 1 if not $self->{generated_first_frame};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"generated_first_frame"} 1 3 4
434  
435 3 my $remainder = length($self->payload()) - $self->{frame_offset};
436  
437 3 ▶ $remainder > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$remainder > 0 ? : 1 2 3
438 }
439  
440 =item next_frame($seqno, $max_size)
441  
442 Returns the "next" frame in the message, based on given maximum size.
443 This method will split the message into multiple frames if the maximum
444 size forces it to. This will return undef when the entire message has
445 been rendered into frames. See the reset_frames() method if you wish
446 to convert the same message into frames multiple times.
447  
448 =cut
449  
450 10 ▶sub next_frame {
Subroutine next_frame: called CC=7 SCAR=19.5
Pod documented
451 10 my $self = shift;
452 10 my $seqno = shift;
453 10 my $max_size = shift;
454  
455 10 my $chno = $self->channel_number();
456  
457 10 ▶ confess "msgno was not set before next_frame()"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->msgno 0 10 10
458 if (not defined $self->msgno());
459  
460 10 ▶ croak "maximum size of zero for message: type = ", $self->type(),
Branch: 1 branch
BranchTrueFalseTotal
if $max_size == 0 0 10 10
461 " msgno = ", $self->msgno(), " chno = $chno\n" if $max_size == 0;
462  
463 10 my $payload = $self->payload();
464 10 my $remainder = length($payload) - $self->{frame_offset};
465  
466 10 ▶ return undef if ($self->{generated_first_frame} and $remainder <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"generated_first_frame"} and $remainder <= 0 2 8 10
Condition100%3/3
$self->{"generated_first_frame"} and $remainder <= 0
!ll&&!rl&&r
532
MC/DC100%2/2
$self->{"generated_first_frame"} and $remainder <= 0
$self->{"generated_first_frame"} $remainder <= 0
Truth table100%3/3
A and B
A= $self->{"generated_first_frame"}B= $remainder <= 0
ABresult
0X0
100
111
467  
468 8 $self->{generated_first_frame} = 1;
469  
470 8 my $more;
471 my $local_payload;
472  
473 8 ▶ if ($remainder > $max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder > $max_size) { } 4 4 8
474 4 ▶ print STDERR "***** fragmenting message.\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 4 4
475 4 $local_payload = substr($payload, $self->{frame_offset}, $max_size);
476 4 $more = '*';
477 } else {
478 4 $local_payload = substr($payload, $self->{frame_offset});
479 4 $more = '.';
480 }
481  
482 8 my $frame = Net::BEEP::Lite::Frame->new
483 (Type => $self->type(),
484 Msgno => $self->msgno(),
485 Ansno => $self->ansno(),
486 More => $more,
487 Seqno => $seqno,
488 Channel => $self->channel_number(),
489 Payload => $local_payload);
490  
491 8 $self->{frame_offset} += length($local_payload);
492  
493 8 $frame;
494 }
495  
496 =item reset_frames()
497  
498 This will reset the counter used by next_frame(). Use this if you want
499 to start calculating frames from the beginning more than once.
500  
501 =cut
502  
503 3 ▶sub reset_frames {
Subroutine reset_frames: called CC=1 SCAR=0.0
Pod documented
504 3 my $self = shift;
505  
506 3 $self->{frame_offset} = 0;
507 3 $self->{generated_first_frame} = 0;
508 }
509  
510 7 ▶sub _decode_mime_entity {
Subroutine _decode_mime_entity: called CC=4 SCAR=13.9
511 7 my $block = shift;
512  
513 7 my @headers;
514  
515 # FIXME: this routine really sucks. We need to find a more reliable
516 # method.
517  
518 # first make sure that this looks like a MIME message at all:
519 7 ▶ if (not $block or not $block =~ /^Content-Type:/im) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block and $block =~ /^Content-Type:/im) 6 1 7
Condition66%2/3
$block and $block =~ /^Content-Type:/im
!ll&&!rl&&r
061
MC/DC50%1/2
$block and $block =~ /^Content-Type:/im
$block $block =~ /^Content-Type:/im
Truth table66%2/3
A and B
A= $blockB= $block =~ /^Content-Type:/im
ABresult
0X0
100
111
520 6 return ($block, @headers);
521 }
522  
523 1 my @lines = split(/\r\n/, $block);
524 1 while (1) {
525 3 my $line = shift @lines;
526 3 chomp $line;
527 3 ▶ last if not $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 1 2 3
528 2 push @headers, $line;
529 }
530  
531 1 my $content = join("\r\n", @lines);
532  
533 1 ($content, @headers);
534 }
535  
536 10 ▶sub _encode_mime_entity {
Subroutine _encode_mime_entity: called CC=3 SCAR=11.0
537 10 my $content = shift;
538 10 my @headers = @_;
539  
540 10 ▶ return $content if (not @headers);
Branch: 1 branch
BranchTrueFalseTotal
unless @headers 3 7 10
541  
542 7 my $res = "";
543 7 for my $header (@headers) {
544 8 chomp $header;
545 8 $res .= $header . "\r\n";
546 }
547 7 $res .= "\r\n";
548 7 $res .= $content;
549  
550 7 $res;
551 }
552  
553 =pod
554  
555 =back
556  
557 =head1 SEE ALSO
558  
559 =over 4
560  
561 =item L<Net::BEEP::Lite::Session>
562  
563 =item L<Net::BEEP::Lite::Frame>
564  
565 =back
566  
567 =cut
568  
569 5 1;
« Lite/ServerSession.pm ↑ Summary Lite/ClientSession.pm »