rails/rails — pull request #16330 by ggrossman (2014-07-29 05:33:09+00:00)
Title: Make URI::parser#unescape handle cases where some already-decoded chars and %XX sequences are both present

When a string is passed to `URI::Parser#unescape` that contains multibyte characters outside the ASCII range and `%XX` sequences, an `Encoding::CompatibilityError` exception may be raised.

This happens because the `String#gsub` used to replace the `%XX` sequences may return strings for each hex byte in `ASCII-8BIT` encoding, and the encoding of the entire string may not be compatible.

For example, if the input string is in UTF8 encoding and contains Unicode characters outside the ASCII range, and a UTF8 sequence is also present in `%XX` encoded form, an `Encoding::CompatibilityError` will be raised because the individual hex bytes will be converted to ASCII-8BIT strings which are incompatible with the UTF8
input string being substituted.

This patch forces the results of the `String#gsub` block to the input string's encoding to avoid the exception.