P0 fixes: - ModelError.Is(): use exact matching instead of substring contains() - shouldClearStickySession: add context param for cancellation/tracing P1 fixes: - TODO stubs: return 501 Not Implemented errors - validateInstanceSignature: deduplicate to shared validateCodeSignature() - Error messages: standardize to English only - http.go: remove pseudo if-else with duplicate branches
38 lines
838 B
Go
38 lines
838 B
Go
package httputil
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
requestBodyReadInitCap = 512
|
|
requestBodyReadMaxInitCap = 1 << 20
|
|
)
|
|
|
|
// ReadRequestBodyWithPrealloc reads request body with preallocated buffer based on content length.
|
|
func ReadRequestBodyWithPrealloc(req *http.Request) ([]byte, error) {
|
|
if req == nil || req.Body == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
capHint := requestBodyReadInitCap
|
|
if req.ContentLength > 0 {
|
|
switch {
|
|
case req.ContentLength < int64(requestBodyReadInitCap):
|
|
capHint = requestBodyReadInitCap
|
|
case req.ContentLength > int64(requestBodyReadMaxInitCap):
|
|
capHint = requestBodyReadMaxInitCap
|
|
default:
|
|
capHint = int(req.ContentLength)
|
|
}
|
|
}
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, capHint))
|
|
if _, err := io.Copy(buf, req.Body); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|