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: add URL.ForceFragment like ForceQuery for empty fragments to stringify with '#' #29603

Open
Gregable opened this issue Jan 8, 2019 · 6 comments

Comments

@Gregable
Copy link

commented Jan 8, 2019

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

go version go1.11.2 linux/amd64

What did you do?

import (
  "fmt"
  "log"
  "net/url"
)

func main() {
    u, err := url.Parse("http://example.com/#")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(u.String())
    fmt.Println(u.Fragment)
}

What did you expect to see?

http://example.com/#
#

What did you see instead?

http://example.com/

What's the issue?

At first glance, http://example.com/# and http://example.com/ are logically the same URL. This is largely true in the context of a browser navigating to that URL as typed into the URL bar, but they are subtly different in a link. If a user clicks the following link:

<a href="http://example.com/#">foo</a>

If the document URL (or <base href>) is http://example.com/ then this click does not cause a navigation - it is a within document link. However, if the link was instead to:

<a href="http://example.com/">foo</a>

Then clicking it causes a navigation. A reload really, but still a navigation.

This becomes more of an issue when net/url is used to resolve a relative URL:

<html>
<head>
  <base href="http://example.com/">
</head>
<body>
  <a href="/">reload</a>
  <a href="#">inert</a>
</body>
</html>
base, _ = url.Parse("http://example.com/")
nav, _ = base.Parse("/")
local, _ = base.Parse("#")

nav and local are now equal, which means it's not possible to distinguish a document local link and a navigation link.

@agnivade agnivade changed the title net/url empty fragment indistinguishable from no fragment net/url: empty fragment indistinguishable from no fragment Jan 8, 2019

@agnivade

This comment has been minimized.

Copy link
Member

commented Jan 8, 2019

/cc @bradfitz

@agnivade agnivade added this to the Unplanned milestone Jan 8, 2019

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jan 8, 2019

In your "What did you expect to see?" section, you expected the value of u.Fragment to be # but it's documented as:

https://golang.org/pkg/net/url/#URL.Fragment

        Fragment   string    // fragment for references, without '#'

So clearly that can't work as you expect.

What we could do perhaps is add URL.ForceFragment like we did for https://golang.org/pkg/net/url/#URL.ForceQuery

@bradfitz bradfitz changed the title net/url: empty fragment indistinguishable from no fragment net/url: add URL.ForceFragment like ForceQuery for empty fragments to stringify with '#' Jan 8, 2019

@bradfitz bradfitz modified the milestones: Unplanned, Go1.13 Jan 8, 2019

@Gregable

This comment has been minimized.

Copy link
Author

commented Jan 9, 2019

Right. "What did you expect to see?" is perhaps the wrong phrasing of the question. I'm only arguing that the API has no way to distinguish these two cases.

It does look like ForceFragment would be the best API for fixing this.

@Gregable

This comment has been minimized.

Copy link
Author

commented Jan 17, 2019

@bradfitz so he sees my reply.

@Gregable Gregable closed this Jan 17, 2019

@Gregable Gregable reopened this Jan 17, 2019

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jan 17, 2019

I see all replies and saw yours last week. I just had nothing further to add.

We're in the Go 1.12 release freeze right now (https://github.com/golang/go/wiki/Go-Release-Cycle) so this isn't a priority.

@Gregable

This comment has been minimized.

Copy link
Author

commented Jan 17, 2019

No worries, just wanted to check. Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.