| 1 | # OpenSearch::Client is an unofficial client for OpenSearch. | ||
| 2 | # It is derived from Search::Elasticsearch version 7.714 | ||
| 3 | # License details from the original work are contained in the | ||
| 4 | # NOTICE file distributed with this work. | ||
| 5 | # | ||
| 6 | #----------------------------------------------------------------------- | ||
| 7 | # OpenSearch::Client | ||
| 8 | #----------------------------------------------------------------------- | ||
| 9 | # Copyright 2026 Mark Dootson | ||
| 10 | # | ||
| 11 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 12 | # you may not use this file except in compliance with the License. | ||
| 13 | # You may obtain a copy of the License at | ||
| 14 | # | ||
| 15 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 16 | # | ||
| 17 | # Unless required by applicable law or agreed to in writing, software | ||
| 18 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 19 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 20 | # See the License for the specific language governing permissions and | ||
| 21 | # limitations under the License. | ||
| 22 | |||
| 23 | package OpenSearch::Client::Cxn::LWP; | ||
| 24 | $OpenSearch::Client::Cxn::LWP::VERSION = '3.007011'; | ||
| 25 | use Moo; | ||
| 26 | with 'OpenSearch::Client::Role::Cxn', 'OpenSearch::Client::Role::Is_Sync'; | ||
| 27 | |||
| 28 | use LWP::UserAgent(); | ||
| 29 | use HTTP::Headers(); | ||
| 30 | use HTTP::Request(); | ||
| 31 | |||
| 32 | my $Cxn_Error = qr/ | ||
| 33 | Can't.connect | ||
| 34 | | Server.closed.connection | ||
| 35 | | Connection.refused | ||
| 36 | /x; | ||
| 37 | |||
| 38 | use namespace::clean; | ||
| 39 | |||
| 40 | #=================================== | ||
| 41 | sub perform_request { | ||
| 42 | #=================================== | ||
| 43 | my ( $self, $params ) = @_; | ||
| 44 | my $uri = $self->build_uri($params); | ||
| 45 | my $method = $params->{method}; | ||
| 46 | |||
| 47 | my %headers; | ||
| 48 | if ( $params->{data} ) { | ||
| 49 | $headers{'Content-Type'} = $params->{mime_type}; | ||
| 50 | $headers{'Content-Encoding'} = $params->{encoding} | ||
| 51 | if $params->{encoding}; | ||
| 52 | } | ||
| 53 | my $request = HTTP::Request->new( | ||
| 54 | $method => $uri, | ||
| 55 | [ %headers, %{ $self->default_headers }, ], | ||
| 56 | $params->{data} | ||
| 57 | ); | ||
| 58 | my $ua = $self->handle; | ||
| 59 | my $timeout = $params->{timeout} || $self->request_timeout; | ||
| 60 | if ( $timeout ne $ua->timeout ) { | ||
| 61 | $ua->conn_cache->drop; | ||
| 62 | $ua->timeout($timeout); | ||
| 63 | } | ||
| 64 | my $response = $ua->request($request); | ||
| 65 | |||
| 66 | return $self->process_response( | ||
| 67 | $params, # request | ||
| 68 | $response->code, # code | ||
| 69 | $response->message, # msg | ||
| 70 | $response->content, # body | ||
| 71 | $response->headers # headers | ||
| 72 | ); | ||
| 73 | } | ||
| 74 | |||
| 75 | #=================================== | ||
| 76 | sub error_from_text { | ||
| 77 | #=================================== | ||
| 78 | local $_ = $_[2]; | ||
| 79 | |||
| 80 | return | ||
| 81 | /read timeout/ ? 'Timeout' | ||
| 82 | : /write failed: Connection reset by peer/ ? 'ContentLength' | ||
| 83 | : /$Cxn_Error/ ? 'Cxn' | ||
| 84 | : 'Request'; | ||
| 85 | } | ||
| 86 | |||
| 87 | #=================================== | ||
| 88 | sub _build_handle { | ||
| 89 | #=================================== | ||
| 90 | my $self = shift; | ||
| 91 | my %args = ( | ||
| 92 | keep_alive => 1, | ||
| 93 | parse_head => 0 | ||
| 94 | ); | ||
| 95 | |||
| 96 | if ( $self->is_https ) { | ||
| 97 | $args{ssl_opts} | ||
| 98 | = $self->has_ssl_options | ||
| 99 | ? $self->ssl_options | ||
| 100 | : { SSL_verify_mode => 0x01 }; | ||
| 101 | } | ||
| 102 | |||
| 103 | my $proxy; | ||
| 104 | |||
| 105 | if ( $self->has_http_proxy ) { | ||
| 106 | $proxy->{http} = $self->http_proxy; | ||
| 107 | } | ||
| 108 | |||
| 109 | if ( $self->has_https_proxy ) { | ||
| 110 | $proxy->{https} = $self->https_proxy; | ||
| 111 | } | ||
| 112 | |||
| 113 | $args{proxy} = [ %$proxy ] if $proxy; | ||
| 114 | $args{no_proxy} = $self->no_proxy if $self->has_no_proxy; | ||
| 115 | return LWP::UserAgent->new( %args, %{ $self->handle_args } ); | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | |||
| 121 | __END__ |