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) Response(*Flow)
// Stream request body modifier // Stream request body modifier
StreamRequestModifier(io.Reader) io.Reader StreamRequestModifier(*Flow, io.Reader) io.Reader
// Stream response body modifier // Stream response body modifier
StreamResponseModifier(io.Reader) io.Reader StreamResponseModifier(*Flow, io.Reader) io.Reader
} }
// BaseAddon do nothing // BaseAddon do nothing
@ -56,10 +56,10 @@ func (addon *BaseAddon) Requestheaders(*Flow) {}
func (addon *BaseAddon) Request(*Flow) {} func (addon *BaseAddon) Request(*Flow) {}
func (addon *BaseAddon) Responseheaders(*Flow) {} func (addon *BaseAddon) Responseheaders(*Flow) {}
func (addon *BaseAddon) Response(*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 return in
} }
func (addon *BaseAddon) StreamResponseModifier(in io.Reader) io.Reader { func (addon *BaseAddon) StreamResponseModifier(f *Flow, in io.Reader) io.Reader {
return in return in
} }

@ -132,18 +132,16 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
logErr(log, err) logErr(log, err)
} }
} }
if response.Body != nil { if response.BodyReader != nil {
if response.BodyReader != nil { _, err := io.Copy(res, response.BodyReader)
_, err := io.Copy(res, response.BodyReader) if err != nil {
if err != nil { logErr(log, err)
logErr(log, err)
}
} }
if len(response.Body) > 0 { }
_, err := res.Write(response.Body) if response.Body != nil && len(response.Body) > 0 {
if err != nil { _, err := res.Write(response.Body)
logErr(log, err) if err != nil {
} logErr(log, err)
} }
} }
} }
@ -199,7 +197,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
} }
for _, addon := range proxy.Addons { 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) proxyReq, err := http.NewRequest(f.Request.Method, f.Request.URL.String(), reqBody)
if err != nil { if err != nil {
@ -260,7 +258,7 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
} }
} }
for _, addon := range proxy.Addons { for _, addon := range proxy.Addons {
resBody = addon.StreamResponseModifier(resBody) resBody = addon.StreamResponseModifier(f, resBody)
} }
reply(f.Response, resBody) reply(f.Response, resBody)

Loading…
Cancel
Save