1+ if ( '0.12.0' ..'0.13.0' ) === Gem . loaded_specs [ 'net-ldap' ] . version . to_s
2+ ##
3+ # A String object with a BER identifier attached.
4+ #
5+ class Net ::BER ::BerIdentifiedString < String
6+ attr_accessor :ber_identifier
7+
8+ # The binary data provided when parsing the result of the LDAP search
9+ # has the encoding 'ASCII-8BIT' (which is basically 'BINARY', or 'unknown').
10+ #
11+ # This is the kind of a backtrace showing how the binary `data` comes to
12+ # BerIdentifiedString.new(data):
13+ #
14+ # @conn.read_ber(syntax)
15+ # -> StringIO.new(self).read_ber(syntax), i.e. included from module
16+ # -> Net::BER::BERParser.read_ber(syntax)
17+ # -> (private)Net::BER::BERParser.parse_ber_object(syntax, id, data)
18+ #
19+ # In the `#parse_ber_object` method `data`, according to its OID, is being
20+ # 'casted' to one of the Net::BER:BerIdentifiedXXX classes.
21+ #
22+ # As we are using LDAP v3 we can safely assume that the data is encoded
23+ # in UTF-8 and therefore the only thing to be done when instantiating is to
24+ # switch the encoding from 'ASCII-8BIT' to 'UTF-8'.
25+ #
26+ # Unfortunately, there are some ActiveDirectory specific attributes
27+ # (like `objectguid`) that should remain binary (do they really?).
28+ # Using the `#valid_encoding?` we can trap this cases. Special cases like
29+ # Japanese, Korean, etc. encodings might also profit from this. However
30+ # I have no clue how this encodings function.
31+ def initialize args
32+ super
33+ #
34+ # Check the encoding of the newly created String and set the encoding
35+ # to 'UTF-8' (NOTE: we do NOT change the bytes, but only set the
36+ # encoding to 'UTF-8').
37+ current_encoding = encoding
38+ if current_encoding == Encoding ::BINARY
39+ force_encoding ( 'UTF-8' )
40+ force_encoding ( current_encoding ) unless valid_encoding?
41+ end
42+ end
43+ end
44+ end
0 commit comments