Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dialer.Host field #196

Merged
merged 1 commit into from Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion dialer.go
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
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
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