rawWebsocket creates a websocket connection to the provided URL using the underlying HTTP transport of the ProtocolIncus receiver. It sets up the request headers, manages the connection handshake, sets TCP timeouts, and handles any errors that may occur during these operations.
(url string)
| 456 | // rawWebsocket creates a websocket connection to the provided URL using the underlying HTTP transport of the ProtocolIncus receiver. |
| 457 | // It sets up the request headers, manages the connection handshake, sets TCP timeouts, and handles any errors that may occur during these operations. |
| 458 | func (r *ProtocolIncus) rawWebsocket(url string) (*websocket.Conn, error) { |
| 459 | // Grab the http transport handler |
| 460 | httpTransport, err := r.getUnderlyingHTTPTransport() |
| 461 | if err != nil { |
| 462 | return nil, err |
| 463 | } |
| 464 | |
| 465 | // Setup a new websocket dialer based on it |
| 466 | dialer := websocket.Dialer{ |
| 467 | NetDialTLSContext: httpTransport.DialTLSContext, |
| 468 | NetDialContext: httpTransport.DialContext, |
| 469 | TLSClientConfig: httpTransport.TLSClientConfig, |
| 470 | Proxy: httpTransport.Proxy, |
| 471 | HandshakeTimeout: time.Second * 5, |
| 472 | } |
| 473 | |
| 474 | // Create temporary http.Request using the http url, not the ws one, so that we can add the client headers |
| 475 | // for the websocket request. |
| 476 | req := &http.Request{URL: &r.httpBaseURL, Header: http.Header{}} |
| 477 | |
| 478 | // Establish the connection |
| 479 | conn, resp, err := r.DoWebsocket(dialer, url, req) |
| 480 | if err != nil { |
| 481 | if resp != nil { |
| 482 | apiResp, _, parseErr := incusParseResponse(resp) |
| 483 | if parseErr != nil { |
| 484 | err = errors.Join(err, parseErr) |
| 485 | } |
| 486 | |
| 487 | if apiResp != nil && apiResp.Error != "" { |
| 488 | err = errors.Join(err, errors.New(apiResp.Error)) |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | return nil, err |
| 493 | } |
| 494 | |
| 495 | // Set TCP timeout options. |
| 496 | remoteTCP, _ := tcp.ExtractConn(conn.UnderlyingConn()) |
| 497 | if remoteTCP != nil { |
| 498 | err = tcp.SetTimeouts(remoteTCP, 0) |
| 499 | if err != nil { |
| 500 | logger.Warn("Failed setting TCP timeouts on remote connection", logger.Ctx{"err": err}) |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Log the data |
| 505 | logger.Debugf("Connected to the websocket: %v", url) |
| 506 | |
| 507 | return conn, nil |
| 508 | } |
| 509 | |
| 510 | // websocket generates a websocket URL based on the provided path and the base URL of the ProtocolIncus receiver. |
| 511 | // It then leverages the rawWebsocket method to establish and return a websocket connection to the generated URL. |
no test coverage detected