diff --git a/dialer.go b/dialer.go index 64d4681..3fd6305 100644 --- a/dialer.go +++ b/dialer.go @@ -87,6 +87,9 @@ type Dialer struct { // land. Header HandshakeHeader + // Host if set will be used as Host header during upgrade. + 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 +313,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, ) } })