Prepare 准备输出
(resp *http.Response, size int64, status int, enableCache bool)
| 103 | |
| 104 | // Prepare 准备输出 |
| 105 | func (this *HTTPWriter) Prepare(resp *http.Response, size int64, status int, enableCache bool) (delayHeaders bool) { |
| 106 | // 清理以前数据,防止重试时发生异常错误 |
| 107 | if this.compressionCacheWriter != nil { |
| 108 | _ = this.compressionCacheWriter.Discard() |
| 109 | this.compressionCacheWriter = nil |
| 110 | } |
| 111 | |
| 112 | if this.cacheWriter != nil { |
| 113 | _ = this.cacheWriter.Discard() |
| 114 | this.cacheWriter = nil |
| 115 | } |
| 116 | |
| 117 | // 新的请求相关数据 |
| 118 | this.size = size |
| 119 | this.statusCode = status |
| 120 | |
| 121 | // 是否为区间请求 |
| 122 | this.isPartial = status == http.StatusPartialContent |
| 123 | |
| 124 | // 不支持对GET以外的方法返回的Partial内容的缓存 |
| 125 | if this.isPartial && this.req.Method() != http.MethodGet { |
| 126 | enableCache = false |
| 127 | } |
| 128 | |
| 129 | if resp != nil && resp.Body != nil { |
| 130 | cacheReader, ok := resp.Body.(caches.Reader) |
| 131 | if ok { |
| 132 | this.cacheReader = cacheReader |
| 133 | } |
| 134 | |
| 135 | this.rawReader = resp.Body |
| 136 | |
| 137 | if enableCache { |
| 138 | this.PrepareCache(resp, size) |
| 139 | } |
| 140 | if !this.isPartial { |
| 141 | this.PrepareWebP(resp, size) |
| 142 | } |
| 143 | this.PrepareCompression(resp, size) |
| 144 | } |
| 145 | |
| 146 | // 是否限速写入 |
| 147 | if this.req.web != nil && |
| 148 | this.req.web.RequestLimit != nil && |
| 149 | this.req.web.RequestLimit.IsOn && |
| 150 | this.req.web.RequestLimit.OutBandwidthPerConnBytes() > 0 { |
| 151 | this.writer = writers.NewRateLimitWriter(this.req.RawReq.Context(), this.writer, this.req.web.RequestLimit.OutBandwidthPerConnBytes()) |
| 152 | } |
| 153 | |
| 154 | return |
| 155 | } |
| 156 | |
| 157 | // PrepareCache 准备缓存 |
| 158 | func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) { |
no test coverage detected