need flow context in stream modifier provide extra info

addon-dailer
yu.deng 2 years ago
parent 396be08c27
commit 9080c0e116

@ -36,10 +36,10 @@ type Addon interface {
Response(*Flow)
// Stream request body modifier
StreamRequestModifier(io.Reader) io.Reader
StreamRequestModifier(*Flow, io.Reader) io.Reader
// Stream response body modifier
StreamResponseModifier(io.Reader) io.Reader
StreamResponseModifier(*Flow, io.Reader) io.Reader
}
// BaseAddon do nothing
@ -56,10 +56,10 @@ func (addon *BaseAddon) Requestheaders(*Flow) {}
func (addon *BaseAddon) Request(*Flow) {}
func (addon *BaseAddon) Responseheaders(*Flow) {}
func (addon *BaseAddon) Response(*Flow) {}
func (addon *BaseAddon) StreamRequestModifier(in io.Reader) io.Reader {
func (addon *BaseAddon) StreamRequestModifier(f *Flow, in io.Reader) io.Reader {
return in
}
func (addon *BaseAddon) StreamResponseModifier(in io.Reader) io.Reader {
func (addon *BaseAddon) StreamResponseModifier(f *Flow, in io.Reader) io.Reader {
return in
}

@ -132,21 +132,19 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
logErr(log, err)
}
}
if response.Body != nil {
if response.BodyReader != nil {
_, err := io.Copy(res, response.BodyReader)
if err != nil {
logErr(log, err)
}
}
if len(response.Body) > 0 {
if response.Body != nil && len(response.Body) > 0 {
_, err := res.Write(response.Body)
if err != nil {
logErr(log, err)
}
}
}
}
// when addons panic
defer func() {
@ -199,7 +197,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
}
for _, addon := range proxy.Addons {
reqBody = addon.StreamRequestModifier(reqBody)
reqBody = addon.StreamRequestModifier(f, reqBody)
}
proxyReq, err := http.NewRequest(f.Request.Method, f.Request.URL.String(), reqBody)
if err != nil {
@ -260,7 +258,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
}
}
for _, addon := range proxy.Addons {
resBody = addon.StreamResponseModifier(resBody)
resBody = addon.StreamResponseModifier(f, resBody)
}
reply(f.Response, resBody)

Loading…
Cancel
Save