blib/lib/Async/Redis/URI.pm

filter: Statementstmt 94.9% 75 / 79 Branchbran 80.4% 37 / 46 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 16 / 16 Podpod 16.6% 2 / 12 totaltotal 83.9% 141 / 168 CC 28 SCAR 26.1
CC
28
Cov
89%
parse
24.1
to_hash
6.0
_decode
2.0
CRAP 13.6
« blib/lib/Async/Redis/Pool.pm ↑ Summary blib/lib/Async/Redis/AutoPipeline.pm »
1 package Async::Redis::URI;
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 2 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # URL decode
8 18 ▶sub _decode {
Subroutine _decode: called CC=2 SCAR=7.0
9 18 my ($str) = @_;
10 18 ▶ return unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 18 18
11 18 $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
12 18 return $str;
13 }
14  
15 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 20 my ($class, %args) = @_;
17 20 return bless \%args, $class;
18 }
19  
20 26 ▶sub parse {
Subroutine parse: called CC=22 SCAR=31.8
Pod documented
21 26 my ($class, $uri_string) = @_;
22  
23 26 ▶ return undef unless defined $uri_string && $uri_string ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri_string and $uri_string ne "" 2 24 26
Condition100%3/3
defined $uri_string and $uri_string ne ""
!ll&&!rl&&r
1124
MC/DC100%2/2
defined $uri_string and $uri_string ne ""
defined $uri_string $uri_string ne ""
Truth table100%3/3
A and B
A= defined $uri_stringB= $uri_string ne ""
ABresult
0X0
100
111
24  
25 24 my %parsed = (
26 host => 'localhost',
27 port => 6379,
28 database => 0,
29 tls => 0,
30 is_unix => 0,
31 );
32  
33 # Handle unix socket: redis+unix://[:password@]/path[?query]
34 24 ▶ if ($uri_string =~ m{^(redis\+unix)://(?:(?::([^@]*))?@)?(/[^?]+)(?:\?(.*))?$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri_string =~ m[^(redis\+unix)://(?:(?::([^\@]*))?@)?(/[^?]+)(?:\?(.*))?$]u) 7 17 24
35 7 $parsed{scheme} = $1;
36 7 ▶ $parsed{password} = _decode($2) if defined $2 && $2 ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 and $2 ne "" 1 6 7
Condition66%2/3
defined $2 and $2 ne ""
!ll&&!rl&&r
601
MC/DC50%1/2
defined $2 and $2 ne ""
defined $2 $2 ne ""
Truth table66%2/3
A and B
A= defined $2B= $2 ne ""
ABresult
0X0
100
111
37 7 $parsed{path} = $3;
38 7 $parsed{is_unix} = 1;
39  
40 # Remove host/port for unix sockets
41 7 delete $parsed{host};
42 7 delete $parsed{port};
43  
44 # Parse query string
45 7 ▶ if (defined $4) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $4) 4 3 7
46 4 my %query;
47 4 for my $pair (split /&/, $4) {
48 5 my ($k, $v) = split /=/, $pair, 2;
49 5 $query{$k} = _decode($v);
50 }
51 4 ▶ $parsed{database} = $query{db} if exists $query{db};
Branch: 1 branch
BranchTrueFalseTotal
if exists $query{"db"} 4 0 4
52 }
53  
54 7 return $class->new(%parsed);
55 }
56  
57 # Standard URI: redis[s]://[user:pass@]host[:port][/database]
58 17 ▶ unless ($uri_string =~ m{^(rediss?)://(.+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri_string =~ m[^(rediss?)://(.+)$]u) 4 13 17
59 4 die "Invalid Redis URI: must start with redis://, rediss://, or redis+unix://";
60 }
61  
62 13 my $scheme = $1;
63 13 my $rest = $2;
64  
65 13 $parsed{scheme} = $scheme;
66 13 ▶ $parsed{tls} = 1 if $scheme eq 'rediss';
Branch: 1 branch
BranchTrueFalseTotal
if $scheme eq "rediss" 3 10 13
67  
68 # Split userinfo from host
69 13 my ($userinfo, $hostinfo);
70 13 ▶ if ($rest =~ /^([^@]*)@(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^([^\@]*)@(.+)$/u) { } 7 6 13
71 7 $userinfo = $1;
72 7 $hostinfo = $2;
73 } else {
74 6 $hostinfo = $rest;
75 }
76  
77 # Parse userinfo: empty, :password, user:password, or just user
78 13 ▶ if (defined $userinfo && $userinfo ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $userinfo and $userinfo ne "") 7 6 13
Condition66%2/3
defined $userinfo and $userinfo ne ""
!ll&&!rl&&r
607
MC/DC50%1/2
defined $userinfo and $userinfo ne ""
defined $userinfo $userinfo ne ""
Truth table66%2/3
A and B
A= defined $userinfoB= $userinfo ne ""
ABresult
0X0
100
111
79 7 ▶ if ($userinfo =~ /^:(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($userinfo =~ /^:(.*)$/u) { } 2 5 7
elsif ($userinfo =~ /^([^:]*):(.*)$/u) { } 5 0 5
80 # :password (no username)
81 2 $parsed{password} = _decode($1);
82 } elsif ($userinfo =~ /^([^:]*):(.*)$/) {
83 # user:password
84 5 $parsed{username} = _decode($1);
85 5 $parsed{password} = _decode($2);
86 } else {
87 # just username
88 0 $parsed{username} = _decode($userinfo);
89 }
90 }
91  
92 # Parse hostinfo: host[:port][/database]
93 13 ▶ if ($hostinfo =~ m{^([^:/]+)(?::(\d+))?(?:/(\d+))?$}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hostinfo =~ m[^([^:/]+)(?::(\d+))?(?:/(\d+))?$]u) { } 13 0 13
elsif ($hostinfo =~ m[^([^:/]+)(?::(\d+))?/?$]u) { } 0 0 0
94 13 $parsed{host} = $1;
95 13 ▶ $parsed{port} = int($2) if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 4 9 13
96 13 ▶ $parsed{database} = int($3) if defined $3;
Branch: 1 branch
BranchTrueFalseTotal
if defined $3 4 9 13
97 } elsif ($hostinfo =~ m{^([^:/]+)(?::(\d+))?/?$}) {
98 # host:port with trailing slash but no database
99 0 $parsed{host} = $1;
100 0 ▶ $parsed{port} = int($2) if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 0 0
101 } else {
102 0 die "Invalid Redis URI format: cannot parse host from '$hostinfo'";
103 }
104  
105 # Validate we got a host
106 13 ▶ die "Invalid Redis URI: empty host" unless $parsed{host};
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed{"host"} 0 13 13
107  
108 13 return $class->new(%parsed);
109 }
110  
111 # Accessors
112 4 ▶sub scheme { shift->{scheme} }
Subroutine scheme: called CC=1 SCAR=0.0
Pod undocumented
113 6 ▶sub host { shift->{host} }
Subroutine host: called CC=1 SCAR=0.0
Pod undocumented
114 7 ▶sub port { shift->{port} }
Subroutine port: called CC=1 SCAR=0.0
Pod undocumented
115 6 ▶sub path { shift->{path} }
Subroutine path: called CC=1 SCAR=0.0
Pod undocumented
116 15 ▶sub database { shift->{database} }
Subroutine database: called CC=1 SCAR=0.0
Pod undocumented
117 12 ▶sub username { shift->{username} }
Subroutine username: called CC=1 SCAR=0.0
Pod undocumented
118 13 ▶sub password { shift->{password} }
Subroutine password: called CC=1 SCAR=0.0
Pod undocumented
119 9 ▶sub tls { shift->{tls} }
Subroutine tls: called CC=1 SCAR=0.0
Pod undocumented
120 8 ▶sub is_unix { shift->{is_unix} }
Subroutine is_unix: called CC=1 SCAR=0.0
Pod undocumented
121  
122 # Convert to hash suitable for Async::Redis->new()
123 5 ▶sub to_hash {
Subroutine to_hash: called CC=6 SCAR=17.9
Pod documented
124 5 my ($self) = @_;
125 5 my %hash;
126  
127 5 ▶ if ($self->is_unix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_unix) { } 3 2 5
128 3 $hash{path} = $self->path;
129 } else {
130 2 $hash{host} = $self->host;
131 2 $hash{port} = $self->port;
132 }
133  
134 5 ▶ $hash{database} = $self->database if $self->database;
Branch: 1 branch
BranchTrueFalseTotal
if $self->database 3 2 5
135 5 ▶ $hash{username} = $self->username if defined $self->username;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->username 1 4 5
136 5 ▶ $hash{password} = $self->password if defined $self->password;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->password 1 4 5
137 5 ▶ $hash{tls} = 1 if $self->tls;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tls 1 4 5
138  
139 5 return %hash;
140 }
141  
142 2 1;
143  
144 __END__
« blib/lib/Async/Redis/Pool.pm ↑ Summary blib/lib/Async/Redis/AutoPipeline.pm »