Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/url: JoinPath() drops trailing slashes #52074

Closed
drigz opened this issue Mar 31, 2022 · 8 comments
Closed

net/url: JoinPath() drops trailing slashes #52074

drigz opened this issue Mar 31, 2022 · 8 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. release-blocker
Milestone

Comments

@drigz
Copy link

drigz commented Mar 31, 2022

What version of Go are you using (go version)?

dev branch (playground)

Does this issue reproduce with the latest release?

No, url.JoinPath() is not in Go 1.18.

What operating system and processor architecture are you using (go env)?

dev branch (playground)

What did you do?

https://go.dev/play/p/xCJcVPN5pPh?v=gotip

func main() {
	fmt.Println(url.JoinPath("http://host/foo", "bar/"))
}

What did you expect to see?

The trailing slash should be preserved: http://host/foo/bar/

What did you see instead?

The trailing slash is lost: http://host/foo/bar

@earthboundkid
Copy link
Contributor

earthboundkid commented Mar 31, 2022

Yes, I think that we shouldn't be following path.Clean quite so closely here.

The quick fix is to change

	if len(elem) > 0 {
		elem = append([]string{u.Path}, elem...)
		url.setPath(path.Join(elem...))
	}

to

	if len(elem) > 0 {
		elem = append([]string{u.Path}, elem...)
                p := path.Join(elem...)
                if strings.HasSuffix(elem[len(elem)-1], "/") {
                   p += "/"
                }
		url.setPath(p)
	}

Another approach is to copy and modify path.Join. Doing a copy could save some allocations by working directly in a strings.Builder instead of bouncing between []byte and string.

@ianlancetaylor
Copy link
Contributor

@gopherbot Please open backport to 1.18

This function is new in 1.18 so we should keep it consistent with this change.

@gopherbot
Copy link

Backport issue(s) opened: #52087 (for 1.18).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

@gopherbot
Copy link

Change https://go.dev/cl/397256 mentions this issue: net/url: preserve a trailing slash in JoinPath

@earthboundkid
Copy link
Contributor

The function is slated for 1.19. It missed 18.

@ianlancetaylor
Copy link
Contributor

Whoops. Thanks.

@cherrymui cherrymui added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Apr 5, 2022
@cherrymui cherrymui added this to the Go1.19 milestone Apr 5, 2022
earthboundkid added a commit to earthboundkid/go that referenced this issue Apr 5, 2022
earthboundkid added a commit to earthboundkid/go that referenced this issue Apr 5, 2022
@earthboundkid
Copy link
Contributor

What should url.JoinPath do in these cases:

url.JoinPath("https://example.com/a//")
url.JoinPath("https://example.com/a//", "")

I think the output should be "https://example.com/a//" and "https://example.com/a/" but I can be argued into thinking otherwise.

@ianlancetaylor
Copy link
Contributor

Personally I think it's OK if JoinPath drops extra trailing slashes. I guess it should be documented, though.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. release-blocker
Projects
None yet
Development

No branches or pull requests

5 participants