feat: implement lab3 load balancing and idempotency

This commit is contained in:
Fedoroff
2026-10-27 20:34:22 +03:00
parent 0ab3a341aa
commit 35ae6f722e
43 changed files with 3045 additions and 42 deletions
+37
View File
@@ -0,0 +1,37 @@
package handler
import (
"encoding/json"
"errors"
"io"
"net/http"
)
// decodeOneObject rejects a second JSON value, trailing garbage and unknown fields.
func decodeOneObject(r *http.Request, target any) error {
dec := json.NewDecoder(io.LimitReader(r.Body, 1<<20))
dec.DisallowUnknownFields()
if err := dec.Decode(target); err != nil {
return err
}
var extra any
if err := dec.Decode(&extra); !errors.Is(err, io.EOF) {
if err != nil {
return err
}
return errors.New("multiple JSON values")
}
return nil
}
func requireEmptyBody(r *http.Request) error {
// A whitespace-only body is accepted as empty; any JSON or other bytes are not.
data, err := io.ReadAll(io.LimitReader(r.Body, 2))
if err != nil {
return err
}
if len(data) != 0 {
return errors.New("request body must be empty")
}
return nil
}