Skip to content

Commit 0e233cc

Browse files
authored
Merge pull request #68 from tharropoulos/fix/keep-alive-dot-segments
fix(api-call): keep dot segments in keep-alive request paths
2 parents 2f460b5 + 26a597a commit 0e233cc

2 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎lib/typesense/api_call.rb‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ def perform_request(method, endpoint, query_parameters: nil, body_parameters: ni
7878
@logger.debug "Attempting #{method.to_s.upcase} request Try ##{num_tries} to Node #{node[:index]}"
7979

8080
begin
81-
conn, request_path = connection_and_path_for(endpoint, node)
81+
conn, request_url = connection_and_url_for(endpoint, node)
8282

8383
headers = default_headers.merge(additional_headers)
8484

85-
response = conn.send(method, request_path) do |req|
85+
# The URL is set on the request instead of being passed to the verb. Faraday
86+
# looks up proxy settings again for every absolute URL passed to the verb.
87+
response = conn.send(method) do |req|
88+
req.url(request_url) unless request_url.nil?
8689
req.headers = headers
8790
req.params = query_parameters unless query_parameters.nil?
8891
unless body_parameters.nil?
@@ -130,17 +133,21 @@ def perform_request(method, endpoint, query_parameters: nil, body_parameters: ni
130133

131134
private
132135

136+
# Faraday 2.9.2 and later only treat a request URL as absolute when it starts
137+
# with a lower-case "http://" or "https://".
133138
def uri_for(endpoint, node)
134-
"#{node[:protocol]}://#{node[:host]}:#{node[:port]}#{endpoint}"
139+
"#{node[:protocol].to_s.downcase}://#{node[:host]}:#{node[:port]}#{endpoint}"
135140
end
136141

137-
# Returns [connection, request_path]. When keep-alive is enabled, the connection
138-
# is cached per (thread, node) and the path is appended at request time. When it
142+
# Returns [connection, request_url]. When keep-alive is enabled, the connection
143+
# is cached per (thread, node) and each request gets the full URL. Faraday merges
144+
# a relative path into the base URL, and that merge drops "." and ".." segments.
145+
# A delete of document ".." would then delete its whole collection. When keep-alive
139146
# is disabled, the original behaviour is preserved: a fresh Faraday is built for
140147
# the full per-request URL, so existing callers and stubs see no change.
141-
def connection_and_path_for(endpoint, node)
148+
def connection_and_url_for(endpoint, node)
142149
if @keep_alive_connections
143-
[connection_for(node), endpoint]
150+
[connection_for(node), uri_for(endpoint, node)]
144151
else
145152
[build_one_shot_connection(endpoint, node), nil]
146153
end

‎spec/typesense/api_call_spec.rb‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,88 @@
376376
end
377377
end
378378

379+
describe 'request paths with stubbed nodes' do
380+
let(:endpoints) do
381+
[
382+
'/collections/c/documents/..',
383+
'/collections/c/documents/.',
384+
'/collections/../documents/x',
385+
'/collections/c/documents/a+b',
386+
'/collections/c/documents/a%20b',
387+
'/collections/c/documents/a%2Fb'
388+
]
389+
end
390+
let(:expected_targets) { endpoints.map { |endpoint| "#{endpoint}?q=a+b" } }
391+
let(:request_targets) { [] }
392+
393+
before do
394+
stub_request(:any, /node0:8108/)
395+
.to_return(status: 200, body: JSON.dump('ok' => true), headers: { 'Content-Type' => 'application/json' })
396+
397+
# WebMock normalizes the URI it records and drops dot segments. Read the
398+
# request target that Faraday hands to Net::HTTP instead (the 4th argument).
399+
allow(Net::HTTPGenericRequest).to receive(:new).and_wrap_original do |original, *args|
400+
request_targets << args[3]
401+
original.call(*args)
402+
end
403+
end
404+
405+
def request_targets_for(keep_alive:, protocol: 'http')
406+
client = Typesense::Client.new(
407+
api_key: 'abcd',
408+
nodes: [{ host: 'node0', port: 8108, protocol: protocol }],
409+
log_level: Logger::ERROR,
410+
keep_alive_connections: keep_alive
411+
)
412+
api_call = described_class.new(client.configuration)
413+
endpoints.each { |endpoint| api_call.delete(endpoint, 'q' => 'a b') }
414+
request_targets
415+
end
416+
417+
it 'sends the endpoint and query unchanged with one-shot connections' do
418+
expect(request_targets_for(keep_alive: false)).to eq(expected_targets)
419+
end
420+
421+
it 'sends the endpoint and query unchanged with keep-alive connections' do
422+
expect(request_targets_for(keep_alive: true)).to eq(expected_targets)
423+
end
424+
425+
it 'sends the endpoint and query unchanged with keep-alive connections and an upper-case protocol' do
426+
expect(request_targets_for(keep_alive: true, protocol: 'HTTP')).to eq(expected_targets)
427+
end
428+
end
429+
430+
describe 'keep-alive requests on a real Typesense server' do
431+
let(:client) do
432+
Typesense::Client.new(
433+
api_key: 'xyz',
434+
nodes: [{ host: 'localhost', port: 8108, protocol: 'http' }],
435+
log_level: Logger::ERROR,
436+
keep_alive_connections: true
437+
)
438+
end
439+
let(:collection) { client.collections['api_call_dot_segments'] }
440+
441+
def delete_collection
442+
collection.delete
443+
rescue Typesense::Error::ObjectNotFound
444+
# Already gone
445+
end
446+
447+
before do
448+
delete_collection
449+
client.collections.create('name' => 'api_call_dot_segments', 'fields' => [{ 'name' => 'title', 'type' => 'string' }])
450+
%w[x ..].each { |id| collection.documents.create('id' => id, 'title' => id) }
451+
end
452+
453+
after { delete_collection }
454+
455+
it 'deletes only the document whose id is ".."' do
456+
expect(collection.documents['..'].delete['id']).to eq('..')
457+
expect(collection.documents['x'].retrieve['id']).to eq('x')
458+
end
459+
end
460+
379461
describe 'keep-alive disabled (default)' do
380462
it 'is off by default on the configuration' do
381463
expect(typesense.configuration.keep_alive_connections).to be(false)

0 commit comments

Comments
 (0)