proxy.Response 添加 BodyReader,支持添加自定义的stream body

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

@ -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,13 +131,22 @@ 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.Body != nil {
if response.BodyReader != nil {
_, err := io.Copy(res, response.BodyReader)
if err != nil {
logErr(log, err)
}
}
if 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)
} }
} }
} }
}
// when addons panic // when addons panic
defer func() { defer func() {

Loading…
Cancel
Save