Elasticsearch/Role/Cxn.pm

filter: Statementstmt 76.5% 196 / 256 Branchbran 50.9% 52 / 102 Conditioncond 63.5% 47 / 74 MC/DCmcdc 39.0% 16 / 41 Subroutinesub 91.8% 34 / 37 Podpod 69.2% 9 / 13 totaltotal 67.6% 354 / 523 CC 82 SCAR 33.2
CC
82
Cov
68%
_munge_elasticsearch_exception
60.1
BUILDARGS
42.2
process_response
33.9
CRAP 27.7
« Elasticsearch/Client/8_0/Role/Bulk.pm ↑ Summary Elasticsearch/TestServer.pm »
1 # Licensed to Elasticsearch B.V. under one or more contributor
2 # license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright
4 # ownership. Elasticsearch B.V. licenses this file to you under
5 # the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17  
18 package Search::Elasticsearch::Role::Cxn;
19 55 $Search::Elasticsearch::Role::Cxn::VERSION = '8.12';
20 55 our $PRODUCT_CHECK_HEADER = 'x-elastic-product';
21 55 our $PRODUCT_CHECK_VALUE = 'Elasticsearch';
22  
23 55 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 55 ▶use Search::Elasticsearch::Util qw(parse_params throw to_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 55 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 55 ▶use URI();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 55 ▶use IO::Compress::Deflate();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 55 ▶use IO::Uncompress::Inflate();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 55 ▶use IO::Compress::Gzip();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 55 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 55 ▶use Search::Elasticsearch::Util qw(to_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 55 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 55 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 55 requires qw(perform_request error_from_text handle);
37  
38 55 has 'client_version' => ( is => 'ro', required => 1, default => $Search::Elasticsearch::VERSION );
39 55 has 'host' => ( is => 'ro', required => 1 );
40 55 has 'port' => ( is => 'ro', required => 1 );
41 55 has 'uri' => ( is => 'ro', required => 1 );
42 55 has 'request_timeout' => ( is => 'ro', default => 30 );
43 55 has 'ping_timeout' => ( is => 'ro', default => 2 );
44 55 has 'sniff_timeout' => ( is => 'ro', default => 1 );
45 55 has 'sniff_request_timeout' => ( is => 'ro', default => 2 );
46 55 has 'next_ping' => ( is => 'rw', default => 0 );
47 55 has 'ping_failures' => ( is => 'rw', default => 0 );
48 55 has 'dead_timeout' => ( is => 'ro', default => 60 );
49 55 has 'max_dead_timeout' => ( is => 'ro', default => 3600 );
50 55 has 'serializer' => ( is => 'ro', required => 1 );
51 55 has 'logger' => ( is => 'ro', required => 1 );
52 175 ▶has 'handle_args' => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
53 172 ▶has 'default_qs_params' => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
54 55 has 'scheme' => ( is => 'ro' );
55 55 has 'is_https' => ( is => 'ro' );
56 55 has 'userinfo' => ( is => 'ro' );
57 55 has 'max_content_length' => ( is => 'ro' );
58 55 has 'default_headers' => ( is => 'ro' );
59 55 has 'deflate' => ( is => 'ro' );
60 55 has 'gzip' => ( is => 'ro' );
61 55 has 'ssl_options' => ( is => 'ro', predicate => 'has_ssl_options' );
62 55 has 'handle' => ( is => 'lazy', clearer => 1 );
63 55 has '_pid' => ( is => 'rw', default => $$ );
64  
65 55 my %Code_To_Error = (
66 400 => 'Request',
67 401 => 'Unauthorized',
68 403 => 'Forbidden',
69 404 => 'Missing',
70 408 => 'RequestTimeout',
71 409 => 'Conflict',
72 413 => 'ContentLength',
73 502 => 'BadGateway',
74 503 => 'Unavailable',
75 504 => 'GatewayTimeout'
76 );
77  
78 #===================================
79 611 ▶sub stringify { shift->uri . '' }
Subroutine stringify: called CC=1 SCAR=0.0
Pod undocumented
80 #===================================
81  
82 #===================================
83 176 ▶sub get_user_agent {
Subroutine get_user_agent: called CC=1 SCAR=0.0
Pod undocumented
84 #===================================
85 176 return sprintf("elasticsearch-perl/%s (%s; perl %s)", $Search::Elasticsearch::VERSION, $^O, $]);
86 }
87  
88 #===================================
89 176 ▶sub get_meta_header {
Subroutine get_meta_header: called CC=1 SCAR=0.0
Pod undocumented
90 #===================================
91 176 return sprintf("es=%s,pl=%s", $Search::Elasticsearch::VERSION, $^V);
92 }
93  
94  
95 #===================================
96 175 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=31 SCAR=37.4
Pod undocumented
97 #===================================
98 175 my ( $class, $params ) = parse_params(@_);
99  
100 my $node = $params->{node}
101 175 ▶ || { host => 'localhost', port => '9200' };
Condition50%1/2
$params->{'node'} || {'host', 'localhost', 'port', '9200'}
l!l
1750
MC/DC0%0/1
$params->{'node'} || {'host', 'localhost', 'port', '9200'}
$params->{'node'}
Truth table50%1/2
A
A= $params->{'node'}
Aresult
11
00
102  
103 175 ▶ unless ( ref $node eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node eq "HASH") 170 5 175
104 170 ▶ $node = "[$node]" if Net::IP::ip_is_ipv6($node);
Branch: 1 branch
BranchTrueFalseTotal
if Net::IP::ip_is_ipv6($node) 2 168 170
105 170 ▶ unless ( $node =~ m{^http(s)?://} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node =~ m[^http(s)?://]) 124 46 170
106 124 ▶ $node = ( $params->{use_https} ? 'https://' : 'http://' ) . $node;
Branch: 1 branch
BranchTrueFalseTotal
$params->{'use_https'} ? : 1 123 124
107 }
108 170 ▶ if ( $params->{port} && $node !~ m{//[^/\[]+:\d+} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"port"} and not $node =~ m(//[^/\[]+:\d+)) 3 167 170
Condition100%3/3
$params->{"port"} and not $node =~ m(//[^/\[]+:\d+)
!ll&&!rl&&r
16613
MC/DC100%2/2
$params->{"port"} and not $node =~ m(//[^/\[]+:\d+)
$params->{"port"} not $node =~ m(//[^/\[]+:\d+)
Truth table100%3/3
A and not(B)
A= $params->{"port"}B= not $node =~ m(//[^/\[]+:\d+)
ABresult
0X0
100
111
109 3 $node =~ s{(//[^/]+)}{$1:$params->{port}};
110 }
111 170 my $uri = URI->new($node);
112 170 $node = {
113 scheme => $uri->scheme,
114 host => $uri->host,
115 port => $uri->port,
116 path => $uri->path,
117 userinfo => $uri->userinfo
118 };
119 }
120  
121 175 ▶ my $host = $node->{host} || 'localhost';
Condition100%2/2
$node->{'host'} || 'localhost'
l!l
1732
MC/DC100%1/1
$node->{'host'} || 'localhost'
$node->{'host'}
Truth table100%2/2
A
A= $node->{'host'}
Aresult
11
00
122 175 ▶ my $userinfo = $node->{userinfo} || $params->{userinfo} || '';
Condition100%5/5
$node->{'userinfo'} || $params->{'userinfo'} || ''
l!l
3172
$node->{"userinfo"} or $params->{"userinfo"}
l!l&&r!l&&!r
21172
MC/DC100%2/2
$node->{'userinfo'} || $params->{'userinfo'} || ''
$node->{"userinfo"} $params->{"userinfo"}
Truth table100%3/3
A or B
A= $node->{"userinfo"}B= $params->{"userinfo"}
ABresult
1X1
011
000
123 my $scheme
124 175 ▶ = $node->{scheme} || ( $params->{use_https} ? 'https' : 'http' );
Branch: 1 branch
BranchTrueFalseTotal
$params->{'use_https'} ? : 0 5 5
Condition66%2/3
$node->{'scheme'} || ($params->{'use_https'} ? 'https' : 'http')
l!l&&r!l&&!r
17050
MC/DC0%0/2
$node->{'scheme'} || ($params->{'use_https'} ? 'https' : 'http')
$node->{'scheme'} ($params->{'use_https'} ? 'https' : 'http')
Truth table66%2/3
A || B
A= $node->{'scheme'}B= ($params->{'use_https'} ? 'https' : 'http')
ABresult
1X1
011
000
125 my $port
126 = $node->{port}
127 || $params->{port}
128 175 ▶ || ( $scheme eq 'http' ? 80 : 443 );
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'http' ? : 4 0 4
Condition66%4/6
$node->{'port'} || $params->{'port'} || ($scheme eq 'http' ? 80 : 443)
l!l&&r!l&&!r
17140
$node->{"port"} or $params->{"port"}
l!l&&r!l&&!r
17104
MC/DC0%0/3
$node->{'port'} || $params->{'port'} || ($scheme eq 'http' ? 80 : 443)
$node->{"port"} $params->{"port"} ($scheme eq 'http' ? 80 : 443)
Truth table50%2/4
A or B || C
A= $node->{"port"}B= $params->{"port"}C= ($scheme eq 'http' ? 80 : 443)
ABCresult
1XX1
01X1
0011
0000
129 175 ▶ my $path = $node->{path} || $params->{path_prefix} || '';
Condition100%5/5
$node->{'path'} || $params->{'path_prefix'} || ''
l!l
10165
$node->{"path"} or $params->{"path_prefix"}
l!l&&r!l&&!r
91165
MC/DC100%2/2
$node->{'path'} || $params->{'path_prefix'} || ''
$node->{"path"} $params->{"path_prefix"}
Truth table100%3/3
A or B
A= $node->{"path"}B= $params->{"path_prefix"}
ABresult
1X1
011
000
130 175 $path =~ s{^/?}{/}g;
131 175 $path =~ s{/+$}{};
132  
133 175 ▶ my %default_headers = %{ $params->{default_headers} || {} };
Condition50%1/2
$params->{'default_headers'} || {}
l!l
0175
MC/DC0%0/1
$params->{'default_headers'} || {}
$params->{'default_headers'}
Truth table50%1/2
A
A= $params->{'default_headers'}
Aresult
11
00
134  
135 175 ▶ if ($userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userinfo) 3 172 175
136 3 require MIME::Base64;
137 3 my $auth = MIME::Base64::encode_base64( $userinfo, "" );
138 3 chomp $auth;
139 3 $default_headers{Authorization} = "Basic $auth";
140 }
141  
142 175 ▶ if ( $params->{gzip} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'gzip'}) { } 0 175 175
elsif ($params->{'deflate'}) { } 1 174 175
143 0 $default_headers{'Accept-Encoding'} = "gzip";
144 }
145  
146 elsif ( $params->{deflate} ) {
147 1 $default_headers{'Accept-Encoding'} = "deflate";
148 }
149  
150 175 $default_headers{'User-Agent'} = $class->get_user_agent();
151  
152 # Add Elastic meta header
153 175 $default_headers{'x-elastic-client-meta'} = $class->get_meta_header();
154  
155 # Compatibility header
156 175 ▶ if (defined $ENV{ELASTIC_CLIENT_APIVERSIONING} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"ELASTIC_CLIENT_APIVERSIONING"} and lc $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq "true" || $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq 1) 0 175 175
Condition16%1/6
defined $ENV{"ELASTIC_CLIENT_APIVERSIONING"} and lc $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq "true" || $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq 1
!ll&&!rl&&r
17500
lc $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq "true" or $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq 1
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ENV{"ELASTIC_CLIENT_APIVERSIONING"} and lc $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq "true" || $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq 1
defined $ENV{"ELASTIC_CLIENT_APIVERSIONING"} lc $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq "true" $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq 1
Truth table25%1/4
A and B or C
A= defined $ENV{"ELASTIC_CLIENT_APIVERSIONING"}B= lc $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq "true"C= $ENV{"ELASTIC_CLIENT_APIVERSIONING"} eq 1
ABCresult
0XX0
1000
11X1
1011
157 (lc($ENV{ELASTIC_CLIENT_APIVERSIONING}) eq 'true' || $ENV{ELASTIC_CLIENT_APIVERSIONING} eq '1')) {
158 0 $default_headers{'Accept'} = 'application/vnd.elasticsearch+json;compatible-with=7';
159 0 $default_headers{'Content-Type'} = 'application/vnd.elasticsearch+json; compatible-with=7';
160 }
161  
162 175 ▶ if (defined $params->{elastic_cloud_api_key} && defined $params->{token_api}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"elastic_cloud_api_key"} and defined $params->{"token_api"}) 0 175 175
Condition33%1/3
defined $params->{"elastic_cloud_api_key"} and defined $params->{"token_api"}
!ll&&!rl&&r
17500
MC/DC0%0/2
defined $params->{"elastic_cloud_api_key"} and defined $params->{"token_api"}
defined $params->{"elastic_cloud_api_key"} defined $params->{"token_api"}
Truth table33%1/3
A and B
A= defined $params->{"elastic_cloud_api_key"}B= defined $params->{"token_api"}
ABresult
0X0
100
111
163 0 throw( 'Request',
164 "You cannot set elastic_cloud_api_key and token_api together" );
165 }
166  
167 # Elastic cloud API key
168 175 ▶ if (defined $params->{elastic_cloud_api_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"elastic_cloud_api_key"}) 0 175 175
169 0 $default_headers{'Authorization'} = sprintf("ApiKey %s", $params->{elastic_cloud_api_key});
170 }
171  
172 # Elasticsearch token API (https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-token.html)
173 175 ▶ if (defined $params->{token_api}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"token_api"}) 0 175 175
174 0 $default_headers{'Authorization'} = sprintf("Bearer %s", $params->{token_api});
175 }
176  
177 # Elasticsearch
178 175 $params->{scheme} = $scheme;
179 175 $params->{is_https} = $scheme eq 'https';
180 175 $params->{host} = $host;
181 175 $params->{port} = $port;
182 175 $params->{path} = $path;
183 175 $params->{userinfo} = $userinfo;
184 175 ▶ $host = "[$host]" if Net::IP::ip_is_ipv6($host);
Branch: 1 branch
BranchTrueFalseTotal
if Net::IP::ip_is_ipv6($host) 6 169 175
185 175 $params->{uri} = URI->new("$scheme://$host:$port$path");
186 175 $params->{default_headers} = \%default_headers;
187 # Add the client version
188 175 ▶ if (defined $params->{client}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{"client"}) 0 175 175
189 0 $params->{client_version} = substr($params->{client}, 0, index($params->{client}, '_'));
190 }
191  
192 175 return $params;
193 }
194  
195 #===================================
196 0 ▶before 'handle' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
197 #===================================
198 0 my $self = shift;
199 0 ▶ if ( $$ != $self->_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ != $self->_pid) 0 0 0
200 0 $self->clear_handle;
201 0 $self->_pid($$);
202 }
203 55 };
204  
205 #===================================
206 182 ▶sub is_live { !shift->next_ping }
Subroutine is_live: called CC=1 SCAR=0.0
Pod documented
207 128 ▶sub is_dead { !!shift->next_ping }
Subroutine is_dead: called CC=1 SCAR=0.0
Pod documented
208 #===================================
209  
210 #===================================
211 149 ▶sub mark_live {
Subroutine mark_live: called CC=1 SCAR=0.0
Pod documented
212 #===================================
213 149 my $self = shift;
214 149 $self->ping_failures(0);
215 149 $self->next_ping(0);
216 }
217  
218 #===================================
219 143 ▶sub mark_dead {
Subroutine mark_dead: called CC=1 SCAR=0.0
Pod documented
220 #===================================
221 143 my $self = shift;
222 143 my $fails = $self->ping_failures;
223 143 $self->ping_failures( $fails + 1 );
224  
225 143 my $timeout
226 = min( $self->dead_timeout * 2**$fails, $self->max_dead_timeout );
227 143 my $next = $self->next_ping( time() + $timeout );
228  
229 143 $self->logger->infof( 'Marking [%s] as dead. Next ping at: %s',
230 $self->stringify, scalar localtime($next) );
231  
232 }
233  
234 #===================================
235 121 ▶sub force_ping {
Subroutine force_ping: called CC=1 SCAR=0.0
Pod documented
236 #===================================
237 121 my $self = shift;
238 121 $self->ping_failures(0);
239 121 $self->next_ping(-1);
240 }
241  
242 #===================================
243 47 ▶sub pings_ok {
Subroutine pings_ok: called CC=1 SCAR=0.0
Pod documented
244 #===================================
245 47 my $self = shift;
246 47 $self->logger->infof( 'Pinging [%s]', $self->stringify );
247 47 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
248 47 $self->perform_request(
249 { method => 'HEAD',
250 path => '/',
251 timeout => $self->ping_timeout,
252 }
253 );
254 33 $self->logger->infof( 'Marking [%s] as live', $self->stringify );
255 33 $self->mark_live;
256 33 1;
257 }
258 14 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
259 14 $self->logger->debug("$_");
260 14 $self->mark_dead;
261 14 0;
262 47 };
263 }
264  
265 #===================================
266 38 ▶sub sniff {
Subroutine sniff: called CC=1 SCAR=0.0
Pod documented
267 #===================================
268 38 my $self = shift;
269 38 $self->logger->infof( 'Sniffing [%s]', $self->stringify );
270 38 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 $self->perform_request(
272 { method => 'GET',
273 path => '/_nodes/http',
274 qs => { timeout => $self->sniff_timeout . 's' },
275 timeout => $self->sniff_request_timeout,
276 }
277 38 )->{nodes};
278 }
279 13 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
280 13 $self->logger->debug($_);
281 13 return;
282 38 };
283 }
284  
285 #===================================
286 5 ▶sub build_uri {
Subroutine build_uri: called CC=2 SCAR=6.9
Pod documented
287 #===================================
288 5 my ( $self, $params ) = @_;
289 5 my $uri = $self->uri->clone;
290 5 $uri->path( $uri->path . $params->{path} );
291 5 ▶ my %qs = ( %{ $self->default_qs_params }, %{ $params->{qs} || {} } );
Condition100%2/2
$params->{'qs'} || {}
l!l
23
MC/DC100%1/1
$params->{'qs'} || {}
$params->{'qs'}
Truth table100%2/2
A
A= $params->{'qs'}
Aresult
11
00
292 5 $uri->query_form( \%qs );
293 5 return $uri;
294 }
295  
296 #===================================
297 233 ▶before 'perform_request' => sub {
Subroutine __ANON__: called CC=4 SCAR=23.7
298 #===================================
299 233 my ( $self, $params ) = @_;
300 233 ▶ return unless defined $params->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"data"} 233 0 233
301  
302 0 $self->_compress_body($params);
303  
304 0 ▶ my $max = $self->max_content_length
Branch: 1 branch
BranchTrueFalseTotal
my $max = $self->max_content_length or return 0 0 0
305 or return;
306  
307 0 ▶ return if length( $params->{data} ) < $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $params->{"data"} < $max 0 0 0
308  
309 0 $self->logger->throw_error( 'ContentLength',
310 "Body is longer than max_content_length ($max)",
311 );
312 55 };
313  
314 #===================================
315 0 ▶sub _compress_body {
Subroutine _compress_body: not called CC=5 SCAR=34.0
316 #===================================
317 0 my ( $self, $params ) = @_;
318 0 my $output;
319 0 ▶ if ( $self->gzip ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->gzip) { } 0 0 0
elsif ($self->deflate) { } 0 0 0
320 0 ▶ IO::Compress::Gzip::gzip( \( $params->{data} ), \$output )
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Gzip::gzip(\$params->{"data"}, \$output) or throw("Request", "Couldn't gzip request: $IO::Compress::Gzip::GzipError") 0 0 0
321 or throw( 'Request',
322 "Couldn't gzip request: $IO::Compress::Gzip::GzipError" );
323 0 $params->{data} = $output;
324 0 $params->{encoding} = 'gzip';
325 }
326 elsif ( $self->deflate ) {
327 0 ▶ IO::Compress::Deflate::deflate( \( $params->{data} ), \$output )
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Deflate::deflate(\$params->{"data"}, \$output) or throw("Request", "Couldn't deflate request: $IO::Compress::Deflate::DeflateError") 0 0 0
328 or throw( 'Request',
329 "Couldn't deflate request: $IO::Compress::Deflate::DeflateError" );
330 0 $params->{data} = $output;
331 0 $params->{encoding} = 'deflate';
332 }
333 }
334  
335 #===================================
336 239 ▶sub _decompress_body {
Subroutine _decompress_body: called CC=6 SCAR=33.1
337 #===================================
338 239 my ( $self, $body_ref, $headers ) = @_;
339 239 ▶ if ( my $encoding = $headers->{'content-encoding'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $headers->{"content-encoding"}) 0 239 239
340 0 my $output;
341 0 ▶ if ( $encoding eq 'gzip' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($encoding eq 'gzip') { } 0 0 0
elsif ($encoding eq 'deflate') { } 0 0 0
342 0 ▶ IO::Uncompress::Gunzip::gunzip( $body_ref, \$output )
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Gunzip::gunzip($body_ref, \$output) or throw("Request", "Couldn't gunzip response: $IO::Uncompress::Gunzip::GunzipError") 0 0 0
343 or throw(
344 'Request',
345 "Couldn't gunzip response: $IO::Uncompress::Gunzip::GunzipError"
346 );
347 }
348 elsif ( $encoding eq 'deflate' ) {
349 0 ▶ IO::Uncompress::Inflate::inflate( $body_ref, \$output,
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Inflate::inflate($body_ref, \$output, "Transparent", 0) or throw("Request", "Couldn't inflate response: $IO::Uncompress::Inflate::InflateError") 0 0 0
350 Transparent => 0 )
351 or throw(
352 'Request',
353 "Couldn't inflate response: $IO::Uncompress::Inflate::InflateError"
354 );
355 }
356 else {
357 0 throw( 'Request', "Unknown content-encoding: $encoding" );
358 }
359 0 ${$body_ref} = $output;
360 }
361 }
362  
363 #===================================
364 240 ▶sub process_response {
Subroutine process_response: called CC=26 SCAR=35.2
Pod documented
365 #===================================
366 240 my ( $self, $params, $code, $msg, $body, $headers ) = @_;
367  
368 # Product check only for 8+ client API version
369 240 ▶ if ( $self->client_version >= 8 and $code >= 200 and $code < 300 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->client_version >= 8 and $code >= 200 and $code < 300) 178 62 240
Condition50%3/6
$self->client_version >= 8 and $code >= 200 and $code < 300
!ll&&!rl&&r
062178
$self->client_version >= 8 and $code >= 200
!ll&&!rl&&r
00240
MC/DC33%1/3
$self->client_version >= 8 and $code >= 200 and $code < 300
$self->client_version >= 8 $code >= 200 $code < 300
Truth table50%2/4
A and B and C
A= $self->client_version >= 8B= $code >= 200C= $code < 300
ABCresult
0XX0
10X0
1100
1111
370 178 ▶ my $product = $headers->{$PRODUCT_CHECK_HEADER} // '';
Condition100%2/2
$headers->{$Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_HEADER} // ""
l!l
1771
MC/DC100%1/1
$headers->{$Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_HEADER} // ""
$headers->{$Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_HEADER}
Truth table100%2/2
A
A= $headers->{$Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_HEADER}
Aresult
11
00
371 178 ▶ if ($product ne $PRODUCT_CHECK_VALUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($product ne $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_VALUE) 1 177 178
372 1 throw( "ProductCheck", "The client noticed that the server is not Elasticsearch and we do not support this unknown product" );
373 }
374 }
375  
376 239 $self->_decompress_body( \$body, $headers );
377  
378 239 ▶ my ($mime_type) = split /\s*;\s*/, ( $headers->{'content-type'} || '' );
Condition100%2/2
$headers->{'content-type'} || ''
l!l
2345
MC/DC100%1/1
$headers->{'content-type'} || ''
$headers->{'content-type'}
Truth table100%2/2
A
A= $headers->{'content-type'}
Aresult
11
00
379  
380 239 ▶ my $is_encoded = $mime_type && $mime_type ne 'text/plain';
Condition100%3/3
$mime_type && $mime_type ne 'text/plain'
!ll&&!rl&&r
51233
MC/DC100%2/2
$mime_type && $mime_type ne 'text/plain'
$mime_type $mime_type ne 'text/plain'
Truth table100%3/3
A && B
A= $mime_typeB= $mime_type ne 'text/plain'
ABresult
0X0
100
111
381  
382 # Deprecation warnings
383 239 ▶ if ( my $warnings = $headers->{warning} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $warnings = $headers->{"warning"}) 0 239 239
384 0 my $warning_string = _parse_warnings($warnings);
385 0 my %temp = (%$params);
386 0 delete $temp{data};
387 0 $self->logger->deprecation( $warning_string, \%temp );
388 }
389  
390 # Request is successful
391 239 ▶ if ( $code >= 200 and $code <= 209 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code >= 200 and $code <= 209) 177 62 239
Condition66%2/3
$code >= 200 and $code <= 209
!ll&&!rl&&r
062177
MC/DC50%1/2
$code >= 200 and $code <= 209
$code >= 200 $code <= 209
Truth table66%2/3
A and B
A= $code >= 200B= $code <= 209
ABresult
0X0
100
111
392 177 ▶ if ( defined $body and length $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and length $body) 141 36 177
Condition100%3/3
defined $body and length $body
!ll&&!rl&&r
333141
MC/DC100%2/2
defined $body and length $body
defined $body length $body
Truth table100%3/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
393 141 ▶ $body = $self->serializer->decode($body)
Branch: 1 branch
BranchTrueFalseTotal
if $is_encoded 140 1 141
394 if $is_encoded;
395 141 return $code, $body;
396 }
397 36 ▶ return ( $code, 1 ) if $params->{method} eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"method"} eq "HEAD" 34 2 36
398 2 return ( $code, '' );
399 }
400  
401 # Check if the error should be ignored
402 62 my @ignore = to_list( $params->{ignore} );
403 62 ▶ push @ignore, 404 if $params->{method} eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"method"} eq "HEAD" 11 51 62
404 62 ▶ return ($code) if grep { $_ eq $code } @ignore;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $code;} @ignore 2 60 62
405  
406 # Determine error type
407 60 my $error_type = $Code_To_Error{$code};
408 60 ▶ unless ($error_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error_type) 49 11 60
409 49 ▶ if ( defined $body and length $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and length $body) 0 49 49
Condition33%1/3
defined $body and length $body
!ll&&!rl&&r
4900
MC/DC0%0/2
defined $body and length $body
defined $body length $body
Truth table33%1/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
410 0 $msg = $body;
411 0 $body = undef;
412 }
413 49 $error_type = $self->error_from_text( $code, $msg );
414 }
415  
416 60 ▶ delete $params->{data} if $params->{body};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"body"} 0 60 60
417 60 my %error_args = ( status_code => $code, request => $params );
418  
419 # Extract error message from the body, if present
420  
421 60 ▶ if ( $body = $self->serializer->decode($body) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body = $self->serializer->decode($body)) 2 58 60
422 2 $error_args{body} = $body;
423 2 ▶ $msg = $self->_munge_elasticsearch_exception($body) || $msg;
Condition33%1/3
$self->_munge_elasticsearch_exception($body) || $msg
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->_munge_elasticsearch_exception($body) || $msg
$self->_munge_elasticsearch_exception($body) $msg
Truth table33%1/3
A || B
A= $self->_munge_elasticsearch_exception($body)B= $msg
ABresult
1X1
011
000
424  
425 2 ▶ $error_args{current_version} = $1
Branch: 1 branch
BranchTrueFalseTotal
if $error_type eq "Conflict" and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/ 0 2 2
Condition33%1/3
$error_type eq "Conflict" and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/
!ll&&!rl&&r
200
MC/DC0%0/2
$error_type eq "Conflict" and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/
$error_type eq "Conflict" $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/
Truth table33%1/3
A and B
A= $error_type eq "Conflict"B= $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/
ABresult
0X0
100
111
426 if $error_type eq 'Conflict'
427 and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/;
428 }
429 60 ▶ $msg ||= $error_type;
Condition33%1/3
$msg ||= $error_type
l!l&&r!l&&!r
6000
MC/DC0%0/2
$msg ||= $error_type
$msg $error_type
Truth table33%1/3
A ||= B
A= $msgB= $error_type
ABresult
1X1
011
000
430  
431 60 chomp $msg;
432 60 throw( $error_type, "[" . $self->stringify . "]-[$code] $msg",
433 \%error_args );
434 }
435  
436 #===================================
437 0 ▶sub _parse_warnings {
Subroutine _parse_warnings: not called CC=4 SCAR=30.0
438 #===================================
439 0 ▶ my @warnings = ref $_[0] eq 'ARRAY' ? @{ shift() } : shift();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
440 0 my @str;
441 0 for (@warnings) {
442 0 ▶ if ( $_ =~ /^\d+\s+\S+\s+"((?:\\"|[^"])+)"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\d+\s+\S+\s+"((?:\\"|[^"])+)"/) { } 0 0 0
443 0 my $msg = $1;
444 0 $msg =~ s/\\"/"/g, push @str, $msg;
445 }
446 else {
447 0 push @str, $_;
448 }
449 }
450 0 return join "; ", @str;
451 }
452  
453 #===================================
454 2 ▶sub _munge_elasticsearch_exception {
Subroutine _munge_elasticsearch_exception: called CC=10 SCAR=41.0
455 #===================================
456 2 my ( $self, $body ) = @_;
457 2 ▶ return $body unless ref $body eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 2 2
458 2 ▶ my $error = $body->{error} || return;
Condition50%1/2
$body->{'error'} || (return)
l!l
20
MC/DC0%0/1
$body->{'error'} || (return)
$body->{'error'}
Truth table50%1/2
A
A= $body->{'error'}
Aresult
11
00
459 2 ▶ return $error unless ref $error eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $error eq "HASH" 2 0 2
460  
461 0 ▶ my $root_causes = $error->{root_cause} || [];
Condition0%0/2
$error->{'root_cause'} || []
l!l
00
MC/DC0%0/1
$error->{'root_cause'} || []
$error->{'root_cause'}
Truth table0%0/2
A
A= $error->{'root_cause'}
Aresult
11
00
462 0 ▶ unless (@$root_causes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$root_causes) 0 0 0
463 0 ▶ my $msg = "[" . $error->{type} . "] " if $error->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $error->{"type"} 0 0 0
464 0 ▶ $msg .= $error->{reason} if $error->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if $error->{"reason"} 0 0 0
465 0 return $msg;
466 }
467  
468 0 my $json = $self->serializer;
469 0 my @msgs;
470 0 for (@$root_causes) {
471 0 my %cause = (%$_);
472 my $msg
473 0 = "[" . ( delete $cause{type} ) . "] " . ( delete $cause{reason} );
474 0 ▶ if ( keys %cause ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %cause) 0 0 0
475 0 $msg .= ", with: " . $json->encode( \%cause );
476 }
477 0 push @msgs, $msg;
478 }
479 0 return ( join ", ", @msgs );
480 }
481  
482 55 1;
483  
484 # ABSTRACT: Provides common functionality to HTTP Cxn implementations
485  
486 __END__
« Elasticsearch/Client/8_0/Role/Bulk.pm ↑ Summary Elasticsearch/TestServer.pm »