Merge pull request #22 from danabr/ldap_strings_are_utf8

Make Net::LDAP use UTF-8 strings
This commit is contained in:
Rory O'Connell 2011-09-24 11:33:10 -07:00
commit dac73f46d2
3 changed files with 31 additions and 2 deletions

View file

@ -295,6 +295,8 @@ class Net::BER::BerIdentifiedString < String
attr_accessor :ber_identifier
def initialize args
super args
# LDAP uses UTF-8 encoded strings
force_encoding('UTF-8') if respond_to?(:encoding)
end
end

View file

@ -12,9 +12,21 @@ module Net::BER::Extensions::String
# User code should call either #to_ber_application_string or
# #to_ber_contextspecific.
def to_ber(code = 0x04)
[code].pack('C') + length.to_ber_length_encoding + self
raw_string = raw_utf8_encoded
[code].pack('C') + raw_string.length.to_ber_length_encoding + raw_string
end
def raw_utf8_encoded
if self.respond_to?(:encode)
# Strings should be UTF-8 encoded according to LDAP.
# However, the BER code is not necessarily valid UTF-8
self.encode('UTF-8').force_encoding('ASCII-8BIT')
else
self
end
end
private :raw_utf8_encoded
##
# Creates an application-specific BER string encoded value with the
# provided syntax code value.