mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Response size clamp (#786)
*) 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
This commit is contained in:
34
api/common/io_utils.go
Normal file
34
api/common/io_utils.go
Normal file
@@ -0,0 +1,34 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user