From 7a4ab13a791666f6af9d2d9de94491be7c423864 Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Wed, 17 Mar 2021 13:12:28 -0400 Subject: [PATCH] API Response implements http.ResponseWriter (#32046) * rename response.Header -> response.SetHeader to free up method name for http.ResponseWriter ifc * normalresponse implements http.ResponseWriter --- pkg/api/dashboard.go | 4 ++-- pkg/api/dashboard_snapshot.go | 2 +- pkg/api/plugins.go | 2 +- pkg/api/response/response.go | 31 ++++++++++++++++++++++++------- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index c7563b1a821..43e92b39365 100644 --- a/pkg/api/dashboard.go +++ b/pkg/api/dashboard.go @@ -561,10 +561,10 @@ func CalculateDashboardDiff(c *models.ReqContext, apiOptions dtos.CalculateDiffO } if options.DiffType == dashdiffs.DiffDelta { - return response.Respond(200, result.Delta).Header("Content-Type", "application/json") + return response.Respond(200, result.Delta).SetHeader("Content-Type", "application/json") } - return response.Respond(200, result.Delta).Header("Content-Type", "text/html") + return response.Respond(200, result.Delta).SetHeader("Content-Type", "text/html") } // RestoreDashboardVersion restores a dashboard to the given version. diff --git a/pkg/api/dashboard_snapshot.go b/pkg/api/dashboard_snapshot.go index 4f7a4b8d090..3bf2158d99c 100644 --- a/pkg/api/dashboard_snapshot.go +++ b/pkg/api/dashboard_snapshot.go @@ -175,7 +175,7 @@ func GetDashboardSnapshot(c *models.ReqContext) response.Response { metrics.MApiDashboardSnapshotGet.Inc() - return response.JSON(200, dto).Header("Cache-Control", "public, max-age=3600") + return response.JSON(200, dto).SetHeader("Cache-Control", "public, max-age=3600") } func deleteExternalDashboardSnapshot(externalUrl string) error { diff --git a/pkg/api/plugins.go b/pkg/api/plugins.go index 65de498ad71..63f10387143 100644 --- a/pkg/api/plugins.go +++ b/pkg/api/plugins.go @@ -243,7 +243,7 @@ func (hs *HTTPServer) GetPluginMarkdown(c *models.ReqContext) response.Response } resp := response.Respond(200, content) - resp.Header("Content-Type", "text/plain; charset=utf-8") + resp.SetHeader("Content-Type", "text/plain; charset=utf-8") return resp } diff --git a/pkg/api/response/response.go b/pkg/api/response/response.go index 297d0051461..46e5aa4cf13 100644 --- a/pkg/api/response/response.go +++ b/pkg/api/response/response.go @@ -1,6 +1,7 @@ package response import ( + "bytes" "encoding/json" "net/http" @@ -22,17 +23,33 @@ type Response interface { func CreateNormalResponse(header http.Header, body []byte, status int) *NormalResponse { return &NormalResponse{ header: header, - body: body, + body: bytes.NewBuffer(body), status: status, } } type NormalResponse struct { status int - body []byte + body *bytes.Buffer header http.Header errMessage string err error + http.ResponseWriter +} + +// Write implements http.ResponseWriter +func (r *NormalResponse) Write(b []byte) (int, error) { + return r.body.Write(b) +} + +// Header implements http.ResponseWriter +func (r *NormalResponse) Header() http.Header { + return r.header +} + +// WriteHeader implements http.ResponseWriter +func (r *NormalResponse) WriteHeader(statusCode int) { + r.status = statusCode } // Status gets the response's status. @@ -42,7 +59,7 @@ func (r *NormalResponse) Status() int { // Body gets the response's body. func (r *NormalResponse) Body() []byte { - return r.body + return r.body.Bytes() } // Err gets the response's err. @@ -65,12 +82,12 @@ func (r *NormalResponse) WriteTo(ctx *models.ReqContext) { header[k] = v } ctx.Resp.WriteHeader(r.status) - if _, err := ctx.Resp.Write(r.body); err != nil { + if _, err := ctx.Resp.Write(r.body.Bytes()); err != nil { ctx.Logger.Error("Error writing to response", "err", err) } } -func (r *NormalResponse) Header(key, value string) *NormalResponse { +func (r *NormalResponse) SetHeader(key, value string) *NormalResponse { r.header.Set(key, value) return r } @@ -137,7 +154,7 @@ func (r *RedirectResponse) Body() []byte { // JSON creates a JSON response. func JSON(status int, body interface{}) *NormalResponse { - return Respond(status, body).Header("Content-Type", "application/json") + return Respond(status, body).SetHeader("Content-Type", "application/json") } // JSONStreaming creates a streaming JSON response. @@ -211,7 +228,7 @@ func Respond(status int, body interface{}) *NormalResponse { return &NormalResponse{ status: status, - body: b, + body: bytes.NewBuffer(b), header: make(http.Header), } }