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

@@ -6,13 +6,14 @@ import "errors"
// 常用的几个错误 // 常用的几个错误
var ( var (
ErrNotFound = errors.New("cache not found") ErrNotFound = errors.New("cache not found")
ErrFileIsWriting = errors.New("the cache file is updating") ErrFileIsWriting = errors.New("the cache file is updating")
ErrInvalidRange = errors.New("invalid range") ErrInvalidRange = errors.New("invalid range")
ErrEntityTooLarge = errors.New("entity too large") ErrEntityTooLarge = errors.New("entity too large")
ErrWritingUnavailable = errors.New("writing unavailable") ErrWritingUnavailable = errors.New("writing unavailable")
ErrWritingQueueFull = errors.New("writing queue full") ErrWritingQueueFull = errors.New("writing queue full")
ErrServerIsBusy = errors.New("server is busy") ErrServerIsBusy = errors.New("server is busy")
ErrUnexpectedContentLength = errors.New("unexpected content length")
) )
// CapacityError 容量错误 // CapacityError 容量错误

View File

@@ -136,6 +136,13 @@ func (this *FileWriter) Close() error {
var path = this.rawWriter.Name() 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)) err := this.WriteHeaderLength(types.Int(this.headerSize))
if err != nil { if err != nil {
fsutils.WriteBegin() fsutils.WriteBegin()

View File

@@ -121,6 +121,14 @@ func (this *MemoryWriter) Close() error {
return nil 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.storage.locker.Lock()
this.item.IsDone = true this.item.IsDone = true
var err error var err error
@@ -129,7 +137,7 @@ func (this *MemoryWriter) Close() error {
this.storage.valuesMap[this.hash] = this.item this.storage.valuesMap[this.hash] = this.item
select { 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) atomic.AddInt64(&this.storage.totalDirtySize, this.bodySize)
default: default:
// remove from values map // remove from values map
@@ -158,7 +166,6 @@ func (this *MemoryWriter) Discard() error {
this.storage.locker.Lock() this.storage.locker.Lock()
delete(this.storage.valuesMap, this.hash) delete(this.storage.valuesMap, this.hash)
this.storage.locker.Unlock() this.storage.locker.Unlock()
return nil return nil
} }