-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubenricher-mime.go
83 lines (69 loc) · 1.68 KB
/
subenricher-mime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"io"
"net/http"
"github.com/gabriel-vasile/mimetype"
ecsx "github.com/telkomindonesia/httpmsg-enricher/ecs/custom"
"go.uber.org/multierr"
)
type writableMimeReader struct {
r *io.PipeReader
w *io.PipeWriter
mime string
}
func newWritableMimeReader() *writableMimeReader {
r, w := io.Pipe()
me := &writableMimeReader{
r: r,
w: w,
}
go me.detectMime()
return me
}
func (me *writableMimeReader) detectMime() {
mtype, err := mimetype.DetectReader(me.r)
if err != nil {
return
}
me.mime = mtype.String()
io.Copy(io.Discard, me.r)
}
func (me *writableMimeReader) Close() error {
return me.w.Close()
}
var _ subEnricher = &mimeEnricher{}
type mimeEnricher struct {
req *writableMimeReader
res *writableMimeReader
}
func (erc *mimeEnricher) Close() (err error) {
if errt := erc.req.Close(); errt != nil {
err = multierr.Append(err, errt)
}
if errt := erc.res.Close(); errt != nil {
err = multierr.Append(err, errt)
}
return
}
func (erc *mimeEnricher) requestBodyWriter() io.WriteCloser {
return erc.req.w
}
func (erc *mimeEnricher) processRequest(req *http.Request) (err error) { return nil }
func (erc *mimeEnricher) responseBodyWriter() io.WriteCloser {
return erc.res.w
}
func (erc *mimeEnricher) processResponse(res *http.Response) (err error) { return nil }
func (erc *mimeEnricher) enrich(doc *ecsx.Document, msg *httpRecordedMessage) (err error) {
if doc.HTTP == nil {
doc.HTTP = &ecsx.HTTP{}
}
if doc.HTTP.Request == nil {
doc.HTTP.Request = &ecsx.HTTPRequest{}
}
if doc.HTTP.Response == nil {
doc.HTTP.Response = &ecsx.HTTPResponse{}
}
doc.HTTP.Request.MimeType = erc.req.mime
doc.HTTP.Response.MimeType = erc.res.mime
return
}