| 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::NetCurl; | ||
| 19 | |||
| 20 | use Moo; | ||
| 21 | with 'Search::Elasticsearch::Role::Cxn', 'Search::Elasticsearch::Role::Is_Sync'; | ||
| 22 | |||
| 23 | use Search::Elasticsearch 7.00; | ||
| 24 | our $VERSION = "7.714"; | ||
| 25 | |||
| 26 | use HTTP::Parser::XS qw(HEADERS_AS_HASHREF parse_http_response); | ||
| 27 | use Try::Tiny; | ||
| 28 | use Net::Curl::Easy qw( | ||
| 29 | CURLOPT_HEADER | ||
| 30 | CURLOPT_VERBOSE | ||
| 31 | CURLOPT_URL | ||
| 32 | CURLOPT_CONNECTTIMEOUT_MS | ||
| 33 | CURLOPT_CUSTOMREQUEST | ||
| 34 | CURLOPT_TIMEOUT_MS | ||
| 35 | CURLOPT_POSTFIELDS | ||
| 36 | CURLOPT_POSTFIELDSIZE | ||
| 37 | CURLOPT_HTTPHEADER | ||
| 38 | CURLOPT_SSL_VERIFYPEER | ||
| 39 | CURLOPT_SSL_VERIFYHOST | ||
| 40 | CURLOPT_WRITEDATA | ||
| 41 | CURLOPT_HEADERDATA | ||
| 42 | CURLINFO_RESPONSE_CODE | ||
| 43 | CURLOPT_TCP_NODELAY | ||
| 44 | CURLOPT_NOBODY | ||
| 45 | ); | ||
| 46 | |||
| 47 | has 'connect_timeout' => ( is => 'ro', default => 2 ); | ||
| 48 | |||
| 49 | use namespace::clean; | ||
| 50 | |||
| 51 | #=================================== | ||
| 52 | sub perform_request { | ||
| 53 | #=================================== | ||
| 54 | my ( $self, $params ) = @_; | ||
| 55 | my $uri = $self->build_uri($params); | ||
| 56 | my $method = $params->{method}; | ||
| 57 | |||
| 58 | my $handle = $self->handle; | ||
| 59 | $handle->reset; | ||
| 60 | |||
| 61 | # $handle->setopt( CURLOPT_VERBOSE, 1 ); | ||
| 62 | |||
| 63 | $handle->setopt( CURLOPT_HEADER, 0 ); | ||
| 64 | $handle->setopt( CURLOPT_TCP_NODELAY, 1 ); | ||
| 65 | $handle->setopt( CURLOPT_URL, $uri ); | ||
| 66 | $handle->setopt( CURLOPT_CUSTOMREQUEST, $method ); | ||
| 67 | $handle->setopt( CURLOPT_NOBODY, 1 ) if $method eq 'HEAD'; | ||
| 68 | |||
| 69 | $handle->setopt( CURLOPT_CONNECTTIMEOUT_MS, $self->connect_timeout * 1000 ); | ||
| 70 | $handle->setopt( CURLOPT_TIMEOUT_MS, | ||
| 71 | 1000 * ( $params->{timeout} || $self->request_timeout ) ); | ||
| 72 | |||
| 73 | my %headers = %{ $self->default_headers }; | ||
| 74 | |||
| 75 | my $data = $params->{data}; | ||
| 76 | if ( defined $data ) { | ||
| 77 | $headers{'Content-Type'} = $params->{mime_type}; | ||
| 78 | $headers{'Expect'} = ''; | ||
| 79 | $headers{'Content-Encoding'} = $params->{encoding} | ||
| 80 | if $params->{encoding}; | ||
| 81 | $handle->setopt( CURLOPT_POSTFIELDS, $data ); | ||
| 82 | $handle->setopt( CURLOPT_POSTFIELDSIZE, length $data ); | ||
| 83 | } | ||
| 84 | |||
| 85 | $handle->setopt( CURLOPT_HTTPHEADER, | ||
| 86 | [ map { "$_: " . $headers{$_} } keys %headers ] ) | ||
| 87 | if %headers; | ||
| 88 | |||
| 89 | my %opts = %{ $self->handle_args }; | ||
| 90 | if ( $self->is_https ) { | ||
| 91 | if ( $self->has_ssl_options ) { | ||
| 92 | %opts = ( %opts, %{ $self->ssl_options } ); | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | %opts = ( | ||
| 96 | %opts, | ||
| 97 | ( CURLOPT_SSL_VERIFYPEER() => 0, | ||
| 98 | CURLOPT_SSL_VERIFYHOST() => 0 | ||
| 99 | ) | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | for ( keys %opts ) { | ||
| 105 | $handle->setopt( $_, $opts{$_} ); | ||
| 106 | } | ||
| 107 | |||
| 108 | my $content = my $head = ''; | ||
| 109 | $handle->setopt( CURLOPT_WRITEDATA, \$content ); | ||
| 110 | $handle->setopt( CURLOPT_HEADERDATA, \$head ); | ||
| 111 | |||
| 112 | my ( $code, $msg, $headers ); | ||
| 113 | |||
| 114 | try { | ||
| 115 | $handle->perform; | ||
| 116 | ( undef, undef, $code, $msg, $headers ) | ||
| 117 | = parse_http_response( $head, HEADERS_AS_HASHREF ); | ||
| 118 | } | ||
| 119 | catch { | ||
| 120 | $code = 509; | ||
| 121 | $msg = ( 0 + $_ ) . ": $_"; | ||
| 122 | $msg . ", " . $handle->error | ||
| 123 | if $handle->error; | ||
| 124 | undef $content; | ||
| 125 | }; | ||
| 126 | |||
| 127 | return $self->process_response( | ||
| 128 | $params, # request | ||
| 129 | $code, # code | ||
| 130 | $msg, # msg | ||
| 131 | $content, # body | ||
| 132 | $headers # headers | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | |||
| 136 | #=================================== | ||
| 137 | sub error_from_text { | ||
| 138 | #=================================== | ||
| 139 | local $_ = $_[2]; | ||
| 140 | shift; | ||
| 141 | return | ||
| 142 | m/^7:/ ? 'Cxn' | ||
| 143 | : m/^28:/ ? 'Timeout' | ||
| 144 | : m/^51:/ ? 'SSL' | ||
| 145 | : m/^55:/ ? 'ContentLength' | ||
| 146 | : 'Request'; | ||
| 147 | |||
| 148 | } | ||
| 149 | |||
| 150 | #=================================== | ||
| 151 | sub _build_handle { Net::Curl::Easy->new } | ||
| 152 | #=================================== | ||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | # ABSTRACT: A Cxn implementation which uses libcurl via Net::Curl | ||
| 157 | |||
| 158 | __END__ |