| 1 | package AnyEvent::Redis::Protocol; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | AnyEvent::Redis::Protocol - Redis response parser (read handler) for AnyEvent | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | This package should not be directly used. It provides an AnyEvent read handler | ||||||||||||||||||||||||||||||||
| 13 | capable of parsing Redis responses. | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | L<AnyEvent::Handle>, | ||||||||||||||||||||||||||||||||
| 18 | Redis Protocol Specification L<http://redis.io/topics/protocol> | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub anyevent_read_type { | ||||||||||||||||||||||||||||||
|
Subroutine
anyevent_read_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 23 | 0 | my ($handle, $cb) = @_; | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $handle->push_read(line => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||
| 27 | 0 | my $line = $_[1]; | |||||||||||||||||||||||||||||||
| 28 | 0 | my $type = substr($line, 0, 1); | |||||||||||||||||||||||||||||||
| 29 | 0 | my $value = substr($line, 1); | |||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if ($type eq '*') { | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 31 | # Multi-bulk reply | ||||||||||||||||||||||||||||||||
| 32 | 0 | my $remaining = $value; | |||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ($remaining == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 34 | 0 | $cb->([]); | |||||||||||||||||||||||||||||||
| 35 | } elsif ($remaining == -1) { | ||||||||||||||||||||||||||||||||
| 36 | 0 | $cb->(undef); | |||||||||||||||||||||||||||||||
| 37 | } else { | ||||||||||||||||||||||||||||||||
| 38 | 0 | my $results = []; | |||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $handle->unshift_read(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||||||
| 40 | 0 | my $need_more_data = 0; | |||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ($handle->{rbuf} =~ /^(\$(-?\d+)\015\012)/) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 43 | 0 | my ($match, $vallen) = ($1, $2); | |||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($vallen == -1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 45 | # Delete the bulk header. | ||||||||||||||||||||||||||||||||
| 46 | 0 | substr($handle->{rbuf}, 0, length($match), ''); | |||||||||||||||||||||||||||||||
| 47 | 0 | push @$results, undef; | |||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | unless (--$remaining) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 0 | $cb->($results); | |||||||||||||||||||||||||||||||
| 50 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | } elsif (length $handle->{rbuf} >= (length($match) + $vallen + 2)) { | ||||||||||||||||||||||||||||||||
| 53 | # OK, we have enough in our buffer. | ||||||||||||||||||||||||||||||||
| 54 | # Delete the bulk header. | ||||||||||||||||||||||||||||||||
| 55 | 0 | substr($handle->{rbuf}, 0, length($match), ''); | |||||||||||||||||||||||||||||||
| 56 | 0 | my $value = substr($handle->{rbuf}, 0, $vallen, ''); | |||||||||||||||||||||||||||||||
| 57 | $value = $handle->{encoding}->decode($value) | ||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if $handle->{encoding} && $vallen; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$handle->{"encoding"} and $vallen
MC/DC0%0/2
$handle->{"encoding"} and $vallen
$handle->{"encoding"}
$vallen
Truth table0%0/3
A and B
A= $handle->{"encoding"}B= $vallen
| |||||||||||||||||||||||||||||||||
| 59 | 0 | push @$results, $value; | |||||||||||||||||||||||||||||||
| 60 | # Delete trailing data characters. | ||||||||||||||||||||||||||||||||
| 61 | 0 | substr($handle->{rbuf}, 0, 2, ''); | |||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | unless (--$remaining) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 0 | $cb->($results); | |||||||||||||||||||||||||||||||
| 64 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||||||
| 67 | 0 | $need_more_data = 1; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | } elsif ($handle->{rbuf} =~ s/^([\+\-:])([^\015\012]*)\015\012//) { | ||||||||||||||||||||||||||||||||
| 70 | 0 | my ($type, $value) = ($1, $2); | |||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($type eq '+' || $type eq ':') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$type eq "+" or $type eq ":"
MC/DC0%0/2
$type eq "+" or $type eq ":"
$type eq "+"
$type eq ":"
Truth table0%0/3
A or B
A= $type eq "+"B= $type eq ":"
| |||||||||||||||||||||||||||||||||
| 72 | 0 | push @$results, $value; | |||||||||||||||||||||||||||||||
| 73 | } elsif ($type eq '-') { | ||||||||||||||||||||||||||||||||
| 74 | # Embedded error; this seems possible only in EXEC answer, | ||||||||||||||||||||||||||||||||
| 75 | # so include error in results; don't abort parsing | ||||||||||||||||||||||||||||||||
| 76 | 0 | push @$results, bless \$value, 'AnyEvent::Redis::Error'; | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless (--$remaining) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 0 | $cb->($results); | |||||||||||||||||||||||||||||||
| 80 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | } elsif (substr($handle->{rbuf}, 0, 1) eq '*') { | ||||||||||||||||||||||||||||||||
| 83 | # Oh, how fun! A nested bulk reply. | ||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $reader; $reader = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $handle->unshift_read("AnyEvent::Redis::Protocol" => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 86 | 0 | push @$results, $_[0]; | |||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if (--$remaining) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 0 | $reader->(); | |||||||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||||||
| 90 | 0 | undef $reader; | |||||||||||||||||||||||||||||||
| 91 | 0 | $cb->($results); | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | 0 | }); | |||||||||||||||||||||||||||||||
| 94 | 0 | }; | |||||||||||||||||||||||||||||||
| 95 | 0 | $reader->(); | |||||||||||||||||||||||||||||||
| 96 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||||||
| 98 | # Nothing matched - read more... | ||||||||||||||||||||||||||||||||
| 99 | 0 | $need_more_data = 1; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | } until $need_more_data; | ||||||||||||||||||||||||||||||||
| 102 | 0 | return; # get more data | |||||||||||||||||||||||||||||||
| 103 | 0 | }); | |||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | ▶ | } elsif ($type eq '+' || $type eq ':') { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
$type eq "+" or $type eq ":"
MC/DC0%0/2
$type eq "+" or $type eq ":"
$type eq "+"
$type eq ":"
Truth table0%0/3
A or B
A= $type eq "+"B= $type eq ":"
| |||||||||||||||||||||||||||||||||
| 106 | # Single line/integer reply | ||||||||||||||||||||||||||||||||
| 107 | 0 | $cb->($value); | |||||||||||||||||||||||||||||||
| 108 | } elsif ($type eq '-') { | ||||||||||||||||||||||||||||||||
| 109 | # Single-line error reply | ||||||||||||||||||||||||||||||||
| 110 | 0 | $cb->($value, 1); | |||||||||||||||||||||||||||||||
| 111 | } elsif ($type eq '$') { | ||||||||||||||||||||||||||||||||
| 112 | # Bulk reply | ||||||||||||||||||||||||||||||||
| 113 | 0 | my $length = $value; | |||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ($length == -1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | 0 | $cb->(undef); | |||||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||||||
| 117 | # We need to read 2 bytes more than the length (stupid | ||||||||||||||||||||||||||||||||
| 118 | # CRLF framing). Then we need to discard them. | ||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $handle->unshift_read(chunk => $length + 2, sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 120 | 0 | my $data = $_[1]; | |||||||||||||||||||||||||||||||
| 121 | 0 | my $value = substr($data, 0, $length); | |||||||||||||||||||||||||||||||
| 122 | $value = $handle->{encoding}->decode($value) | ||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if $handle->{encoding} && $length; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$handle->{"encoding"} and $length
MC/DC0%0/2
$handle->{"encoding"} and $length
$handle->{"encoding"}
$length
Truth table0%0/3
A and B
A= $handle->{"encoding"}B= $length
| |||||||||||||||||||||||||||||||||
| 124 | 0 | $cb->($value); | |||||||||||||||||||||||||||||||
| 125 | 0 | }); | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 129 | 0 | }); | |||||||||||||||||||||||||||||||
| 130 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 131 | 0 | }; | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | Michael S. Fischer <michael+cpan@dynamine.net> | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | Copyright (C) 2010 Michael S. Fischer. | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 143 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||
| 144 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 8 | 1; | |||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||||||