dumper only dump text content-type

addon-dailer
lqqyt2423 4 years ago
parent cabb1e9a20
commit 9ded568d41

@ -66,9 +66,9 @@ func (d *Dumper) dump(f *flow.Flow) {
} }
buf.WriteString("\r\n") buf.WriteString("\r\n")
if d.level == 1 && f.Response.Body != nil && len(f.Response.Body) > 0 { if d.level == 1 && f.Response.Body != nil && len(f.Response.Body) > 0 && f.Response.IsTextContentType() {
body, _ := f.Response.DecodedBody() body, err := f.Response.DecodedBody()
if len(body) > 0 && CanPrint(body) { if err == nil && body != nil && len(body) > 0 {
buf.Write(body) buf.Write(body)
buf.WriteString("\r\n\r\n") buf.WriteString("\r\n\r\n")
} }

@ -5,44 +5,66 @@ import (
"compress/gzip" "compress/gzip"
"errors" "errors"
"io" "io"
"strings"
) )
// handle http header: content-encoding // handle http header: content-encoding
var EncodingNotSupport = errors.New("content-encoding not support") var EncodingNotSupport = errors.New("content-encoding not support")
func (r *Response) DecodedBody() ([]byte, bool) { var textContentTypes = []string{
"text",
"javascript",
"json",
}
func (r *Response) IsTextContentType() bool {
contentType := r.Header.Get("Content-Type")
if contentType == "" {
return false
}
for _, substr := range textContentTypes {
if strings.Contains(contentType, substr) {
return true
}
}
return false
}
func (r *Response) DecodedBody() ([]byte, error) {
if r.decodedBody != nil { if r.decodedBody != nil {
return r.decodedBody, r.decoded return r.decodedBody, nil
} }
if r.decodedErr != nil { if r.decodedErr != nil {
return nil, r.decoded return nil, r.decodedErr
} }
if r.Body == nil { if r.Body == nil {
return nil, r.decoded return nil, nil
} }
if len(r.Body) == 0 { if len(r.Body) == 0 {
r.decodedBody = r.Body r.decodedBody = r.Body
return r.decodedBody, r.decoded return r.decodedBody, nil
} }
enc := r.Header.Get("Content-Encoding") enc := r.Header.Get("Content-Encoding")
if enc == "" { if enc == "" {
r.decodedBody = r.Body r.decodedBody = r.Body
return r.decodedBody, r.decoded return r.decodedBody, nil
} }
r.decodedBody, r.decodedErr = Decode(enc, r.Body) decodedBody, decodedErr := Decode(enc, r.Body)
if r.decodedErr != nil { if decodedErr != nil {
r.decodedErr = decodedErr
log.Error(r.decodedErr) log.Error(r.decodedErr)
} else { return nil, decodedErr
r.decoded = true
} }
return r.decodedBody, r.decoded r.decodedBody = decodedBody
r.decoded = true
return r.decodedBody, nil
} }
// 当 Response.Body 替换为解压的内容时调用 // 当 Response.Body 替换为解压的内容时调用

Loading…
Cancel
Save