HttpWriter暴露两个方法/默认Buffer为4K

This commit is contained in:
刘祥超
2022-03-24 21:42:03 +08:00
parent 99227bb4f2
commit f6a983e683
2 changed files with 24 additions and 0 deletions

View File

@@ -1584,6 +1584,9 @@ func (this *HTTPRequest) addError(err error) {
// 计算合适的buffer size // 计算合适的buffer size
func (this *HTTPRequest) bytePool(contentLength int64) *utils.BytePool { func (this *HTTPRequest) bytePool(contentLength int64) *utils.BytePool {
if contentLength < 0 {
return utils.BytePool4k
}
if contentLength < 8192 { // 8K if contentLength < 8192 { // 8K
return utils.BytePool1k return utils.BytePool1k
} }

View File

@@ -740,6 +740,27 @@ func (this *HTTPWriter) SendFile(status int, path string) (int64, error) {
return written, nil 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 读取状态码 // StatusCode 读取状态码
func (this *HTTPWriter) StatusCode() int { func (this *HTTPWriter) StatusCode() int {
if this.statusCode == 0 { if this.statusCode == 0 {