Skip to content

Commit 4600238

Browse files
authored
Add Dialer.Host field (#196)
1 parent c7418fd commit 4600238

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

dialer.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ type Dialer struct {
8787
// land.
8888
Header HandshakeHeader
8989

90+
// Host is an optional string that could be used to specify the host during
91+
// HTTP upgrade request by setting 'Host' header.
92+
//
93+
// Default value is an empty string, which results in setting 'Host' header
94+
// equal to the URL hostname given to Dialer.Dial().
95+
Host string
96+
9097
// OnStatusError is the callback that will be called after receiving non
9198
// "101 Continue" HTTP response status. It receives an io.Reader object
9299
// representing server response bytes. That is, it gives ability to parse
@@ -310,7 +317,7 @@ func (d Dialer) Upgrade(conn io.ReadWriter, u *url.URL) (br *bufio.Reader, hs Ha
310317
nonce := make([]byte, nonceSize)
311318
initNonce(nonce)
312319

313-
httpWriteUpgradeRequest(bw, u, nonce, d.Protocols, d.Extensions, d.Header)
320+
httpWriteUpgradeRequest(bw, u, nonce, d.Protocols, d.Extensions, d.Header, d.Host)
314321
if err := bw.Flush(); err != nil {
315322
return br, hs, err
316323
}

http.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,16 @@ func httpWriteUpgradeRequest(
286286
protocols []string,
287287
extensions []httphead.Option,
288288
header HandshakeHeader,
289+
host string,
289290
) {
290291
bw.WriteString("GET ")
291292
bw.WriteString(u.RequestURI())
292293
bw.WriteString(" HTTP/1.1\r\n")
293294

294-
httpWriteHeader(bw, headerHost, u.Host)
295+
if host == "" {
296+
host = u.Host
297+
}
298+
httpWriteHeader(bw, headerHost, host)
295299

296300
httpWriteHeaderBts(bw, headerUpgrade, specHeaderValueUpgrade)
297301
httpWriteHeaderBts(bw, headerConnection, specHeaderValueConnection)

http_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,15 @@ func BenchmarkHttpWriteUpgradeRequest(b *testing.B) {
9999
protocols []string
100100
extensions []httphead.Option
101101
headers HandshakeHeaderFunc
102+
host string
102103
}{
103104
{
104105
url: makeURL("ws://example.org"),
105106
},
107+
{
108+
url: makeURL("ws://example.org"),
109+
host: "test-host",
110+
},
106111
} {
107112
bw := bufio.NewWriter(ioutil.Discard)
108113
nonce := make([]byte, nonceSize)
@@ -122,6 +127,7 @@ func BenchmarkHttpWriteUpgradeRequest(b *testing.B) {
122127
test.protocols,
123128
test.extensions,
124129
headers,
130+
test.host,
125131
)
126132
}
127133
})

0 commit comments

Comments
 (0)