| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $uri_string and $uri_string ne ""
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 ""
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 7 | $parsed{scheme} = $1; | |||||||||||||||||||||||||||
| 36 | 7 | ▶ | $parsed{password} = _decode($2) if defined $2 && $2 ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $2 and $2 ne ""
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 ""
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Split userinfo from host | ||||||||||||||||||||||||||||
| 69 | 13 | my ($userinfo, $hostinfo); | |||||||||||||||||||||||||||
| 70 | 13 | ▶ | if ($rest =~ /^([^@]*)@(.+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $userinfo and $userinfo ne ""
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 ""
| |||||||||||||||||||||||||||||
| 79 | 7 | ▶ | if ($userinfo =~ /^:(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 94 | 13 | $parsed{host} = $1; | |||||||||||||||||||||||||||
| 95 | 13 | ▶ | $parsed{port} = int($2) if defined $2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 13 | ▶ | $parsed{database} = int($3) if defined $3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 135 | 5 | ▶ | $hash{username} = $self->username if defined $self->username; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 5 | ▶ | $hash{password} = $self->password if defined $self->password; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 5 | ▶ | $hash{tls} = 1 if $self->tls; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 5 | return %hash; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | 1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||