-
Notifications
You must be signed in to change notification settings - Fork 252
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
Adds correct UTF-8 encoding to Net::BER::BerIdentifiedString #242
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,13 +293,43 @@ def to_arr | |
|
||
## | ||
# A String object with a BER identifier attached. | ||
# | ||
class Net::BER::BerIdentifiedString < String | ||
attr_accessor :ber_identifier | ||
|
||
# The binary data provided when parsing the result of the LDAP search | ||
# has the encoding 'ASCII-8BIT' (which is basically 'BINARY', or 'unknown'). | ||
# | ||
# This is the kind of a backtrace showing how the binary `data` comes to | ||
# BerIdentifiedString.new(data): | ||
# | ||
# @conn.read_ber(syntax) | ||
# -> StringIO.new(self).read_ber(syntax), i.e. included from module | ||
# -> Net::BER::BERParser.read_ber(syntax) | ||
# -> (private)Net::BER::BERParser.parse_ber_object(syntax, id, data) | ||
# | ||
# In the `#parse_ber_object` method `data`, according to its OID, is being | ||
# 'casted' to one of the Net::BER:BerIdentifiedXXX classes. | ||
# | ||
# As we are using LDAP v3 we can safely assume that the data is encoded | ||
# in UTF-8 and therefore the only thing to be done when instantiating is to | ||
# switch the encoding from 'ASCII-8BIT' to 'UTF-8'. | ||
# | ||
# Unfortunately, there are some ActiveDirectory specific attributes | ||
# (like `objectguid`) that should remain binary (do they really?). | ||
# Using the `#valid_encoding?` we can trap this cases. Special cases like | ||
# Japanese, Korean, etc. encodings might also profit from this. However | ||
# I have no clue how this encodings function. | ||
def initialize args | ||
super begin | ||
args.respond_to?(:encode) ? args.encode('UTF-8') : args | ||
rescue | ||
args | ||
super | ||
# | ||
# Check the encoding of the newly created String and set the encoding | ||
# to 'UTF-8' (NOTE: we do NOT change the bytes, but only set the | ||
# encoding to 'UTF-8'). | ||
current_encoding = encoding | ||
if current_encoding == Encoding::BINARY | ||
force_encoding('UTF-8') | ||
force_encoding(current_encoding) unless valid_encoding? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a reasonable fallback. |
||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,12 +130,20 @@ def test_binary_data | |
def test_ascii_data_in_utf8 | ||
data = "some text".force_encoding("UTF-8") | ||
bis = Net::BER::BerIdentifiedString.new(data) | ||
|
||
assert bis.valid_encoding?, "should be a valid encoding" | ||
assert_equal "UTF-8", bis.encoding.name | ||
end | ||
|
||
def test_umlaut_data_in_utf8 | ||
data = "Müller".force_encoding("UTF-8") | ||
bis = Net::BER::BerIdentifiedString.new(data) | ||
|
||
assert bis.valid_encoding?, "should be a valid encoding" | ||
assert_equal "UTF-8", bis.encoding.name | ||
end | ||
|
||
def test_ut8_data_in_utf8 | ||
def test_utf8_data_in_utf8 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for fixing this. |
||
data = ["e4b8ad"].pack("H*").force_encoding("UTF-8") | ||
bis = Net::BER::BerIdentifiedString.new(data) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not need to check for respond to anymore because we dropped 1.8, 1.9 support