Replace CGI.parse with internal parser for Ruby 4.0 compatibility#13
Replace CGI.parse with internal parser for Ruby 4.0 compatibility#13Filipe Goncalves (basex) wants to merge 2 commits into
Conversation
Ruby 4.0 removed CGI.parse from the cgi stdlib, so parsing a referer's query string raised "undefined method 'parse' for class CGI". Add a small dependency-free parse_query helper (using the still-available CGI.unescape) that returns the same Hash-of-arrays shape CGI.parse produced. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thanks for your pull request. Is this your first contribution to a Snowplow open source project? Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://docs.snowplowanalytics.com/docs/contributing/contributor-license-agreement/ to learn more and sign. Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates referer query parsing to avoid relying on CGI.parse by introducing a local query-string parser.
Changes:
- Replaced
CGI.parse(url.query)with a newparse_query(url.query)helper. - Added
parse_queryto parseapplication/x-www-form-urlencodedquery strings into a hash-of-arrays.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
A bare key like "a" in "a&b=1" was dropped because its value is nil. CGI.parse
keeps it as an empty array ("a&b=1" => {"a"=>[], "b"=>["1"]}; "a=" => {"a"=>[""]}).
Always materialize the key and only append when a value is present, and set the
hash default to [] so missing keys behave like CGI.parse.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
I signed it! |
Problem
Ruby 4.0 slimmed down the
cgistandard library and removedCGI.parse. As a result,RefererParser::Parser#parseraises when a referer URL has a query string:(
CGI.escape/CGI.unescaperemain available; onlyCGI.parseand the larger CGI surface were removed.)Fix
Replace the
CGI.parsecall with a small internalparse_queryhelper that returns the sameHash-of-arrays shape, using the still-availableCGI.unescapefor value decoding. No new dependencies, and it remains compatible with older Rubies.Verification
Tested on Ruby 4.0.5 against real referer URLs:
+and%XXdecoding (q=hello+world,q=ruby%204)q=&q=second-> picks first non-blank)nilnilq=x&flag&=&weird) -> no longer raisesAll produce the expected results; behavior matches the previous
CGI.parse-based output.