Skip to content

Commit f40c46e

Browse files
committed
Merge pull request #115 from jch/search-timeout
Implement search timeout parameter
2 parents 3320248 + 69c7593 commit f40c46e

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

lib/net/ldap.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ def open
610610
# Net::LDAP::SearchScope_WholeSubtree. Default is WholeSubtree.)
611611
# * :size (an integer indicating the maximum number of search entries to
612612
# return. Default is zero, which signifies no limit.)
613+
# * :time (an integer restricting the maximum time in seconds allowed for a search. Default is zero, no time limit RFC 4511 4.5.1.5)
613614
# * :deref (one of: Net::LDAP::DerefAliases_Never, Net::LDAP::DerefAliases_Search,
614615
# Net::LDAP::DerefAliases_Find, Net::LDAP::DerefAliases_Always. Default is Never.)
615616
#
@@ -683,8 +684,8 @@ def search(args = {})
683684
when ResultStrings.key("Success")
684685
# everything good
685686
result_set
686-
when ResultStrings.key("Size Limit Exceeded")
687-
# LDAP: Size limit exceeded
687+
when ResultStrings.key("Size Limit Exceeded"), ResultStrings.key("Time Limit Exceeded")
688+
# LDAP: Size/Time limit exceeded
688689
# This happens when we use size option and results are truncated
689690
# Still we need to return user results
690691
result_set

lib/net/ldap/connection.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ def search(args = nil)
375375

376376
# limiting, paging, sorting
377377
# size: https://tools.ietf.org/html/rfc4511#section-4.5.1.4
378+
# time: https://tools.ietf.org/html/rfc4511#section-4.5.1.5
378379
size = args[:size].to_i
380+
time = args[:time].to_i
379381
paged = args[:paged_searches_supported]
380382
sort = args.fetch(:sort_controls, false)
381383

@@ -421,7 +423,8 @@ def search(args = nil)
421423
filter: filter,
422424
base: base,
423425
scope: scope,
424-
limit: size,
426+
size: size,
427+
time: time,
425428
sort: sort,
426429
referrals: refs,
427430
deref: deref,
@@ -443,7 +446,7 @@ def search(args = nil)
443446
scope.to_ber_enumerated,
444447
deref.to_ber_enumerated,
445448
query_limit.to_ber, # size limit
446-
0.to_ber,
449+
time.to_ber,
447450
attrs_only.to_ber,
448451
filter.to_ber,
449452
ber_attrs.to_ber_sequence

test/integration/test_return_codes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ def test_protocol_error
2121
end
2222

2323
def test_time_limit_exceeded
24-
refute @ldap.search(filter: "cn=timeLimitExceeded", base: "ou=Retcodes,dc=rubyldap,dc=com")
24+
assert @ldap.search(filter: "cn=timeLimitExceeded", base: "ou=Retcodes,dc=rubyldap,dc=com")
2525
assert result = @ldap.get_operation_result
2626

2727
assert_equal 3, result.code
2828
assert_equal Net::LDAP::ResultStrings[3], result.message
2929
end
3030

3131
def test_size_limit_exceeded
32-
@ldap.search(filter: "cn=sizeLimitExceeded", base: "ou=Retcodes,dc=rubyldap,dc=com")
32+
assert @ldap.search(filter: "cn=sizeLimitExceeded", base: "ou=Retcodes,dc=rubyldap,dc=com")
3333
assert result = @ldap.get_operation_result
3434

3535
assert_equal 4, result.code

test/integration/test_search.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ def test_search_without_result
2525
refute_equal entries, result
2626
end
2727

28+
def test_search_timeout
29+
entries = []
30+
events = @service.subscribe "search.net_ldap_connection"
31+
32+
result = @ldap.search(base: "dc=rubyldap,dc=com", time: 5) do |entry|
33+
assert_kind_of Net::LDAP::Entry, entry
34+
entries << entry
35+
end
36+
37+
payload, _ = events.pop
38+
assert_equal 5, payload[:time]
39+
assert_equal entries, result
40+
end
41+
2842
def test_search_with_size
2943
entries = []
3044

0 commit comments

Comments
 (0)