"Life is all about sharing. If we are good at something, pass it on." - Mary Berry

URI network-path reference

2021-09-23

Categories: Programming

Problem

I’ve just updated the code to always prefix the post URI with a slash:

1if !strings.HasPrefix(p.URI, "/") {
2    p.URI = "/" + p.URI
3}

and I think that no need to update the HTML template:

1<a href="/{{ .URI }}">{{ .Title }}</a>

as the redundant slash will be removed. But I was wrong.

Looking at the link address, instead of seeing:

1http://localhost//2021/09/23/uri-network-path-reference

I saw:

1http://0.0.7.229/09/23/uri-network-path-reference

What’s going on?

Troubleshooting

The first question comes to my mind is what does href do if it begins with two slashes?

Take a look at the RFC 3986:

A relative reference that begins with two slash characters is termed a network-path reference

So, //2021 is termed a network-path relative reference. But why:

Please pay attention to the “network” in the above quote. href treated //2021 as a network address:

12^8 = 256
22021 = 256 * 7 + 229

That’s reason why http://localhost//2021 becomes http://0.0.7.229.

Conclusion

If you are not using network-path relative reference, make sure that the URI has only one forward slash.

Happy learning.

Tags: uri

Edit on GitHub

Related Posts: