Skip to content

Commit

Permalink
Add Dialer.Host field (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Jan 8, 2024
1 parent c7418fd commit 4600238
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
9 changes: 8 additions & 1 deletion dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -122,6 +127,7 @@ func BenchmarkHttpWriteUpgradeRequest(b *testing.B) {
test.protocols,
test.extensions,
headers,
test.host,
)
}
})
Expand Down

0 comments on commit 4600238

Please sign in to comment.