Issue #3457 has been updated by Alex Neth.


For those who wish to encode a URI segment, the way to do this is:

URI.encode("some weird filename with []", /[^#{URI::REGEXP::PATTERN::UNRESERVED}]/)

This uses a custom regexp which encodes all non-reserved characters.

The default encodes all characters that are NEITHER non-reserved or reserved, which is a pretty useless behavior unless you want to do really sloppy encoding.

Note that this will also encode slashes, so it will not behave well with full paths.


----------------------------------------
Bug #3457: URI.encode does not escape square brackets
http://redmine.ruby-lang.org/issues/3457

Author: Gregor Schmidt
Status: Rejected
Priority: Normal
Assignee: 
Category: lib
Target version: 
ruby -v: ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10.0.0]


=begin
 According to RFC 3986 URIs may only contain square brackets to enclose IPv6 addresses within the host part of the URI. In other parts of the uri the are not allowed and need to be escaped.
 
 > A host identified by an Internet Protocol literal address, version 6
 > [RFC3513] or later, is distinguished by enclosing the IP literal
 > within square brackets ("[" and "]").  This is the only place where
 > square bracket characters are allowed in the URI syntax.
 
 > Square brackets are now specified as reserved within the
 > authority component and are not allowed outside their use as
 > delimiters for an IP literal within host.
 
 The attached test case should test the desired behavior. I've tested it on 1.8.7 but I a would guess, that it is failing for all major ruby versions and interpreters.
 
 I'm not sure, how this should be implemented while keeping the API of URI.escape in tact. The escaping needs to use two groups of safe characters, one for the host part and one for the rest. This would result in 2 optional parameters for URI.escape to provide custom safe characters for each part.
=end



-- 
http://redmine.ruby-lang.org