Skip to content

Commit

Permalink
devlxd: Avoid superfluous call to WriteHeader (#14759)
Browse files Browse the repository at this point in the history
The returned response will be rendered using the same `ResponseWriter`
object, resulting on a redundant call to `WriteHeader` on the second
Render call.

Closes #14641
  • Loading branch information
tomponline authored Jan 9, 2025
2 parents 5235a07 + 6b5f5c5 commit c07e768
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
9 changes: 1 addition & 8 deletions lxd/devlxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,7 @@ func devlxdImageExportHandler(d *Daemon, c instance.Instance, w http.ResponseWri
return response.DevLxdErrorResponse(api.StatusErrorf(http.StatusForbidden, "not authorized"), c.Type() == instancetype.VM)
}

resp := imageExport(d, r)

err := resp.Render(w, r)
if err != nil {
return response.DevLxdErrorResponse(api.StatusErrorf(http.StatusInternalServerError, "internal server error"), c.Type() == instancetype.VM)
}

return response.DevLxdResponse(http.StatusOK, "", "raw", c.Type() == instancetype.VM)
return imageExport(d, r)
}

var devlxdMetadataGet = devLxdHandler{
Expand Down
10 changes: 2 additions & 8 deletions lxd/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ type devLxdResponse struct {
}

// 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

func (r *devLxdResponse) Render(w http.ResponseWriter, req *http.Request) (err error) {
if r.code != http.StatusOK {
http.Error(w, fmt.Sprintf("%s", r.content), r.code)
} else if r.contentType == "json" {
Expand All @@ -72,11 +70,7 @@ func (r *devLxdResponse) Render(w http.ResponseWriter, req *http.Request) error
_, err = fmt.Fprint(w, r.content.(string))
}

if err != nil {
return err
}

return nil
return err
}

func (r *devLxdResponse) String() string {
Expand Down

0 comments on commit c07e768

Please sign in to comment.