Skip to content

Commit 71fa72d

Browse files
canning-duckgaryburd
authored andcommitted
Replace parseURL() with net/url.Parse() (#290)
1 parent f918560 commit 71fa72d

File tree

3 files changed

+2
-86
lines changed

3 files changed

+2
-86
lines changed

client.go

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -88,50 +88,6 @@ type Dialer struct {
8888

8989
var errMalformedURL = errors.New("malformed ws or wss URL")
9090

91-
// parseURL parses the URL.
92-
//
93-
// This function is a replacement for the standard library url.Parse function.
94-
// In Go 1.4 and earlier, url.Parse loses information from the path.
95-
func parseURL(s string) (*url.URL, error) {
96-
// From the RFC:
97-
//
98-
// ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
99-
// wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
100-
var u url.URL
101-
switch {
102-
case strings.HasPrefix(s, "ws://"):
103-
u.Scheme = "ws"
104-
s = s[len("ws://"):]
105-
case strings.HasPrefix(s, "wss://"):
106-
u.Scheme = "wss"
107-
s = s[len("wss://"):]
108-
default:
109-
return nil, errMalformedURL
110-
}
111-
112-
if i := strings.Index(s, "?"); i >= 0 {
113-
u.RawQuery = s[i+1:]
114-
s = s[:i]
115-
}
116-
117-
if i := strings.Index(s, "/"); i >= 0 {
118-
u.Opaque = s[i:]
119-
s = s[:i]
120-
} else {
121-
u.Opaque = "/"
122-
}
123-
124-
u.Host = s
125-
126-
if strings.Contains(u.Host, "@") {
127-
// Don't bother parsing user information because user information is
128-
// not allowed in websocket URIs.
129-
return nil, errMalformedURL
130-
}
131-
132-
return &u, nil
133-
}
134-
13591
func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
13692
hostPort = u.Host
13793
hostNoPort = u.Host
@@ -177,7 +133,7 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
177133
return nil, nil, err
178134
}
179135

180-
u, err := parseURL(urlStr)
136+
u, err := url.Parse(urlStr)
181137
if err != nil {
182138
return nil, nil, err
183139
}

client_server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func TestDialCookieJar(t *testing.T) {
237237
d := cstDialer
238238
d.Jar = jar
239239

240-
u, _ := parseURL(s.URL)
240+
u, _ := url.Parse(s.URL)
241241

242242
switch u.Scheme {
243243
case "ws":

client_test.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,9 @@ package websocket
66

77
import (
88
"net/url"
9-
"reflect"
109
"testing"
1110
)
1211

13-
var parseURLTests = []struct {
14-
s string
15-
u *url.URL
16-
rui string
17-
}{
18-
{"ws://example.com/", &url.URL{Scheme: "ws", Host: "example.com", Opaque: "/"}, "/"},
19-
{"ws://example.com", &url.URL{Scheme: "ws", Host: "example.com", Opaque: "/"}, "/"},
20-
{"ws://example.com:7777/", &url.URL{Scheme: "ws", Host: "example.com:7777", Opaque: "/"}, "/"},
21-
{"wss://example.com/", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/"}, "/"},
22-
{"wss://example.com/a/b", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/a/b"}, "/a/b"},
23-
{"ss://example.com/a/b", nil, ""},
24-
{"ws://[email protected]/", nil, ""},
25-
{"wss://example.com/a/b?x=y", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/a/b", RawQuery: "x=y"}, "/a/b?x=y"},
26-
{"wss://example.com?x=y", &url.URL{Scheme: "wss", Host: "example.com", Opaque: "/", RawQuery: "x=y"}, "/?x=y"},
27-
}
28-
29-
func TestParseURL(t *testing.T) {
30-
for _, tt := range parseURLTests {
31-
u, err := parseURL(tt.s)
32-
if tt.u != nil && err != nil {
33-
t.Errorf("parseURL(%q) returned error %v", tt.s, err)
34-
continue
35-
}
36-
if tt.u == nil {
37-
if err == nil {
38-
t.Errorf("parseURL(%q) did not return error", tt.s)
39-
}
40-
continue
41-
}
42-
if !reflect.DeepEqual(u, tt.u) {
43-
t.Errorf("parseURL(%q) = %v, want %v", tt.s, u, tt.u)
44-
continue
45-
}
46-
if u.RequestURI() != tt.rui {
47-
t.Errorf("parseURL(%q).RequestURI() = %v, want %v", tt.s, u.RequestURI(), tt.rui)
48-
}
49-
}
50-
}
51-
5212
var hostPortNoPortTests = []struct {
5313
u *url.URL
5414
hostPort, hostNoPort string

0 commit comments

Comments
 (0)