File Coverage

blib/lib/POE/Filter/Slim/CLI.pm
Criterion Covered Total %
statement 43 44 97.7
branch 3 4 75.0
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 57 59 96.6


line stmt bran cond sub pod time code
1             package POE::Filter::Slim::CLI;
2              
3             #
4             # $Id: CLI.pm 26 2007-09-07 04:32:54Z andy $
5             #
6             # Filter lines according to SlimServer's CLI spec:
7             # Any combination of CR, LF, and \0 is accepted, and
8             # the same line-ending must be used for the response
9             #
10             # This filter also splits and unescapes any CLI commands
11              
12 1     1   1005 use strict;
  1         2  
  1         42  
13 1     1   7 use base 'POE::Filter::Line';
  1         2  
  1         1014  
14              
15 1     1   4635 use Carp qw(carp croak);
  1         2  
  1         64  
16 1     1   7 use Clone qw(clone);
  1         2  
  1         50  
17 1     1   1719 use URI::Escape qw(uri_escape);
  1         3177  
  1         676  
18              
19             sub DEBUG () { 0 }
20              
21             sub FRAMING_BUFFER () { 0 }
22             sub INPUT_REGEXP () { 1 }
23             sub OUTPUT_LITERAL () { 2 }
24              
25             if ( DEBUG ) {
26             require Data::Dump;
27             }
28              
29             our $VERSION = '0.02';
30              
31             sub new {
32 1     1 1 3195 my $type = shift;
33            
34 1         5 my $input_regexp = qr/[\x0D|\x0A|\x00]+/;
35            
36 1         11 my $self = $type->SUPER::new(
37             InputRegexp => $input_regexp,
38             );
39            
40 1         45 return $self;
41             }
42              
43             sub get_one {
44 8     8 1 2725 my $self = shift;
45            
46             LINE:
47 8         8 while (1) {
48             last LINE
49 8 100       77 unless $self->[FRAMING_BUFFER] =~ s/^(.*?)($self->[INPUT_REGEXP])//s;
50            
51 6         7 DEBUG && warn "using line ending: << ", unpack('H*', $2), " >>\n";
52            
53             # Save line-ending used in the request, it will be sent in the response
54 6         13 $self->[OUTPUT_LITERAL] = $2;
55            
56             # Split and unescape CLI command
57 6         16 my @elements = split / /, $1;
58 6         22 s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg for @elements;
  2         9  
59            
60 6         20 return [ \@elements ];
61             }
62            
63 2         5 return [];
64             }
65              
66             sub put {
67 2     2 1 2528 my ( $self, $lines ) = @_;
68            
69 2         27 $lines = clone($lines);
70            
71 2         3 my @raw;
72 2         3 foreach my $line ( @{$lines} ) {
  2         5  
73 4 50       12 if ( ref $line eq 'ARRAY' ) {
74             # Send an array of CLI commands
75 4         4 my @output;
76 4         5 for my $item ( @{$line} ) {
  4         8  
77 7         18 $item = uri_escape($item);
78 7         111 push @output, $item;
79             }
80 4         17 push @raw, join( ' ', @output ) . $self->[OUTPUT_LITERAL];
81             }
82             else {
83 0         0 push @raw, $line . $self->[OUTPUT_LITERAL];
84             }
85             }
86            
87 2         2 DEBUG && warn "CLI Filter sent: " . Data::Dump::dump(\@raw) . "\n";
88            
89 2         8 return \@raw;
90             }
91              
92             1;
93             __END__