Merge pull request #23 from pepesi/feature-another-stream-modifier

支持 stream 的修改
addon-dailer
liqiang 2 years ago committed by GitHub
commit 2f519abbec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
package proxy package proxy
import ( import (
"io"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -33,6 +34,12 @@ type Addon interface {
// The full HTTP response has been read. // The full HTTP response has been read.
Response(*Flow) Response(*Flow)
// Stream request body modifier
StreamRequestModifier(*Flow, io.Reader) io.Reader
// Stream response body modifier
StreamResponseModifier(*Flow, io.Reader) io.Reader
} }
// BaseAddon do nothing // BaseAddon do nothing
@ -49,6 +56,12 @@ 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(f *Flow, in io.Reader) io.Reader {
return in
}
func (addon *BaseAddon) StreamResponseModifier(f *Flow, in io.Reader) io.Reader {
return in
}
// LogAddon log connection and flow // LogAddon log connection and flow
type LogAddon struct { type LogAddon struct {

@ -3,6 +3,7 @@ package proxy
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"io"
"net/http" "net/http"
"net/url" "net/url"
@ -96,6 +97,7 @@ type Response struct {
StatusCode int `json:"statusCode"` StatusCode int `json:"statusCode"`
Header http.Header `json:"header"` Header http.Header `json:"header"`
Body []byte `json:"-"` Body []byte `json:"-"`
BodyReader io.Reader
decodedBody []byte decodedBody []byte
decoded bool // decoded reports whether the response was sent compressed but was decoded to decodedBody. decoded bool // decoded reports whether the response was sent compressed but was decoded to decodedBody.

@ -131,7 +131,14 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if err != nil { if err != nil {
logErr(log, err) logErr(log, err)
} }
} else if response.Body != nil && len(response.Body) > 0 { }
if response.BodyReader != nil {
_, err := io.Copy(res, response.BodyReader)
if err != nil {
logErr(log, err)
}
}
if response.Body != nil && len(response.Body) > 0 {
_, err := res.Write(response.Body) _, err := res.Write(response.Body)
if err != nil { if err != nil {
logErr(log, err) logErr(log, err)
@ -189,6 +196,9 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
} }
} }
for _, addon := range proxy.Addons {
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 {
log.Error(err) log.Error(err)
@ -247,6 +257,9 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
} }
} }
} }
for _, addon := range proxy.Addons {
resBody = addon.StreamResponseModifier(f, resBody)
}
reply(f.Response, resBody) reply(f.Response, resBody)
} }

Loading…
Cancel
Save