blib/lib/POE/Filter/Line.pm

filter: Statementstmt 98.9% 95 / 96 Branchbran 90.3% 47 / 52 Conditioncond 80.9% 17 / 21 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 92.8% 183 / 197 CC 35 SCAR 26.0
CC
35
Cov
94%
new
20.1
get_one
13.1
get_one_start
2.0
CRAP 13.4
« blib/lib/POE/Component/Client/TCP.pm ↑ Summary blib/lib/POE/Loop/Select.pm »
1 package POE::Filter::Line;
2  
3 106 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 106 ▶use POE::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 106 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 106 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
8 106 @ISA = qw(POE::Filter);
9  
10 106 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub DEBUG () { 0 }
13  
14 sub FRAMING_BUFFER () { 0 }
15 sub INPUT_REGEXP () { 1 }
16 sub OUTPUT_LITERAL () { 2 }
17 sub AUTODETECT_STATE () { 3 }
18 sub MAX_LENGTH () { 4 }
19 sub MAX_BUFFER () { 5 }
20 sub FIRST_UNUSED () { 6 } # First unused $self offset.
21  
22 sub AUTO_STATE_DONE () { 0x00 }
23 sub AUTO_STATE_FIRST () { 0x01 }
24 sub AUTO_STATE_SECOND () { 0x02 }
25  
26 106 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 106 our @EXPORT_OK = qw( FIRST_UNUSED );
28  
29 #------------------------------------------------------------------------------
30  
31 1065 ▶sub new {
Subroutine new: called CC=20 SCAR=30.0
Pod documented
32 1065 my $type = shift;
33  
34 1065 ▶ croak "$type requires an even number of parameters" if @_ and @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ and @_ & 1 1 1064 1065
Condition100%3/3
@_ and @_ & 1
!ll&&!rl&&r
3427221
MC/DC100%2/2
@_ and @_ & 1
@_ @_ & 1
Truth table100%3/3
A and B
A= @_B= @_ & 1
ABresult
0X0
100
111
35 1064 my %params = @_;
36  
37 croak "$type cannot have both Regexp and Literal line endings" if (
38 defined $params{Regexp} and defined $params{Literal}
39 1064 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Regexp"} and defined $params{"Literal"} 1 1063 1064
Condition66%2/3
defined $params{"Regexp"} and defined $params{"Literal"}
!ll&&!rl&&r
106301
MC/DC50%1/2
defined $params{"Regexp"} and defined $params{"Literal"}
defined $params{"Regexp"} defined $params{"Literal"}
Truth table66%2/3
A and B
A= defined $params{"Regexp"}B= defined $params{"Literal"}
ABresult
0X0
100
111
40  
41 1063 my ($input_regexp, $output_literal);
42 1063 my $autodetect = AUTO_STATE_DONE;
43  
44 # Literal newline for both incoming and outgoing. Every other known
45 # parameter conflicts with this one.
46 1063 ▶ if (defined $params{Literal}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'Literal'}) { } 707 356 1063
47 croak "A defined Literal must have a nonzero length"
48 707 ▶ unless defined($params{Literal}) and length($params{Literal});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"Literal"} and length $params{"Literal"} 1 706 707
Condition66%2/3
defined $params{"Literal"} and length $params{"Literal"}
!ll&&!rl&&r
01706
MC/DC50%1/2
defined $params{"Literal"} and length $params{"Literal"}
defined $params{"Literal"} length $params{"Literal"}
Truth table66%2/3
A and B
A= defined $params{"Literal"}B= length $params{"Literal"}
ABresult
0X0
100
111
49 706 $input_regexp = quotemeta $params{Literal};
50 706 $output_literal = $params{Literal};
51 706 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"InputLiteral"} or defined $params{"InputRegexp"} or defined $params{"OutputLiteral"}) 3 703 706
Condition100%6/6
exists $params{"InputLiteral"} or defined $params{"InputRegexp"} or defined $params{"OutputLiteral"}
l!l&&r!l&&!r
21703
exists $params{"InputLiteral"} or defined $params{"InputRegexp"}
l!l&&r!l&&!r
11704
MC/DC100%3/3
exists $params{"InputLiteral"} or defined $params{"InputRegexp"} or defined $params{"OutputLiteral"}
exists $params{"InputLiteral"} defined $params{"InputRegexp"} defined $params{"OutputLiteral"}
Truth table100%4/4
A or B or C
A= exists $params{"InputLiteral"}B= defined $params{"InputRegexp"}C= defined $params{"OutputLiteral"}
ABCresult
1XX1
01X1
0011
0000
52 exists $params{InputLiteral} or # undef means something
53 defined $params{InputRegexp} or
54 defined $params{OutputLiteral}
55 ) {
56 3 croak "$type cannot have Literal with any other parameter";
57 }
58 }
59  
60 # Input and output are specified separately, then.
61 else {
62  
63 # Input can be either a literal or a regexp. The regexp may be
64 # compiled or not; we don't rightly care at this point.
65 356 ▶ if (exists $params{InputLiteral}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params{'InputLiteral'}) { } 5 351 356
elsif (defined $params{'InputRegexp'}) { } 2 349 351
66 5 $input_regexp = $params{InputLiteral};
67  
68 # InputLiteral is defined. Turn it into a regexp and be done.
69 # Otherwise we will autodetect it.
70 5 ▶ if (defined($input_regexp) and length($input_regexp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input_regexp and length $input_regexp) { } 2 3 5
Condition66%2/3
defined $input_regexp and length $input_regexp
!ll&&!rl&&r
302
MC/DC50%1/2
defined $input_regexp and length $input_regexp
defined $input_regexp length $input_regexp
Truth table66%2/3
A and B
A= defined $input_regexpB= length $input_regexp
ABresult
0X0
100
111
71 2 $input_regexp = quotemeta $input_regexp;
72 }
73 else {
74 3 $autodetect = AUTO_STATE_FIRST;
75 3 $input_regexp = '';
76 }
77  
78 croak "$type cannot have both InputLiteral and InputRegexp"
79 5 ▶ if defined $params{InputRegexp};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"InputRegexp"} 1 4 5
80 }
81 elsif (defined $params{InputRegexp}) {
82 2 $input_regexp = $params{InputRegexp};
83 croak "$type cannot have both InputLiteral and InputRegexp"
84 2 ▶ if defined $params{InputLiteral};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"InputLiteral"} 0 2 2
85 }
86 else {
87 349 $input_regexp = "(\\x0D\\x0A?|\\x0A\\x0D?)";
88 }
89  
90 355 ▶ if (defined $params{OutputLiteral}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'OutputLiteral'}) { } 6 349 355
91 6 $output_literal = $params{OutputLiteral};
92 }
93 else {
94 349 $output_literal = "\x0D\x0A";
95 }
96 }
97  
98 1058 my $max_buffer = $type->__param_max( MaxBuffer => 512*1024*1024, \%params );
99 1056 my $max_length = $type->__param_max( MaxLength => 64*1024*1024, \%params );
100 1054 ▶ croak "MaxBuffer is not large enough for MaxLength blocks"
Branch: 1 branch
BranchTrueFalseTotal
unless $max_buffer >= $max_length 1 1053 1054
101 unless $max_buffer >= $max_length;
102  
103 1053 delete @params{qw(Literal InputLiteral OutputLiteral InputRegexp MaxLength MaxBuffer)};
104 1053 ▶ carp("$type ignores unknown parameters: ", join(', ', sort keys %params))
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %params 0 1053 1053
105 if scalar keys %params;
106  
107 1053 my $self = bless [
108 '', # FRAMING_BUFFER
109 $input_regexp, # INPUT_REGEXP
110 $output_literal, # OUTPUT_LITERAL
111 $autodetect, # AUTODETECT_STATE
112 $max_length, # MAX_LENGTH
113 $max_buffer # MAX_BUFFER
114 ], $type;
115  
116 1053 DEBUG and warn join ':', @$self;
117  
118 1053 $self;
119 }
120  
121  
122 #------------------------------------------------------------------------------
123 # get() is inherited from POE::Filter.
124  
125 #------------------------------------------------------------------------------
126 # 2001-07-27 RCC: Add get_one_start() and get_one() to correct filter
127 # changing and make input flow control possible.
128  
129 474 ▶sub get_one_start {
Subroutine get_one_start: called CC=2 SCAR=6.9
Pod documented
130 474 my ($self, $stream) = @_;
131  
132 474 DEBUG and do {
133 my $temp = join '', @$stream;
134 $temp = unpack 'H*', $temp;
135 warn "got some raw data: $temp\n";
136 };
137  
138 474 $self->[FRAMING_BUFFER] .= join '', @$stream;
139 474 ▶ die "Framing buffer exceeds the limit"
Branch: 1 branch
BranchTrueFalseTotal
if $self->[5] < length $self->[0] 1 473 474
140 if $self->[MAX_BUFFER] < length( $self->[FRAMING_BUFFER] );
141 }
142  
143 # TODO There is a lot of code duplicated here. What can be done?
144  
145 2816 ▶sub get_one {
Subroutine get_one: called CC=13 SCAR=25.7
Pod documented
146 2816 my $self = shift;
147  
148 # Process as many newlines an we can find.
149 2816 LINE: while (1) {
150  
151 # Autodetect is done, or it never started. Parse some buffer!
152 2818 ▶ unless ($self->[AUTODETECT_STATE]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[3]) 2811 7 2818
153 2811 DEBUG and warn unpack 'H*', $self->[INPUT_REGEXP];
154 last LINE
155 2811 ▶ unless $self->[FRAMING_BUFFER] =~ s/^(.*?)$self->[INPUT_REGEXP]//s;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[0] =~ s/^(.*?)$self->[1]//s 457 2354 2811
156 2354 DEBUG and warn "got line: <<", unpack('H*', $1), ">>\n";
157 2354 my $line = $1;
158 2354 ▶ die "Next line exceeds maximum line length"
Branch: 1 branch
BranchTrueFalseTotal
if length $line > $self->[4] 1 2353 2354
159 if length( $line ) > $self->[MAX_LENGTH];
160  
161 2353 return [ $line ];
162 }
163  
164 # Waiting for the first line ending. Look for a generic newline.
165 7 ▶ if ($self->[AUTODETECT_STATE] & AUTO_STATE_FIRST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3] & 1) 3 4 7
166 last LINE
167 3 ▶ unless $self->[FRAMING_BUFFER] =~ s/^(.*?)(\x0D\x0A?|\x0A\x0D?)//;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[0] =~ s/^(.*?)(\x0D\x0A?|\x0A\x0D?)// 0 3 3
168  
169 3 my $line = $1;
170  
171 # The newline can be complete under two conditions. First: If
172 # it's two characters. Second: If there's more data in the
173 # framing buffer. Loop around in case there are more lines.
174 3 ▶ if ( (length($2) == 2) or
Branch: 1 branch
BranchTrueFalseTotal
if (length $2 == 2 or length $self->[0]) { } 1 2 3
Condition66%2/3
length $2 == 2 or length $self->[0]
l!l&&r!l&&!r
102
MC/DC50%1/2
length $2 == 2 or length $self->[0]
length $2 == 2 length $self->[0]
Truth table66%2/3
A or B
A= length $2 == 2B= length $self->[0]
ABresult
1X1
011
000
175 (length $self->[FRAMING_BUFFER])
176 ) {
177 1 DEBUG and warn "detected complete newline after line: <<$1>>\n";
178 1 $self->[INPUT_REGEXP] = $2;
179 1 $self->[AUTODETECT_STATE] = AUTO_STATE_DONE;
180 }
181  
182 # The regexp has matched a potential partial newline. Save it,
183 # and move to the next state. There is no more data in the
184 # framing buffer, so we're done.
185 else {
186 2 DEBUG and warn "detected suspicious newline after line: <<$1>>\n";
187 2 $self->[INPUT_REGEXP] = $2;
188 2 $self->[AUTODETECT_STATE] = AUTO_STATE_SECOND;
189 }
190 3 ▶ die "Next line exceeds maximum line length"
Branch: 1 branch
BranchTrueFalseTotal
if length $line > $self->[4] 0 3 3
191 if length( $line ) > $self->[MAX_LENGTH];
192  
193 3 return [ $line ];
194 }
195  
196 # Waiting for the second line beginning. Bail out if we don't
197 # have anything in the framing buffer.
198 4 ▶ if ($self->[AUTODETECT_STATE] & AUTO_STATE_SECOND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3] & 2) 4 0 4
199 4 ▶ return [ ] unless length $self->[FRAMING_BUFFER];
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->[0] 2 2 4
200  
201 # Test the first character to see if it completes the previous
202 # potentially partial newline.
203 2 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (substr($self->[0], 0, 1) eq ($self->[1] eq "\r" ? "\n" : "\r")) { } 1 1 2
$self->[1] eq "\r" ? : 1 1 2
204 substr($self->[FRAMING_BUFFER], 0, 1) eq
205 ( $self->[INPUT_REGEXP] eq "\x0D" ? "\x0A" : "\x0D" )
206 ) {
207  
208 # Combine the first character with the previous newline, and
209 # discard the newline from the buffer. This is two statements
210 # for backward compatibility.
211 1 DEBUG and warn "completed newline after line: <<$1>>\n";
212 1 $self->[INPUT_REGEXP] .= substr($self->[FRAMING_BUFFER], 0, 1);
213 1 substr($self->[FRAMING_BUFFER], 0, 1) = '';
214 }
215 elsif (DEBUG) {
216 warn "decided prior suspicious newline is okay\n";
217 }
218  
219 # Regardless, whatever is in INPUT_REGEXP is now a complete
220 # newline. End autodetection, post-process the found newline,
221 # and loop to see if there are other lines in the buffer.
222 2 $self->[INPUT_REGEXP] = $self->[INPUT_REGEXP];
223 2 $self->[AUTODETECT_STATE] = AUTO_STATE_DONE;
224 2 next LINE;
225 }
226  
227 0 die "consistency error: AUTODETECT_STATE = $self->[AUTODETECT_STATE]";
228 }
229  
230 457 return [ ];
231 }
232  
233 #------------------------------------------------------------------------------
234 # New behavior. First translate system newlines ("\n") into whichever
235 # newlines are supposed to be sent. Second, add a trailing newline if
236 # one doesn't already exist. Since the referenced output list is
237 # supposed to contain one line per element, we also do a split and
238 # join. Bleah. ... why isn't the code doing what the comment says?
239  
240 328 ▶sub put {
Subroutine put: called CC=2 SCAR=6.9
Pod documented
241 328 my ($self, $lines) = @_;
242  
243 328 my @raw;
244 328 foreach (@$lines) {
245 352 push @raw, $_ . $self->[OUTPUT_LITERAL];
246 }
247  
248 328 \@raw;
249 }
250  
251 #------------------------------------------------------------------------------
252  
253 18 ▶sub get_pending {
Subroutine get_pending: called CC=2 SCAR=6.9
Pod documented
254 18 my $self = shift;
255 18 ▶ return [ $self->[FRAMING_BUFFER] ] if length $self->[FRAMING_BUFFER];
Branch: 1 branch
BranchTrueFalseTotal
if length $self->[0] 6 12 18
256 12 return undef;
257 }
258  
259 106 1;
260  
261 __END__
« blib/lib/POE/Component/Client/TCP.pm ↑ Summary blib/lib/POE/Loop/Select.pm »