From 69e62acfab75606a3526a48a353cccaeba9b5e1e Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 24 Mar 2022 21:42:03 +0800 Subject: [PATCH] =?UTF-8?q?HttpWriter=E6=9A=B4=E9=9C=B2=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=96=B9=E6=B3=95/=E9=BB=98=E8=AE=A4Buffer=E4=B8=BA4K?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request.go | 3 +++ internal/nodes/http_writer.go | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/internal/nodes/http_request.go b/internal/nodes/http_request.go index c268ffd..6fcd203 100644 --- a/internal/nodes/http_request.go +++ b/internal/nodes/http_request.go @@ -1584,6 +1584,9 @@ func (this *HTTPRequest) addError(err error) { // 计算合适的buffer size func (this *HTTPRequest) bytePool(contentLength int64) *utils.BytePool { + if contentLength < 0 { + return utils.BytePool4k + } if contentLength < 8192 { // 8K return utils.BytePool1k } diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index ae83891..960b843 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -740,6 +740,27 @@ func (this *HTTPWriter) SendFile(status int, path string) (int64, error) { return written, nil } +// SendResp 发送响应对象 +func (this *HTTPWriter) SendResp(resp *http.Response) (int64, error) { + this.isFinished = true + + for k, v := range resp.Header { + this.SetHeader(k, v) + } + this.WriteHeader(resp.StatusCode) + var bufPool = this.req.bytePool(resp.ContentLength) + var buf = bufPool.Get() + defer bufPool.Put(buf) + + return io.CopyBuffer(this, resp.Body, buf) +} + +// Redirect 跳转 +func (this *HTTPWriter) Redirect(status int, url string) { + http.Redirect(this.rawWriter, this.req.RawReq, url, status) + this.isFinished = true +} + // StatusCode 读取状态码 func (this *HTTPWriter) StatusCode() int { if this.statusCode == 0 {