volume: report Content-MD5 in response header

This commit is contained in:
Chris Lu 2020-08-06 05:22:53 -07:00
parent 4703a3daad
commit 4ecfa9879d
4 changed files with 11 additions and 7 deletions

View file

@ -18,7 +18,7 @@ type MockClient struct {
} }
func (m *MockClient) Do(req *http.Request) (*http.Response, error) { 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 { if m.needleHandling != nil {
m.needleHandling(n, originalSize, err) m.needleHandling(n, originalSize, err)
} }

View file

@ -42,7 +42,7 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
return 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 { if ne != nil {
writeJsonError(w, r, http.StatusBadRequest, ne) writeJsonError(w, r, http.StatusBadRequest, ne)
return return
@ -70,6 +70,7 @@ func (vs *VolumeServer) PostHandler(w http.ResponseWriter, r *http.Request) {
ret.ETag = reqNeedle.Etag() ret.ETag = reqNeedle.Etag()
ret.Mime = string(reqNeedle.Mime) ret.Mime = string(reqNeedle.Mime)
setEtag(w, ret.ETag) setEtag(w, ret.ETag)
w.Header().Set("Content-MD5", contentMd5)
writeJsonQuiet(w, r, httpStatus, ret) writeJsonQuiet(w, r, httpStatus, ret)
} }

View file

@ -48,7 +48,7 @@ func (n *Needle) String() (str string) {
return 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) n = new(Needle)
pu, e := ParseUpload(r, sizeLimit) pu, e := ParseUpload(r, sizeLimit)
if e != nil { if e != nil {

View file

@ -29,6 +29,7 @@ type ParsedUpload struct {
Ttl *TTL Ttl *TTL
IsChunkedFile bool IsChunkedFile bool
UncompressedData []byte UncompressedData []byte
ContentMd5 string
} }
func ParseUpload(r *http.Request, sizeLimit int64) (pu *ParsedUpload, e error) { 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 != "" { if expectedChecksum := r.Header.Get("Content-MD5"); expectedChecksum != "" {
h := md5.New() if expectedChecksum != pu.ContentMd5 {
h.Write(pu.UncompressedData) e = fmt.Errorf("Content-MD5 did not match md5 of file data [%s] != [%s]", expectedChecksum, pu.ContentMd5)
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)
return return
} }
} }