MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / readBodyForDecode

Function readBodyForDecode

internal/server/middleware_request_text.go:708–721  ·  view source on GitHub ↗

readBodyForDecode reads the whole request body so it can be scanned before it is decoded, with the caller's size cap applied. Buffering is not a cost paid for the scan. json.Decoder.Decode already holds the entire top-level value in memory before it finishes — refill accumulates into dec.buf and gr

(r *http.Request, maxBytes int64)

Source from the content-addressed store, hash-verified

706// 512.5 MiB. Peak heap is indistinguishable between the first two and is
707// order-dependent, so it does not discriminate. BUG-2803.
708func readBodyForDecode(r *http.Request, maxBytes int64) ([]byte, error) {
709 if r.Body == nil {
710 return nil, io.EOF
711 }
712 // The nil ResponseWriter is deliberate and its consequence is worth
713 // stating, because the comment this replaces got it wrong twice over:
714 // MaxBytesReader.Close forwards to the underlying body rather than being
715 // a no-op, and with a nil writer there is no automatic 413 — hitting the
716 // cap surfaces as a read error, which decodeJSONWithLimit wraps and every
717 // caller turns into a 400. That is the existing behaviour, unchanged
718 // here; only the claim about it is corrected (codex round 8).
719 r.Body = http.MaxBytesReader(nil, r.Body, maxBytes)
720 return io.ReadAll(r.Body)
721}
722
723// truncateBindableText cuts s to at most maxBytes bytes WITHOUT splitting a
724// rune, so the result is still valid UTF-8.

Callers 1

decodeJSONWithLimitFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected