Skip to content
This repository was archived by the owner on May 29, 2018. It is now read-only.

Commit d7a3223

Browse files
committed
Handle implicit port
1 parent cd296ac commit d7a3223

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"net/url"
1313
"os"
14+
"strings"
1415
"time"
1516
)
1617

@@ -110,11 +111,12 @@ func (pw *appendWriteCloser) Close() error {
110111
func newClient(u *url.URL, method string) (*websocket.Conn, *http.Response, error) {
111112
var c net.Conn
112113
var err error
114+
hostport := hostPort(u)
113115
switch u.Scheme {
114116
case "http":
115-
c, err = net.Dial("tcp", u.Host)
117+
c, err = net.Dial("tcp", hostport)
116118
case "https":
117-
c, err = tls.Dial("tcp", u.Host, nil)
119+
c, err = tls.Dial("tcp", hostport, nil)
118120
default:
119121
return nil, nil, errors.New("unrecognized URL scheme")
120122
}
@@ -124,6 +126,13 @@ func newClient(u *url.URL, method string) (*websocket.Conn, *http.Response, erro
124126
return websocket.NewClient(c, u, http.Header{xVerb: []string{method}}, readBufSize, writeBufSize)
125127
}
126128

129+
func hostPort(u *url.URL) string {
130+
if strings.Contains(u.Host, ":") {
131+
return u.Host
132+
}
133+
return u.Host + ":" + u.Scheme
134+
}
135+
127136
// errorFromResponse returns err if err != nil, or another non-nil error if resp
128137
// indicates a non-HTTP 200 response.
129138
func errorFromResponse(resp *http.Response, err error) error {

client_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,27 @@ type fixedReader struct {
152152
func (r *fixedReader) Read(p []byte) (n int, err error) {
153153
return io.LimitReader(r.R, int64(r.N)).Read(p)
154154
}
155+
156+
func TestHostPort(t *testing.T) {
157+
tests := []struct {
158+
input string
159+
expected string
160+
}{
161+
{"http://example.com", "example.com:http"},
162+
{"https://example.com", "example.com:https"},
163+
{"http://example.com:1234", "example.com:1234"},
164+
{"https://example.com:1234", "example.com:1234"},
165+
}
166+
167+
for _, test := range tests {
168+
u, err := url.Parse(test.input)
169+
if err != nil {
170+
t.Errorf("%s: url.Parse: %s", test.input, err)
171+
continue
172+
}
173+
got := hostPort(u)
174+
if test.expected != got {
175+
t.Errorf("%s: want %q, got %q", test.input, test.expected, got)
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)