From ef0536998d313302f3083d2b90cc526d52853f01 Mon Sep 17 00:00:00 2001 From: Jakob Skjerning Date: Fri, 6 Dec 2024 11:02:21 +0100 Subject: [PATCH] Extract methods to decode and encode URI segments I wonder if we might want to roll our own eventually, seeing the state of Ruby URI encoding/decoding methods are kinda of a mess. - URI.encode_uri_component encodes ~ as %7E, which is not what we want. - ERB::Util.url_encode does not encode ~, but has no symmetric ERB::Util.url_decode method. --- lib/route_downcaser/downcase_route_middleware.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/route_downcaser/downcase_route_middleware.rb b/lib/route_downcaser/downcase_route_middleware.rb index 7037128..ca00ea6 100644 --- a/lib/route_downcaser/downcase_route_middleware.rb +++ b/lib/route_downcaser/downcase_route_middleware.rb @@ -62,7 +62,7 @@ def downcased_path(uri) end def downcase_path_segment(segment) - URI.encode_www_form_component(URI.decode_www_form_component(segment).downcase) + uri_encode(uri_decode(segment).downcase) end def path(uri) @@ -77,6 +77,14 @@ def has_querystring?(uri) uri_items(uri).length > 1 end + def uri_decode(uri_segment) + URI.decode_www_form_component(uri_segment) + end + + def uri_encode(uri_segment) + URI.encode_www_form_component(uri_segment) + end + def uri_items(uri) uri.split("?", 2) end