From 396be08c27a62901cc783f41d7b3b00d7839ff34 Mon Sep 17 00:00:00 2001 From: "yu.deng" Date: Thu, 30 Jun 2022 15:20:06 +0800 Subject: [PATCH] =?UTF-8?q?proxy.Response=20=E6=B7=BB=E5=8A=A0=20BodyReade?= =?UTF-8?q?r=EF=BC=8C=E6=94=AF=E6=8C=81=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9A=84stream=20body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/flow.go | 2 ++ proxy/proxy.go | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/proxy/flow.go b/proxy/flow.go index 2bec3b6..2c09c87 100644 --- a/proxy/flow.go +++ b/proxy/flow.go @@ -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. diff --git a/proxy/proxy.go b/proxy/proxy.go index 88d87a4..8776329 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -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) + } } } }