mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
rpc: add HTTP status code to handshake error
This makes it easier to debug failing connections.
This commit is contained in:
parent
bbfe6f9c17
commit
80f3a0647f
2 changed files with 23 additions and 3 deletions
|
|
@ -111,6 +111,19 @@ func wsHandshakeValidator(allowedOrigins []string) func(*http.Request) bool {
|
|||
return f
|
||||
}
|
||||
|
||||
type wsHandshakeError struct {
|
||||
err error
|
||||
status string
|
||||
}
|
||||
|
||||
func (e wsHandshakeError) Error() string {
|
||||
s := e.err.Error()
|
||||
if e.status != "" {
|
||||
s += " (HTTP status " + e.status + ")"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// DialWebsocket creates a new RPC client that communicates with a JSON-RPC server
|
||||
// that is listening on the given endpoint.
|
||||
//
|
||||
|
|
@ -127,9 +140,13 @@ func DialWebsocket(ctx context.Context, endpoint, origin string) (*Client, error
|
|||
WriteBufferPool: wsBufferPool,
|
||||
}
|
||||
return newClient(ctx, func(ctx context.Context) (ServerCodec, error) {
|
||||
conn, _, err := dialer.DialContext(ctx, endpoint, header)
|
||||
conn, resp, err := dialer.DialContext(ctx, endpoint, header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
hErr := wsHandshakeError{err: err}
|
||||
if resp != nil {
|
||||
hErr.status = resp.Status
|
||||
}
|
||||
return nil, hErr
|
||||
}
|
||||
return newWebsocketCodec(conn), nil
|
||||
})
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -63,10 +64,12 @@ func TestWebsocketOriginCheck(t *testing.T) {
|
|||
client.Close()
|
||||
t.Fatal("no error for wrong origin")
|
||||
}
|
||||
if err != websocket.ErrBadHandshake {
|
||||
wantErr := wsHandshakeError{websocket.ErrBadHandshake, "403 Forbidden"}
|
||||
if !reflect.DeepEqual(err, wantErr) {
|
||||
t.Fatalf("wrong error for wrong origin: %q", err)
|
||||
}
|
||||
|
||||
// Connections without origin header should work.
|
||||
client, err = DialWebsocket(context.Background(), wsURL, "")
|
||||
if err != nil {
|
||||
t.Fatal("error for empty origin")
|
||||
|
|
|
|||
Loading…
Reference in a new issue