lib/OpenSearch/PPLQuery.pm

filter: Statementstmt 93.7% 226 / 241 Branchbran 65.7% 92 / 140 Conditioncond 65.0% 67 / 103 MC/DCmcdc 37.9% 22 / 58 Subroutinesub 100% 31 / 31 Podpod 0.0% 0 / 23 totaltotal 73.4% 438 / 596 CC 116 SCAR 30.1
CC
116
Cov
80%
validate_options
45.0
format_error
31.6
execute
30.7
CRAP 20.4
« lib/OpenSearch/PPLQuery/Connection.pm ↑ Summary script/pplquery »
1 package OpenSearch::PPLQuery;
2  
3 # HTTP client for the OpenSearch PPL plugin, and the renderers used by the
4 # bin/pplquery command. User documentation lives in PPLQuery.pod at the
5 # distribution root and is injected below at build time; this module's own
6 # interface is described in comments, because it exists to serve the command
7 # and is not intended as a general-purpose OpenSearch library.
8 #
9 # Interface, for maintainers:
10 #
11 # OpenSearch::PPLQuery->new(%options)
12 # url required base URL; validated by validate_options
13 # user Basic-auth username; requires password and an https url
14 # password Basic-auth password; requires user
15 # ca_file CA certificate path; https only, excludes insecure
16 # insecure disable certificate/hostname verification; https only
17 # timeout seconds, defaults to 60, must be positive
18 #
19 # $client->execute($query)
20 # Returns ($document, $error_status). $error_status is undef on success
21 # and the HTTP status otherwise, so a caller can render the cluster's own
22 # error document instead of discarding it. Transport failures and
23 # undecodable responses throw.
24 #
25 # render_json / render_table / render_csv ($document, %options)
26 # Plain functions, not methods. render_table takes max_width.
27 #
28 # Every string crossing this boundary is character data, never octets.
29 # assert_unicode_text enforces that at new(), the one edge where a caller can
30 # hand over octets, so an encoding fault is reported where it enters rather
31 # than as mojibake in the output. A decoded response needs no such check: the
32 # response decoder is configured with utf8(1) and can only produce characters.
33  
34 28 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 28 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 28 ▶use Encode qw(decode encode FB_CROAK LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 28 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 28 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 28 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 28 ▶use Unicode::UCD qw(charprop);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 28 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 28 our $VERSION = '1.1.0';
45  
46 28 my $MAX_TIMEOUT = 86_400;
47  
48 28 my $JSON_TEXT = Cpanel::JSON::XS->new->utf8(0)->canonical(1);
49 28 my $JSON_PRETTY = Cpanel::JSON::XS->new->utf8(0)->canonical(1)->pretty(1);
50 28 my $JSON_LWP = Cpanel::JSON::XS->new->utf8(1)->canonical(1);
51 28 my %NUMERIC_TYPE = map { $_ => 1 } qw(int integer long bigint short smallint byte float double);
52 28 my %CHARACTER_WIDTH;
53  
54 # A connection field can be set from several places. Rather than track which
55 # one supplied a rejected value, an error names them all and lets the reader
56 # check the ones they use. Wording is user-facing on purpose: this module
57 # serves bin/pplquery, so its messages name what the user typed.
58 28 my $URL_SOURCES = "Set it with --url, a 'url' header field, or PPLQUERY_URL.\n";
59 28 my $CA_SOURCES = "--ca-file, a 'ca-file' header field, or PPLQUERY_CA_FILE";
60 28 my $VERIFY_OFF_SOURCES = "--insecure, a 'tls-verify: false' header field, or PPLQUERY_TLS_VERIFY=false";
61 28 my $PASSWORD_SOURCES = "--password-file, a 'password-environment' or 'password-file' header field, PPLQUERY_PASSWORD, or PPLQUERY_PASSWORD_FILE";
62  
63 74 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
64 74 my ($class, %options) = @_;
65 74 ▶ $options{timeout} = 60 if !defined $options{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"timeout"} 2 72 74
66 74 validate_options(\%options);
67 69 return bless \%options, $class;
68 }
69  
70 16 ▶sub execute {
Subroutine execute: called CC=15 SCAR=34.2
Pod undocumented
71 16 my ($self, $query) = @_;
72 16 ▶ die "Query is empty\n" if !defined($query) || $query !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query and $query =~ /\S/u 0 16 16
Condition33%1/3
defined $query and $query =~ /\S/u
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $query and $query =~ /\S/u
defined $query $query =~ /\S/u
Truth table33%1/3
A and B
A= defined $queryB= $query =~ /\S/u
ABresult
0X0
100
111
73  
74 16 my $endpoint = URI->new($self->{url});
75 16 $endpoint->path('/_plugins/_ppl');
76 16 $endpoint->query_form(format => 'jdbc');
77  
78 16 my $request = HTTP::Request->new(POST => $endpoint);
79 16 $request->header('Accept' => 'application/json');
80 16 $request->header('Content-Type' => 'application/json; charset=utf-8');
81 16 ▶ $request->authorization_basic($self->{user}, $self->{password}) if defined $self->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"user"} 0 16 16
82  
83 16 $request->content($JSON_LWP->encode({query => $query}));
84  
85 16 my $ua = LWP::UserAgent->new(agent => "OpenSearch-PPLQuery/$VERSION", timeout => $self->{timeout});
86 16 $ua->requests_redirectable([]);
87 16 ▶ if ($endpoint->scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoint->scheme eq "https") 0 16 16
88 0 ▶ if ($self->{insecure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'insecure'}) { } 0 0 0
89 0 $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
90 } else {
91 0 $ua->ssl_opts(verify_hostname => 1);
92 0 ▶ $ua->ssl_opts(SSL_ca_file => encode('UTF-8', $self->{ca_file}, FB_CROAK | LEAVE_SRC)) if defined $self->{ca_file};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ca_file"} 0 0 0
93 }
94 }
95  
96 16 my $response;
97 {
98 16 local $@;
99 16 $response = eval { $ua->request($request) };
100 16 ▶ die "HTTP request failed: " . exception_text($@) if !defined $response;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response 0 16 16
101 }
102  
103 # LWP reports a transport failure as a synthetic 500 response rather than
104 # by throwing, so this has to be checked before the status is believed.
105 # Reporting it as a status from OpenSearch would blame the cluster for a
106 # connection that was never established.
107 16 ▶ die transport_error($response, $self->{url}) if ($response->header('Client-Warning') // '') eq 'Internal response';
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header("Client-Warning") // "") eq "Internal response" 1 15 16
Condition100%2/2
$response->header("Client-Warning") // ""
l!l
115
MC/DC100%1/1
$response->header("Client-Warning") // ""
$response->header("Client-Warning")
Truth table100%2/2
A
A= $response->header("Client-Warning")
Aresult
11
00
108  
109 15 my $response_octets = $response->content;
110 15 my $document;
111 {
112 15 local $@;
113 15 $document = eval { $JSON_LWP->decode($response_octets) };
114 15 ▶ if ($@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "") 0 15 15
115 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
116 0 my $response_text = decode_utf8($response_octets, 'OpenSearch response');
117 0 $response_text =~ s/\s+\z//;
118 0 ▶ my $detail = $response_text eq '' ? 'an empty body' : terminal_text($response_text);
Branch: 1 branch
BranchTrueFalseTotal
$response_text eq '' ? : 0 0 0
119 0 ▶ $detail = substr($detail, 0, 300) . "\x{2026}" if length($detail) > 300;
Branch: 1 branch
BranchTrueFalseTotal
if length $detail > 300 0 0 0
120 0 die 'HTTP ' . $response->code . " from $self->{url}, with a body that is not JSON: $detail\n"
121 . "A proxy, load balancer, or sign-in page may have answered instead of OpenSearch.\n";
122 }
123 0 die "The response from $self->{url} is not valid JSON: " . exception_text($@)
124 . "A proxy, load balancer, or sign-in page may have answered instead of OpenSearch.\n";
125 }
126 }
127  
128 15 ▶ return ($document, $response->is_success ? undef : $response->code);
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 13 2 15
129 }
130  
131 5 ▶sub render_json {
Subroutine render_json: called CC=1 SCAR=0.0
Pod undocumented
132 5 my ($document) = @_;
133 5 return $JSON_PRETTY->encode($document);
134 }
135  
136 9 ▶sub render_table {
Subroutine render_table: called CC=12 SCAR=24.9
Pod undocumented
137 9 my ($document, %options) = @_;
138 9 my $names = validate_result_shape($document);
139 9 ▶ my $max_width = $options{max_width} // 0;
Condition100%2/2
$options{"max_width"} // 0
l!l
81
MC/DC100%1/1
$options{"max_width"} // 0
$options{"max_width"}
Truth table100%2/2
A
A= $options{"max_width"}
Aresult
11
00
140  
141 9 my @headers = map { measured_cell($_, $max_width) } @$names;
142 9 my @rows;
143 9 for my $row (@{$document->{datarows}}) {
144 17 push @rows, [map { measured_cell($_, $max_width) } @$row];
145 }
146 9 ▶ return scalar(@rows) . (@rows == 1 ? " row\n" : " rows\n") if !@headers;
Branch: 2 branches
BranchTrueFalseTotal
unless @headers 0 9 9
@rows == 1 ? : 0 0 0
147  
148 9 my @widths = map { $_->[1] } @headers;
149 9 for my $row (@rows) {
150 17 for my $index (0 .. $#headers) {
151 34 ▶ $widths[$index] = $row->[$index][1] if $row->[$index][1] > $widths[$index];
Branch: 1 branch
BranchTrueFalseTotal
if $row->[$index][1] > $widths[$index] 11 23 34
152 }
153 }
154  
155 9 ▶ my @aligns = map { $NUMERIC_TYPE{$_->{type} // ''} ? 1 : 0 } @{$document->{schema}};
Branch: 1 branch
BranchTrueFalseTotal
$NUMERIC_TYPE{$_->{'type'} // ''} ? : 2 16 18
Condition50%1/2
$_->{"type"} // ""
l!l
180
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
Truth table50%1/2
A
A= $_->{"type"}
Aresult
11
00
156 9 my $separator = '+' . join('+', map { '-' x ($_ + 2) } @widths) . '+';
157 9 $separator .= "\n";
158 9 my $output = $separator;
159 9 $output .= render_table_row(\@headers, \@widths, \@aligns);
160 9 $output .= $separator;
161 9 for my $index (0 .. $#rows) {
162 17 $output .= render_table_row($rows[$index], \@widths, \@aligns);
163 }
164 9 $output .= $separator;
165 9 ▶ $output .= scalar(@rows) . (@rows == 1 ? " row\n" : " rows\n");
Branch: 1 branch
BranchTrueFalseTotal
@rows == 1 ? : 7 2 9
166 9 return $output;
167 }
168  
169 2 ▶sub render_csv {
Subroutine render_csv: called CC=1 SCAR=0.0
Pod undocumented
170 2 my ($document) = @_;
171 2 my $names = validate_result_shape($document);
172 2 return csv_row($names) . join('', map { csv_row($_) } @{$document->{datarows}});
173 }
174  
175 # The cluster's own 'reason' is the sentence a user can act on, so it leads.
176 # The whole document stays reachable through --format json.
177 1 ▶sub format_error {
Subroutine format_error: called CC=10 SCAR=34.5
Pod undocumented
178 1 my ($status, $document) = @_;
179 1 ▶ my $error = ref($document) eq 'HASH' ? $document->{error} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $document eq 'HASH' ? : 1 0 1
180 1 ▶ if (ref($error) eq 'HASH' && defined($error->{reason}) && !ref($error->{reason})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error eq "HASH" and defined $error->{"reason"} and not ref $error->{"reason"}) 1 0 1
Condition33%2/6
ref $error eq "HASH" and defined $error->{"reason"} and not ref $error->{"reason"}
!ll&&!rl&&r
001
ref $error eq "HASH" and defined $error->{"reason"}
!ll&&!rl&&r
001
MC/DC0%0/3
ref $error eq "HASH" and defined $error->{"reason"} and not ref $error->{"reason"}
ref $error eq "HASH" defined $error->{"reason"} not ref $error->{"reason"}
Truth table25%1/4
A and B and not(C)
A= ref $error eq "HASH"B= defined $error->{"reason"}C= not ref $error->{"reason"}
ABCresult
0XX0
10X0
1100
1111
181 1 my $detail = terminal_text("$error->{reason}");
182 1 ▶ $detail .= ' (' . terminal_text("$error->{type}") . ')' if defined($error->{type}) && !ref($error->{type});
Branch: 1 branch
BranchTrueFalseTotal
if defined $error->{"type"} and not ref $error->{"type"} 1 0 1
Condition33%1/3
defined $error->{"type"} and not ref $error->{"type"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $error->{"type"} and not ref $error->{"type"}
defined $error->{"type"} not ref $error->{"type"}
Truth table33%1/3
A and not(B)
A= defined $error->{"type"}B= not ref $error->{"type"}
ABresult
0X0
100
111
183 1 return "OpenSearch returned HTTP $status: $detail\n"
184 . "Run the same query with --format json to see the full error document.\n";
185 }
186 my $detail = ref($document) eq 'HASH' && exists $document->{error}
187 0 ▶ ? (ref($error) ? $JSON_TEXT->encode($error) : terminal_text("$error"))
Branch: 2 branches
BranchTrueFalseTotal
ref $document eq 'HASH' && exists $document->{'error'} ? : 0 0 0
ref $error ? : 0 0 0
Condition0%0/3
ref $document eq "HASH" and exists $document->{"error"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $document eq "HASH" and exists $document->{"error"}
ref $document eq "HASH" exists $document->{"error"}
Truth table0%0/3
A and B
A= ref $document eq "HASH"B= exists $document->{"error"}
ABresult
0X0
100
111
188 : $JSON_TEXT->encode($document);
189 0 return "OpenSearch returned HTTP $status: $detail\n";
190 }
191  
192 142 ▶sub validate_options {
Subroutine validate_options: called CC=37 SCAR=38.1
Pod undocumented
193 142 my ($options) = @_;
194 142 for my $name (qw(url user password ca_file)) {
195 568 ▶ assert_unicode_text($options->{$name}, "option $name") if defined $options->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{$name} 267 301 568
196 }
197 142 ▶ die "OpenSearch URL is required\n" if !defined $options->{url};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"url"} 0 142 142
198 142 validate_timeout($options->{timeout}, 'timeout');
199 142 ▶ if (defined($options->{user}) != defined($options->{password})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"user"} != defined $options->{"password"}) 0 142 142
200 die defined($options->{user})
201 0 ▶ ? "Basic-auth username and password must be set together, but only a username was supplied. Add a password with $PASSWORD_SOURCES.\n"
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'user'} ? : 0 0 0
202 : "Basic-auth username and password must be set together, but only a password was supplied. Add a username with --user, a 'user' header field, or PPLQUERY_USER.\n";
203 }
204 142 require_ascii($options->{url}, 'OpenSearch URL');
205 142 ▶ require_ascii($options->{user}, 'Basic-auth username') if defined $options->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"user"} 50 92 142
206 141 ▶ require_ascii($options->{password}, 'Basic-auth password') if defined $options->{password};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"password"} 49 92 141
207 die 'Basic-auth username must not contain a colon or control character: ' . terminal_text($options->{user}) . "\n"
208 141 ▶ if defined($options->{user}) && $options->{user} =~ /[:\x00-\x1f\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"user"} and $options->{"user"} =~ /[:\x00-\x1f\x7f]/u 1 140 141
Condition100%3/3
defined $options->{"user"} and $options->{"user"} =~ /[:\x00-\x1f\x7f]/u
!ll&&!rl&&r
92481
MC/DC100%2/2
defined $options->{"user"} and $options->{"user"} =~ /[:\x00-\x1f\x7f]/u
defined $options->{"user"} $options->{"user"} =~ /[:\x00-\x1f\x7f]/u
Truth table100%3/3
A and B
A= defined $options->{"user"}B= $options->{"user"} =~ /[:\x00-\x1f\x7f]/u
ABresult
0X0
100
111
209  
210 140 ▶ die url_error($options->{url}, 'must not contain whitespace or URI wrappers') if $options->{url} =~ /[\x00-\x20<>"\\{}|^`]/;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"url"} =~ /[\x00-\x20<>"\\{}|^`]/u 2 138 140
211 138 my $uri = URI->new($options->{url});
212 138 ▶ die url_error($options->{url}, 'must use http or https') if !defined($uri->scheme) || ($uri->scheme ne 'http' && $uri->scheme ne 'https');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $uri->scheme or $uri->scheme ne "http" and $uri->scheme ne "https" 1 137 138
Condition66%4/6
not defined $uri->scheme or $uri->scheme ne "http" and $uri->scheme ne "https"
l!l&&r!l&&!r
10137
$uri->scheme ne "http" and $uri->scheme ne "https"
!ll&&!rl&&r
251120
MC/DC33%1/3
not defined $uri->scheme or $uri->scheme ne "http" and $uri->scheme ne "https"
not defined $uri->scheme $uri->scheme ne "http" $uri->scheme ne "https"
Truth table75%3/4
not(A) or B and C
A= not defined $uri->schemeB= $uri->scheme ne "http"C= $uri->scheme ne "https"
ABCresult
1XX1
0111
00X0
0100
213 137 ▶ die url_error($options->{url}, 'must not include credentials, a query, or a fragment') if defined($uri->userinfo) || defined($uri->query) || defined($uri->fragment);
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->userinfo or defined $uri->query or defined $uri->fragment 0 137 137
Condition33%2/6
defined $uri->userinfo or defined $uri->query or defined $uri->fragment
l!l&&r!l&&!r
00137
defined $uri->userinfo or defined $uri->query
l!l&&r!l&&!r
00137
MC/DC0%0/3
defined $uri->userinfo or defined $uri->query or defined $uri->fragment
defined $uri->userinfo defined $uri->query defined $uri->fragment
Truth table25%1/4
A or B or C
A= defined $uri->userinfoB= defined $uri->queryC= defined $uri->fragment
ABCresult
1XX1
01X1
0011
0000
214 137 my $authority = $uri->authority;
215 137 ▶ die url_error($options->{url}, 'must contain a valid host and optional numeric port')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $authority and $authority =~ /\A(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9][A-Za-z0-9.-]*)(?::[0-9]+)?\z/u 1 136 137
Condition66%2/3
defined $authority and $authority =~ /\A(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9][A-Za-z0-9.-]*)(?::[0-9]+)?\z/u
!ll&&!rl&&r
01136
MC/DC50%1/2
defined $authority and $authority =~ /\A(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9][A-Za-z0-9.-]*)(?::[0-9]+)?\z/u
defined $authority $authority =~ /\A(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9][A-Za-z0-9.-]*)(?::[0-9]+)?\z/u
Truth table66%2/3
A and B
A= defined $authorityB= $authority =~ /\A(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9][A-Za-z0-9.-]*)(?::[0-9]+)?\z/u
ABresult
0X0
100
111
216 if !defined($authority) || $authority !~ /\A(?:\[[0-9A-Fa-f:.]+\]|[A-Za-z0-9][A-Za-z0-9.-]*)(?::[0-9]+)?\z/;
217 136 ▶ die url_error($options->{url}, 'port must not exceed 65535') if $authority =~ /:([0-9]+)\z/ && $1 > 65_535;
Branch: 1 branch
BranchTrueFalseTotal
if $authority =~ /:([0-9]+)\z/u and $1 > 65535 0 136 136
Condition66%2/3
$authority =~ /:([0-9]+)\z/u and $1 > 65535
!ll&&!rl&&r
111250
MC/DC0%0/2
$authority =~ /:([0-9]+)\z/u and $1 > 65535
$authority =~ /:([0-9]+)\z/u $1 > 65535
Truth table66%2/3
A and B
A= $authority =~ /:([0-9]+)\z/uB= $1 > 65535
ABresult
0X0
100
111
218 136 ▶ die url_error($options->{url}, 'must be a base URL without a path') if $uri->path ne '' && $uri->path ne '/';
Branch: 1 branch
BranchTrueFalseTotal
if $uri->path ne "" and $uri->path ne "/" 1 135 136
Condition66%2/3
$uri->path ne "" and $uri->path ne "/"
!ll&&!rl&&r
13501
MC/DC50%1/2
$uri->path ne "" and $uri->path ne "/"
$uri->path ne "" $uri->path ne "/"
Truth table66%2/3
A and B
A= $uri->path ne ""B= $uri->path ne "/"
ABresult
0X0
100
111
219 die 'Basic authentication requires an https URL, but the URL is ' . terminal_text($options->{url}) . "\n$URL_SOURCES"
220 135 ▶ if defined($options->{user}) && $uri->scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"user"} and $uri->scheme ne "https" 2 133 135
Condition100%3/3
defined $options->{"user"} and $uri->scheme ne "https"
!ll&&!rl&&r
87462
MC/DC100%2/2
defined $options->{"user"} and $uri->scheme ne "https"
defined $options->{"user"} $uri->scheme ne "https"
Truth table100%3/3
A and B
A= defined $options->{"user"}B= $uri->scheme ne "https"
ABresult
0X0
100
111
221 die 'A CA file requires an https URL, but the URL is ' . terminal_text($options->{url}) . "\nThe CA file was set with $CA_SOURCES.\n"
222 133 ▶ if defined($options->{ca_file}) && $uri->scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"ca_file"} and $uri->scheme ne "https" 2 131 133
Condition100%3/3
defined $options->{"ca_file"} and $uri->scheme ne "https"
!ll&&!rl&&r
108232
MC/DC100%2/2
defined $options->{"ca_file"} and $uri->scheme ne "https"
defined $options->{"ca_file"} $uri->scheme ne "https"
Truth table100%3/3
A and B
A= defined $options->{"ca_file"}B= $uri->scheme ne "https"
ABresult
0X0
100
111
223 die 'Disabling TLS verification requires an https URL, but the URL is ' . terminal_text($options->{url}) . "\nVerification was disabled with $VERIFY_OFF_SOURCES.\n"
224 131 ▶ if $options->{insecure} && $uri->scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"insecure"} and $uri->scheme ne "https" 0 131 131
Condition66%2/3
$options->{"insecure"} and $uri->scheme ne "https"
!ll&&!rl&&r
116150
MC/DC0%0/2
$options->{"insecure"} and $uri->scheme ne "https"
$options->{"insecure"} $uri->scheme ne "https"
Truth table66%2/3
A and B
A= $options->{"insecure"}B= $uri->scheme ne "https"
ABresult
0X0
100
111
225 die "A CA file and disabled TLS verification cannot be used together; a CA file already restricts which certificates are accepted.\n"
226 . "Use either a CA file ($CA_SOURCES) or disabled verification ($VERIFY_OFF_SOURCES), not both.\n"
227 131 ▶ if defined($options->{ca_file}) && $options->{insecure};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"ca_file"} and $options->{"insecure"} 1 130 131
Condition100%3/3
defined $options->{"ca_file"} and $options->{"insecure"}
!ll&&!rl&&r
108221
MC/DC100%2/2
defined $options->{"ca_file"} and $options->{"insecure"}
defined $options->{"ca_file"} $options->{"insecure"}
Truth table100%3/3
A and B
A= defined $options->{"ca_file"}B= $options->{"insecure"}
ABresult
0X0
100
111
228 die 'insecure must be 0 or 1, not ' . terminal_text(ref($options->{insecure}) ? ref($options->{insecure}) . ' reference' : "'$options->{insecure}'") . "\n"
229 130 ▶ if defined($options->{insecure}) && (ref($options->{insecure}) || ($options->{insecure} ne '0' && $options->{insecure} ne '1'));
Branch: 2 branches
BranchTrueFalseTotal
if defined $options->{"insecure"} and ref $options->{"insecure"} || $options->{"insecure"} ne 0 && $options->{"insecure"} ne 1 1 129 130
ref $options->{'insecure'} ? : 0 1 1
Condition88%8/9
defined $options->{"insecure"} and ref $options->{"insecure"} || $options->{"insecure"} ne 0 && $options->{"insecure"} ne 1
!ll&&!rl&&r
107221
ref $options->{"insecure"} or $options->{"insecure"} ne 0 and $options->{"insecure"} ne 1
l!l&&r!l&&!r
0122
$options->{"insecure"} ne 0 and $options->{"insecure"} ne 1
!ll&&!rl&&r
9131
MC/DC75%3/4
defined $options->{"insecure"} and ref $options->{"insecure"} || $options->{"insecure"} ne 0 && $options->{"insecure"} ne 1
defined $options->{"insecure"} ref $options->{"insecure"} $options->{"insecure"} ne 0 $options->{"insecure"} ne 1
Truth table80%4/5
A and B or C and D
A= defined $options->{"insecure"}B= ref $options->{"insecure"}C= $options->{"insecure"} ne 0D= $options->{"insecure"} ne 1
ABCDresult
0XXX0
100X0
10100
11XX1
10111
230 129 ▶ $options->{insecure} = 0 + $options->{insecure} if defined $options->{insecure};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"insecure"} 22 107 129
231 129 $options->{url} = $uri->as_string;
232 }
233  
234 175 ▶sub validate_timeout {
Subroutine validate_timeout: called CC=7 SCAR=20.2
Pod undocumented
235 175 my ($timeout, $label) = @_;
236 175 ▶ $label //= 'timeout';
Condition50%1/2
$label //= "timeout"
l!l
1750
MC/DC0%0/1
$label //= "timeout"
$label
Truth table50%1/2
A
A= $label
Aresult
11
00
237 # The digit bound keeps the value small enough to compare numerically, so
238 # an arbitrarily long digit string never reaches the comparison.
239 175 ▶ die "$label must be a positive integer no greater than $MAX_TIMEOUT, not "
Branch: 3 branches
BranchTrueFalseTotal
if not defined $timeout && "$timeout" =~ /\A[1-9][0-9]{0,4}\z/u or $timeout > $MAX_TIMEOUT 17 158 175
defined $timeout ? : 17 0 17
ref $timeout ? : 0 17 17
Condition83%5/6
not defined $timeout && "$timeout" =~ /\A[1-9][0-9]{0,4}\z/u or $timeout > $MAX_TIMEOUT
l!l&&r!l&&!r
161158
defined $timeout && "$timeout" =~ /\A[1-9][0-9]{0,4}\z/u
!ll&&!rl&&r
016159
MC/DC66%2/3
not defined $timeout && "$timeout" =~ /\A[1-9][0-9]{0,4}\z/u or $timeout > $MAX_TIMEOUT
defined $timeout "$timeout" =~ /\A[1-9][0-9]{0,4}\z/u $timeout > $MAX_TIMEOUT
Truth table75%3/4
not(A && B) or C
A= defined $timeoutB= "$timeout" =~ /\A[1-9][0-9]{0,4}\z/uC= $timeout > $MAX_TIMEOUT
ABCresult
0XX1
10X1
1111
1100
240 . (defined($timeout) ? "'" . terminal_text(ref($timeout) ? ref($timeout) . ' reference' : "$timeout") . "'" : 'undefined') . "\n"
241 if !defined($timeout) || "$timeout" !~ /\A[1-9][0-9]{0,4}\z/ || $timeout > $MAX_TIMEOUT;
242 158 return 0 + $timeout;
243 }
244  
245 11 ▶sub validate_result_shape {
Subroutine validate_result_shape: called CC=11 SCAR=26.6
Pod undocumented
246 11 my ($document) = @_;
247 11 my $inspect = "Run the same query with --format json to see the document OpenSearch returned.\n";
248 11 ▶ die "OpenSearch response is not a JSON object\n$inspect" if ref($document) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $document ne "HASH" 0 11 11
249 11 ▶ die "OpenSearch response has no schema array\n$inspect" if ref($document->{schema}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $document->{"schema"} ne "ARRAY" 0 11 11
250 11 ▶ die "OpenSearch response has no datarows array\n$inspect" if ref($document->{datarows}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $document->{"datarows"} ne "ARRAY" 0 11 11
251 11 my @names;
252 11 for my $column (@{$document->{schema}}) {
253 22 ▶ die "OpenSearch schema entry is not an object with a name\n" if ref($column) ne 'HASH' || !defined($column->{name}) || ref($column->{name});
Branch: 1 branch
BranchTrueFalseTotal
if ref $column ne "HASH" or not defined $column->{"name"} or ref $column->{"name"} 0 22 22
Condition33%2/6
ref $column ne "HASH" or not defined $column->{"name"} or ref $column->{"name"}
l!l&&r!l&&!r
0022
ref $column ne "HASH" or not defined $column->{"name"}
l!l&&r!l&&!r
0022
MC/DC0%0/3
ref $column ne "HASH" or not defined $column->{"name"} or ref $column->{"name"}
ref $column ne "HASH" not defined $column->{"name"} ref $column->{"name"}
Truth table25%1/4
A or not(B) or C
A= ref $column ne "HASH"B= not defined $column->{"name"}C= ref $column->{"name"}
ABCresult
1XX1
01X1
0011
0000
254 22 push @names, $column->{name};
255 }
256 11 for my $row (@{$document->{datarows}}) {
257 19 ▶ die "OpenSearch data row is not an array\n" if ref($row) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $row ne "ARRAY" 0 19 19
258 19 ▶ die 'OpenSearch data row has ' . scalar(@$row) . ' values for ' . scalar(@names) . " columns\n" if @$row != @names;
Branch: 1 branch
BranchTrueFalseTotal
if @$row != @names 0 19 19
259 }
260 11 return \@names;
261 }
262  
263 5 ▶sub url_error {
Subroutine url_error: called CC=1 SCAR=0.0
Pod undocumented
264 5 my ($url, $problem) = @_;
265 5 return "OpenSearch URL $problem: " . terminal_text($url) . "\n$URL_SOURCES";
266 }
267  
268 # The real cause is in the synthetic response's status message; its body
269 # repeats that and appends the Perl file and line, which tells a user nothing.
270 1 ▶sub transport_error {
Subroutine transport_error: called CC=4 SCAR=15.2
Pod undocumented
271 1 my ($response, $url) = @_;
272 1 my $message = $response->message;
273 1 ▶ my $reason = defined($message) && $message =~ /\S/ ? terminal_text(exception_text($message)) : 'the connection failed';
Branch: 1 branch
BranchTrueFalseTotal
defined $message && $message =~ /\S/u ? : 1 0 1
Condition33%1/3
defined $message and $message =~ /\S/u
!ll&&!rl&&r
001
MC/DC0%0/2
defined $message and $message =~ /\S/u
defined $message $message =~ /\S/u
Truth table33%1/3
A and B
A= defined $messageB= $message =~ /\S/u
ABresult
0X0
100
111
274 1 ▶ my $hint = $reason =~ /certificate verify failed/i
Branch: 1 branch
BranchTrueFalseTotal
$reason =~ /certificate verify failed/iu ? : 0 1 1
275 ? "Supply the issuing CA certificate with $CA_SOURCES. Verification can be disabled with $VERIFY_OFF_SOURCES, at the cost of the check that detects an impersonated server.\n"
276 : '';
277 1 return "Cannot reach OpenSearch at $url: $reason\n$hint";
278 }
279  
280 26 ▶sub render_table_row {
Subroutine render_table_row: called CC=3 SCAR=11.0
Pod undocumented
281 26 my ($row, $widths, $aligns) = @_;
282 26 my @cells;
283 26 for my $index (0 .. $#$row) {
284 52 my ($text, $width) = @{$row->[$index]};
285 52 my $pad = ' ' x ($widths->[$index] - $width);
286 52 ▶ push @cells, $aligns->[$index] ? $pad . $text : $text . $pad;
Branch: 1 branch
BranchTrueFalseTotal
$aligns->[$index] ? : 12 40 52
287 }
288 26 return '| ' . join(' | ', @cells) . " |\n";
289 }
290  
291 # A cell paired with the number of terminal columns it occupies. The width is
292 # measured here and carried, so no cell is scanned more than once, and
293 # truncation cuts between grapheme clusters rather than inside one.
294 57 ▶sub measured_cell {
Subroutine measured_cell: called CC=5 SCAR=16.1
Pod undocumented
295 57 my ($value, $max_width) = @_;
296 57 my $text = display_scalar($value);
297 57 my $width = display_width($text);
298 57 ▶ return [$text, $width] if $max_width == 0 || $width <= $max_width;
Branch: 1 branch
BranchTrueFalseTotal
if $max_width == 0 or $width <= $max_width 46 11 57
Condition100%3/3
$max_width == 0 or $width <= $max_width
l!l&&r!l&&!r
341211
MC/DC100%2/2
$max_width == 0 or $width <= $max_width
$max_width == 0 $width <= $max_width
Truth table100%3/3
A or B
A= $max_width == 0B= $width <= $max_width
ABresult
1X1
011
000
299  
300 # Cutting on a grapheme boundary keeps combining marks with the character
301 # they belong to, so truncation never leaves a mark stranded on its own.
302 11 my ($kept, $used) = ('', 0);
303 11 ▶ while ($text =~ /(\X)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(\X)/gu) 36 0 36
304 36 my $cluster_width = display_width($1);
305 36 ▶ last if $used + $cluster_width > $max_width - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $used + $cluster_width > $max_width - 1 11 25 36
306 25 $kept .= $1;
307 25 $used += $cluster_width;
308 }
309 11 return [$kept . "\x{2026}", $used + 1];
310 }
311  
312 # Terminal columns, not characters, following the same rules as wcwidth(3),
313 # which is what the terminal itself uses to decide where a line wraps.
314 126 ▶sub display_width {
Subroutine display_width: called CC=2 SCAR=6.9
Pod undocumented
315 126 my ($text) = @_;
316 126 my $width = 0;
317 126 $width += character_width($_) for split //, $text;
318 126 return $width;
319 }
320  
321 # Nonspacing and enclosing marks stack on the preceding character and occupy
322 # nothing; spacing marks, as used for Indic vowel signs, do occupy a column.
323 # Format characters, including the zero-width joiner, occupy nothing. East
324 # Asian Ambiguous counts as one, as wcwidth does outside East Asian locales.
325 # charprop is slow, so each codepoint is asked about once and remembered; a
326 # table holds far fewer distinct characters than it holds cells.
327 743 ▶sub character_width {
Subroutine character_width: called CC=6 SCAR=18.0
Pod undocumented
328 743 my ($character) = @_;
329 743 ▶ return $CHARACTER_WIDTH{$character} //= do {
Condition66%2/3
$CHARACTER_WIDTH{$character} //= do { my $codepoint = ord $character; my $category = &charprop($codepoint, "General_Category"); $category eq "Nonspacing_Mark" || $category eq "Enclosing_Mark" || $category eq "Format" ? 0 : (&charprop($codepoint, "East_Asian_Width") =~ /\A(?:Wide|Fullwidth)\z/u ? 2 : 1) }
l!l&&r!l&&!r
5062370
MC/DC0%0/2
$CHARACTER_WIDTH{$character} //= do { my $codepoint = ord $character; my $category = &charprop($codepoint, "General_Category"); $category eq "Nonspacing_Mark" || $category eq "Enclosing_Mark" || $category eq "Format" ? 0 : (&charprop($codepoint, "East_Asian_Width") =~ /\A(?:Wide|Fullwidth)\z/u ? 2 : 1) }
$CHARACTER_WIDTH{$character} do { my $codepoint = ord $character; my $category = &charprop($codepoint, "General_Category"); $category eq "Nonspacing_Mark" || $category eq "Enclosing_Mark" || $category eq "Format" ? 0 : (&charprop($codepoint, "East_Asian_Width") =~ /\A(?:Wide|Fullwidth)\z/u ? 2 : 1) }
Truth table66%2/3
A //= B
A= $CHARACTER_WIDTH{$character}B= do { my $codepoint = ord $character; my $category = &charprop($codepoint, "General_Category"); $category eq "Nonspacing_Mark" || $category eq "Enclosing_Mark" || $category eq "Format" ? 0 : (&charprop($codepoint, "East_Asian_Width") =~ /\A(?:Wide|Fullwidth)\z/u ? 2 : 1) }
ABresult
1X1
011
000
330 237 my $codepoint = ord($character);
331 237 my $category = charprop($codepoint, 'General_Category');
332 237 ▶ $category eq 'Nonspacing_Mark' || $category eq 'Enclosing_Mark' || $category eq 'Format' ? 0
Branch: 2 branches
BranchTrueFalseTotal
$category eq 'Nonspacing_Mark' || $category eq 'Enclosing_Mark' || $category eq 'Format' ? : 7 230 237
&charprop($codepoint, 'East_Asian_Width') =~ /\A(?:Wide|Fullwidth)\z/u ? : 14 216 230
Condition83%5/6
$category eq "Nonspacing_Mark" or $category eq "Enclosing_Mark" or $category eq "Format"
l!l&&r!l&&!r
61230
$category eq "Nonspacing_Mark" or $category eq "Enclosing_Mark"
l!l&&r!l&&!r
60231
MC/DC66%2/3
$category eq "Nonspacing_Mark" or $category eq "Enclosing_Mark" or $category eq "Format"
$category eq "Nonspacing_Mark" $category eq "Enclosing_Mark" $category eq "Format"
Truth table75%3/4
A or B or C
A= $category eq "Nonspacing_Mark"B= $category eq "Enclosing_Mark"C= $category eq "Format"
ABCresult
1XX1
01X1
0011
0000
333 : charprop($codepoint, 'East_Asian_Width') =~ /\A(?:Wide|Fullwidth)\z/ ? 2
334 : 1;
335 };
336 }
337  
338 4 ▶sub csv_row { return join(',', map { csv_field($_) } @{$_[0]}) . "\n"; }
Subroutine csv_row: called CC=1 SCAR=0.0
Pod undocumented
339  
340 8 ▶sub csv_field {
Subroutine csv_field: called CC=4 SCAR=13.9
Pod undocumented
341 8 my ($value) = @_;
342 8 ▶ return '' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 7 8
343 7 ▶ my $text = ref($value) ? $JSON_TEXT->encode($value) : "$value";
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 7 7
344 7 ▶ return $text if $text !~ /["\r\n,]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /["\r\n,]/u 6 1 7
345 1 $text =~ s/"/""/g;
346 1 return qq{"$text"};
347 }
348  
349 57 ▶sub display_scalar {
Subroutine display_scalar: called CC=3 SCAR=11.1
Pod undocumented
350 57 my ($value) = @_;
351 57 ▶ return 'NULL' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 54 57
352 54 ▶ return terminal_text(ref($value) ? $JSON_TEXT->encode($value) : "$value");
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 54 54
353 }
354  
355 314 ▶sub terminal_text {
Subroutine terminal_text: called CC=1 SCAR=0.0
Pod undocumented
356 314 my ($text) = @_;
357 314 $text =~ s/\r/\\r/g;
358 314 $text =~ s/\n/\\n/g;
359 314 $text =~ s/\t/\\t/g;
360 0 $text =~ s/([\x{00}-\x{08}\x{0b}\x{0c}\x{0e}-\x{1f}\x{7f}])/sprintf('\\u%04x', ord($1))/ge;
361 314 return $text;
362 }
363  
364 411 ▶sub decode_utf8 {
Subroutine decode_utf8: called CC=3 SCAR=11.0
Pod undocumented
365 411 my ($octets, $label) = @_;
366 411 ▶ return $octets if utf8::is_utf8($octets);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($octets) 2 409 411
367 409 my $text;
368 {
369 409 local $@;
370 409 $text = eval { decode('UTF-8', $octets, FB_CROAK | LEAVE_SRC) };
371 409 ▶ die "$label is not valid UTF-8. It must be encoded as UTF-8.\n" if $@ ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 4 405 409
372 }
373 405 return $text;
374 }
375  
376 11 ▶sub exception_text {
Subroutine exception_text: called CC=3 SCAR=11.3
Pod undocumented
377 11 my ($exception) = @_;
378 11 ▶ $exception = "$exception" if ref($exception);
Branch: 1 branch
BranchTrueFalseTotal
if ref $exception 0 11 11
379 11 local $@;
380 11 ▶ return eval { decode_utf8($exception, 'exception') } // 'dependency returned a non-UTF-8 exception';
Condition50%1/2
eval { do { decode_utf8($exception, "exception") } } // "dependency returned a non-UTF-8 exception"
l!l
110
MC/DC0%0/1
eval { do { decode_utf8($exception, "exception") } } // "dependency returned a non-UTF-8 exception"
eval { do { decode_utf8($exception, "exception") } }
Truth table50%1/2
A
A= eval { do { decode_utf8($exception, "exception") } }
Aresult
11
00
381 }
382  
383 267 ▶sub assert_unicode_text {
Subroutine assert_unicode_text: called CC=4 SCAR=14.9
Pod undocumented
384 267 my ($text, $label) = @_;
385 267 ▶ die "$label contains a downgraded non-ASCII string\n" if defined($text) && !utf8::is_utf8($text) && $text =~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text and not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u 0 267 267
Condition66%4/6
defined $text and not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u
!ll&&!rl&&r
701970
defined $text and not utf8::is_utf8($text)
!ll&&!rl&&r
070197
MC/DC0%0/3
defined $text and not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u
defined $text not utf8::is_utf8($text) $text =~ /[^\x00-\x7f]/u
Truth table50%2/4
A and not(B) and C
A= defined $textB= not utf8::is_utf8($text)C= $text =~ /[^\x00-\x7f]/u
ABCresult
0XX0
10X0
1100
1111
386 }
387  
388 241 ▶sub require_ascii {
Subroutine require_ascii: called CC=2 SCAR=6.9
Pod undocumented
389 241 my ($text, $label) = @_;
390 241 ▶ die "$label must contain only ASCII characters\n" if $text =~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /[^\x00-\x7f]/u 1 240 241
391 }
392  
393 28 1;
394  
395 __END__
« lib/OpenSearch/PPLQuery/Connection.pm ↑ Summary script/pplquery »