| 1 | # Licensed to Elasticsearch B.V under one or more agreements. | ||
| 2 | # Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| 3 | # See the LICENSE file in the project root for more information | ||
| 4 | |||
| 5 | package Search::Elasticsearch::Cxn::LWP; | ||
| 6 | $Search::Elasticsearch::Cxn::LWP::VERSION = '7.30_1'; # TRIAL | ||
| 7 | |||
| 8 | $Search::Elasticsearch::Cxn::LWP::VERSION = '7.301'; | ||
| 9 | use Moo; | ||
| 10 | with 'Search::Elasticsearch::Role::Cxn', 'Search::Elasticsearch::Role::Is_Sync'; | ||
| 11 | |||
| 12 | use LWP::UserAgent(); | ||
| 13 | use HTTP::Headers(); | ||
| 14 | use HTTP::Request(); | ||
| 15 | |||
| 16 | my $Cxn_Error = qr/ | ||
| 17 | Can't.connect | ||
| 18 | | Server.closed.connection | ||
| 19 | | Connection.refused | ||
| 20 | /x; | ||
| 21 | |||
| 22 | use namespace::clean; | ||
| 23 | |||
| 24 | #=================================== | ||
| 25 | sub perform_request { | ||
| 26 | #=================================== | ||
| 27 | my ( $self, $params ) = @_; | ||
| 28 | my $uri = $self->build_uri($params); | ||
| 29 | my $method = $params->{method}; | ||
| 30 | |||
| 31 | my %headers; | ||
| 32 | if ( $params->{data} ) { | ||
| 33 | $headers{'Content-Type'} = $params->{mime_type}; | ||
| 34 | $headers{'Content-Encoding'} = $params->{encoding} | ||
| 35 | if $params->{encoding}; | ||
| 36 | } | ||
| 37 | my $request = HTTP::Request->new( | ||
| 38 | $method => $uri, | ||
| 39 | [ %headers, %{ $self->default_headers }, ], | ||
| 40 | $params->{data} | ||
| 41 | ); | ||
| 42 | my $ua = $self->handle; | ||
| 43 | my $timeout = $params->{timeout} || $self->request_timeout; | ||
| 44 | if ( $timeout ne $ua->timeout ) { | ||
| 45 | $ua->conn_cache->drop; | ||
| 46 | $ua->timeout($timeout); | ||
| 47 | } | ||
| 48 | my $response = $ua->request($request); | ||
| 49 | |||
| 50 | return $self->process_response( | ||
| 51 | $params, # request | ||
| 52 | $response->code, # code | ||
| 53 | $response->message, # msg | ||
| 54 | $response->content, # body | ||
| 55 | $response->headers # headers | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | #=================================== | ||
| 60 | sub error_from_text { | ||
| 61 | #=================================== | ||
| 62 | local $_ = $_[2]; | ||
| 63 | |||
| 64 | return | ||
| 65 | /read timeout/ ? 'Timeout' | ||
| 66 | : /write failed: Connection reset by peer/ ? 'ContentLength' | ||
| 67 | : /$Cxn_Error/ ? 'Cxn' | ||
| 68 | : 'Request'; | ||
| 69 | } | ||
| 70 | |||
| 71 | #=================================== | ||
| 72 | sub _build_handle { | ||
| 73 | #=================================== | ||
| 74 | my $self = shift; | ||
| 75 | my %args = ( | ||
| 76 | keep_alive => 1, | ||
| 77 | parse_head => 0 | ||
| 78 | ); | ||
| 79 | if ( $self->is_https ) { | ||
| 80 | $args{ssl_opts} | ||
| 81 | = $self->has_ssl_options | ||
| 82 | ? $self->ssl_options | ||
| 83 | : { verify_hostname => 0, SSL_verify_mode => 0x00 }; | ||
| 84 | } | ||
| 85 | return LWP::UserAgent->new( %args, %{ $self->handle_args } ); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | # ABSTRACT: A Cxn implementation which uses LWP | ||
| 91 | |||
| 92 | __END__ |