diff --git a/internal/aghhttp/aghhttp.go b/internal/aghhttp/aghhttp.go index 6cb2c6709f4..88dfe7a79f7 100644 --- a/internal/aghhttp/aghhttp.go +++ b/internal/aghhttp/aghhttp.go @@ -2,7 +2,6 @@ package aghhttp import ( - "encoding/json" "fmt" "io" "net/http" @@ -61,23 +60,3 @@ func WriteTextPlainDeprecated(w http.ResponseWriter, r *http.Request) (isPlainTe return true } - -// WriteJSONResponse sets the content-type header in w.Header() to -// "application/json", writes a header with a "200 OK" status, encodes resp to -// w, calls [Error] on any returned error, and returns it as well. -func WriteJSONResponse(w http.ResponseWriter, r *http.Request, resp any) (err error) { - return WriteJSONResponseCode(w, r, http.StatusOK, resp) -} - -// WriteJSONResponseCode is like [WriteJSONResponse] but adds the ability to -// redefine the status code. -func WriteJSONResponseCode(w http.ResponseWriter, r *http.Request, code int, resp any) (err error) { - w.Header().Set(httphdr.ContentType, HdrValApplicationJSON) - w.WriteHeader(code) - err = json.NewEncoder(w).Encode(resp) - if err != nil { - Error(r, w, http.StatusInternalServerError, "encoding resp: %s", err) - } - - return err -} diff --git a/internal/aghhttp/json.go b/internal/aghhttp/json.go index ec124127ae0..0f3742f080d 100644 --- a/internal/aghhttp/json.go +++ b/internal/aghhttp/json.go @@ -86,28 +86,35 @@ func (t *JSONTime) UnmarshalJSON(b []byte) (err error) { return nil } -// WriteJSONOKResponse writes headers with the code 200 OK, encodes v into w, -// and logs any errors it encounters. r is used to get additional information -// from the request. -func WriteJSONOKResponse(w http.ResponseWriter, r *http.Request, v any) { - writeJSONResponse(w, r, v, http.StatusOK) +// WriteJSONResponse sets the content-type header in w.Header() to +// "application/json", writes a header with a "200 OK" status, encodes resp to +// w, calls [Error] on any returned error, and returns it as well. +func WriteJSONResponse(w http.ResponseWriter, r *http.Request, resp any) (err error) { + return WriteJSONResponseCode(w, r, http.StatusOK, resp) } -// writeJSONResponse writes headers with code, encodes v into w, and logs any -// errors it encounters. r is used to get additional information from the -// request. -func writeJSONResponse(w http.ResponseWriter, r *http.Request, v any, code int) { - // TODO(a.garipov): Put some of these to a middleware. +// WriteJSONResponseCode is like [WriteJSONResponse] but adds the ability to +// redefine the status code. +func WriteJSONResponseCode(w http.ResponseWriter, r *http.Request, code int, resp any) (err error) { h := w.Header() h.Set(httphdr.ContentType, HdrValApplicationJSON) h.Set(httphdr.Server, UserAgent()) w.WriteHeader(code) - err := json.NewEncoder(w).Encode(v) + err = json.NewEncoder(w).Encode(resp) if err != nil { - log.Error("websvc: writing resp to %s %s: %s", r.Method, r.URL.Path, err) + Error(r, w, http.StatusInternalServerError, "encoding resp: %s", err) } + + return err +} + +// WriteJSONOKResponse writes headers with the code 200 OK, encodes v into w, +// and logs any errors it encounters. r is used to get additional information +// from the request. +func WriteJSONOKResponse(w http.ResponseWriter, r *http.Request, v any) { + _ = WriteJSONResponseCode(w, r, http.StatusOK, v) } // ErrorCode is the error code as used by the HTTP API. See the ErrorCode @@ -136,8 +143,8 @@ type HTTPAPIErrorResp struct { func WriteJSONErrorResponse(w http.ResponseWriter, r *http.Request, err error) { log.Error("websvc: %s %s: %s", r.Method, r.URL.Path, err) - writeJSONResponse(w, r, &HTTPAPIErrorResp{ + _ = WriteJSONResponseCode(w, r, http.StatusUnprocessableEntity, &HTTPAPIErrorResp{ Code: ErrorCodeTMP000, Msg: err.Error(), - }, http.StatusUnprocessableEntity) + }) } diff --git a/internal/home/clientshttp.go b/internal/home/clientshttp.go index 181dd8ad624..febf5c43378 100644 --- a/internal/home/clientshttp.go +++ b/internal/home/clientshttp.go @@ -153,15 +153,21 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C weekly, ignoreQueryLog, ignoreStatistics := cj.copySettings(prev) + bs := &filtering.BlockedServices{ + Schedule: weekly, + IDs: cj.BlockedServices, + } + err = bs.Validate() + if err != nil { + return nil, fmt.Errorf("validating blocked services: %w", err) + } + c = &Client{ safeSearchConf: safeSearchConf, Name: cj.Name, - BlockedServices: &filtering.BlockedServices{ - Schedule: weekly, - IDs: cj.BlockedServices, - }, + BlockedServices: bs, IDs: cj.IDs, Tags: cj.Tags, @@ -176,11 +182,6 @@ func (clients *clientsContainer) jsonToClient(cj clientJSON, prev *Client) (c *C IgnoreStatistics: ignoreStatistics, } - err = c.BlockedServices.Validate() - if err != nil { - return nil, fmt.Errorf("validating blocked services: %w", err) - } - if safeSearchConf.Enabled { err = c.setSafeSearch( safeSearchConf,