-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename IDNA backend implementations and refactor loading mechanism
- Loading branch information
Showing
11 changed files
with
218 additions
and
168 deletions.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- | ||
# Copyright (C) Bob Aman | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#++ | ||
|
||
# libidn1 implementing IDNA2003 | ||
require "idn" | ||
|
||
module Addressable | ||
module IDNA | ||
module Libidn1 | ||
class << self | ||
# @deprecated Use {String#unicode_normalize(:nfkc)} instead | ||
def unicode_normalize_kc(value) | ||
value.to_s.unicode_normalize(:nfkc) | ||
end | ||
|
||
extend Gem::Deprecate | ||
deprecate :unicode_normalize_kc, "String#unicode_normalize(:nfkc)", 2023, 4 | ||
end | ||
|
||
def self.to_ascii(value) | ||
value.to_s.split('.', -1).map do |segment| | ||
if segment.size > 0 && segment.size < 64 | ||
IDN::Idna.toASCII(segment, IDN::Idna::ALLOW_UNASSIGNED) | ||
elsif segment.size >= 64 | ||
segment | ||
else | ||
'' | ||
end | ||
end.join('.') | ||
end | ||
|
||
def self.to_unicode(value) | ||
value.to_s.split('.', -1).map do |segment| | ||
if segment.size > 0 && segment.size < 64 | ||
IDN::Idna.toUnicode(segment, IDN::Idna::ALLOW_UNASSIGNED) | ||
elsif segment.size >= 64 | ||
segment | ||
else | ||
'' | ||
end | ||
end.join('.') | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- | ||
# Copyright (C) Bob Aman | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#++ | ||
|
||
# libidn2 implementing IDNA2008+TR46 | ||
require "ffi" | ||
|
||
module Addressable | ||
module IDNA | ||
module Libidn2 | ||
extend FFI::Library | ||
|
||
ffi_lib ["idn2", "libidn2.0", "libidn2.so.0"] | ||
|
||
attach_function :idn2_to_ascii_8z, %i[string pointer int], :int | ||
attach_function :idn2_to_unicode_8z8z, %i[string pointer int], :int | ||
attach_function :idn2_strerror, [:int], :string | ||
attach_function :idn2_free, [:pointer], :void | ||
|
||
IDN2_TRANSITIONAL = 4 | ||
IDN2_NONTRANSITIONAL = 8 | ||
|
||
def self.to_ascii(value) | ||
return value if value.ascii_only? | ||
pointer = FFI::MemoryPointer.new(:pointer) | ||
res = idn2_to_ascii_8z(value, pointer, IDN2_NONTRANSITIONAL) | ||
# Fallback to Transitional mode in case of disallowed character | ||
res = idn2_to_ascii_8z(value, pointer, IDN2_TRANSITIONAL) if res != 0 | ||
raise "libidn2 failed to convert \"#{value}\" to ascii (#{idn2_strerror(res)})" if res != 0 | ||
result = pointer.read_pointer.read_string | ||
idn2_free(pointer.read_pointer) | ||
result | ||
end | ||
|
||
def self.to_unicode(value) | ||
pointer = FFI::MemoryPointer.new(:pointer) | ||
res = idn2_to_unicode_8z8z(value, pointer, IDN2_NONTRANSITIONAL) | ||
return value if res != 0 | ||
result = pointer.read_pointer.read_string | ||
idn2_free(pointer.read_pointer) | ||
result.force_encoding('UTF-8') | ||
end | ||
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 |
---|---|---|
@@ -1,58 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- | ||
# Copyright (C) Bob Aman | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
#++ | ||
|
||
# libidn1 implementing IDNA2003 | ||
require "idn" | ||
|
||
module Addressable | ||
module IDNA | ||
class << self | ||
# @deprecated Use {String#unicode_normalize(:nfkc)} instead | ||
def unicode_normalize_kc(value) | ||
value.to_s.unicode_normalize(:nfkc) | ||
end | ||
|
||
extend Gem::Deprecate | ||
deprecate :unicode_normalize_kc, "String#unicode_normalize(:nfkc)", 2023, 4 | ||
end | ||
|
||
def self.to_ascii(value) | ||
value.to_s.split('.', -1).map do |segment| | ||
if segment.size > 0 && segment.size < 64 | ||
IDN::Idna.toASCII(segment, IDN::Idna::ALLOW_UNASSIGNED) | ||
elsif segment.size >= 64 | ||
segment | ||
else | ||
'' | ||
end | ||
end.join('.') | ||
end | ||
|
||
def self.to_unicode(value) | ||
value.to_s.split('.', -1).map do |segment| | ||
if segment.size > 0 && segment.size < 64 | ||
IDN::Idna.toUnicode(segment, IDN::Idna::ALLOW_UNASSIGNED) | ||
elsif segment.size >= 64 | ||
segment | ||
else | ||
'' | ||
end | ||
end.join('.') | ||
end | ||
end | ||
end | ||
# Deprecated, for backward compatibility only | ||
require "addressable/idna/libidn1" | ||
Addressable::IDNA.backend = Addressable::IDNA::Libidn1 | ||
warn "NOTE: loading 'addressable/idna/native' is deprecated; use 'addressable/idna/libidn1' instead and set `Addressable::IDNA.backend = Addressable::IDNA::Libidn1` to force libidn1." |
Oops, something went wrong.