Skip to content

Extract paged search result cookie handling logic #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions lib/net/ldap/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,34 +535,9 @@ def search(args = nil)
payload[:page_count] ||= 0
payload[:page_count] += 1

# When we get here, we have seen a type-5 response. If there is no
# error AND there is an RFC-2696 cookie, then query again for the next
# page of results. If not, we're done. Don't screw this up or we'll
# break every search we do.
#
# Noticed 02Sep06, look at the read_ber call in this loop, shouldn't
# that have a parameter of AsnSyntax? Does this just accidentally
# work? According to RFC-2696, the value expected in this position is
# of type OCTET STRING, covered in the default syntax supported by
# read_ber, so I guess we're ok.
more_pages = false
if result_pdu.result_code == Net::LDAP::ResultCodeSuccess and controls
controls.each do |c|
if c.oid == Net::LDAP::LDAPControls::PAGED_RESULTS
# just in case some bogus server sends us more than 1 of these.
more_pages = false
if c.value and c.value.length > 0
cookie = c.value.read_ber[1]
if cookie and cookie.length > 0
rfc2696_cookie[1] = cookie
more_pages = true
end
end
end
end
end

break unless more_pages
cookie = next_search_result_cookie(result_pdu.result_code, controls)
break if cookie.nil?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could reorder this and skip the unless cookie.nil?:

cookie = next_search_result_cookie(result.pdu.result_code, controls)
break if cookie.nil?
rfc2696_cookie[1] = cookie

rfc2696_cookie[1] = cookie
end # loop

# track total result count
Expand All @@ -583,6 +558,32 @@ def search(args = nil)
end
end

##
# Return RFC 2696 search result cookie
#
# RFC 2696 describes an LDAPv3 control extension for simple paging of search
# results. See http://www.ietf.org/rfc/rfc2696.txt.
#
# If there is no error AND there is an RFC-2696 cookie, then query again for
# the next page of results.

def next_search_result_cookie(result_code, controls)
found_cookie = nil
if result_code == Net::LDAP::ResultCodeSuccess && controls
controls.each do |c|
if c.oid == Net::LDAP::LDAPControls::PAGED_RESULTS
if c.value && c.value.length > 0
cookie = c.value.read_ber[1]
if cookie && cookie.length > 0
found_cookie = cookie
end
end
end
end
end
found_cookie
end

MODIFY_OPERATIONS = { #:nodoc:
:add => 0,
:delete => 1,
Expand Down