Skip to content

Commit

Permalink
Make sure we're only dealing with UTF-8 when unencoding.
Browse files Browse the repository at this point in the history
Closes #154.
  • Loading branch information
sporkmonger committed Mar 24, 2014
1 parent d612184 commit e4f2bd6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/addressable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,13 @@ def self.unencode(uri, return_type=String, leave_encoded='')
"Expected Class (String or Addressable::URI), " +
"got #{return_type.inspect}"
end
result = uri.gsub(/%[0-9a-f]{2}/i) do |sequence|
uri = uri.dup
# Seriously, only use UTF-8. I'm really not kidding!
uri.force_encoding("utf-8") if uri.respond_to?(:force_encoding)
leave_encoded.force_encoding("utf-8") if leave_encoded.respond_to?(:force_encoding)
result = uri.gsub(/%[0-9a-f]{2}/iu) do |sequence|
c = sequence[1..3].to_i(16).chr
c.force_encoding("utf-8") if c.respond_to?(:force_encoding)
leave_encoded.include?(c) ? sequence : c
end
result.force_encoding("utf-8") if result.respond_to?(:force_encoding)
Expand Down
4 changes: 4 additions & 0 deletions spec/addressable/uri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5426,6 +5426,10 @@ def to_str
Addressable::URI.unencode_component("g%C3%BCnther").should == "günther"
end

it "should consistently use UTF-8 internally" do
Addressable::URI.unencode_component("ski=%BA%DAɫ").should == "ski=\xBA\xDAɫ"
end

it "should result in correct percent encoded sequence as a URI" do
Addressable::URI.unencode(
"/path?g%C3%BCnther", ::Addressable::URI
Expand Down

0 comments on commit e4f2bd6

Please sign in to comment.