mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
*) Limit response http body or json response size to FN_MAX_RESPONSE_SIZE (default unlimited) *) If limits are exceeded 502 is returned with 'body too large' in the error message
35 lines
630 B
Go
35 lines
630 B
Go
package common
|
|
|
|
import (
|
|
"io"
|
|
)
|
|
|
|
type clampWriter struct {
|
|
w io.Writer
|
|
remaining int64
|
|
overflowErr error
|
|
}
|
|
|
|
func NewClampWriter(buf io.Writer, maxResponseSize uint64, overflowErr error) io.Writer {
|
|
if maxResponseSize != 0 {
|
|
return &clampWriter{w: buf, remaining: int64(maxResponseSize), overflowErr: overflowErr}
|
|
}
|
|
return buf
|
|
}
|
|
|
|
func (g *clampWriter) Write(p []byte) (int, error) {
|
|
if g.remaining <= 0 {
|
|
return 0, g.overflowErr
|
|
}
|
|
if int64(len(p)) > g.remaining {
|
|
p = p[0:g.remaining]
|
|
}
|
|
|
|
n, err := g.w.Write(p)
|
|
g.remaining -= int64(n)
|
|
if g.remaining <= 0 {
|
|
err = g.overflowErr
|
|
}
|
|
return n, err
|
|
}
|