mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-12-24 14:00:19 +00:00
f12b6064d6
Bumps the golang-dependencies group with 4 updates: [github.com/golang/protobuf](https://github.com/golang/protobuf), [golang.org/x/crypto](https://github.com/golang/crypto), [golang.org/x/net](https://github.com/golang/net) and [golang.org/x/sys](https://github.com/golang/sys). Updates `github.com/golang/protobuf` from 1.5.3 to 1.5.4 - [Release notes](https://github.com/golang/protobuf/releases) - [Commits](https://github.com/golang/protobuf/compare/v1.5.3...v1.5.4) Updates `golang.org/x/crypto` from 0.20.0 to 0.21.0 - [Commits](https://github.com/golang/crypto/compare/v0.20.0...v0.21.0) Updates `golang.org/x/net` from 0.21.0 to 0.22.0 - [Commits](https://github.com/golang/net/compare/v0.21.0...v0.22.0) Updates `golang.org/x/sys` from 0.17.0 to 0.18.0 - [Commits](https://github.com/golang/sys/compare/v0.17.0...v0.18.0) --- updated-dependencies: - dependency-name: github.com/golang/protobuf dependency-type: direct:production update-type: version-update:semver-patch dependency-group: golang-dependencies - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor dependency-group: golang-dependencies - dependency-name: golang.org/x/net dependency-type: direct:production update-type: version-update:semver-minor dependency-group: golang-dependencies - dependency-name: golang.org/x/sys dependency-type: direct:production update-type: version-update:semver-minor dependency-group: golang-dependencies ... Signed-off-by: dependabot[bot] <support@github.com>
140 lines
3.5 KiB
Go
140 lines
3.5 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package websocket
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// DialError is an error that occurs while dialling a websocket server.
|
|
type DialError struct {
|
|
*Config
|
|
Err error
|
|
}
|
|
|
|
func (e *DialError) Error() string {
|
|
return "websocket.Dial " + e.Config.Location.String() + ": " + e.Err.Error()
|
|
}
|
|
|
|
// NewConfig creates a new WebSocket config for client connection.
|
|
func NewConfig(server, origin string) (config *Config, err error) {
|
|
config = new(Config)
|
|
config.Version = ProtocolVersionHybi13
|
|
config.Location, err = url.ParseRequestURI(server)
|
|
if err != nil {
|
|
return
|
|
}
|
|
config.Origin, err = url.ParseRequestURI(origin)
|
|
if err != nil {
|
|
return
|
|
}
|
|
config.Header = http.Header(make(map[string][]string))
|
|
return
|
|
}
|
|
|
|
// NewClient creates a new WebSocket client connection over rwc.
|
|
func NewClient(config *Config, rwc io.ReadWriteCloser) (ws *Conn, err error) {
|
|
br := bufio.NewReader(rwc)
|
|
bw := bufio.NewWriter(rwc)
|
|
err = hybiClientHandshake(config, br, bw)
|
|
if err != nil {
|
|
return
|
|
}
|
|
buf := bufio.NewReadWriter(br, bw)
|
|
ws = newHybiClientConn(config, buf, rwc)
|
|
return
|
|
}
|
|
|
|
// Dial opens a new client connection to a WebSocket.
|
|
func Dial(url_, protocol, origin string) (ws *Conn, err error) {
|
|
config, err := NewConfig(url_, origin)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if protocol != "" {
|
|
config.Protocol = []string{protocol}
|
|
}
|
|
return DialConfig(config)
|
|
}
|
|
|
|
var portMap = map[string]string{
|
|
"ws": "80",
|
|
"wss": "443",
|
|
}
|
|
|
|
func parseAuthority(location *url.URL) string {
|
|
if _, ok := portMap[location.Scheme]; ok {
|
|
if _, _, err := net.SplitHostPort(location.Host); err != nil {
|
|
return net.JoinHostPort(location.Host, portMap[location.Scheme])
|
|
}
|
|
}
|
|
return location.Host
|
|
}
|
|
|
|
// DialConfig opens a new client connection to a WebSocket with a config.
|
|
func DialConfig(config *Config) (ws *Conn, err error) {
|
|
return config.DialContext(context.Background())
|
|
}
|
|
|
|
// DialContext opens a new client connection to a WebSocket, with context support for timeouts/cancellation.
|
|
func (config *Config) DialContext(ctx context.Context) (*Conn, error) {
|
|
if config.Location == nil {
|
|
return nil, &DialError{config, ErrBadWebSocketLocation}
|
|
}
|
|
if config.Origin == nil {
|
|
return nil, &DialError{config, ErrBadWebSocketOrigin}
|
|
}
|
|
|
|
dialer := config.Dialer
|
|
if dialer == nil {
|
|
dialer = &net.Dialer{}
|
|
}
|
|
|
|
client, err := dialWithDialer(ctx, dialer, config)
|
|
if err != nil {
|
|
return nil, &DialError{config, err}
|
|
}
|
|
|
|
// Cleanup the connection if we fail to create the websocket successfully
|
|
success := false
|
|
defer func() {
|
|
if !success {
|
|
_ = client.Close()
|
|
}
|
|
}()
|
|
|
|
var ws *Conn
|
|
var wsErr error
|
|
doneConnecting := make(chan struct{})
|
|
go func() {
|
|
defer close(doneConnecting)
|
|
ws, err = NewClient(config, client)
|
|
if err != nil {
|
|
wsErr = &DialError{config, err}
|
|
}
|
|
}()
|
|
|
|
// The websocket.NewClient() function can block indefinitely, make sure that we
|
|
// respect the deadlines specified by the context.
|
|
select {
|
|
case <-ctx.Done():
|
|
// Force the pending operations to fail, terminating the pending connection attempt
|
|
_ = client.SetDeadline(time.Now())
|
|
<-doneConnecting // Wait for the goroutine that tries to establish the connection to finish
|
|
return nil, &DialError{config, ctx.Err()}
|
|
case <-doneConnecting:
|
|
if wsErr == nil {
|
|
success = true // Disarm the deferred connection cleanup
|
|
}
|
|
return ws, wsErr
|
|
}
|
|
}
|