| 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::Cxn::LWP; | ||
| 19 | $Search::Elasticsearch::Cxn::LWP::VERSION = '8.12'; | ||
| 20 | use Moo; | ||
| 21 | with 'Search::Elasticsearch::Role::Cxn', 'Search::Elasticsearch::Role::Is_Sync'; | ||
| 22 | |||
| 23 | use LWP::UserAgent(); | ||
| 24 | use HTTP::Headers(); | ||
| 25 | use HTTP::Request(); | ||
| 26 | |||
| 27 | my $Cxn_Error = qr/ | ||
| 28 | Can't.connect | ||
| 29 | | Server.closed.connection | ||
| 30 | | Connection.refused | ||
| 31 | /x; | ||
| 32 | |||
| 33 | use namespace::clean; | ||
| 34 | |||
| 35 | #=================================== | ||
| 36 | sub perform_request { | ||
| 37 | #=================================== | ||
| 38 | my ( $self, $params ) = @_; | ||
| 39 | my $uri = $self->build_uri($params); | ||
| 40 | my $method = $params->{method}; | ||
| 41 | |||
| 42 | my %headers; | ||
| 43 | if ( $params->{data} ) { | ||
| 44 | $headers{'Content-Type'} = $params->{mime_type}; | ||
| 45 | $headers{'Content-Encoding'} = $params->{encoding} | ||
| 46 | if $params->{encoding}; | ||
| 47 | } | ||
| 48 | my $request = HTTP::Request->new( | ||
| 49 | $method => $uri, | ||
| 50 | [ %headers, %{ $self->default_headers }, ], | ||
| 51 | $params->{data} | ||
| 52 | ); | ||
| 53 | my $ua = $self->handle; | ||
| 54 | my $timeout = $params->{timeout} || $self->request_timeout; | ||
| 55 | if ( $timeout ne $ua->timeout ) { | ||
| 56 | $ua->conn_cache->drop; | ||
| 57 | $ua->timeout($timeout); | ||
| 58 | } | ||
| 59 | my $response = $ua->request($request); | ||
| 60 | |||
| 61 | return $self->process_response( | ||
| 62 | $params, # request | ||
| 63 | $response->code, # code | ||
| 64 | $response->message, # msg | ||
| 65 | $response->content, # body | ||
| 66 | $response->headers # headers | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | #=================================== | ||
| 71 | sub error_from_text { | ||
| 72 | #=================================== | ||
| 73 | local $_ = $_[2]; | ||
| 74 | |||
| 75 | return | ||
| 76 | /read timeout/ ? 'Timeout' | ||
| 77 | : /write failed: Connection reset by peer/ ? 'ContentLength' | ||
| 78 | : /$Cxn_Error/ ? 'Cxn' | ||
| 79 | : 'Request'; | ||
| 80 | } | ||
| 81 | |||
| 82 | #=================================== | ||
| 83 | sub _build_handle { | ||
| 84 | #=================================== | ||
| 85 | my $self = shift; | ||
| 86 | my %args = ( | ||
| 87 | keep_alive => 1, | ||
| 88 | parse_head => 0 | ||
| 89 | ); | ||
| 90 | if ( $self->is_https ) { | ||
| 91 | $args{ssl_opts} | ||
| 92 | = $self->has_ssl_options | ||
| 93 | ? $self->ssl_options | ||
| 94 | : { verify_hostname => 0, SSL_verify_mode => 0x00 }; | ||
| 95 | } | ||
| 96 | return LWP::UserAgent->new( %args, %{ $self->handle_args } ); | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | # ABSTRACT: A Cxn implementation which uses LWP | ||
| 102 | |||
| 103 | __END__ |