Partial Content从源站读取数据时验证本地缓存的Content-MD5是否一致
This commit is contained in:
@@ -42,8 +42,25 @@ func (this *HTTPRequestPartialReader) Read(p []byte) (n int, err error) {
|
||||
err = io.ErrUnexpectedEOF
|
||||
return
|
||||
}
|
||||
|
||||
this.resp = resp
|
||||
|
||||
// 对比Content-MD5
|
||||
partialReader, ok := this.cacheReader.(*caches.PartialFileReader)
|
||||
if ok {
|
||||
if partialReader.Ranges().Version >= 2 && resp.Header.Get("Content-MD5") != partialReader.Ranges().ContentMD5 {
|
||||
err = io.ErrUnexpectedEOF
|
||||
|
||||
var storage = this.req.writer.cacheStorage
|
||||
if storage != nil {
|
||||
_ = storage.Delete(this.req.cacheKey + caches.SuffixPartial)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 准备写入
|
||||
this.prepareCacheWriter()
|
||||
}
|
||||
|
||||
|
||||
@@ -403,6 +403,11 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) {
|
||||
return
|
||||
}
|
||||
partialWriter.SetBodyLength(total)
|
||||
|
||||
var contentMD5 = this.rawWriter.Header().Get("Content-MD5")
|
||||
if len(contentMD5) > 0 {
|
||||
partialWriter.SetContentMD5(contentMD5)
|
||||
}
|
||||
}
|
||||
var filterReader = readers.NewFilterReaderCloser(resp.Body)
|
||||
this.cacheIsFinished = true
|
||||
@@ -462,6 +467,11 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) {
|
||||
// 写入total
|
||||
if !writtenTotal && total > 0 {
|
||||
partialWriter.SetBodyLength(total)
|
||||
var contentMD5 = this.rawWriter.Header().Get("Content-MD5")
|
||||
if len(contentMD5) > 0 {
|
||||
partialWriter.SetContentMD5(contentMD5)
|
||||
}
|
||||
|
||||
writtenTotal = true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user