缓存写入结束时检查Content-Length是否和实际内容长度一致

This commit is contained in:
GoEdgeLab
2024-04-05 11:45:18 +08:00
parent 1377f25fa4
commit 2213934795
3 changed files with 24 additions and 9 deletions

View File

@@ -13,6 +13,7 @@ var (
ErrWritingUnavailable = errors.New("writing unavailable")
ErrWritingQueueFull = errors.New("writing queue full")
ErrServerIsBusy = errors.New("server is busy")
ErrUnexpectedContentLength = errors.New("unexpected content length")
)
// CapacityError 容量错误

View File

@@ -136,6 +136,13 @@ func (this *FileWriter) Close() error {
var path = this.rawWriter.Name()
// check content length
if this.metaBodySize > 0 && this.bodySize != this.metaBodySize {
_ = this.rawWriter.Close()
_ = os.Remove(path)
return ErrUnexpectedContentLength
}
err := this.WriteHeaderLength(types.Int(this.headerSize))
if err != nil {
fsutils.WriteBegin()

View File

@@ -121,6 +121,14 @@ func (this *MemoryWriter) Close() error {
return nil
}
// check content length
if this.expectedBodySize > 0 && this.bodySize != this.expectedBodySize {
this.storage.locker.Lock()
delete(this.storage.valuesMap, this.hash)
this.storage.locker.Unlock()
return ErrUnexpectedContentLength
}
this.storage.locker.Lock()
this.item.IsDone = true
var err error
@@ -158,7 +166,6 @@ func (this *MemoryWriter) Discard() error {
this.storage.locker.Lock()
delete(this.storage.valuesMap, this.hash)
this.storage.locker.Unlock()
return nil
}