Manik Taneja
Manik Taneja

Reputation: 114

Extra escape character in go URL

I have the following snippet of code :

    u := *baseURL
    u.User = nil 
    if q := strings.Index(path, "?"); q > 0 { 
            u.Path = path[:q]
            u.RawQuery = path[q+1:]
    } else {
            u.Path = path
    }   

    log.Printf(" url %v, u.String())

I see that when the baseurl is set to something like this http://localhost:9000/buckets/test%?bucket_uuid=7864b0dcdf0a578bd0012c70aef58aca the url package seems to add an extra escape character near the % sign. For e.g. the output of the above print statement is the following :

2015/03/25 12:02:49  url http://localhost:9000/pools/default/buckets/test%2525?bucket_uuid=7864b0dcdf0a578bd0012c70aef58aca

This seems to only happen when the RawQuery field of the URL is set. Any idea why this is happening ? I'm using go version 1.3.3

Cheers, Manik

Upvotes: 2

Views: 4586

Answers (1)

icza
icza

Reputation: 417462

URLs may only contain characters of the ASCII character set, but it is often intended to include/transfer characters outside of this ASCII set. In such cases the URL has to be converted into a valid ASCII format.

If the raw URL contains characters outside of the allowed set, they are escaped: they are replaced with a '%' followed by two hexadecimal digits. Therefore the character '%' is special and also has to be escaped (and its escaped form will start with '%' as well, and its hexadecimal code is 25).

Since your raw URL contains the character '%', it will be replaced by "%25".

Back to your example: in the printed form you see "%2525". You could ask why not just "%25"?

This is because your original url contains a '%' in its escaped form which means its raw form contains the escape sequence "%25". If you use/interpret this as raw input, the '%' will be replaced by "%25" which will be followed by the "25" from the input hence resulting in "%2525".

See: HTML URL Encoding Reference

Also: RFC 1738 - Uniform Resource Locators (URL)

And also: RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax

Upvotes: 5

Related Questions