优化结构
This commit is contained in:
@@ -12,14 +12,16 @@ import (
|
||||
// OperationAudit consumes these values after the handler returns, avoiding a
|
||||
// second body read/writer wrapper.
|
||||
const (
|
||||
ctxReqBodyKey = "kra_req_body"
|
||||
ctxRespBufferKey = "kra_resp_buffer"
|
||||
ctxReqBodyKey = "kra_req_body"
|
||||
ctxRespBufferKey = "kra_resp_buffer"
|
||||
ctxRespTruncatedKey = "kra_resp_truncated"
|
||||
)
|
||||
|
||||
type captureWriter struct {
|
||||
gin.ResponseWriter
|
||||
body bytes.Buffer
|
||||
maxBytes int
|
||||
body bytes.Buffer
|
||||
maxBytes int
|
||||
truncated bool
|
||||
}
|
||||
|
||||
func (w *captureWriter) Write(data []byte) (int, error) {
|
||||
@@ -31,6 +33,7 @@ func (w *captureWriter) Write(data []byte) (int, error) {
|
||||
remaining := limit - w.body.Len()
|
||||
if len(data) > remaining {
|
||||
_, _ = w.body.Write(data[:remaining])
|
||||
w.truncated = true
|
||||
} else {
|
||||
_, _ = w.body.Write(data)
|
||||
}
|
||||
@@ -38,6 +41,8 @@ func (w *captureWriter) Write(data []byte) (int, error) {
|
||||
return w.ResponseWriter.Write(data)
|
||||
}
|
||||
|
||||
func (w *captureWriter) Truncated() bool { return w != nil && w.truncated }
|
||||
|
||||
func redactJSON(raw []byte, contentType string, limit int) string {
|
||||
if len(raw) == 0 {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user