add http body for handler recorder
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -24,3 +26,47 @@ func GetClientIP(req *http.Request) net.IP {
|
||||
|
||||
return net.ParseIP(sip)
|
||||
}
|
||||
|
||||
type Body struct {
|
||||
r io.ReadCloser
|
||||
buf bytes.Buffer
|
||||
length int64
|
||||
recordSize int
|
||||
}
|
||||
|
||||
func NewBody(r io.ReadCloser, maxRecordSize int) *Body {
|
||||
p := &Body{
|
||||
r: r,
|
||||
recordSize: maxRecordSize,
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Body) Read(b []byte) (n int, err error) {
|
||||
n, err = p.r.Read(b)
|
||||
p.length += int64(n)
|
||||
|
||||
if p.recordSize > 0 {
|
||||
b = b[:n]
|
||||
if n > p.recordSize {
|
||||
b = b[:p.recordSize]
|
||||
}
|
||||
p.buf.Write(b)
|
||||
p.recordSize -= n
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (p *Body) Close() error {
|
||||
return p.r.Close()
|
||||
}
|
||||
|
||||
func (p *Body) Content() []byte {
|
||||
return p.buf.Bytes()
|
||||
}
|
||||
|
||||
func (p *Body) Length() int64 {
|
||||
return p.length
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user