Skip to content

Commit f10eeb8

Browse files
committed
[Client] Updates curb implementation
1 parent 5381d72 commit f10eeb8

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

elasticsearch-transport/lib/elasticsearch/transport/transport/http/curb.rb

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,44 @@ def perform_request(method, path, params={}, body=nil, headers=nil, opts={})
3737
connection.connection.url = connection.full_url(path, params)
3838

3939
case method
40-
when 'HEAD'
41-
connection.connection.set :nobody, true
42-
when 'GET', 'POST', 'PUT', 'DELETE'
43-
connection.connection.set :nobody, false
44-
45-
connection.connection.put_data = __convert_to_json(body) if body
46-
47-
if headers
48-
if connection.connection.headers
49-
connection.connection.headers.merge!(headers)
50-
else
51-
connection.connection.headers = headers
52-
end
40+
when 'HEAD'
41+
connection.connection.set :nobody, true
42+
when 'GET', 'POST', 'PUT', 'DELETE'
43+
connection.connection.set :nobody, false
44+
connection.connection.put_data = __convert_to_json(body) if body
45+
if headers
46+
if connection.connection.headers
47+
connection.connection.headers.merge!(headers)
48+
else
49+
connection.connection.headers = headers
5350
end
54-
55-
else raise ArgumentError, "Unsupported HTTP method: #{method}"
51+
end
52+
else
53+
raise ArgumentError, "Unsupported HTTP method: #{method}"
5654
end
5755

5856
connection.connection.http(method.to_sym)
5957

60-
response_headers = {}
61-
response_headers['content-type'] = 'application/json' if connection.connection.header_str =~ /\/json/
58+
Response.new(
59+
connection.connection.response_code,
60+
decompress_response(connection.connection.body_str),
61+
headers(connection)
62+
)
63+
end
64+
end
6265

63-
Response.new connection.connection.response_code,
64-
decompress_response(connection.connection.body_str),
65-
response_headers
66+
def headers(connection)
67+
headers_string = connection.connection.header_str
68+
return nil if headers_string.nil?
69+
70+
response_headers = headers_string&.split(/\\r\\n|\r\n/).reject(&:empty?)
71+
response_headers.shift # Removes HTTP status string
72+
processed_header = response_headers.flat_map { |s| s.scan(/^(\S+): (.+)/) }
73+
headers_hash = Hash[processed_header].transform_keys(&:downcase)
74+
if headers_hash['content-type']&.match?(/application\/json/)
75+
headers_hash['content-type'] = 'application/json'
6676
end
77+
headers_hash
6778
end
6879

6980
# Builds and returns a connection
@@ -72,9 +83,8 @@ def perform_request(method, path, params={}, body=nil, headers=nil, opts={})
7283
#
7384
def __build_connection(host, options={}, block=nil)
7485
client = ::Curl::Easy.new
75-
7686
apply_headers(client, options)
77-
client.url = __full_url(host)
87+
client.url = __full_url(host)
7888

7989
if host[:user]
8090
client.http_auth_types = host[:auth_type] || :basic
@@ -106,13 +116,13 @@ def host_unreachable_exceptions
106116

107117
def user_agent_header(client)
108118
@user_agent ||= begin
109-
meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
110-
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
111-
meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} #{RbConfig::CONFIG['target_cpu']}"
112-
end
113-
meta << "Curb #{Curl::CURB_VERSION}"
114-
"elasticsearch-ruby/#{VERSION} (#{meta.join('; ')})"
115-
end
119+
meta = ["RUBY_VERSION: #{RUBY_VERSION}"]
120+
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
121+
meta << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} #{RbConfig::CONFIG['target_cpu']}"
122+
end
123+
meta << "Curb #{Curl::CURB_VERSION}"
124+
"elasticsearch-ruby/#{VERSION} (#{meta.join('; ')})"
125+
end
116126
end
117127
end
118128
end

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,11 +1814,12 @@
18141814
end
18151815

18161816
context 'when using Curb as the transport', unless: jruby? do
1817-
18181817
let(:client) do
1819-
described_class.new(hosts: ELASTICSEARCH_HOSTS,
1820-
compression: true,
1821-
transport_class: Elasticsearch::Transport::Transport::HTTP::Curb)
1818+
described_class.new(
1819+
hosts: ELASTICSEARCH_HOSTS,
1820+
compression: true,
1821+
transport_class: Elasticsearch::Transport::Transport::HTTP::Curb
1822+
)
18221823
end
18231824

18241825
it 'compresses the request and decompresses the response' do
@@ -1835,7 +1836,6 @@
18351836
end
18361837

18371838
context 'when using Manticore as the transport', if: jruby? do
1838-
18391839
let(:client) do
18401840
described_class.new(hosts: ELASTICSEARCH_HOSTS,
18411841
compression: true,
@@ -1849,9 +1849,7 @@
18491849
end
18501850

18511851
describe '#perform_request' do
1852-
18531852
context 'when a request is made' do
1854-
18551853
before do
18561854
client.perform_request('DELETE', '_all')
18571855
client.perform_request('DELETE', 'myindex') rescue
@@ -1874,7 +1872,6 @@
18741872
end
18751873

18761874
context 'when an invalid url is specified' do
1877-
18781875
it 'raises an exception' do
18791876
expect {
18801877
client.perform_request('GET', 'myindex/mydoc/1?routing=FOOBARBAZ')
@@ -1883,7 +1880,6 @@
18831880
end
18841881

18851882
context 'when the \'ignore\' parameter is specified' do
1886-
18871883
let(:response) do
18881884
client.perform_request('PUT', '_foobar', ignore: 400)
18891885
end

elasticsearch-transport/spec/elasticsearch/transport/meta_header_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,16 @@ def meta_version
255255
end
256256

257257
context 'when using custom transport implementation' do
258-
class MyTransport
259-
include Elasticsearch::Transport::Transport::Base
260-
def initialize(args); end
258+
let (:transport_class) do
259+
Class.new do
260+
include Elasticsearch::Transport::Transport::Base
261+
262+
def initialize(args)
263+
end
264+
end
261265
end
262-
let(:client) { Elasticsearch::Client.new(transport_class: MyTransport) }
263-
let(:subject){ client.instance_variable_get("@arguments")[:transport_options][:headers] }
266+
let(:client) { Elasticsearch::Client.new(transport_class: transport_class) }
267+
let(:subject) { client.instance_variable_get('@arguments')[:transport_options][:headers] }
264268
let(:meta_header) do
265269
if jruby?
266270
"es=#{meta_version},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jv=#{ENV_JAVA['java.version']},jr=#{JRUBY_VERSION}"

elasticsearch-transport/test/unit/transport_curb_test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Minitest::Test
8484
@transport.connections.first.connection.expects(:http).with(:GET).returns(stub_everything)
8585
@transport.connections.first.connection.expects(:body_str).returns('{"foo":"bar"}')
8686
@transport.connections.first.connection.expects(:header_str).returns('HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\nContent-Length: 311\r\n\r\n')
87-
8887
response = @transport.perform_request 'GET', '/'
8988

9089
assert_equal 'application/json', response.headers['content-type']

0 commit comments

Comments
 (0)