Re: When and why should I escape a path string?



Matthias Kaeppler wrote:
> Hi,
> 
> I just encountered a bug in my program, where I would try to create a 
> Gnome::Vfs::Uri from an escaped path (the directory in question was the 
> 'lost+found' directory which is found on ext3 partitions and which was 
> converted by gnome-vfs to 'lost%2Bfound'). I was getting exceptions 
> thrown at me when trying to create an URI from this string ("file does 
> not exist").
> 
> After unescaping the string, creating an URI from it worked. So when and 
> why am I supposed to use escaped paths? Obviously, gnome-vfs somewhere 
> internally already escaped the path, so it should be able to deal with 
> them. But then, why does creating an URI from an escaped path fail? ^^
> 

The URI rfc[1] says:

2.2. Reserved Characters

   Many URI include components consisting of or delimited by, certain
   special characters.  These characters are called "reserved", since
   their usage within the URI component is limited to their reserved
   purpose.  If the data for a URI component would conflict with the
   reserved purpose, then the conflicting data must be escaped before
   forming the URI.

      reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                    "$" | ","

   The "reserved" syntax class above refers to those characters that are
   allowed within a URI, but which may not be allowed within a
   particular component of the generic URI syntax; they are used as
   delimiters of the components described in Section 3.



For your case, the '+' character is not a special character for the
file:// uri, so you can pass it unescaped. On the contrary, the '@' is a
special character for the ftp:// uri, as it is used to delimit between
user and server, same as ':' between user and password, so if my
username were 'user name', then it will have to be escaped in the uri, like:
 ftp://user%40name:mypass ftp myserver com/somedir/file.txt

Same if username had whitespaces or had a ':', they should be escaped.

[1] http://www.ietf.org/rfc/rfc2396.txt







[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]