Skip to content

Commit a9216a0

Browse files
johnnyluodsnet
authored andcommitted
net/url: make Parse+String round trip magnet URLs
Fixes #20054 Change-Id: I3c660ca0c56cdde2c2ac2f6a666d8531ab5588c5 Reviewed-on: https://go-review.googlesource.com/49050 Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Joe Tsai <[email protected]>
1 parent 4d269ad commit a9216a0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/net/url/url.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,9 @@ func (u *URL) String() string {
726726
buf.WriteString(u.Opaque)
727727
} else {
728728
if u.Scheme != "" || u.Host != "" || u.User != nil {
729-
buf.WriteString("//")
729+
if u.Host != "" || u.Path != "" || u.User != nil {
730+
buf.WriteString("//")
731+
}
730732
if ui := u.User; ui != nil {
731733
buf.WriteString(ui.String())
732734
buf.WriteByte('@')

src/net/url/url_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,28 @@ var urltests = []URLTest{
568568
},
569569
"",
570570
},
571+
// test we can roundtrip magnet url
572+
// fix issue https://golang.org/issue/20054
573+
{
574+
"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
575+
&URL{
576+
Scheme: "magnet",
577+
Host: "",
578+
Path: "",
579+
RawQuery: "xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
580+
},
581+
"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn",
582+
},
583+
{
584+
"mailto:?subject=hi",
585+
&URL{
586+
Scheme: "mailto",
587+
Host: "",
588+
Path: "",
589+
RawQuery: "subject=hi",
590+
},
591+
"mailto:?subject=hi",
592+
},
571593
}
572594

573595
// more useful string for debugging than fmt's struct printer

0 commit comments

Comments
 (0)