Fix Ruby 4.0 compatibility: replace CGI with URI in DirectFileStore - #330
Merged
Conversation
Member
|
Sorry this has taken me so long to get to. I definitely like the idea of using I wanted to be really sure that we weren't going to run into any unintended side-effects with weird Unicode characters. It struck me that computers are fast and there aren't that many characters, so it's easy to exhaustively test the two serialisation/deserialisation approaches: #!/usr/bin/env ruby
require 'cgi'
require 'uri'
# Encoding differences
(0x0000..0x10FFFF).map { |i|
begin
s = i.chr(Encoding::UTF_8)
rescue RangeError => _
next
end
[
i,
s,
CGI::escape(s),
URI::encode_www_form([s]),
]
}.compact.filter { |a|
a[2] != a[3]
}.each { |a|
puts "Encoding differed for codepoint: #{a[0]} CGI: #{a[2]} URI: #{a[3]}"
}
# Round-trip failures
(0x0000..0x10FFFF).map { |i|
begin
s = i.chr(Encoding::UTF_8)
rescue RangeError => _
next
end
[
i,
s,
CGI::parse(CGI::escape(s)).first.first,
URI::decode_www_form(URI::encode_www_form([s])).first.first,
]
}.compact.filter { |a|
a[1] != a[2] || a[1] != a[3]
}.each { |a|
puts "Round-trip differed for codepoint: #{a[0]} CGI: #{a[2]} URI: #{a[3]}"
}which results in: Most importantly, round-tripping works equally well in both approaches. I don't think the two characters they encode differently are problematic for our usage. I'm going to merge this and get a release out. |
Sinjo
approved these changes
Jul 26, 2026
CGI.parse and CGI.escape were removed in Ruby 4.0. Use URI.decode_www_form and URI.encode_www_form instead. Signed-off-by: Danila Poyarkov <dev@dannote.net>
This was introduced because `cgi` was moved out of the default gems in 4.0. It turns out we can simply use `uri`, which still ships with Ruby, for our purposes instead. Signed-off-by: Chris Sinjakli <chris@sinjakli.co.uk>
Sinjo
force-pushed
the
fix-ruby-4-cgi-removal
branch
from
July 26, 2026 00:24
eb80d09 to
461b1ce
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
CGI.parse and CGI.escape methods were removed from the
cgigem in Ruby 4.0. Thecgigem now only providesCGI.escape/CGI.unescapefor HTML escaping, andCGI.escapeURIComponent/CGI.unescapeURIComponentfor URI components.This PR replaces:
CGI::parse(query_string)→URI.decode_www_form(query_string).to_hCGI::escapein query string building →URI.encode_www_formThe
urimodule is part of Ruby's standard library and doesn't require an additional dependency.Testing: