Quellcode durchsuchen

Split raw query from opaque in URL parser

Prior to this change, the client passed query strings from the
application to the network via the net/url#URL.Opaque field. This works,
but may not be something the authors of the net/url and net/http
packages expect. To play it safe, this change parses the query string to
the net/url#URL.RawQuery field.
Gary Burd vor 10 Jahren
Ursprung
Commit
3986be78bf
2 geänderte Dateien mit 12 neuen und 4 gelöschten Zeilen
  1. 10 3
      client.go
  2. 2 1
      client_test.go

+ 10 - 3
client.go

@@ -95,13 +95,20 @@ func parseURL(s string) (*url.URL, error) {
 		return nil, errMalformedURL
 	}
 
-	u.Host = s
-	u.Opaque = "/"
+	if i := strings.Index(s, "?"); i >= 0 {
+		u.RawQuery = s[i+1:]
+		s = s[:i]
+	}
+
 	if i := strings.Index(s, "/"); i >= 0 {
-		u.Host = s[:i]
 		u.Opaque = s[i:]
+		s = s[:i]
+	} else {
+		u.Opaque = "/"
 	}
 
+	u.Host = s
+
 	if strings.Contains(u.Host, "@") {
 		// Don't bother parsing user information because user information is
 		// not allowed in websocket URIs.

+ 2 - 1
client_test.go

@@ -22,7 +22,8 @@ var parseURLTests = []struct {
 	{"wss://example.com/a/b", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/a/b"}, "/a/b"},
 	{"ss://example.com/a/b", nil, ""},
 	{"ws://webmaster@example.com/", nil, ""},
-	{"wss://example.com/a/b?x=y", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/a/b?x=y"}, "/a/b?x=y"},
+	{"wss://example.com/a/b?x=y", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/a/b", RawQuery: "x=y"}, "/a/b?x=y"},
+	{"wss://example.com?x=y", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/", RawQuery: "x=y"}, "/?x=y"},
 }
 
 func TestParseURL(t *testing.T) {