缓存写入结束时检查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

@@ -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
@@ -129,7 +137,7 @@ func (this *MemoryWriter) Close() error {
this.storage.valuesMap[this.hash] = this.item
select {
case this.storage.dirtyChan <- types.String(this.bodySize) + "@" +this.key :
case this.storage.dirtyChan <- types.String(this.bodySize) + "@" + this.key:
atomic.AddInt64(&this.storage.totalDirtySize, this.bodySize)
default:
// remove from values map
@@ -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
}