middleware/logger/body_util.go

32 lines
467 B
Go
Raw Permalink Normal View History

2020-11-07 11:59:33 +00:00
package logger
import (
"bytes"
"io"
"regexp"
)
var regexpMultiWhitespace = regexp.MustCompile(`[\s\p{Zs}]{2,}`)
func peek(r io.Reader, n int64) (io.Reader, string, bool, error) {
if n < 0 {
n = 0
}
var buf bytes.Buffer
_, err := io.CopyN(&buf, r, n+1)
if err == io.EOF {
return &buf, buf.String(), false, nil
}
if err != nil {
return nil, "", false, err
}
s := buf.String()
s = s[:len(s)-1]
return io.MultiReader(&buf, r), s, true, nil
}