diff --git a/dialer.go b/dialer.go index 64d4681..e66678e 100644 --- a/dialer.go +++ b/dialer.go @@ -87,6 +87,13 @@ type Dialer struct { // land. Header HandshakeHeader + // Host is an optional string that could be used to specify the host during + // HTTP upgrade request by setting 'Host' header. + // + // Default value is an empty string, which results in setting 'Host' header + // equal to the URL hostname given to Dialer.Dial(). + Host string + // OnStatusError is the callback that will be called after receiving non // "101 Continue" HTTP response status. It receives an io.Reader object // 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 nonce := make([]byte, nonceSize) initNonce(nonce) - httpWriteUpgradeRequest(bw, u, nonce, d.Protocols, d.Extensions, d.Header) + httpWriteUpgradeRequest(bw, u, nonce, d.Protocols, d.Extensions, d.Header, d.Host) if err := bw.Flush(); err != nil { return br, hs, err } diff --git a/http.go b/http.go index 129e77e..a3a682d 100644 --- a/http.go +++ b/http.go @@ -286,12 +286,16 @@ func httpWriteUpgradeRequest( protocols []string, extensions []httphead.Option, header HandshakeHeader, + host string, ) { bw.WriteString("GET ") bw.WriteString(u.RequestURI()) bw.WriteString(" HTTP/1.1\r\n") - httpWriteHeader(bw, headerHost, u.Host) + if host == "" { + host = u.Host + } + httpWriteHeader(bw, headerHost, host) httpWriteHeaderBts(bw, headerUpgrade, specHeaderValueUpgrade) httpWriteHeaderBts(bw, headerConnection, specHeaderValueConnection) diff --git a/http_test.go b/http_test.go index 802e937..7e2ff1e 100644 --- a/http_test.go +++ b/http_test.go @@ -99,10 +99,15 @@ func BenchmarkHttpWriteUpgradeRequest(b *testing.B) { protocols []string extensions []httphead.Option headers HandshakeHeaderFunc + host string }{ { url: makeURL("ws://example.org"), }, + { + url: makeURL("ws://example.org"), + host: "test-host", + }, } { bw := bufio.NewWriter(ioutil.Discard) nonce := make([]byte, nonceSize) @@ -122,6 +127,7 @@ func BenchmarkHttpWriteUpgradeRequest(b *testing.B) { test.protocols, test.extensions, headers, + test.host, ) } })