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