furl icon indicating copy to clipboard operation
furl copied to clipboard

if path end with "/", then add one path start with "/" will become "//"

Open sankforever opened this issue 3 years ago • 2 comments

In [2]: u = furl("https://www.baidu.com/aaa") / "/bbbb/ccc" In [3]: u.url Out[3]: 'https://www.baidu.com/aaa/bbbb/ccc' In [4]: u = furl("https://www.baidu.com/aaa/") / "/bbbb/ccc" In [6]: u.url Out[6]: 'https://www.baidu.com/aaa//bbbb/ccc'

sankforever avatar Jul 09 '21 08:07 sankforever

this isn't an outright bug, as 'https://www.baidu.com/aaa//bbbb/ccc', with back to back //, is a perfectly valid url. furl doesn't prevent you from creating such urls. you can always do

>>> f = furl()
>>> f.path = 'a////b'
>>> f.url
'a////b'

if you want to remove the back to back //, you can normalize() the path. eg

>>> from furl import furl
>>> f = furl('https://www.baidu.com/aaa//bbbb/ccc')
>>> f.path.normalize()
>>> f.url
'https://www.baidu.com/aaa/bbbb/ccc'

does furl.path.normalize() solve your problem?

gruns avatar Jul 09 '21 20:07 gruns

Hello @gruns , can we have a parameter for furl to normalize by default. when you are calling obj.url for instance.

For me it is okay to store internally everything not normalized, but I would like to get at the end normalized path/url.

Another question is, can we support the settings on furl class to have the trailing slash always or never for instance. I would like to create url, add path segments to it and always be sure that it ends with a trailing slash. Sometimes we are adding dynamic user input strings and we do not want to check everything by ourselves.

current solution:

f = furl(url='https://example.com/hello/' / some_var_path / '/')
f.path.normalize()
return f.url

It looks so unnatural and instead of 1 line I should write 3 lines to make sure I do not have double slashes and also I do have trailing slash (see this / '/' at the end?!

It would be great like:

return furl(url='https://example.com/hello/' / some_var_path, normalize=True, trailing_slash=True).url

sshishov avatar Mar 10 '23 19:03 sshishov