2020-10-20 15:26:55 +00:00
|
|
|
package ws
|
|
|
|
|
|
|
|
// RejectOption represents an option used to control the way connection is
|
|
|
|
// rejected.
|
2024-10-17 20:09:39 +00:00
|
|
|
type RejectOption func(*ConnectionRejectedError)
|
2020-10-20 15:26:55 +00:00
|
|
|
|
|
|
|
// RejectionReason returns an option that makes connection to be rejected with
|
|
|
|
// given reason.
|
|
|
|
func RejectionReason(reason string) RejectOption {
|
2024-10-17 20:09:39 +00:00
|
|
|
return func(err *ConnectionRejectedError) {
|
2020-10-20 15:26:55 +00:00
|
|
|
err.reason = reason
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RejectionStatus returns an option that makes connection to be rejected with
|
|
|
|
// given HTTP status code.
|
|
|
|
func RejectionStatus(code int) RejectOption {
|
2024-10-17 20:09:39 +00:00
|
|
|
return func(err *ConnectionRejectedError) {
|
2020-10-20 15:26:55 +00:00
|
|
|
err.code = code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RejectionHeader returns an option that makes connection to be rejected with
|
|
|
|
// given HTTP headers.
|
|
|
|
func RejectionHeader(h HandshakeHeader) RejectOption {
|
2024-10-17 20:09:39 +00:00
|
|
|
return func(err *ConnectionRejectedError) {
|
2020-10-20 15:26:55 +00:00
|
|
|
err.header = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 20:09:39 +00:00
|
|
|
// RejectConnectionError constructs an error that could be used to control the
|
|
|
|
// way handshake is rejected by Upgrader.
|
2020-10-20 15:26:55 +00:00
|
|
|
func RejectConnectionError(options ...RejectOption) error {
|
2024-10-17 20:09:39 +00:00
|
|
|
err := new(ConnectionRejectedError)
|
2020-10-20 15:26:55 +00:00
|
|
|
for _, opt := range options {
|
|
|
|
opt(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-17 20:09:39 +00:00
|
|
|
// ConnectionRejectedError represents a rejection of connection during
|
|
|
|
// WebSocket handshake error.
|
2020-10-20 15:26:55 +00:00
|
|
|
//
|
2024-10-17 20:09:39 +00:00
|
|
|
// It can be returned by Upgrader's On* hooks to indicate that WebSocket
|
|
|
|
// handshake should be rejected.
|
|
|
|
type ConnectionRejectedError struct {
|
2020-10-20 15:26:55 +00:00
|
|
|
reason string
|
|
|
|
code int
|
|
|
|
header HandshakeHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error implements error interface.
|
2024-10-17 20:09:39 +00:00
|
|
|
func (r *ConnectionRejectedError) Error() string {
|
2020-10-20 15:26:55 +00:00
|
|
|
return r.reason
|
|
|
|
}
|
2024-10-17 20:09:39 +00:00
|
|
|
|
|
|
|
func (r *ConnectionRejectedError) StatusCode() int {
|
|
|
|
return r.code
|
|
|
|
}
|