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

错误提示优化 #125

Merged
merged 4 commits into from
Mar 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"encoding/base64"
"encoding/binary"
"fmt"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -96,6 +97,9 @@ func (c *connector) request() (*http.Response, *bufio.Reader, error) {
return nil, nil, err
}
for k, v := range c.option.RequestHeader {
if k == "Host" && len(v) > 0 {
r.Host = v[0]
}
r.Header[k] = v
}
r.Header.Set(internal.Connection.Key, internal.Connection.Val)
Expand Down Expand Up @@ -219,16 +223,16 @@ func (c *connector) getSubProtocol(resp *http.Response) (string, error) {
// Checks the response headers to verify if the handshake was successful
func (c *connector) checkHeaders(resp *http.Response) error {
if resp.StatusCode != http.StatusSwitchingProtocols {
return ErrHandshake
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
if !internal.HttpHeaderContains(resp.Header.Get(internal.Connection.Key), internal.Connection.Val) {
return ErrHandshake
return fmt.Errorf("missing %s header", internal.Connection.Key)
}
if !strings.EqualFold(resp.Header.Get(internal.Upgrade.Key), internal.Upgrade.Val) {
return ErrHandshake
return fmt.Errorf("missing %s header", internal.Upgrade.Key)
}
if resp.Header.Get(internal.SecWebSocketAccept.Key) != internal.ComputeAcceptKey(c.secWebsocketKey) {
return ErrHandshake
return fmt.Errorf("invalid %s header", internal.SecWebSocketAccept.Key)
}
return nil
}