| 1 | package Net::Async::Redis::Protocol; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '2.004_001'; # TRIAL VERSION | |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | Net::Async::Redis::Protocol - simple implementation of the Redis wire protocol | ||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | Used internally by L<Net::Async::Redis> and L<Net::Async::Redis::Server>. | ||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Scalar::Util qw(blessed reftype looks_like_number); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | 2 | my $CRLF = "\x0D\x0A"; | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | sub new { bless { @_[1..$#_] }, $_[0] } | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | =head2 encode | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | Given a Perl data structure, will return data suitable for sending | ||||||||||||||||||||||||||||||||||
| 28 | back as a response as from a Redis server. | ||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | Note that this is not the correct format for client requests, | ||||||||||||||||||||||||||||||||||
| 31 | see L</encode_from_client> instead. | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | sub encode { | ||||||||||||||||||||||||||||||||||
| 36 | 44 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=11 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 37 | 44 | ▶ | die 'blessed data is not ok' if blessed $data; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 38 | 44 | ▶ | if(my $type = reftype $data) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 39 | 12 | ▶ | if($type eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 40 | 12 | return '*' . (0 + @$data) . $CRLF . join '', map $self->encode($_), @$data | |||||||||||||||||||||||||||||||||
| 41 | } elsif($type eq 'HASH') { | ||||||||||||||||||||||||||||||||||
| 42 | 0 | die 'no hash support' | |||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||
| 44 | 0 | die 'no support for ' . $type | |||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||
| 46 | 32 | ▶ | if(!defined($data)) { | ||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||
| 47 | 4 | return '$-1' . $CRLF; | |||||||||||||||||||||||||||||||||
| 48 | } elsif(!length($data)) { | ||||||||||||||||||||||||||||||||||
| 49 | 4 | return '$0' . $CRLF . $CRLF; | |||||||||||||||||||||||||||||||||
| 50 | ▶ | } elsif(($data ^ $data) eq "0" and int(0+$data) eq $data) { | |||||||||||||||||||||||||||||||||
|
Condition66%2/3
($data ^ $data) eq 0 and int 0 + $data eq $data
MC/DC50%1/2
($data ^ $data) eq 0 and int 0 + $data eq $data
($data ^ $data) eq 0
int 0 + $data eq $data
Truth table66%2/3
A and B
A= ($data ^ $data) eq 0B= int 0 + $data eq $data
| |||||||||||||||||||||||||||||||||||
| 51 | 12 | return ':' . (0 + $data) . $CRLF; | |||||||||||||||||||||||||||||||||
| 52 | ▶ | } elsif(length($data) < 100 and $data !~ /[$CRLF]/) { | |||||||||||||||||||||||||||||||||
|
Condition66%2/3
length $data < 100 and not $data =~ /[$CRLF]/
MC/DC50%1/2
length $data < 100 and not $data =~ /[$CRLF]/
length $data < 100
not $data =~ /[$CRLF]/
Truth table66%2/3
A and not(B)
A= length $data < 100B= not $data =~ /[$CRLF]/
| |||||||||||||||||||||||||||||||||||
| 53 | 10 | return '+' . $data . $CRLF; | |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||
| 55 | 2 | return '$' . length($data) . $CRLF . $data . $CRLF; | |||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | =head2 encode_from_client | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | Handles client format encoding. Expects a list of data items, and will | ||||||||||||||||||||||||||||||||||
| 61 | convert them into length-prefixed bulk strings as a single response item. | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | sub encode_from_client { | ||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my ($self, @data) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
encode_from_client: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 67 | return '*' . (0 + @data) . $CRLF . join '', map { | ||||||||||||||||||||||||||||||||||
| 68 | 0 | '$' . length($_) . $CRLF . $_ . $CRLF | |||||||||||||||||||||||||||||||||
| 69 | } @data; | ||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | =head2 decode | ||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | Decodes wire protocol data into Perl data structures. | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | Expects to be called with a reference to a byte string, and will | ||||||||||||||||||||||||||||||||||
| 77 | extract as much as it can from that string (destructively). | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | Likely to call L</item> or L</item_error> zero or more times. | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | sub decode { | ||||||||||||||||||||||||||||||||||
| 84 | 28 | ▶ | my ($self, $bytes) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=19 SCAR=31.7
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 28 | my $len = $self->{parsing_bulk}; | |||||||||||||||||||||||||||||||||
| 87 | ITEM: | ||||||||||||||||||||||||||||||||||
| 88 | 28 | for ($$bytes) { | |||||||||||||||||||||||||||||||||
| 89 | 50 | ▶ | if(defined($len)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | last ITEM unless length($_) >= $len + 2; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 6 | ▶ | die 'invalid bulk data, did not end in CRLF' unless substr($_, $len, 2, '') eq $CRLF; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | 6 | $self->item(substr $_, 0, delete $self->{parsing_bulk}, ''); | |||||||||||||||||||||||||||||||||
| 93 | 6 | undef $len; | |||||||||||||||||||||||||||||||||
| 94 | 6 | ▶ | last ITEM unless length; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | 44 | ▶ | if(s{^\+([^\x0D]*)$CRLF}{}) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 97 | 10 | $self->item("$1"); | |||||||||||||||||||||||||||||||||
| 98 | } elsif(s{^:([^\x0D]*)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||
| 99 | 12 | my $int = $1; | |||||||||||||||||||||||||||||||||
| 100 | 12 | ▶ | die 'invalid integer value ' . $int unless looks_like_number($int) && int($int) eq $int; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&looks_like_number($int) and int $int eq $int
MC/DC0%0/2
&looks_like_number($int) and int $int eq $int
&looks_like_number($int)
int $int eq $int
Truth table33%1/3
A and B
A= &looks_like_number($int)B= int $int eq $int
| |||||||||||||||||||||||||||||||||||
| 101 | 12 | $self->item(0 + $int); | |||||||||||||||||||||||||||||||||
| 102 | } elsif(s{^\$-1$CRLF}{}) { | ||||||||||||||||||||||||||||||||||
| 103 | 4 | $self->item(undef); | |||||||||||||||||||||||||||||||||
| 104 | } elsif(s{^\$([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||
| 105 | 6 | $len = $1; | |||||||||||||||||||||||||||||||||
| 106 | 6 | ▶ | die 'invalid numeric value for length ' . $len unless 0+$len eq $len; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 6 | $self->{parsing_bulk} = $len; | |||||||||||||||||||||||||||||||||
| 108 | } elsif(s{^\*-1$CRLF}{}) { | ||||||||||||||||||||||||||||||||||
| 109 | 0 | $self->item(undef); | |||||||||||||||||||||||||||||||||
| 110 | } elsif(s{^\*([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||
| 111 | 12 | my $pending = $1; | |||||||||||||||||||||||||||||||||
| 112 | 12 | ▶ | die 'invalid numeric value for array ' . $pending unless 0+$pending eq $pending; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | 12 | ▶ | if($pending) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 114 | 10 | push @{$self->{active}}, { array => $pending }; | |||||||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||||||||
| 116 | 2 | $self->item([]); | |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | } elsif(s{^-([^\x0D]*)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||
| 119 | 0 | $self->item_error($1); | |||||||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||||||||
| 121 | 0 | last ITEM; | |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||
| 123 | 44 | ▶ | redo ITEM if length; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub parse { $_[0]->decode($_[1]) } | ||||||||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | sub item { | ||||||||||||||||||||||||||||||||||
| 130 | 34 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
item: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 131 | 34 | while(1) { | |||||||||||||||||||||||||||||||||
| 132 | 44 | ▶ | return $self->{handler}->($data) unless @{$self->{active} || []}; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | 16 | push @{$self->{active}[-1]{items}}, $data; | |||||||||||||||||||||||||||||||||
| 135 | 16 | ▶ | return if --$self->{active}[-1]{array}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 136 | 10 | $data = (pop @{$self->{active}})->{items}; | |||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | sub item_error { | ||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my ($self, $err) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
item_error: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | $self->{error}->($err) if $self->{error}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 143 | 0 | $self | |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||||||||