package Search::Elasticsearch::Cxn::HTTPTiny; $Search::Elasticsearch::Cxn::HTTPTiny::VERSION = '1.19'; use Moo; with 'Search::Elasticsearch::Role::Cxn::HTTP', 'Search::Elasticsearch::Role::Cxn', 'Search::Elasticsearch::Role::Is_Sync'; use HTTP::Tiny 0.043 (); use namespace::clean; my $Cxn_Error = qr/ Connection.(?:timed.out|re(?:set|fused)) | connect:.timeout | Host.is.down | No.route.to.host | temporarily.unavailable /x; #=================================== sub perform_request { #=================================== my ( $self, $params ) = @_; my $uri = $self->build_uri($params); my $method = $params->{method}; my %args; if ( defined $params->{data} ) { $args{content} = $params->{data}; $args{headers}{'Content-Type'} = $params->{mime_type}; } my $handle = $self->handle; $handle->timeout( $params->{timeout} || $self->request_timeout ); my $response = $handle->request( $method, "$uri", \%args ); return $self->process_response( $params, # request $response->{status}, # code $response->{reason}, # msg $response->{content}, # body $response->{headers} # headers ); } #=================================== sub error_from_text { #=================================== local $_ = $_[2]; return /[Tt]imed out/ ? 'Timeout' : /Unexpected end of stream/ ? 'ContentLength' : /SSL connection failed/ ? 'SSL' : /$Cxn_Error/ ? 'Cxn' : 'Request'; } #=================================== sub _build_handle { #=================================== my $self = shift; my %args = ( default_headers => $self->default_headers ); if ( $self->is_https && $self->has_ssl_options ) { $args{SSL_options} = $self->ssl_options; if ( $args{SSL_options}{SSL_verify_mode} ) { $args{verify_ssl} = 1; } } return HTTP::Tiny->new( %args, %{ $self->handle_args } ); } 1; # ABSTRACT: A Cxn implementation which uses HTTP::Tiny __END__ =pod =encoding UTF-8 =head1 NAME Search::Elasticsearch::Cxn::HTTPTiny - A Cxn implementation which uses HTTP::Tiny =head1 VERSION version 1.19 =head1 DESCRIPTION Provides the default HTTP Cxn class and is based on L. The HTTP::Tiny backend is fast, uses pure Perl, support proxies and https and provides persistent connections. This class does L, whose documentation provides more information, L and L. =head1 CONFIGURATION =head2 Inherited configuration From L =over =item * L =item * L =item * L =back From L =over =item * L =item * L =item * L =item * L =item * L =item * L =item * L =back =head1 SSL/TLS L uses L to support HTTPS. By default, no validation of the remote host is performed. This behaviour can be changed by passing the C parameter with any options accepted by L. 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; use IO::Socket::SSL; my $es = Search::Elasticsearch->new( nodes => [ "https://node1.mydomain.com:9200", "https://node2.mydomain.com:9200", ], ssl_options => { SSL_verify_mode => SSL_VERIFY_PEER, SSL_ca_file => '/path/to/cacert.pem' } ); If the remote server cannot be verified, an L will be thrown. If you want your client to present its own certificate to the remote server, then use: use Search::Elasticsearch; use IO::Socket::SSL; my $es = Search::Elasticsearch->new( nodes => [ "https://node1.mydomain.com:9200", "https://node2.mydomain.com:9200", ], ssl_options => { SSL_verify_mode => SSL_VERIFY_PEER, SSL_use_cert => 1, SSL_ca_file => '/path/to/cacert.pem', SSL_cert_file => '/path/to/client.pem', SSL_key_file => '/path/to/client.pem', } ); =head1 METHODS =head2 C ($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 =over =item * L =item * L =item * L =item * L =item * L =item * L =back From L =over =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =back =head1 SEE ALSO =over =item * L =item * L =item * L =item * L =back =head1 AUTHOR Clinton Gormley =head1 COPYRIGHT AND LICENSE This software is Copyright (c) 2015 by Elasticsearch BV. This is free software, licensed under: The Apache License, Version 2.0, January 2004 =cut