GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package websocket
  2. import (
  3. "fmt"
  4. "net"
  5. "net/http"
  6. "net/url"
  7. "github.com/gorilla/websocket"
  8. "github.com/pkg/errors"
  9. "github.com/fluxcd/flux/http/client"
  10. )
  11. type DialErr struct {
  12. URL *url.URL
  13. HTTPResponse *http.Response
  14. }
  15. func (de DialErr) Error() string {
  16. if de.URL != nil && de.HTTPResponse != nil {
  17. return fmt.Sprintf("connecting to websocket %s (http status code = %v)", de.URL, de.HTTPResponse.StatusCode)
  18. }
  19. return "connecting to websocket (unknown error)"
  20. }
  21. // Dial initiates a new websocket connection.
  22. func Dial(client *http.Client, ua string, token client.Token, u *url.URL) (Websocket, error) {
  23. // Build the http request
  24. req, err := http.NewRequest("GET", u.String(), nil)
  25. if err != nil {
  26. return nil, errors.Wrapf(err, "constructing request %s", u)
  27. }
  28. // Send version in user-agent
  29. req.Header.Set("User-Agent", ua)
  30. // Add authentication if provided
  31. token.Set(req)
  32. // Use http client to do the http request
  33. conn, resp, err := dialer(client).Dial(u.String(), req.Header)
  34. if err != nil {
  35. if resp != nil {
  36. err = &DialErr{u, resp}
  37. }
  38. return nil, err
  39. }
  40. // Set up the ping heartbeat
  41. return Ping(conn), nil
  42. }
  43. func dialer(client *http.Client) *websocket.Dialer {
  44. return &websocket.Dialer{
  45. NetDial: func(network, addr string) (net.Conn, error) {
  46. return net.DialTimeout(network, addr, client.Timeout)
  47. },
  48. HandshakeTimeout: client.Timeout,
  49. Jar: client.Jar,
  50. // TODO: TLSClientConfig: client.TLSClientConfig,
  51. // TODO: Proxy
  52. }
  53. }