blib/lib/XAS/Lib/Stomp/Parser.pm

filter: Statementstmt 19.5% 17 / 87 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 3 / 6 Podpod 66.6% 2 / 3 totaltotal 15.7% 22 / 140 CC 19 SCAR 56.3
CC
19
Cov
14%
parse
349.9
get_pending
2.0
init
2.0
CRAP 277.3
« blib/lib/XAS/Lib/Modules/Environment.pm ↑ Summary blib/lib/XAS/Lib/Service/Unix.pm »
1 package XAS::Lib::Stomp::Parser;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use XAS::Lib::Stomp::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use XAS::Class
8 1 debug => 0,
9 version => $VERSION,
10 base => 'XAS::Base',
11 mixin => 'XAS::Lib::Mixins::Bufops',
12 utils => 'trim',
13 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $EOF = "\000";
16 1 our $CNTRL = qr((?:[[:cntrl:]])+);
17 1 our $HEADER = qr(([\w\-~]+)\s*:\s*(.*));
18 1 our $EOL = qr((\015\012?|\012\015?|\015|\012));
19 1 our $BEOH = qr((\015\012\000?|\012\015\000?|\015\000|\012\000));
20 1 our $EOH = qr((\015\012\015\012?|\012\015\012\015?|\015\015|\012\012));
21  
22 #use Data::Dumper;
23 #use Data::Hexdumper;
24  
25 # ----------------------------------------------------------------------
26 # Public Methods
27 # ----------------------------------------------------------------------
28  
29 sub parse {
30 0 ▶ my $self = shift;
Subroutine parse: not called CC=19 SCAR=58.6
Pod undocumented
31 0 my $buffer = shift;
32  
33 0 my $line;
34 my $length;
35 0 my $clength;
36 0 my $count = 0;
37 0 my $frame = undef;
38  
39 1 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 0 $self->{buffer} .= $buffer;
42  
43 0 $self->log->debug('stomp parse');
44 # $self->log->debug(hexdump($self->{buffer}));
45  
46 # A valid frame is usually this:
47 #
48 # command<eol> - command
49 # header<eol> - 0 or more
50 # <eol> - seperator
51 # body<eof> - body
52 #
53 # as of v1.1 this is a valid frame
54 #
55 # <eol><eol><eol><eof>
56 #
57 # and is used as a NOOP, which is used as a protocol keepalive. This
58 # module will create a fake 'NOOP' command for that frame. Frame
59 # stringification does the right thing.
60 #
61 # All current versions define <eof> as \000.
62 #
63 # In v1.0 and v1.1, <eol> was defined as NEWLINE, which is \012, but,
64 # common usage was \n, which is platform specific, hence the $EOL, $EOH
65 # and $BEOH regexs to match against.
66 #
67 # v1.2 changes <eol> to \015\012
68 #
69  
70 0 for (;;) {
71  
72 0 $self->log->debug('state = ' . $self->{state});
73  
74 0 ▶ if ($self->{state} eq 'command') {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'state'} eq 'command') { } 0 0 0
elsif ($self->{'state'} eq 'headers') { } 0 0 0
elsif ($self->{'state'} eq 'body') { } 0 0 0
elsif ($self->{'state'} eq 'frame') { } 0 0 0
75  
76 # start of the frame
77 # check for a valid buffer, must have a EOL someplace.
78  
79 0 ▶ if ($line = $self->buf_get_line(\$self->{buffer}, $EOL)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $self->buf_get_line(\$self->{'buffer'}, $XAS::Lib::Stomp::Parser::EOL)) { } 0 0 0
80  
81 0 $self->log->debug('command');
82 # $self->log->debug(hexdump($line));
83  
84 0 $line = trim($line);
85  
86 0 ▶ $self->{command} = ($line eq '') ? 'NOOP' : $line;
Branch: 1 branch
BranchTrueFalseTotal
$line eq '' ? : 0 0 0
87 0 $self->{state} = 'headers';
88  
89 0 } else { last; }
90  
91 } elsif ($self->{state} eq 'headers') {
92  
93 # start of the headers, they last until a standalone <eol>
94 # or <eof> is reached.
95  
96 0 $self->log->debug("header");
97  
98 0 $length = length($self->{buffer});
99  
100 0 $self->{buffer} =~ m/$EOH/g;
101 0 ▶ $clength = pos($self->{buffer}) || -1;
Condition0%0/2
pos $self->{'buffer'} || -1
l!l
00
MC/DC0%0/1
pos $self->{'buffer'} || -1
pos $self->{'buffer'}
Truth table0%0/2
A
A= pos $self->{'buffer'}
Aresult
11
00
102  
103 0 $self->log->debug("end of headers $clength");
104  
105 0 ▶ if ($clength == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clength == -1) 0 0 0
106  
107 0 pos($self->{buffer}) = 0;
108 0 $self->{buffer} =~ m/$BEOH/g;
109 0 ▶ $clength = pos($self->{buffer}) || -1;
Condition0%0/2
pos $self->{'buffer'} || -1
l!l
00
MC/DC0%0/1
pos $self->{'buffer'} || -1
pos $self->{'buffer'}
Truth table0%0/2
A
A= pos $self->{'buffer'}
Aresult
11
00
110  
111 0 $self->log->debug("end of frame $clength");
112  
113 }
114  
115 0 ▶ if (($clength != -1) && ($clength <= $length)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clength != -1 and $clength <= $length) { } 0 0 0
Condition0%0/3
$clength != -1 and $clength <= $length
!ll&&!rl&&r
000
MC/DC0%0/2
$clength != -1 and $clength <= $length
$clength != -1 $clength <= $length
Truth table0%0/3
A and B
A= $clength != -1B= $clength <= $length
ABresult
0X0
100
111
116  
117 0 $line = $self->buf_slurp(\$self->{buffer}, $clength);
118 # $self->log->debug(hexdump($line));
119  
120 0 ▶ while ($line =~ s/^$HEADER//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^$XAS::Lib::Stomp::Parser::HEADER//) 0 0 0
121  
122 0 $self->log->debug('valid header');
123  
124 0 my $key = lc($1);
125 0 my $value = trim($2);
126  
127 # v1.2 says that the first defined header is
128 # to be honored. v1.0 and v1.1 implies that
129 # the last defined header is honored. The duplictes
130 # are discarded.
131  
132 0 ▶ if ($self->env->mqlevel < 1.2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->env->mqlevel < 1.2) { } 0 0 0
133  
134 0 $self->{headers}->{$key} = $value;
135  
136 } else {
137  
138 0 ▶ unless (defined($self->{headers}->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"headers"}{$key}) 0 0 0
139  
140 0 $self->{headers}->{$key} = $value;
141  
142 }
143  
144 }
145  
146 0 $line =~ s/$EOL//;
147  
148 }
149  
150 0 $self->{state} = 'body';
151  
152 0 } else { last; }
153  
154 } elsif ($self->{state} eq 'body') {
155  
156 0 $self->log->debug('body');
157  
158 # start of the body, determine wither to use
159 # content-length or EOF to find the end
160  
161 0 $length = length($self->{buffer});
162  
163 0 ▶ if ($clength = $self->{headers}->{'content-length'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clength = $self->{'headers'}{'content-length'}) { } 0 0 0
164  
165 0 $self->log->debug('using content-length');
166  
167 0 ▶ if ($clength <= $length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clength <= $length) { } 0 0 0
168  
169 0 $self->{body} = $self->buf_slurp(\$self->{buffer}, $clength);
170 0 $self->{state} = 'frame';
171  
172 0 } else { last; }
173  
174 } else {
175  
176 0 $self->log->debug('using EOF');
177  
178 0 $clength = index($self->{buffer}, $EOF);
179  
180 0 ▶ if (($clength != -1) && ($clength <= $length)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clength != -1 and $clength <= $length) { } 0 0 0
Condition0%0/3
$clength != -1 and $clength <= $length
!ll&&!rl&&r
000
MC/DC0%0/2
$clength != -1 and $clength <= $length
$clength != -1 $clength <= $length
Truth table0%0/3
A and B
A= $clength != -1B= $clength <= $length
ABresult
0X0
100
111
181  
182 0 $self->{body} = $self->buf_get_line(\$self->{buffer}, $EOF);
183 0 chop $self->{body};
184 0 $self->{state} = 'frame';
185  
186 0 } else { last; }
187  
188 }
189  
190 } elsif ($self->{state} eq 'frame') {
191  
192 0 $self->log->debug('building frame');
193  
194 # clear out inter-frame crap and create the object.
195  
196 0 $self->{buffer} =~ s/^$CNTRL//;
197  
198 $frame = XAS::Lib::Stomp::Frame->new(
199 -command => $self->{command},
200 -headers => $self->{headers},
201 -body => $self->{body}
202 0 );
203  
204 # reset ourselves
205  
206 0 $count = 0;
207  
208 0 delete $self->{command};
209 0 delete $self->{headers};
210 0 delete $self->{body};
211  
212 0 $self->{state} = 'command';
213  
214 }
215  
216 }
217  
218 0 return $frame;
219  
220 }
221  
222 sub get_pending {
223 0 ▶ my $self = shift;
Subroutine get_pending: not called CC=1 SCAR=6.9
Pod documented
224  
225 0 return $self->{buffer};
226  
227 }
228  
229 # ----------------------------------------------------------------------
230 # Private Methods
231 # ----------------------------------------------------------------------
232  
233 sub init {
234 0 ▶ my $class = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
235  
236 0 my $self = $class->SUPER::init(@_);
237
238 0 $self->{state} = 'command';
239  
240 0 return $self;
241  
242 }
243  
244 1 1;
245  
246 __END__
247  
248 =head1 NAME
249  
250 XAS::Lib::Stomp::Parse - Create a STOMP Frame From a Buffer
251  
252 =head1 SYNOPSIS
253  
254 use XAS::Lib::Stomp::Parser;
255  
256 my $parser = XAS::Lib::Stomp::Parser->new();
257  
258 while (my $buffer = read()) {
259  
260 if (my $frame = $parser->parse($buffer)) {
261  
262 # do something...
263  
264 }
265  
266 }
267  
268 =head1 DESCRIPTION
269  
270 This module creates STOMP frames from a buffer. STOMP is the
271 Streaming Text Orientated Messaging Protocol (or the Protocol Briefly
272 Known as TTMP and Represented by the symbol :ttmp). It's a simple and easy to
273 implement protocol for working with Message Orientated Middleware from
274 any language. This module supports v1.0, v1.1 and v1.2 frames with limited
275 interoperability between the frame types.
276  
277 A STOMP frame consists of a command, a series of headers and a body.
278  
279 =head1 METHODS
280  
281 =head2 new
282  
283 Creates a new parser.
284  
285 =head2 get_pending
286  
287 Returns the contents of the internal buffer.
288  
289 =head1 SEE ALSO
290  
291 =over 4
292  
293 =item L<XAS|XAS>
294  
295 =back
296  
297 For more information on the STOMP protocol, please refer to: L<http://stomp.github.io/> .
298  
299 =head1 AUTHOR
300  
301 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
302  
303 =head1 COPYRIGHT AND LICENSE
304  
305 Copyright (C) 2014 Kevin L. Esteb
306  
307 This is free software; you can redistribute it and/or modify it under
308 the terms of the Artistic License 2.0. For details, see the full text
309 of the license at http://www.perlfoundation.org/artistic_license_2_0.
310  
311 =cut
« blib/lib/XAS/Lib/Modules/Environment.pm ↑ Summary blib/lib/XAS/Lib/Service/Unix.pm »