From 4ecfa9879d8f7c70b4515ac31440a111ae274dc3 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 6 Aug 2020 05:22:53 -0700 Subject: [PATCH] volume: report Content-MD5 in response header --- weed/operation/needle_parse_test.go | 2 +- weed/server/volume_server_handlers_write.go | 3 ++- weed/storage/needle/needle.go | 2 +- weed/storage/needle/needle_parse_upload.go | 11 +++++++---- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/weed/operation/needle_parse_test.go b/weed/operation/needle_parse_test.go index 74d58d1b5..177c620f4 100644 --- a/weed/operation/needle_parse_test.go +++ b/weed/operation/needle_parse_test.go @@ -18,7 +18,7 @@ type MockClient struct { } func (m *MockClient) Do(req *http.Request) (*http.Response, error) { - n, originalSize, err := needle.CreateNeedleFromRequest(req, false, 1024*1024) + n, originalSize, _, err := needle.CreateNeedleFromRequest(req, false, 1024*1024) if m.needleHandling != nil { m.needleHandling(n, originalSize, err) } diff --git a/weed/server/volume_server_handlers_write.go b/weed/server/volume_server_handlers_write.go index 74dad28de..b4f8a90b2 100644 --- a/weed/server/volume_server_handlers_write.go +++ b/weed/server/volume_server_handlers_write.go @@ -42,7 +42,7 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) { return } - reqNeedle, originalSize, ne := needle.CreateNeedleFromRequest(r, vs.FixJpgOrientation, vs.fileSizeLimitBytes) + reqNeedle, originalSize, contentMd5, ne := needle.CreateNeedleFromRequest(r, vs.FixJpgOrientation, vs.fileSizeLimitBytes) if ne != nil { writeJsonError(w, r, http.StatusBadRequest, ne) return @@ -70,6 +70,7 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) { ret.ETag = reqNeedle.Etag() ret.Mime = string(reqNeedle.Mime) setEtag(w, ret.ETag) + w.Header().Set("Content-MD5", contentMd5) writeJsonQuiet(w, r, httpStatus, ret) } diff --git a/weed/storage/needle/needle.go b/weed/storage/needle/needle.go index 150d6ee4b..eb1d9537b 100644 --- a/weed/storage/needle/needle.go +++ b/weed/storage/needle/needle.go @@ -48,7 +48,7 @@ func (n *Needle) String() (str string) { return } -func CreateNeedleFromRequest(r *http.Request, fixJpgOrientation bool, sizeLimit int64) (n *Needle, originalSize int, e error) { +func CreateNeedleFromRequest(r *http.Request, fixJpgOrientation bool, sizeLimit int64) (n *Needle, originalSize int, md5 string, e error) { n = new(Needle) pu, e := ParseUpload(r, sizeLimit) if e != nil { diff --git a/weed/storage/needle/needle_parse_upload.go b/weed/storage/needle/needle_parse_upload.go index dd678f87f..d97ca3101 100644 --- a/weed/storage/needle/needle_parse_upload.go +++ b/weed/storage/needle/needle_parse_upload.go @@ -29,6 +29,7 @@ type ParsedUpload struct { Ttl *TTL IsChunkedFile bool UncompressedData []byte + ContentMd5 string } func ParseUpload(r *http.Request, sizeLimit int64) (pu *ParsedUpload, e error) { @@ -83,11 +84,13 @@ func ParseUpload(r *http.Request, sizeLimit int64) (pu *ParsedUpload, e error) { } } + // md5 + h := md5.New() + h.Write(pu.UncompressedData) + pu.ContentMd5 = base64.StdEncoding.EncodeToString(h.Sum(nil)) if expectedChecksum := r.Header.Get("Content-MD5"); expectedChecksum != "" { - h := md5.New() - h.Write(pu.UncompressedData) - if receivedChecksum := base64.StdEncoding.EncodeToString(h.Sum(nil)); expectedChecksum != receivedChecksum { - e = fmt.Errorf("Content-MD5 did not match md5 of file data [%s] != [%s]", expectedChecksum, receivedChecksum) + if expectedChecksum != pu.ContentMd5 { + e = fmt.Errorf("Content-MD5 did not match md5 of file data [%s] != [%s]", expectedChecksum, pu.ContentMd5) return } }