1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
package Search::Elasticsearch::Cxn::LWP;
$Search::Elasticsearch::Cxn::LWP::VERSION = '8.12';
use Moo;
with 'Search::Elasticsearch::Role::Cxn', 'Search::Elasticsearch::Role::Is_Sync';
use LWP::UserAgent();
use HTTP::Headers();
use HTTP::Request();
my $Cxn_Error = qr/
Can't.connect
| Server.closed.connection
| Connection.refused
/x;
use namespace::clean;
#===================================
sub perform_request {
#===================================
my ( $self, $params ) = @_;
my $uri = $self->build_uri($params);
my $method = $params->{method};
my %headers;
if ( $params->{data} ) {
$headers{'Content-Type'} = $params->{mime_type};
$headers{'Content-Encoding'} = $params->{encoding}
if $params->{encoding};
}
my $request = HTTP::Request->new(
$method => $uri,
[ %headers, %{ $self->default_headers }, ],
$params->{data}
);
my $ua = $self->handle;
my $timeout = $params->{timeout} || $self->request_timeout;
if ( $timeout ne $ua->timeout ) {
$ua->conn_cache->drop;
$ua->timeout($timeout);
}
my $response = $ua->request($request);
return $self->process_response(
$params, # request
$response->code, # code
$response->message, # msg
$response->content, # body
$response->headers # headers
);
}
#===================================
sub error_from_text {
#===================================
local $_ = $_[2];
return
/read timeout/ ? 'Timeout'
: /write failed: Connection reset by peer/ ? 'ContentLength'
: /$Cxn_Error/ ? 'Cxn'
: 'Request';
}
#===================================
sub _build_handle {
#===================================
my $self = shift;
my %args = (
keep_alive => 1,
parse_head => 0
);
if ( $self->is_https ) {
$args{ssl_opts}
= $self->has_ssl_options
? $self->ssl_options
: { verify_hostname => 0, SSL_verify_mode => 0x00 };
}
return LWP::UserAgent->new( %args, %{ $self->handle_args } );
}
1;
# ABSTRACT: A Cxn implementation which uses LWP
__END__
=pod
=encoding UTF-8
=head1 NAME
Search::Elasticsearch::Cxn::LWP - A Cxn implementation which uses LWP
=head1 VERSION
version 8.12
=head1 DESCRIPTION
Provides an HTTP Cxn class and based on L<LWP>.
The LWP backend uses pure Perl and persistent connections.
This class does L<Search::Elasticsearch::Role::Cxn>, whose documentation
provides more information, and L<Search::Elasticsearch::Role::Is_Sync>.
=head1 CONFIGURATION
=head2 Inherited configuration
From L<Search::Elasticsearch::Role::Cxn>
=over
=item * L<node|Search::Elasticsearch::Role::Cxn/"node">
=item * L<max_content_length|Search::Elasticsearch::Role::Cxn/"max_content_length">
=item * L<deflate|Search::Elasticsearch::Role::Cxn/"gzip">
=item * L<deflate|Search::Elasticsearch::Role::Cxn/"deflate">
=item * L<request_timeout|Search::Elasticsearch::Role::Cxn/"request_timeout">
=item * L<ping_timeout|Search::Elasticsearch::Role::Cxn/"ping_timeout">
=item * L<dead_timeout|Search::Elasticsearch::Role::Cxn/"dead_timeout">
=item * L<max_dead_timeout|Search::Elasticsearch::Role::Cxn/"max_dead_timeout">
=item * L<sniff_request_timeout|Search::Elasticsearch::Role::Cxn/"sniff_request_timeout">
=item * L<sniff_timeout|Search::Elasticsearch::Role::Cxn/"sniff_timeout">
=item * L<handle_args|Search::Elasticsearch::Role::Cxn/"handle_args">
=item * L<handle_args|Search::Elasticsearch::Role::Cxn/"default_qs_params">
=back
=head1 SSL/TLS
L<Search::Elasticsearch::Cxn::LWP> uses L<IO::Socket::SSL> to support
HTTPS. By default, no validation of the remote host is performed.
This behaviour can be changed by passing the C<ssl_options> parameter
with any options accepted by L<IO::Socket::SSL>. For instance, to check
that the remote host has a trusted certificate, and to avoid man-in-the-middle
attacks, you could do the following:
use Search::Elasticsearch;
my $es = Search::Elasticsearch->new(
cxn => 'LWP',
nodes => [
"https://node1.mydomain.com:9200",
"https://node2.mydomain.com:9200",
],
ssl_options => {
verify_hostname => 1,
SSL_ca_file => '/path/to/cacert.pem'
}
);
If the remote server cannot be verified, an
L<Search::Elasticsearch::Error|Cxn error> will be thrown - LWP does not
allow us to detect that the connection error was due to invalid SSL.
If you want your client to present its own certificate to the remote
server, then use:
use Search::Elasticsearch;
my $es = Search::Elasticsearch->new(
cxn => 'LWP',
nodes => [
"https://node1.mydomain.com:9200",
"https://node2.mydomain.com:9200",
],
ssl_options => {
verify_hostname => 1,
SSL_ca_file => '/path/to/cacert.pem',
SSL_use_cert => 1,
SSL_cert_file => '/path/to/client.pem',
SSL_key_file => '/path/to/client.pem',
}
);
=head1 METHODS
=head2 C<perform_request()>
($status,$body) = $self->perform_request({
# required
method => 'GET|HEAD|POST|PUT|DELETE',
path => '/path/of/request',
qs => \%query_string_params,
# optional
data => $body_as_string,
mime_type => 'application/json',
timeout => $timeout
});
Sends the request to the associated Elasticsearch node and returns
a C<$status> code and the decoded response C<$body>, or throws an
error if the request failed.
=head2 Inherited methods
From L<Search::Elasticsearch::Role::Cxn>
=over
=item * L<scheme()|Search::Elasticsearch::Role::Cxn/"scheme()">
=item * L<is_https()|Search::Elasticsearch::Role::Cxn/"is_https()">
=item * L<userinfo()|Search::Elasticsearch::Role::Cxn/"userinfo()">
=item * L<default_headers()|Search::Elasticsearch::Role::Cxn/"default_headers()">
=item * L<max_content_length()|Search::Elasticsearch::Role::Cxn/"max_content_length()">
=item * L<build_uri()|Search::Elasticsearch::Role::Cxn/"build_uri()">
=item * L<host()|Search::Elasticsearch::Role::Cxn/"host()">
=item * L<port()|Search::Elasticsearch::Role::Cxn/"port()">
=item * L<uri()|Search::Elasticsearch::Role::Cxn/"uri()">
=item * L<is_dead()|Search::Elasticsearch::Role::Cxn/"is_dead()">
=item * L<is_live()|Search::Elasticsearch::Role::Cxn/"is_live()">
=item * L<next_ping()|Search::Elasticsearch::Role::Cxn/"next_ping()">
=item * L<ping_failures()|Search::Elasticsearch::Role::Cxn/"ping_failures()">
=item * L<mark_dead()|Search::Elasticsearch::Role::Cxn/"mark_dead()">
=item * L<mark_live()|Search::Elasticsearch::Role::Cxn/"mark_live()">
=item * L<force_ping()|Search::Elasticsearch::Role::Cxn/"force_ping()">
=item * L<pings_ok()|Search::Elasticsearch::Role::Cxn/"pings_ok()">
=item * L<sniff()|Search::Elasticsearch::Role::Cxn/"sniff()">
=item * L<process_response()|Search::Elasticsearch::Role::Cxn/"process_response()">
=back
=head1 SEE ALSO
=over
=item * L<Search::Elasticsearch::Role::Cxn>
=item * L<Search::Elasticsearch::Cxn::HTTPTiny>
=item * L<Search::Elasticsearch::Cxn::NetCurl>
=back
=head1 AUTHOR
Enrico Zimuel <enrico.zimuel@elastic.co>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2024 by Elasticsearch BV.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|