Skip to content

Commit

Permalink
Split raw query from opaque in URL parser
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
garyburd committed Dec 16, 2015
1 parent 9727ab9 commit 3986be7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 10 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3986be7

Please sign in to comment.