Add Etag support

This commit is contained in:
Chris Lu
2014-07-22 00:24:50 -07:00
parent 5d88cec2df
commit 530927db64
2 changed files with 14 additions and 0 deletions

View File

@@ -1,6 +1,8 @@
package storage package storage
import ( import (
"code.google.com/p/weed-fs/go/util"
"fmt"
"hash/crc32" "hash/crc32"
) )
@@ -19,3 +21,9 @@ func (c CRC) Update(b []byte) CRC {
func (c CRC) Value() uint32 { func (c CRC) Value() uint32 {
return uint32(c>>15|c<<17) + 0xa282ead8 return uint32(c>>15|c<<17) + 0xa282ead8
} }
func (n *Needle) Etag() string {
bits := make([]byte, 4)
util.Uint32toBytes(bits, uint32(n.Checksum))
return fmt.Sprintf("%x", bits)
}

View File

@@ -90,6 +90,12 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
} }
} }
} }
etag := n.Etag()
if inm := r.Header.Get("If-None-Match"); inm == etag {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("Etag", etag)
if n.NameSize > 0 && filename == "" { if n.NameSize > 0 && filename == "" {
filename = string(n.Name) filename = string(n.Name)
dotIndex := strings.LastIndex(filename, ".") dotIndex := strings.LastIndex(filename, ".")