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:
http://localhost//2021
becomeshttp://0.0.7.229
?http://localhost//2020
becomeshttp://0.0.7.228
?http://localhost//2019
becomeshttp://0.0.7.227
?
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
Related Posts:
- Integration testing TUI applications in Rust
- libp2p performance benchmarking
- Learning Rust by building Tetris: my favorite childhood game
- SICP Exercise 2.77: expected a procedure that can be applied to arguments, given #f
- A terminal UI for Taskwarrior