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

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

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

@ -131,10 +131,19 @@ func (proxy *Proxy) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if err != nil {
logErr(log, err)
}
} else if response.Body != nil && len(response.Body) > 0 {
_, err := res.Write(response.Body)
if err != nil {
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 {
_, err := res.Write(response.Body)
if err != nil {
logErr(log, err)
}
}
}
}

Loading…
Cancel
Save