From 796045353da8de7b5a8ebdd07e26c2988668bedf Mon Sep 17 00:00:00 2001 From: hamistao Date: Thu, 29 Aug 2024 17:07:37 -0300 Subject: [PATCH 1/5] lxd: Add request argument on `Render` Signed-off-by: hamistao (cherry picked from commit ba31a90cfdb86ecabacc5f23e1c34e5bedbbf297) --- lxd/api.go | 10 +++++----- lxd/api_cluster.go | 4 ++-- lxd/api_internal.go | 2 +- lxd/auth/oidc/oidc.go | 10 +++++----- lxd/cluster/notify_test.go | 2 +- lxd/daemon.go | 18 +++++++++--------- lxd/devlxd.go | 4 ++-- lxd/events.go | 2 +- lxd/instance_sftp.go | 2 +- lxd/operations.go | 6 +++--- lxd/operations/response.go | 4 ++-- lxd/operations/websocket.go | 4 ++-- lxd/response/response.go | 16 ++++++++-------- 13 files changed, 42 insertions(+), 42 deletions(-) diff --git a/lxd/api.go b/lxd/api.go index d0c8f62ae770..9e52e86f68ab 100644 --- a/lxd/api.go +++ b/lxd/api.go @@ -174,7 +174,7 @@ func restServer(d *Daemon) *http.Server { } // Normal client handling. - _ = response.SyncResponse(true, []string{"/1.0"}).Render(w) + _ = response.SyncResponse(true, []string{"/1.0"}).Render(w, r) }) for endpoint, f := range d.gateway.HandlerFuncs(d.heartbeatHandler, d.identityCache) { @@ -206,7 +206,7 @@ func restServer(d *Daemon) *http.Server { mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { logger.Info("Sending top level 404", logger.Ctx{"url": r.URL, "method": r.Method, "remote": r.RemoteAddr}) w.Header().Set("Content-Type", "application/json") - _ = response.NotFound(nil).Render(w) + _ = response.NotFound(nil).Render(w, r) }) return &http.Server{ @@ -232,7 +232,7 @@ func hoistReqVM(f func(*Daemon, instance.Instance, http.ResponseWriter, *http.Re } resp := f(d, inst, w, r) - _ = resp.Render(w) + _ = resp.Render(w, r) } } @@ -248,7 +248,7 @@ func metricsServer(d *Daemon) *http.Server { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - _ = response.SyncResponse(true, []string{"/1.0"}).Render(w) + _ = response.SyncResponse(true, []string{"/1.0"}).Render(w, r) }) for endpoint, f := range d.gateway.HandlerFuncs(d.heartbeatHandler, d.identityCache) { @@ -261,7 +261,7 @@ func metricsServer(d *Daemon) *http.Server { mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { logger.Info("Sending top level 404", logger.Ctx{"url": r.URL, "method": r.Method, "remote": r.RemoteAddr}) w.Header().Set("Content-Type", "application/json") - _ = response.NotFound(nil).Render(w) + _ = response.NotFound(nil).Render(w, r) }) return &http.Server{Handler: &lxdHTTPServer{r: mux, d: d}} diff --git a/lxd/api_cluster.go b/lxd/api_cluster.go index 81fabb330a61..e37e3d33fd40 100644 --- a/lxd/api_cluster.go +++ b/lxd/api_cluster.go @@ -949,7 +949,7 @@ func clusterPutDisable(d *Daemon, r *http.Request, req api.ClusterPut) response. }() return response.ManualResponse(func(w http.ResponseWriter) error { - err := response.EmptySyncResponse.Render(w) + err := response.EmptySyncResponse.Render(w, r) if err != nil { return err } @@ -2039,7 +2039,7 @@ func clusterNodeDelete(d *Daemon, r *http.Request) response.Response { } return response.ManualResponse(func(w http.ResponseWriter) error { - err := response.EmptySyncResponse.Render(w) + err := response.EmptySyncResponse.Render(w, r) if err != nil { return err } diff --git a/lxd/api_internal.go b/lxd/api_internal.go index e2822ad2dc07..135950e0ab0b 100644 --- a/lxd/api_internal.go +++ b/lxd/api_internal.go @@ -247,7 +247,7 @@ func internalShutdown(d *Daemon, r *http.Request) response.Response { // Run shutdown sequence synchronously. stopErr := d.Stop(forceCtx, unix.SIGPWR) - err := response.SmartError(stopErr).Render(w) + err := response.SmartError(stopErr).Render(w, r) if err != nil { return err } diff --git a/lxd/auth/oidc/oidc.go b/lxd/auth/oidc/oidc.go index 9559781dc949..b4440924c2f0 100644 --- a/lxd/auth/oidc/oidc.go +++ b/lxd/auth/oidc/oidc.go @@ -275,7 +275,7 @@ func (o *Verifier) getGroupsFromClaims(customClaims map[string]any) []string { func (o *Verifier) Login(w http.ResponseWriter, r *http.Request) { err := o.ensureConfig(r.Context(), r.Host) if err != nil { - _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("Login failed: %w", err).Error()).Render(w) + _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("Login failed: %w", err).Error()).Render(w, r) return } @@ -287,7 +287,7 @@ func (o *Verifier) Login(w http.ResponseWriter, r *http.Request) { func (o *Verifier) Logout(w http.ResponseWriter, r *http.Request) { err := o.setCookies(w, nil, uuid.UUID{}, "", "", true) if err != nil { - _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("Failed to delete login information: %w", err).Error()).Render(w) + _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("Failed to delete login information: %w", err).Error()).Render(w, r) return } @@ -298,7 +298,7 @@ func (o *Verifier) Logout(w http.ResponseWriter, r *http.Request) { func (o *Verifier) Callback(w http.ResponseWriter, r *http.Request) { err := o.ensureConfig(r.Context(), r.Host) if err != nil { - _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("OIDC callback failed: %w", err).Error()).Render(w) + _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("OIDC callback failed: %w", err).Error()).Render(w, r) return } @@ -306,13 +306,13 @@ func (o *Verifier) Callback(w http.ResponseWriter, r *http.Request) { sessionID := uuid.New() secureCookie, err := o.secureCookieFromSession(sessionID) if err != nil { - _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("Failed to start a new session: %w", err).Error()).Render(w) + _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("Failed to start a new session: %w", err).Error()).Render(w, r) return } err = o.setCookies(w, secureCookie, sessionID, tokens.IDToken, tokens.RefreshToken, false) if err != nil { - _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("Failed to set login information: %w", err).Error()).Render(w) + _ = response.ErrorResponse(http.StatusInternalServerError, fmt.Errorf("Failed to set login information: %w", err).Error()).Render(w, r) return } diff --git a/lxd/cluster/notify_test.go b/lxd/cluster/notify_test.go index dc53fd70d31e..d5cdb09bbdf2 100644 --- a/lxd/cluster/notify_test.go +++ b/lxd/cluster/notify_test.go @@ -274,7 +274,7 @@ func (h *notifyFixtures) Unavailable(i int, err error) { mux.HandleFunc("/1.0/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") err := response.Unavailable(err) - _ = err.Render(w) + _ = err.Render(w, r) }) h.servers[i].Config.Handler = mux diff --git a/lxd/daemon.go b/lxd/daemon.go index 1788e42701d3..12f6ae986070 100644 --- a/lxd/daemon.go +++ b/lxd/daemon.go @@ -637,7 +637,7 @@ func (d *Daemon) createCmd(restAPI *mux.Router, version string, c APIEndpoint) { case <-d.setupChan: default: response := response.Unavailable(fmt.Errorf("LXD daemon setup in progress")) - _ = response.Render(w) + _ = response.Render(w, r) return } } @@ -654,11 +654,11 @@ func (d *Daemon) createCmd(restAPI *mux.Router, version string, c APIEndpoint) { // Return 401 Unauthorized error. This indicates to the client that it needs to use the // headers we've set above to get an access token and try again. - _ = response.Unauthorized(err).Render(w) + _ = response.Unauthorized(err).Render(w, r) return } - _ = response.Forbidden(err).Render(w) + _ = response.Forbidden(err).Render(w, r) return } @@ -670,7 +670,7 @@ func (d *Daemon) createCmd(restAPI *mux.Router, version string, c APIEndpoint) { // Except for the initial cluster accept request (done over trusted TLS) if !trusted || c.Path != "cluster/accept" || protocol != api.AuthenticationMethodTLS { logger.Warn("Rejecting remote internal API request", logger.Ctx{"ip": r.RemoteAddr}) - _ = response.Forbidden(nil).Render(w) + _ = response.Forbidden(nil).Render(w, r) return } } @@ -720,7 +720,7 @@ func (d *Daemon) createCmd(restAPI *mux.Router, version string, c APIEndpoint) { } logger.Warn("Rejecting request from untrusted client", logger.Ctx{"ip": r.RemoteAddr}) - _ = response.Forbidden(nil).Render(w) + _ = response.Forbidden(nil).Render(w, r) return } @@ -731,7 +731,7 @@ func (d *Daemon) createCmd(restAPI *mux.Router, version string, c APIEndpoint) { multiW := io.MultiWriter(newBody, captured) _, err := io.Copy(multiW, r.Body) if err != nil { - _ = response.InternalError(err).Render(w) + _ = response.InternalError(err).Render(w, r) return } @@ -766,7 +766,7 @@ func (d *Daemon) createCmd(restAPI *mux.Router, version string, c APIEndpoint) { } if d.shutdownCtx.Err() == context.Canceled && !allowedDuringShutdown() { - _ = response.Unavailable(fmt.Errorf("LXD is shutting down")).Render(w) + _ = response.Unavailable(fmt.Errorf("LXD is shutting down")).Render(w, r) return } @@ -814,9 +814,9 @@ func (d *Daemon) createCmd(restAPI *mux.Router, version string, c APIEndpoint) { } // Handle errors - err = resp.Render(w) + err = resp.Render(w, r) if err != nil { - writeErr := response.SmartError(err).Render(w) + writeErr := response.SmartError(err).Render(w, r) if writeErr != nil { logger.Warn("Failed writing error for HTTP response", logger.Ctx{"url": uri, "err": err, "writeErr": writeErr}) } diff --git a/lxd/devlxd.go b/lxd/devlxd.go index 2d4c45569223..efdb7df9a28a 100644 --- a/lxd/devlxd.go +++ b/lxd/devlxd.go @@ -121,7 +121,7 @@ func devlxdImageExportHandler(d *Daemon, c instance.Instance, w http.ResponseWri resp := imageExport(d, r) - err := resp.Render(w) + err := resp.Render(w, r) if err != nil { return response.DevLxdErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM) } @@ -390,7 +390,7 @@ func hoistReq(f func(*Daemon, instance.Instance, http.ResponseWriter, *http.Requ } resp := f(d, c, w, r) - _ = resp.Render(w) + _ = resp.Render(w, r) } } diff --git a/lxd/events.go b/lxd/events.go index 1a814b5fd8b0..cca40808e296 100644 --- a/lxd/events.go +++ b/lxd/events.go @@ -35,7 +35,7 @@ type eventsServe struct { } // Render starts event socket. -func (r *eventsServe) Render(w http.ResponseWriter) error { +func (r *eventsServe) Render(w http.ResponseWriter, req *http.Request) error { return eventsSocket(r.s, r.req, w) } diff --git a/lxd/instance_sftp.go b/lxd/instance_sftp.go index f954dd70efe6..226761671745 100644 --- a/lxd/instance_sftp.go +++ b/lxd/instance_sftp.go @@ -109,7 +109,7 @@ func (r *sftpServeResponse) String() string { } // Render renders the server response. -func (r *sftpServeResponse) Render(w http.ResponseWriter) error { +func (r *sftpServeResponse) Render(w http.ResponseWriter, req *http.Request) error { defer func() { _ = r.instConn.Close() }() hijacker, ok := w.(http.Hijacker) diff --git a/lxd/operations.go b/lxd/operations.go index ac07dd6da006..13d8796296d5 100644 --- a/lxd/operations.go +++ b/lxd/operations.go @@ -983,17 +983,17 @@ func operationWaitGet(d *Daemon, r *http.Request) response.Response { // Wait for the operation. err = op.Wait(ctx) if err != nil { - _ = response.SmartError(err).Render(w) + _ = response.SmartError(err).Render(w, r) return nil } _, body, err := op.Render() if err != nil { - _ = response.SmartError(err).Render(w) + _ = response.SmartError(err).Render(w, r) return nil } - _ = response.SyncResponse(true, body).Render(w) + _ = response.SyncResponse(true, body).Render(w, r) return nil } diff --git a/lxd/operations/response.go b/lxd/operations/response.go index bd871a839e82..cc40032202fc 100644 --- a/lxd/operations/response.go +++ b/lxd/operations/response.go @@ -21,7 +21,7 @@ func OperationResponse(op *Operation) response.Response { return &operationResponse{op} } -func (r *operationResponse) Render(w http.ResponseWriter) error { +func (r *operationResponse) Render(w http.ResponseWriter, req *http.Request) error { err := r.op.Start() if err != nil { return err @@ -79,7 +79,7 @@ func ForwardedOperationResponse(project string, op *api.Operation) response.Resp } } -func (r *forwardedOperationResponse) Render(w http.ResponseWriter) error { +func (r *forwardedOperationResponse) Render(w http.ResponseWriter, req *http.Request) error { url := fmt.Sprintf("/%s/operations/%s", version.APIVersion, r.op.ID) if r.project != "" { url += fmt.Sprintf("?project=%s", r.project) diff --git a/lxd/operations/websocket.go b/lxd/operations/websocket.go index eaf6052ea3d2..ba4c72ba42d9 100644 --- a/lxd/operations/websocket.go +++ b/lxd/operations/websocket.go @@ -20,7 +20,7 @@ func OperationWebSocket(req *http.Request, op *Operation) response.Response { return &operationWebSocket{req, op} } -func (r *operationWebSocket) Render(w http.ResponseWriter) error { +func (r *operationWebSocket) Render(w http.ResponseWriter, req *http.Request) error { chanErr, err := r.op.Connect(r.req, w) if err != nil { return err @@ -50,7 +50,7 @@ func ForwardedOperationWebSocket(req *http.Request, id string, source *websocket return &forwardedOperationWebSocket{req, id, source} } -func (r *forwardedOperationWebSocket) Render(w http.ResponseWriter) error { +func (r *forwardedOperationWebSocket) Render(w http.ResponseWriter, req *http.Request) error { // Upgrade target connection to websocket. target, err := ws.Upgrader.Upgrade(w, r.req, nil) if err != nil { diff --git a/lxd/response/response.go b/lxd/response/response.go index 75b9a0cd9991..5da139243c0d 100644 --- a/lxd/response/response.go +++ b/lxd/response/response.go @@ -28,7 +28,7 @@ func Init(d bool) { // Response represents an API response. type Response interface { - Render(w http.ResponseWriter) error + Render(w http.ResponseWriter, r *http.Request) error String() string } @@ -39,7 +39,7 @@ type devLxdResponse struct { contentType string } -func (r *devLxdResponse) Render(w http.ResponseWriter) error { +func (r *devLxdResponse) Render(w http.ResponseWriter, req *http.Request) error { var err error if r.code != http.StatusOK { @@ -144,7 +144,7 @@ func SyncResponsePlain(success bool, compress bool, metadata string) Response { return &syncResponse{success: success, metadata: metadata, plaintext: true, compress: compress} } -func (r *syncResponse) Render(w http.ResponseWriter) error { +func (r *syncResponse) Render(w http.ResponseWriter, req *http.Request) error { // Set an appropriate ETag header if r.etag != nil { etag, err := util.EtagHash(r.etag) @@ -196,7 +196,7 @@ func (r *syncResponse) Render(w http.ResponseWriter) error { // to propagate the data and preserve the status code. err, ok := r.metadata.(error) if ok { - return SmartError(err).Render(w) + return SmartError(err).Render(w, req) } } @@ -327,7 +327,7 @@ func (r *errorResponse) String() string { return r.msg } -func (r *errorResponse) Render(w http.ResponseWriter) error { +func (r *errorResponse) Render(w http.ResponseWriter, req *http.Request) error { var output io.Writer buf := &bytes.Buffer{} @@ -396,7 +396,7 @@ func FileResponse(r *http.Request, files []FileResponseEntry, headers map[string return &fileResponse{r, files, headers} } -func (r *fileResponse) Render(w http.ResponseWriter) error { +func (r *fileResponse) Render(w http.ResponseWriter, req *http.Request) error { if r.headers != nil { for k, v := range r.headers { w.Header().Set(k, v) @@ -517,7 +517,7 @@ func ForwardedResponse(client lxd.InstanceServer, request *http.Request) Respons } } -func (r *forwardedResponse) Render(w http.ResponseWriter) error { +func (r *forwardedResponse) Render(w http.ResponseWriter, req *http.Request) error { info, err := r.client.GetConnectionInfo() if err != nil { return err @@ -568,7 +568,7 @@ func ManualResponse(hook func(w http.ResponseWriter) error) Response { return &manualResponse{hook: hook} } -func (r *manualResponse) Render(w http.ResponseWriter) error { +func (r *manualResponse) Render(w http.ResponseWriter, req *http.Request) error { return r.hook(w) } From 64006b3c66a1d6efc493f9e507503138e9d9ab34 Mon Sep 17 00:00:00 2001 From: hamistao Date: Thu, 22 Aug 2024 17:52:13 -0300 Subject: [PATCH 2/5] lxd-agent: Add request argument to `Render` Signed-off-by: hamistao (cherry picked from commit 9143502719344473f7b912060f04f4b51c79d123) --- lxd-agent/devlxd.go | 2 +- lxd-agent/events.go | 4 ++-- lxd-agent/server.go | 10 +++++----- lxd-agent/sftp.go | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lxd-agent/devlxd.go b/lxd-agent/devlxd.go index 5916ce9ecb01..cff186de220d 100644 --- a/lxd-agent/devlxd.go +++ b/lxd-agent/devlxd.go @@ -176,7 +176,7 @@ var devLxdEventsGet = devLxdHandler{ } func devlxdEventsGetHandler(d *Daemon, w http.ResponseWriter, r *http.Request) *devLxdResponse { - err := eventsGet(d, r).Render(w) + err := eventsGet(d, r).Render(w, r) if err != nil { return smartResponse(err) } diff --git a/lxd-agent/events.go b/lxd-agent/events.go index 32bb14fac645..cbf810725d7a 100644 --- a/lxd-agent/events.go +++ b/lxd-agent/events.go @@ -30,8 +30,8 @@ type eventsServe struct { } // Render starts event socket. -func (r *eventsServe) Render(w http.ResponseWriter) error { - return eventsSocket(r.d, r.req, w) +func (r *eventsServe) Render(w http.ResponseWriter, request *http.Request) error { + return eventsSocket(r.d, request, w) } func (r *eventsServe) String() string { diff --git a/lxd-agent/server.go b/lxd-agent/server.go index e3a15e47c796..7bb75f20fc10 100644 --- a/lxd-agent/server.go +++ b/lxd-agent/server.go @@ -23,7 +23,7 @@ func restServer(tlsConfig *tls.Config, cert *x509.Certificate, d *Daemon) *http. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - _ = response.SyncResponse(true, []string{"/1.0"}).Render(w) + _ = response.SyncResponse(true, []string{"/1.0"}).Render(w, r) }) for _, c := range api10 { @@ -46,7 +46,7 @@ func createCmd(restAPI *mux.Router, version string, c APIEndpoint, cert *x509.Ce if !authenticate(r, cert) { logger.Error("Not authorized") - _ = response.InternalError(fmt.Errorf("Not authorized")).Render(w) + _ = response.InternalError(fmt.Errorf("Not authorized")).Render(w, r) return } @@ -57,7 +57,7 @@ func createCmd(restAPI *mux.Router, version string, c APIEndpoint, cert *x509.Ce multiW := io.MultiWriter(newBody, captured) _, err := io.Copy(multiW, r.Body) if err != nil { - _ = response.InternalError(err).Render(w) + _ = response.InternalError(err).Render(w, r) return } @@ -92,9 +92,9 @@ func createCmd(restAPI *mux.Router, version string, c APIEndpoint, cert *x509.Ce } // Handle errors - err := resp.Render(w) + err := resp.Render(w, r) if err != nil { - writeErr := response.InternalError(err).Render(w) + writeErr := response.InternalError(err).Render(w, r) if writeErr != nil { logger.Error("Failed writing error for HTTP response", logger.Ctx{"url": uri, "err": err, "writeErr": writeErr}) } diff --git a/lxd-agent/sftp.go b/lxd-agent/sftp.go index f09749adb812..e9d00cfbc718 100644 --- a/lxd-agent/sftp.go +++ b/lxd-agent/sftp.go @@ -29,9 +29,9 @@ func (r *sftpServe) String() string { return "sftp handler" } -func (r *sftpServe) Render(w http.ResponseWriter) error { +func (r *sftpServe) Render(w http.ResponseWriter, request *http.Request) error { // Upgrade to sftp. - if r.r.Header.Get("Upgrade") != "sftp" { + if request.Header.Get("Upgrade") != "sftp" { http.Error(w, "Missing or invalid upgrade header", http.StatusBadRequest) return nil } From 0fb6fc3f685aafb8b1422545013269803dcbade2 Mon Sep 17 00:00:00 2001 From: hamistao Date: Tue, 20 Aug 2024 18:02:06 -0300 Subject: [PATCH 3/5] lxd: Delete duplicate `operationWebSocket` Signed-off-by: hamistao (cherry picked from commit 815cdb2a8c9b72ca19d63bb61b7f64cb64fce459) --- lxd/operations.go | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/lxd/operations.go b/lxd/operations.go index 13d8796296d5..f8858f70c93c 100644 --- a/lxd/operations.go +++ b/lxd/operations.go @@ -1034,32 +1034,6 @@ func operationWaitGet(d *Daemon, r *http.Request) response.Response { return response.ForwardedResponse(client, r) } -type operationWebSocket struct { - req *http.Request - op *operations.Operation -} - -// Render implements response.Response for operationWebSocket. -func (r *operationWebSocket) Render(w http.ResponseWriter) error { - chanErr, err := r.op.Connect(r.req, w) - if err != nil { - return err - } - - err = <-chanErr - return err -} - -// String implements fmt.Stringer for operationWebSocket. -func (r *operationWebSocket) String() string { - _, md, err := r.op.Render() - if err != nil { - return fmt.Sprintf("error: %s", err) - } - - return md.ID -} - // swagger:operation GET /1.0/operations/{id}/websocket?public operations operation_websocket_get_untrusted // // Get the websocket stream @@ -1125,7 +1099,7 @@ func operationWebsocketGet(d *Daemon, r *http.Request) response.Response { // First check if the query is for a local operation from this node op, err := operations.OperationGetInternal(id) if err == nil { - return &operationWebSocket{r, op} + return operations.OperationWebSocket(r, op) } // Then check if the query is from an operation on another node, and, if so, forward it From 5b93b7a6e084d40fe933a1249161d20a6b965e8d Mon Sep 17 00:00:00 2001 From: hamistao Date: Thu, 22 Aug 2024 09:17:00 -0300 Subject: [PATCH 4/5] lxd-agent: Add comment to exported function Signed-off-by: hamistao (cherry picked from commit b3230b46cdce822ae008ac0cb1aa4ee266063e4b) --- lxd-agent/sftp.go | 1 + 1 file changed, 1 insertion(+) diff --git a/lxd-agent/sftp.go b/lxd-agent/sftp.go index e9d00cfbc718..d07357436dd4 100644 --- a/lxd-agent/sftp.go +++ b/lxd-agent/sftp.go @@ -29,6 +29,7 @@ func (r *sftpServe) String() string { return "sftp handler" } +// Render hijacks the connection and starts a sftp server. func (r *sftpServe) Render(w http.ResponseWriter, request *http.Request) error { // Upgrade to sftp. if request.Header.Get("Upgrade") != "sftp" { From 8661c6f800194f4f8fcc6e1d51e437158b41686a Mon Sep 17 00:00:00 2001 From: hamistao Date: Thu, 29 Aug 2024 23:33:30 -0300 Subject: [PATCH 5/5] lxd: Add comments to exported functions Signed-off-by: hamistao (cherry picked from commit f6e4f994fc36c1a8880cb613fcf18cc2dc16666e) --- lxd/operations/response.go | 2 ++ lxd/operations/websocket.go | 2 ++ lxd/response/response.go | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/lxd/operations/response.go b/lxd/operations/response.go index cc40032202fc..16229563d364 100644 --- a/lxd/operations/response.go +++ b/lxd/operations/response.go @@ -21,6 +21,7 @@ func OperationResponse(op *Operation) response.Response { return &operationResponse{op} } +// Render builds operationResponse and writes it to http.ResponseWriter. func (r *operationResponse) Render(w http.ResponseWriter, req *http.Request) error { err := r.op.Start() if err != nil { @@ -79,6 +80,7 @@ func ForwardedOperationResponse(project string, op *api.Operation) response.Resp } } +// Render builds forwardedOperationResponse and writes it to http.ResponseWriter. func (r *forwardedOperationResponse) Render(w http.ResponseWriter, req *http.Request) error { url := fmt.Sprintf("/%s/operations/%s", version.APIVersion, r.op.ID) if r.project != "" { diff --git a/lxd/operations/websocket.go b/lxd/operations/websocket.go index ba4c72ba42d9..af5ade4d5183 100644 --- a/lxd/operations/websocket.go +++ b/lxd/operations/websocket.go @@ -20,6 +20,7 @@ func OperationWebSocket(req *http.Request, op *Operation) response.Response { return &operationWebSocket{req, op} } +// Render renders a websocket operation response. func (r *operationWebSocket) Render(w http.ResponseWriter, req *http.Request) error { chanErr, err := r.op.Connect(r.req, w) if err != nil { @@ -50,6 +51,7 @@ func ForwardedOperationWebSocket(req *http.Request, id string, source *websocket return &forwardedOperationWebSocket{req, id, source} } +// Render renders a forwarded websocket operation response. func (r *forwardedOperationWebSocket) Render(w http.ResponseWriter, req *http.Request) error { // Upgrade target connection to websocket. target, err := ws.Upgrader.Upgrade(w, r.req, nil) diff --git a/lxd/response/response.go b/lxd/response/response.go index 5da139243c0d..d2833acd318c 100644 --- a/lxd/response/response.go +++ b/lxd/response/response.go @@ -39,6 +39,7 @@ type devLxdResponse struct { contentType string } +// Render renders a response for requests against the /dev/lxd socket. func (r *devLxdResponse) Render(w http.ResponseWriter, req *http.Request) error { var err error @@ -144,6 +145,7 @@ func SyncResponsePlain(success bool, compress bool, metadata string) Response { return &syncResponse{success: success, metadata: metadata, plaintext: true, compress: compress} } +// Render renders a synchronous response. func (r *syncResponse) Render(w http.ResponseWriter, req *http.Request) error { // Set an appropriate ETag header if r.etag != nil { @@ -327,6 +329,7 @@ func (r *errorResponse) String() string { return r.msg } +// Render renders a response that indicates an error on the request handling. func (r *errorResponse) Render(w http.ResponseWriter, req *http.Request) error { var output io.Writer @@ -396,6 +399,7 @@ func FileResponse(r *http.Request, files []FileResponseEntry, headers map[string return &fileResponse{r, files, headers} } +// Render renders a file response. func (r *fileResponse) Render(w http.ResponseWriter, req *http.Request) error { if r.headers != nil { for k, v := range r.headers { @@ -517,6 +521,7 @@ func ForwardedResponse(client lxd.InstanceServer, request *http.Request) Respons } } +// Render renders a response for a forwarded request. func (r *forwardedResponse) Render(w http.ResponseWriter, req *http.Request) error { info, err := r.client.GetConnectionInfo() if err != nil { @@ -568,6 +573,7 @@ func ManualResponse(hook func(w http.ResponseWriter) error) Response { return &manualResponse{hook: hook} } +// Render renders a manual response. func (r *manualResponse) Render(w http.ResponseWriter, req *http.Request) error { return r.hook(w) }