From 530927db64ea523db2efb4cff6fe5f12d9378d03 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 22 Jul 2014 00:24:50 -0700 Subject: [PATCH] Add Etag support --- go/storage/crc.go | 8 ++++++++ go/weed/weed_server/volume_server_handlers.go | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/go/storage/crc.go b/go/storage/crc.go index 198352e68..41f7f6d00 100644 --- a/go/storage/crc.go +++ b/go/storage/crc.go @@ -1,6 +1,8 @@ package storage import ( + "code.google.com/p/weed-fs/go/util" + "fmt" "hash/crc32" ) @@ -19,3 +21,9 @@ func (c CRC) Update(b []byte) CRC { func (c CRC) Value() uint32 { 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) +} diff --git a/go/weed/weed_server/volume_server_handlers.go b/go/weed/weed_server/volume_server_handlers.go index 303d63207..eed198e4a 100644 --- a/go/weed/weed_server/volume_server_handlers.go +++ b/go/weed/weed_server/volume_server_handlers.go @@ -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 == "" { filename = string(n.Name) dotIndex := strings.LastIndex(filename, ".")