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

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