Reviewed by Fefe

This commit is contained in:
Daniel A. Nagy 2015-02-10 23:45:27 +01:00
parent fc8358a261
commit 304ddcf562

View file

@ -95,7 +95,7 @@ func handler(w http.ResponseWriter, r *http.Request, dpa *DPA) {
http.ServeContent(w, r, name+".bin", time.Unix(0, 0), dpa.Retrieve(key)) http.ServeContent(w, r, name+".bin", time.Unix(0, 0), dpa.Retrieve(key))
} else if manifestMatcher.MatchString(uri) { } else if manifestMatcher.MatchString(uri) {
name := uri[1:65] name := uri[1:65]
path := uri[65:] path := uri[65:] // typically begins with a /
key := ethutil.Hex2Bytes(name) key := ethutil.Hex2Bytes(name)
manifestReader := dpa.Retrieve(key) manifestReader := dpa.Retrieve(key)
// TODO check size for oversized manifests // TODO check size for oversized manifests
@ -103,8 +103,8 @@ func handler(w http.ResponseWriter, r *http.Request, dpa *DPA) {
manifestReader.Read(manifest) manifestReader.Read(manifest)
manifestEntries := make([]manifestEntry, 0) manifestEntries := make([]manifestEntry, 0)
json.Unmarshal(manifest, &manifestEntries) json.Unmarshal(manifest, &manifestEntries)
var hash []byte
var mimeType string var mimeType string
key = nil
prefix := 0 prefix := 0
status := int16(404) status := int16(404)
for i, entry := range manifestEntries { for i, entry := range manifestEntries {
@ -123,16 +123,17 @@ func handler(w http.ResponseWriter, r *http.Request, dpa *DPA) {
pathLen := len(entry.Path) pathLen := len(entry.Path)
if len(path) >= pathLen && path[:pathLen] == entry.Path && prefix < pathLen { if len(path) >= pathLen && path[:pathLen] == entry.Path && prefix < pathLen {
prefix = pathLen prefix = pathLen
hash = ethutil.Hex2Bytes(entry.Hash) key = ethutil.Hex2Bytes(entry.Hash)
mimeType = entry.Content_type mimeType = entry.Content_type
status = entry.Status status = entry.Status
} }
} }
if hash == nil { if key == nil {
http.Error(w, "Object "+uri+" not found.", http.StatusNotFound) http.Error(w, "Object "+uri+" not found.", http.StatusNotFound)
} else { } else {
w.Header().Set("Content-Type", mimeType) w.Header().Set("Content-Type", mimeType)
http.ServeContent(w, r, "", time.Unix(0, 0), dpa.Retrieve(hash)) w.WriteHeader(status)
http.ServeContent(w, r, "", time.Unix(0, 0), dpa.Retrieve(key))
} }
} else { } else {
http.Error(w, "Object "+uri+" not found.", http.StatusNotFound) http.Error(w, "Object "+uri+" not found.", http.StatusNotFound)