blib/lib/DBIO/PostgreSQL/EV/ConnectInfo.pm

filter: Statementstmt 98.0% 147 / 150 Branchbran 78.0% 89 / 114 Conditioncond 66.1% 43 / 65 MC/DCmcdc 42.1% 16 / 38 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 81.4% 316 / 388 CC 86 SCAR 28.6
CC
86
Cov
85%
_uri_params
31.1
dbi_to_conninfo
22.3
_dbd_pg_params
20.1
CRAP 17.5
↑ Summary blib/lib/DBIO/PostgreSQL/EV/Storage.pm »
1 package DBIO::PostgreSQL::EV::ConnectInfo;
2 # ABSTRACT: PostgreSQL connection string utilities for DBIO async driver
3  
4 96 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 96 our $VERSION = '0.900007';
8  
9 96 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 96 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 96 our @EXPORT_OK = qw(
13 escape_conninfo conninfo_string with_connect_timeout dbi_to_conninfo dbi_dsn_parts is_libpq_parameter
14 );
15  
16  
17 3479 ▶sub escape_conninfo {
Subroutine escape_conninfo: called CC=6 SCAR=18.5
Pod documented
18 3479 my ($val, $key) = @_;
19 3479 ▶ _refuse_nul($key) if defined $val && $val =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val and $val =~ /\0/ 4 3475 3479
Condition66%2/3
defined $val and $val =~ /\0/
!ll&&!rl&&r
034754
MC/DC50%1/2
defined $val and $val =~ /\0/
defined $val $val =~ /\0/
Truth table66%2/3
A and B
A= defined $valB= $val =~ /\0/
ABresult
0X0
100
111
20 3475 ▶ return "''" unless defined $val && length $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and length $val 0 3475 3475
Condition33%1/3
defined $val and length $val
!ll&&!rl&&r
003475
MC/DC0%0/2
defined $val and length $val
defined $val length $val
Truth table33%1/3
A and B
A= defined $valB= length $val
ABresult
0X0
100
111
21 3475 ▶ return $val unless $val =~ /[\s'\\]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /[\s'\\]/ 3437 38 3475
22 38 $val =~ s/\\/\\\\/g;
23 38 $val =~ s/'/\\'/g;
24 38 return "'$val'";
25 }
26  
27  
28 1790 ▶sub conninfo_string {
Subroutine conninfo_string: called CC=2 SCAR=6.9
Pod documented
29 1790 my ($ci) = @_;
30 1790 ▶ return $ci unless ref $ci;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ci 709 1081 1790
31  
32 3476 return join(' ', map { "$_=" . escape_conninfo($ci->{$_}, $_) }
33 1081 grep { defined $ci->{$_} } keys %$ci);
34 }
35  
36 # A NUL byte ends the C string libpq reads, and the parameters behind it are
37 # lost without a word (karr k100). Refused as the transport refuses, naming
38 # the parameter and never its value. The transport is loaded here and not
39 # above: it loads this module.
40 8 ▶sub _refuse_nul {
Subroutine _refuse_nul: called CC=4 SCAR=14.1
41 8 my ($key) = @_;
42 8 require DBIO::PostgreSQL::EV::Storage;
43 8 ▶ DBIO::PostgreSQL::EV::Storage::_pg_refuse(
Branch: 1 branch
BranchTrueFalseTotal
defined $key && length $key && !($key =~ /\0/) ? : 7 1 8
Condition66%4/6
defined $key and length $key and not $key =~ /\0/
!ll&&!rl&&r
107
defined $key and length $key
!ll&&!rl&&r
107
MC/DC33%1/3
defined $key and length $key and not $key =~ /\0/
defined $key length $key not $key =~ /\0/
Truth table50%2/4
A and B and not(C)
A= defined $keyB= length $keyC= not $key =~ /\0/
ABCresult
0XX0
10X0
1100
1111
44 defined $key && length $key && $key !~ /\0/
45 ? "connection parameter '$key' holds a NUL byte, which ends the string libpq reads"
46 : 'a connection parameter holds a NUL byte, which ends the string libpq reads'
47 );
48 }
49  
50  
51 748 ▶sub with_connect_timeout {
Subroutine with_connect_timeout: called CC=8 SCAR=21.3
Pod documented
52 748 my ($conninfo) = @_;
53 748 ▶ return $conninfo unless defined $conninfo && !ref $conninfo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conninfo and not ref $conninfo 0 748 748
Condition33%1/3
defined $conninfo and not ref $conninfo
!ll&&!rl&&r
00748
MC/DC0%0/2
defined $conninfo and not ref $conninfo
defined $conninfo not ref $conninfo
Truth table33%1/3
A and not(B)
A= defined $conninfoB= not ref $conninfo
ABresult
0X0
100
111
54 748 ▶ return $conninfo if $conninfo =~ m{\A\s*postgres(?:ql)?://}i;
Branch: 1 branch
BranchTrueFalseTotal
if $conninfo =~ m[\A\s*postgres(?:ql)?://]i 1 747 748
55 747 ▶ while ($conninfo =~ /\G\s*([^\s=]+)\s*=\s*(?:'(?:[^'\\]|\\.)*'|(?:[^\s\\]|\\.)*)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conninfo =~ /\G\s*([^\s=]+)\s*=\s*(?:'(?:[^'\\]|\\.)*'|(?:[^\s\\]|\\.)*)/cg) 1837 730 2567
56 1837 ▶ return $conninfo if lc $1 eq 'connect_timeout';
Branch: 1 branch
BranchTrueFalseTotal
if lc $1 eq "connect_timeout" 17 1820 1837
57 }
58 730 ▶ return $conninfo unless $conninfo =~ /\G\s*\z/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $conninfo =~ /\G\s*\z/cg 0 730 730
59 730 ▶ return $conninfo =~ /\S/ ? "$conninfo connect_timeout=10" : 'connect_timeout=10';
Branch: 1 branch
BranchTrueFalseTotal
$conninfo =~ /\S/ ? : 719 11 730
60 }
61  
62  
63 96 my %LIBPQ_PARAMETER = map { $_ => 1 } qw(
64 service user password passfile channel_binding connect_timeout dbname host
65 hostaddr port client_encoding options application_name
66 fallback_application_name keepalives keepalives_idle keepalives_interval
67 keepalives_count tcp_user_timeout sslmode requiressl sslnegotiation
68 sslcompression sslcert sslkey sslcertmode sslpassword sslrootcert sslcrl
69 sslcrldir sslsni sslkeylogfile requirepeer require_auth
70 ssl_min_protocol_version ssl_max_protocol_version min_protocol_version
71 max_protocol_version gssencmode krbsrvname gsslib gssdelegation replication
72 target_session_attrs load_balance_hosts oauth_issuer oauth_client_id
73 oauth_client_secret oauth_scope scram_client_key scram_server_key
74 );
75  
76 579 ▶sub is_libpq_parameter {
Subroutine is_libpq_parameter: called CC=4 SCAR=14.9
Pod documented
77 579 my ($name) = @_;
78 579 ▶ return defined $name && !ref $name && $LIBPQ_PARAMETER{lc $name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $name && !ref($name) && $LIBPQ_PARAMETER{lc $name} ? : 99 480 579
Condition50%3/6
defined $name and not ref $name and $LIBPQ_PARAMETER{lc $name}
!ll&&!rl&&r
048099
defined $name and not ref $name
!ll&&!rl&&r
00579
MC/DC33%1/3
defined $name and not ref $name and $LIBPQ_PARAMETER{lc $name}
defined $name not ref $name $LIBPQ_PARAMETER{lc $name}
Truth table50%2/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $LIBPQ_PARAMETER{lc $name}
ABCresult
0XX0
10X0
1100
1111
79 }
80  
81  
82 505 ▶sub dbi_to_conninfo {
Subroutine dbi_to_conninfo: called CC=22 SCAR=31.1
Pod documented
83 505 my ($info, %opt) = @_;
84 # Copies: a slice of @$info itself would create the slots it looks at, and
85 # the array is the caller's.
86 505 my ($dsn, $user, $pass, $attrs, $extra_attrs) = @$info;
87 505 my ($driver, $in_dsn, $params) = dbi_dsn_parts($dsn);
88 # The DSN is named, never its password (karr k78): masked as core masks
89 # connect info in every message, given the connect info it belongs to.
90 505 ▶ die DBIO::Util::mask_connect_secrets(
Branch: 2 branches
BranchTrueFalseTotal
unless defined $driver and $driver =~ /\APg\z/i 0 505 505
defined $dsn ? : 0 0 0
Condition33%1/3
defined $driver and $driver =~ /\APg\z/i
!ll&&!rl&&r
00505
MC/DC0%0/2
defined $driver and $driver =~ /\APg\z/i
defined $driver $driver =~ /\APg\z/i
Truth table33%1/3
A and B
A= defined $driverB= $driver =~ /\APg\z/i
ABresult
0X0
100
111
91 'expected dbi:Pg:... DSN, got: ' . (defined $dsn ? $dsn : '(undef)') . "\n", $info,
92 ) unless defined $driver && $driver =~ /\APg\z/i;
93  
94 # DBI->connect (DBI.pm): the attributes are \%attr -- for the sync storage
95 # \%attrs and \%extra_attributes merged, the later one winning -- and then
96 # those in front of the DSN, split the way DBI splits them; a name without
97 # a value is an attribute that is undefined.
98 505 ▶ my @in_dsn = $in_dsn ? split(/\s*=>?\s*|\s*,\s*/, $in_dsn, -1) : ();
Branch: 1 branch
BranchTrueFalseTotal
$in_dsn ? : 64 441 505
99 505 ▶ push @in_dsn, undef if @in_dsn % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @in_dsn % 2 0 505 505
100 505 my @attributes = ((grep { ref $_ eq 'HASH' } $attrs, $extra_attrs), { @in_dsn });
101 505 my %dbi = map { %$_ } @attributes;
102  
103 # A libpq parameter among the attributes (karr k80): DBI takes any name
104 # for an attribute, and a pool connecting from the DSN alone would be
105 # connecting without it -- silently, where it was given for the connection.
106 # DBI's Password is both, and read below. Names only, never a value.
107 505 my %refused;
108 505 for my $given (@attributes) {
109 794 $refused{$_} = 1 for grep {
110 is_libpq_parameter($_) && $_ ne 'Password'
111 423 ▶ && defined $given->{$_} && length $given->{$_}
Condition100%9/9
is_libpq_parameter($_) && $_ ne 'Password' && defined $given->{$_} && length $given->{$_}
!ll&&!rl&&r
397224
is_libpq_parameter($_) and $_ ne "Password" and defined $given->{$_}
!ll&&!rl&&r
395226
is_libpq_parameter($_) and $_ ne "Password"
!ll&&!rl&&r
3425328
MC/DC100%4/4
is_libpq_parameter($_) && $_ ne 'Password' && defined $given->{$_} && length $given->{$_}
is_libpq_parameter($_) $_ ne "Password" defined $given->{$_} length $given->{$_}
Truth table100%5/5
A and B and C && D
A= is_libpq_parameter($_)B= $_ ne "Password"C= defined $given->{$_}D= length $given->{$_}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
112 } keys %$given;
113 }
114 505 ▶ die 'the connect info gives libpq connection parameter(s) among its DBI '
Branch: 3 branches
BranchTrueFalseTotal
if %refused 22 483 505
keys %refused > 1 ? : 2 20 22
keys %refused > 1 ? : 2 20 22
115 . 'attributes (' . join(', ', sort keys %refused) . '): DBI takes '
116 . (keys %refused > 1 ? 'them for attributes' : 'it for an attribute') . ' and the pool '
117 . 'would connect without ' . (keys %refused > 1 ? 'them' : 'it') . ". A libpq parameter belongs in "
118 . "the DSN, 'dbi:Pg:...;<parameter>=...'\n"
119 if %refused;
120  
121 # ... and "$user = $attr->{Username} if defined $attr->{Username}", the
122 # same for the password: what DBI hands DBD::Pg in place of its arguments.
123 483 ▶ $user = $dbi{Username} if defined $dbi{Username};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbi{"Username"} 34 449 483
124 483 ▶ $pass = $dbi{Password} if defined $dbi{Password};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbi{"Password"} 35 448 483
125  
126 # ... and then DBI::dr::default_user: DBI_USER and DBI_PASS for what is
127 # still undefined (karr k66), for static connect info only (option env):
128 # never for a broker's answer, whose values the broker decides (core k271).
129 # DBI_DSN is not read: core refuses a DSN that is not given
130 # (DBIO::Storage::DBI::_assert_static_connect_info_has_dsn).
131 483 ▶ if ($opt{env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"env"}) 185 298 483
132 185 ▶ $user = $ENV{DBI_USER} unless defined $user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user 15 170 185
133 185 ▶ $pass = $ENV{DBI_PASS} unless defined $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pass 19 166 185
134 }
135  
136 483 my %h = %{ _dbd_pg_params($params, $user, $pass) };
137 # pool_size is an attribute of the connect info (karr k66): libpq refuses
138 # it as a connection parameter, so the dbh of the same connect info fails
139 # of it, and the pool must not connect from what the dbh cannot.
140 die "the dbi:Pg: DSN names pool_size, which is an attribute of the connect info "
141 . "and no DSN parameter: give it as { pool_size => N } or among the DBI "
142 . "attributes in front of the DSN\n"
143 468 ▶ if exists $h{pool_size};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h{"pool_size"} 8 460 468
144 460 my %opts;
145 # Of those that give a pool size, the later one wins.
146 460 for my $given (@attributes) {
147 720 ▶ $h{pool_size} = $given->{pool_size} if $given->{pool_size};
Branch: 1 branch
BranchTrueFalseTotal
if $given->{"pool_size"} 85 635 720
148 }
149 460 return [ \%h, \%opts ];
150 }
151  
152  
153 999 ▶sub dbi_dsn_parts {
Subroutine dbi_dsn_parts: called CC=4 SCAR=13.9
Pod documented
154 999 my ($dsn) = @_;
155 999 ▶ return unless defined $dsn && !ref $dsn
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dsn and not ref $dsn and $dsn =~ /^dbi:(\w*?)(?:\((.*?)\))?:/i 6 993 999
Condition83%5/6
defined $dsn and not ref $dsn and $dsn =~ /^dbi:(\w*?)(?:\((.*?)\))?:/i
!ll&&!rl&&r
24993
defined $dsn and not ref $dsn
!ll&&!rl&&r
20997
MC/DC66%2/3
defined $dsn and not ref $dsn and $dsn =~ /^dbi:(\w*?)(?:\((.*?)\))?:/i
defined $dsn not ref $dsn $dsn =~ /^dbi:(\w*?)(?:\((.*?)\))?:/i
Truth table75%3/4
A and not(B) and C
A= defined $dsnB= not ref $dsnC= $dsn =~ /^dbi:(\w*?)(?:\((.*?)\))?:/i
ABCresult
0XX0
10X0
1100
1111
156 && $dsn =~ /^dbi:(\w*?)(?:\((.*?)\))?:/i;
157 993 return ($1, $2, substr($dsn, $+[0]));
158 }
159  
160 # The libpq parameters DBD::Pg connects with, given what follows "dbi:Pg:" in
161 # a DSN and the user and password beside it. A port of what DBD::Pg does to
162 # the three and of how libpq reads the result, step by step and not a grammar
163 # of its own, so that it agrees with them where they are odd as well
164 # (DBD::Pg 3.20.2, libpq 17).
165 483 ▶sub _dbd_pg_params {
Subroutine _dbd_pg_params: called CC=20 SCAR=30.0
166 483 my ($conn_str, $user, $pass) = @_;
167  
168 # DBD::Pg::dr::connect (Pg.pm): "db" and "database" are synonyms for
169 # "dbname" -- the first one found, wherever it stands -- and a database name
170 # in double quotes is one in single quotes.
171 483 $conn_str =~ s/\b(?:db|database)\s*=/dbname=/;
172 483 $conn_str =~ s/dbname\s*=\s*"(.+?)"/dbname='$1'/;
173  
174 # dbd_db_login6 (dbdimp.c): every ';' outside single quotes becomes a space.
175 # A backslash takes the next character along, so \' opens and closes nothing.
176 483 my $in_quote = 0;
177 483 $conn_str =~ s{(\\.|'|;)}{
178 557 ▶ $in_quote = !$in_quote if $1 eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "'" 26 531 557
179 557 ▶ $1 eq ';' && !$in_quote ? ' ' : $1;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq ';' && !$in_quote ? : 531 26 557
Condition66%2/3
$1 eq ";" and not $in_quote
!ll&&!rl&&r
260531
MC/DC50%1/2
$1 eq ";" and not $in_quote
$1 eq ";" not $in_quote
Truth table66%2/3
A and not(B)
A= $1 eq ";"B= not $in_quote
ABresult
0X0
100
111
180 }gse;
181  
182 # A postgresql:// URI where the parameters stand (karr k66): libpq takes one
183 # whole as a conninfo, and DBD::Pg hands it through -- after the two rewrites
184 # above, which is why they come first here. Read here into parameters, and
185 # not handed on as a string, so that a user and a password beside it count
186 # as they do beside "keyword=value" parameters (the dbh fails of them: it
187 # appends them to the URI, which is then none), and so that the pool size
188 # and the AccessBroker merge find a hash. libpq tells a URI by its start, in
189 # lower case and with nothing before it.
190 483 ▶ if ($conn_str =~ m{\Apostgres(?:ql)?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn_str =~ m[\Apostgres(?:ql)?://]) 22 461 483
191 22 my %h = %{ _uri_params($conn_str) };
192 18 for my $given ([ user => $user ], [ password => $pass ]) {
193 36 my ($keyword, $value) = @$given;
194 36 ▶ _refuse_nul($keyword) if defined $value && $value =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $value =~ /\0/ 0 36 36
Condition66%2/3
defined $value and $value =~ /\0/
!ll&&!rl&&r
16200
MC/DC0%0/2
defined $value and $value =~ /\0/
defined $value $value =~ /\0/
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /\0/
ABresult
0X0
100
111
195 36 ▶ $h{$keyword} = $value if defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and length $value 20 16 36
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
16020
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
196 }
197 18 return \%h;
198 }
199  
200 # ... and a user and a password that are not empty are appended, quoted.
201 # Behind the DSN's parameters, which is why they win: libpq keeps the later
202 # of two values. As text, which is why a DSN ending in "keyword=" takes the
203 # appended user for that keyword's value.
204 461 for my $given ([ user => $user ], [ password => $pass ]) {
205 921 my ($keyword, $value) = @$given;
206 921 ▶ next unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 107 814 921
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
1052814
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
207 814 ▶ _refuse_nul($keyword) if $value =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\0/ 2 812 814
208 812 $conn_str .= " $keyword='" . ($value =~ s/(['\\])/\\$1/gr) . "'";
209 }
210  
211 # libpq's conninfo_parse (fe-connect.c): keyword = value, separated by
212 # whitespace; a value in single quotes or bare, a backslash escaping the
213 # next character in both. Whitespace is what isspace() says in the C
214 # locale, hence /a.
215 459 my %h;
216 459 $conn_str =~ /\G\s+/agc;
217 459 ▶ while ((pos($conn_str) // 0) < length $conn_str) {
Branch: 1 branch
BranchTrueFalseTotal
if ((pos $conn_str // 0) < length $conn_str) 1701 450 2151
Condition100%2/2
pos $conn_str // 0
l!l
1751400
MC/DC100%1/1
pos $conn_str // 0
pos $conn_str
Truth table100%2/2
A
A= pos $conn_str
Aresult
11
00
218 1701 ▶ $conn_str =~ /\G([^=\s]*)\s*=\s*/agc
Branch: 1 branch
BranchTrueFalseTotal
$conn_str =~ /\G([^=\s]*)\s*=\s*/acg or die "the dbi:Pg: DSN cannot be read as libpq parameters: missing \"=\" after a parameter name\n" 1694 7 1701
219 or die qq{the dbi:Pg: DSN cannot be read as libpq parameters: missing "=" after a parameter name\n};
220 1694 my $keyword = $1;
221 # Quoted when it starts with a quote, and then up to the closing one.
222 1694 ▶ $conn_str =~ /\G(?:'((?:[^'\\]|\\.)*)'|(?!')((?:[^\s\\]|\\.|\\\z)*))/agcs
Branch: 1 branch
BranchTrueFalseTotal
$conn_str =~ /\G(?:'((?:[^'\\]|\\.)*)'|(?!')((?:[^\s\\]|\\.|\\\z)*))/acgs or die "the dbi:Pg: DSN cannot be read as libpq parameters: unterminated quoted value\n" 1694 0 1694
223 or die "the dbi:Pg: DSN cannot be read as libpq parameters: unterminated quoted value\n";
224 1694 ▶ my $value = $1 // $2;
Condition100%3/3
$1 // $2
l!l&&r!l&&!r
797547350
MC/DC100%2/2
$1 // $2
$1 $2
Truth table100%3/3
A // B
A= $1B= $2
ABresult
1X1
011
000
225 1694 $value =~ s/\\(.?)/$1/gs;
226 1694 ▶ _refuse_nul($keyword) if $value =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\0/ 2 1692 1694
227 1692 $h{$keyword} = $value;
228 1692 $conn_str =~ /\G\s+/agc;
229 }
230 450 return \%h;
231 }
232  
233 # A postgresql:// or postgres:// URI as the libpq parameters it stands for,
234 # read the way libpq's conninfo_uri_parse does (libpq 17, fe-connect.c; held
235 # against DBD::Pg's connect to a live server in t/72):
236 #
237 # postgresql://[user[:password]@][host[:port][,host[:port]...]][/dbname][?keyword=value[&...]]
238 #
239 # Every part is percent-decoded ("%40" is "@"); a "+" is a plus. A part that
240 # is empty sets nothing. The credentials are looked for first, and up to the
241 # first "@" before a "/" -- so a "?" in front of the "@" belongs to the user
242 # or the password, as it does for libpq. The host may be an IPv6 address in
243 # brackets; several hosts make one "host" of comma-separated names and "port"
244 # a list of as many ports. In the query "ssl=true" is "sslmode=require", as
245 # libpq has it for JDBC's sake, and a keyword given twice has its later value.
246 # A keyword libpq does not know is not checked here, as it is not for
247 # keyword=value parameters: libpq refuses it when the connection is opened.
248 #
249 # Dies where libpq refuses to read the URI, saying why and nothing of the URI:
250 # libpq's own message quotes it whole, password included.
251 22 ▶sub _uri_params {
Subroutine _uri_params: called CC=22 SCAR=34.4
252 22 my ($uri) = @_;
253 22 ▶ my $fail = sub { die "the dbi:Pg: DSN cannot be read as a libpq URI: $_[0]\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
254 172 ▶ my $decode = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
255 172 my ($part) = @_;
256 172 $part =~ s{%([0-9A-Fa-f]{2})?}{
257 118 ▶ defined $1 ? chr hex $1 : $fail->('invalid percent-encoded token');
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 116 2 118
258 }gse;
259 170 ▶ $fail->('a percent-encoded NUL is forbidden') if $part =~ /\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /\0/ 0 170 170
260 # What was encoded is UTF-8: a character string where it is valid, as
261 # EV::Pg hands libpq the bytes of the string it is given.
262 170 utf8::decode($part);
263 170 return $part;
264 22 };
265  
266 22 my %h;
267 22 my $rest = $uri =~ s{\Apostgres(?:ql)?://}{}r;
268  
269 # The credentials, "user[:password]@", where an "@" comes before a "/".
270 22 ▶ if ($rest =~ m{\A([^@/]*)\@}s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ m[\A([^\@/]*)\@]s) 20 2 22
271 20 my $cred = $1;
272 20 $rest = substr $rest, $+[0];
273 20 my ($user, $password) = split /:/, $cred, 2;
274 20 ▶ $h{user} = $decode->($user) if defined $user && length $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user and length $user 20 0 20
Condition33%1/3
defined $user and length $user
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $user and length $user
defined $user length $user
Truth table33%1/3
A and B
A= defined $userB= length $user
ABresult
0X0
100
111
275 20 ▶ $h{password} = $decode->($password) if defined $password && length $password;
Branch: 1 branch
BranchTrueFalseTotal
if defined $password and length $password 20 0 20
Condition33%1/3
defined $password and length $password
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $password and length $password
defined $password length $password
Truth table33%1/3
A and B
A= defined $passwordB= length $password
ABresult
0X0
100
111
276 }
277  
278 # The hosts, each with its port.
279 22 my (@hosts, @ports);
280 22 while (1) {
281 22 my ($host, $port) = ('', '');
282 22 ▶ if ($rest =~ s{\A\[}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A\[//) { } 2 20 22
283 2 ▶ $rest =~ s{\A([^\]]*)\]}{}s
Branch: 1 branch
BranchTrueFalseTotal
$rest =~ s/\A([^\]]*)\]//s or &$fail("the IPv6 host address in brackets is not closed") 0 2 2
284 or $fail->('the IPv6 host address in brackets is not closed');
285 0 $host = $1;
286 0 ▶ length $host or $fail->('the IPv6 host address in brackets is empty');
Branch: 1 branch
BranchTrueFalseTotal
length $host or &$fail("the IPv6 host address in brackets is empty") 0 0 0
287 0 ▶ $rest =~ m{\A(?:[:/?,]|\z)}
Branch: 1 branch
BranchTrueFalseTotal
$rest =~ m[\A(?:[:/?,]|\z)] or &$fail("an IPv6 host address in brackets must be followed by \":\", \"/\" or \"?\"") 0 0 0
288 or $fail->('an IPv6 host address in brackets must be followed by ":", "/" or "?"');
289 }
290 else {
291 20 $rest =~ s{\A([^:/?,]*)}{};
292 20 $host = $1;
293 }
294 20 ▶ $port = $1 if $rest =~ s{\A:([^/?,]*)}{};
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ s[\A:([^/?,]*)][] 16 4 20
295 20 push @hosts, $decode->($host);
296 20 push @ports, $decode->($port);
297 20 ▶ last unless $rest =~ s{\A,}{};
Branch: 1 branch
BranchTrueFalseTotal
unless $rest =~ s/\A,// 20 0 20
298 }
299 20 my $hosts = join ',', @hosts;
300 20 my $ports = join ',', @ports;
301 20 ▶ $h{host} = $hosts if length $hosts;
Branch: 1 branch
BranchTrueFalseTotal
if length $hosts 20 0 20
302 20 ▶ $h{port} = $ports if $ports =~ /[^,]/;
Branch: 1 branch
BranchTrueFalseTotal
if $ports =~ /[^,]/ 16 4 20
303  
304 # The database, up to the query.
305 20 ▶ if ($rest =~ s{\A/([^?]*)}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s[\A/([^?]*)][]) 20 0 20
306 20 ▶ $h{dbname} = $decode->($1) if length $1;
Branch: 1 branch
BranchTrueFalseTotal
if length $1 20 0 20
307 }
308  
309 # The query.
310 20 ▶ if ($rest =~ s{\A\?}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\A\?//) 20 0 20
311 20 for my $pair (split /&/, $rest) {
312 36 ▶ next unless length $pair;
Branch: 1 branch
BranchTrueFalseTotal
unless length $pair 0 36 36
313 36 my ($keyword, $value) = split /=/, $pair, 2;
314 36 ▶ defined $value or $fail->('a query parameter without "="');
Branch: 1 branch
BranchTrueFalseTotal
defined $value or &$fail("a query parameter without \"=\"") 36 0 36
315 36 ($keyword, $value) = ($decode->($keyword), $decode->($value));
316 34 ▶ ($keyword, $value) = (sslmode => 'require') if $keyword eq 'ssl' && $value eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $keyword eq "ssl" and $value eq "true" 0 34 34
Condition33%1/3
$keyword eq "ssl" and $value eq "true"
!ll&&!rl&&r
3400
MC/DC0%0/2
$keyword eq "ssl" and $value eq "true"
$keyword eq "ssl" $value eq "true"
Truth table33%1/3
A and B
A= $keyword eq "ssl"B= $value eq "true"
ABresult
0X0
100
111
317 34 $h{$keyword} = $value;
318 }
319 }
320 18 return \%h;
321 }
322  
323 96 1;
324  
325 __END__
↑ Summary blib/lib/DBIO/PostgreSQL/EV/Storage.pm »