mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2026-01-15 21:38:11 +00:00
Squashed commit of the following: commit 9324a0066202f1677bfd033d40d3a82fa9756ed9 Merge:8a1b5cad4f9da40e39Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Oct 23 17:48:01 2025 +0300 Merge branch 'master' into AGDNS-3224-aghhttp-register-slog commit8a1b5cad4cAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 21 15:51:48 2025 +0300 filtering: imp code commitfe569166efMerge:9a101a2f59be4ca90eAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 21 15:45:42 2025 +0300 Merge branch 'master' into AGDNS-3224-aghhttp-register-slog commit9a101a2f5fAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Oct 15 18:52:22 2025 +0300 home: imp code commit727e1663baAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Oct 15 10:19:56 2025 +0300 all: imp code commit113a9017dfAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 13 23:10:06 2025 +0300 home: fix typo commit6588dd2dadAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 13 22:46:28 2025 +0300 all: imp naming commit44278505a9Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Oct 10 16:20:17 2025 +0300 home: fix typo commit7b4b57628bAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Oct 10 15:58:07 2025 +0300 all: web mw commit93168142cbAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Oct 8 22:20:07 2025 +0300 all: aghhttp slog commit9155edef67Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Oct 8 15:38:01 2025 +0300 aghhttp: registrar commita356473855Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 7 15:32:30 2025 +0300 all: http registrar
176 lines
4.3 KiB
Go
176 lines
4.3 KiB
Go
package aghhttp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/httphdr"
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
|
)
|
|
|
|
// JSON Utilities
|
|
|
|
// nsecPerMsec is the number of nanoseconds in a millisecond.
|
|
const nsecPerMsec = float64(time.Millisecond / time.Nanosecond)
|
|
|
|
// JSONDuration is a time.Duration that can be decoded from JSON and encoded
|
|
// into JSON according to our API conventions.
|
|
type JSONDuration time.Duration
|
|
|
|
// type check
|
|
var _ json.Marshaler = JSONDuration(0)
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for JSONDuration. err is
|
|
// always nil.
|
|
func (d JSONDuration) MarshalJSON() (b []byte, err error) {
|
|
msec := float64(time.Duration(d)) / nsecPerMsec
|
|
b = strconv.AppendFloat(nil, msec, 'f', -1, 64)
|
|
|
|
return b, nil
|
|
}
|
|
|
|
// type check
|
|
var _ json.Unmarshaler = (*JSONDuration)(nil)
|
|
|
|
// UnmarshalJSON implements the json.Marshaler interface for *JSONDuration.
|
|
func (d *JSONDuration) UnmarshalJSON(b []byte) (err error) {
|
|
if d == nil {
|
|
return fmt.Errorf("json duration is nil")
|
|
}
|
|
|
|
msec, err := strconv.ParseFloat(string(b), 64)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing json time: %w", err)
|
|
}
|
|
|
|
*d = JSONDuration(int64(msec * nsecPerMsec))
|
|
|
|
return nil
|
|
}
|
|
|
|
// JSONTime is a time.Time that can be decoded from JSON and encoded into JSON
|
|
// according to our API conventions.
|
|
type JSONTime time.Time
|
|
|
|
// type check
|
|
var _ json.Marshaler = JSONTime{}
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for JSONTime. err is
|
|
// always nil.
|
|
func (t JSONTime) MarshalJSON() (b []byte, err error) {
|
|
msec := float64(time.Time(t).UnixNano()) / nsecPerMsec
|
|
b = strconv.AppendFloat(nil, msec, 'f', -1, 64)
|
|
|
|
return b, nil
|
|
}
|
|
|
|
// type check
|
|
var _ json.Unmarshaler = (*JSONTime)(nil)
|
|
|
|
// UnmarshalJSON implements the json.Marshaler interface for *JSONTime.
|
|
func (t *JSONTime) UnmarshalJSON(b []byte) (err error) {
|
|
if t == nil {
|
|
return fmt.Errorf("json time is nil")
|
|
}
|
|
|
|
msec, err := strconv.ParseFloat(string(b), 64)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing json time: %w", err)
|
|
}
|
|
|
|
*t = JSONTime(time.Unix(0, int64(msec*nsecPerMsec)).UTC())
|
|
|
|
return nil
|
|
}
|
|
|
|
// WriteJSONResponse writes headers with the code, encodes resp into w, and logs
|
|
// any errors it encounters. r is used to get additional information from the
|
|
// request. l, w, and r must not be nil.
|
|
func WriteJSONResponse(
|
|
ctx context.Context,
|
|
l *slog.Logger,
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
code int,
|
|
resp any,
|
|
) {
|
|
h := w.Header()
|
|
h.Set(httphdr.ContentType, HdrValApplicationJSON)
|
|
h.Set(httphdr.Server, UserAgent())
|
|
|
|
w.WriteHeader(code)
|
|
|
|
err := json.NewEncoder(w).Encode(resp)
|
|
if err != nil {
|
|
l.ErrorContext(
|
|
ctx,
|
|
"writing json response",
|
|
"method", r.Method,
|
|
"path", r.URL.Path,
|
|
slogutil.KeyError, err,
|
|
)
|
|
}
|
|
}
|
|
|
|
// WriteJSONResponseOK 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. l, w, and r must not be nil.
|
|
func WriteJSONResponseOK(
|
|
ctx context.Context,
|
|
l *slog.Logger,
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
v any,
|
|
) {
|
|
WriteJSONResponse(ctx, l, w, r, http.StatusOK, v)
|
|
}
|
|
|
|
// ErrorCode is the error code as used by the HTTP API. See the ErrorCode
|
|
// definition in the OpenAPI specification.
|
|
type ErrorCode string
|
|
|
|
// ErrorCode constants.
|
|
//
|
|
// TODO(a.garipov): Expand and document codes.
|
|
const (
|
|
// ErrorCodeTMP000 is the temporary error code used for all errors.
|
|
ErrorCodeTMP000 = ""
|
|
)
|
|
|
|
// HTTPAPIErrorResp is the error response as used by the HTTP API. See the
|
|
// BadRequestResp, InternalServerErrorResp, and similar objects in the OpenAPI
|
|
// specification.
|
|
type HTTPAPIErrorResp struct {
|
|
Code ErrorCode `json:"code"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
// WriteJSONResponseError encodes err as a JSON error into w, and logs any
|
|
// errors it encounters. r is used to get additional information from the
|
|
// request. l, w, and r must not be nil.
|
|
func WriteJSONResponseError(
|
|
ctx context.Context,
|
|
l *slog.Logger,
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
err error,
|
|
) {
|
|
l.ErrorContext(
|
|
ctx,
|
|
"writing json error",
|
|
"method", r.Method,
|
|
"path", r.URL.Path,
|
|
slogutil.KeyError, err,
|
|
)
|
|
|
|
WriteJSONResponse(ctx, l, w, r, http.StatusUnprocessableEntity, &HTTPAPIErrorResp{
|
|
Code: ErrorCodeTMP000,
|
|
Msg: err.Error(),
|
|
})
|
|
}
|