URL parsing with special characters.

78 views
Skip to first unread message

Vivek

unread,
Apr 30, 2020, 1:05:45 PM4/30/20
to golang-nuts
I am trying to parse  a url similar to "ftp://user:pa...@192.168.0.1/path/file%ver3.txt" using `net/url` Parse function but I am getting a error

`parse "ftp://user:pa...@192.168.0.1/path/file%ver3.txt": invalid URL escape "%ve"`


The same path parses fine with python(urllib.parse.urlparse and C# URI). 

I appreciate any help on this.

Regards,
Vivek

Brian Candler

unread,
Apr 30, 2020, 2:09:00 PM4/30/20
to golang-nuts
In a URL, the percent sign should appear as %25  (% marks the start of a hex-encoded character)

The URL as shown is invalid.

Ian Lance Taylor

unread,
Apr 30, 2020, 6:04:01 PM4/30/20
to Brian Candler, golang-nuts
I *think* you are saying that the u.Path field should use %25 rather
than plain %. But as the documentation says
(https://golang.org/pkg/net/url/#URL) the Path field is the decoded
form. If you simply print the URL, you will see %25. Consider
https://play.golang.org/p/Z0eUYfkm3PR.

If you mean something else, can you explain? Thanks.

Ian

Kurtis Rader

unread,
Apr 30, 2020, 9:48:44 PM4/30/20
to Vivek, golang-nuts
On Thu, Apr 30, 2020 at 10:05 AM Vivek <vickyw...@gmail.com> wrote:
I am trying to parse  a url similar to "ftp://user:pa...@192.168.0.1/path/file%ver3.txt" using `net/url` Parse function but I am getting a error

`parse "ftp://user:pa...@192.168.0.1/path/file%ver3.txt": invalid URL escape "%ve"`

That is not a valid URL. The Python urllib.parse module explicitly ignores invalid percent encodings and treats the % in that case as a literal percent-sign. That URL should be written as `.../file%25ver3.txt`; assuming you want a literal percent-sign in the path.

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Brian Candler

unread,
May 1, 2020, 2:32:41 AM5/1/20
to golang-nuts
On Thursday, 30 April 2020 23:04:01 UTC+1, Ian Lance Taylor wrote:
On Thu, Apr 30, 2020 at 11:09 AM Brian Candler <b.ca...@pobox.com> wrote:
>
> In a URL, the percent sign should appear as %25  (% marks the start of a hex-encoded character)
> https://play.golang.org/p/gMC1tdpJER4
>
> The URL as shown is invalid.

I *think* you are saying that the u.Path field should use %25 rather
than plain %.


No: I'm just saying the library is working correctly, by rejecting the invalid input that the OP gave.

If the input URL is correct to file%25ver3, then the parsed u.Path contains file%ver3 as expected.

Vivek

unread,
May 1, 2020, 7:48:21 AM5/1/20
to golang-nuts
Thanks everyone.

I am using "github.com/jlaffaye/ftp" library which returned unescaped file names when doing directory listing. I constructed the full url by doing 

filePath = "ftp://user:pa...@192.168.0.1/path/" + "file%ver3.txt" and later tried to parse the url with url.Parse function somewhere else in the code which failed.

I am now calling url.Escape on the file names before appending. 

Regards,
Vivek
Reply all
Reply to author
Forward
0 new messages