net/url: add URL.RawFragment, URL.EscapedFragment

These are analogous to URL.RawPath and URL.EscapedPath
and allow users fine-grained control over how the fragment
section of the URL is escaped. Some tools care about / vs %2f,
same problem as in paths.

Fixes #37776.

Change-Id: Ie6f556d86bdff750c47fe65398cbafd834152b47
Reviewed-on: https://go-review.googlesource.com/c/go/+/227645
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
This commit is contained in:
Russ Cox 2020-04-08 15:00:41 -04:00
parent d4d298040d
commit 8c00e07c01
4 changed files with 112 additions and 26 deletions

View file

@ -82,13 +82,31 @@ func ExampleParseQuery() {
}
func ExampleURL_EscapedPath() {
u, err := url.Parse("http://example.com/path with spaces")
u, err := url.Parse("http://example.com/x/y%2Fz")
if err != nil {
log.Fatal(err)
}
fmt.Println(u.EscapedPath())
fmt.Println("Path:", u.Path)
fmt.Println("RawPath:", u.RawPath)
fmt.Println("EscapedPath:", u.EscapedPath())
// Output:
// /path%20with%20spaces
// Path: /x/y/z
// RawPath: /x/y%2Fz
// EscapedPath: /x/y%2Fz
}
func ExampleURL_EscapedFragment() {
u, err := url.Parse("http://example.com/#x/y%2Fz")
if err != nil {
log.Fatal(err)
}
fmt.Println("Fragment:", u.Fragment)
fmt.Println("RawFragment:", u.RawFragment)
fmt.Println("EscapedFragment:", u.EscapedFragment())
// Output:
// Fragment: x/y/z
// RawFragment: x/y%2Fz
// EscapedFragment: x/y%2Fz
}
func ExampleURL_Hostname() {