Skip to content

Fix obsoleted URI.{encode,decode} calls for Ruby 2.7

Stan Hu requested to merge sh-fix-uri-encode-obsolete into master

Attempting to use these calls results in these obsolete warnings:

warning: URI.escape is obsolete

As mentioned by the Knapsack Pro blog (https://docs.knapsackpro.com/2020/uri-escape-is-obsolete-percent-encoding-your-query-string):

The trouble with a concept of “escaping the URI” is that URI consists of many components (like path or query), and we don’t want to escape them in the same way. For example, it’s fine for a # character to appear at the end of the URI (when it’s used as what’s usually called an anchor, or in URI parlance - a fragment component) - but when the same # is part of user’s input (like in a search query), we want to encode it to ensure correct interpretation. Similarly, if the query string value contains other reserved characters, like = or &, we do want to escape them so that they are not incorrectly interpreted as delimiters, as if they were used as reserved characters.

URI.escape relies on a simple gsub operation for the whole string and doesn’t differentiate between distinct components, which doesn’t take into account intricacies like those mentioned above.

CGI.escape and CGI.encode_www_form_component are often mentioned as replacements, but both substitute spaces for + instead of %20. This doesn't work for Grafana. As https://stackoverflow.com/a/47188851/1992201 mentions, using %20 for spaces everywhere is valid, which is what Addressable::URI does.

Edited by Stan Hu

Merge request reports

Loading