59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package demo
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"example.com/compatibility/internal/types"
|
|
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
|
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func (h *Handler) bindCustom(c *httpx.Context, req *types.CustomReq) error {
|
|
decoder := json.NewDecoder(c.Request().Body)
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(req); err != nil {
|
|
return apierrors.BadRequest("INVALID_REQUEST", "invalid request").WithCause(err)
|
|
}
|
|
var extra any
|
|
if err := decoder.Decode(&extra); err != io.EOF {
|
|
return apierrors.BadRequest("INVALID_REQUEST", "one JSON value required").WithCause(err)
|
|
}
|
|
req.Value = strings.ToUpper(req.Value)
|
|
return nil
|
|
}
|
|
func (h *Handler) Upload(c *httpx.Context) error {
|
|
// The small memory threshold deliberately exercises disk spill and cleanup.
|
|
if err := c.Request().ParseMultipartForm(8); err != nil {
|
|
return err
|
|
}
|
|
file, _, err := c.Request().FormFile("file")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
count, err := io.Copy(io.Discard, file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.OK(map[string]any{"bytes": count})
|
|
}
|
|
func (h *Handler) Callback(c *httpx.Context) error {
|
|
raw, err := io.ReadAll(c.Request().Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// This fixed key belongs only to the test fixture. Applications inject secrets.
|
|
mac := hmac.New(sha256.New, []byte("fixture-only-key"))
|
|
mac.Write(raw)
|
|
signature, err := hex.DecodeString(c.Header("X-Signature"))
|
|
if err != nil || !hmac.Equal(signature, mac.Sum(nil)) {
|
|
return c.Text(http.StatusUnauthorized, "invalid signature")
|
|
}
|
|
return c.Text(http.StatusOK, "accepted")
|
|
}
|