From 8d1c447aac9d57d08d4dcdbd4b9d1cfac645589f Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Thu, 22 Jan 2015 14:24:18 -0800 Subject: [PATCH] Adding HEAD verb to minioapi --- pkg/storage/inmemory/inmemory.go | 8 ++++++-- pkg/storage/storage.go | 2 +- pkg/webapi/minioapi/minioapi.go | 34 ++++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/pkg/storage/inmemory/inmemory.go b/pkg/storage/inmemory/inmemory.go index 1a3b72fa4..386492923 100644 --- a/pkg/storage/inmemory/inmemory.go +++ b/pkg/storage/inmemory/inmemory.go @@ -115,8 +115,12 @@ func start(ctrlChannel <-chan string, errorChannel chan<- error) { close(errorChannel) } -func (storage *storage) GetObjectMetadata(bucket, key string) mstorage.ObjectMetadata { +func (storage *storage) GetObjectMetadata(bucket, key string) (mstorage.ObjectMetadata, error) { objectKey := bucket + ":" + key - return storage.objectdata[objectKey].metadata + if object, ok := storage.objectdata[objectKey]; ok == true { + return object.metadata, nil + } else { + return mstorage.ObjectMetadata{}, mstorage.ObjectNotFound{Bucket: bucket, Path: key} + } } diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index a80e91b66..64000f826 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -28,7 +28,7 @@ type Storage interface { ListBuckets(prefix string) []BucketMetadata // Object Operations - GetObjectMetadata(bucket string, object string) ObjectMetadata + GetObjectMetadata(bucket string, object string) (ObjectMetadata, error) CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error) StoreObject(bucket string, key string, data io.Reader) error ListObjects(bucket, prefix string, count int) []ObjectMetadata diff --git a/pkg/webapi/minioapi/minioapi.go b/pkg/webapi/minioapi/minioapi.go index 396a5ab8d..a61a1e6fd 100644 --- a/pkg/webapi/minioapi/minioapi.go +++ b/pkg/webapi/minioapi/minioapi.go @@ -22,6 +22,7 @@ import ( "encoding/xml" "log" "net/http" + "strconv" "time" "github.com/gorilla/mux" @@ -47,6 +48,7 @@ func HttpHandler(storage mstorage.Storage) http.Handler { mux.HandleFunc("/{bucket}/", api.listObjectsHandler).Methods("GET") mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET") mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT") + mux.HandleFunc("/{bucket}/{object:.*}", api.headObjectHandler).Methods("HEAD") return mux } @@ -55,11 +57,12 @@ func (server *minioApi) getObjectHandler(w http.ResponseWriter, req *http.Reques bucket := vars["bucket"] object := vars["object"] - metadata := server.storage.GetObjectMetadata(bucket, object) + metadata, err := server.storage.GetObjectMetadata(bucket, object) lastModified := metadata.Created.Format(time.RFC1123) w.Header().Set("ETag", metadata.ETag) w.Header().Set("Last-Modified", lastModified) - _, err := server.storage.CopyObjectToWriter(w, bucket, object) + w.Header().Set("Content-Length", strconv.Itoa(metadata.Size)) + w.Header().Set("Content-Type", "text/plain") switch err := err.(type) { case nil: // success { @@ -76,6 +79,33 @@ func (server *minioApi) getObjectHandler(w http.ResponseWriter, req *http.Reques w.WriteHeader(http.StatusInternalServerError) } } + if _, err := server.storage.CopyObjectToWriter(w, bucket, object); err != nil { + w.WriteHeader(http.StatusBadRequest) + } +} + +func (server *minioApi) headObjectHandler(w http.ResponseWriter, req *http.Request) { + vars := mux.Vars(req) + bucket := vars["bucket"] + object := vars["object"] + + metadata, err := server.storage.GetObjectMetadata(bucket, object) + switch err := err.(type) { + case nil: // success + case mstorage.ObjectNotFound: + log.Println(err) + w.WriteHeader(http.StatusNotFound) + return + default: + log.Println(err) + w.WriteHeader(http.StatusBadRequest) + return + } + lastModified := metadata.Created.Format(time.RFC1123) + w.Header().Set("ETag", metadata.ETag) + w.Header().Set("Last-Modified", lastModified) + w.Header().Set("Content-Length", strconv.Itoa(metadata.Size)) + w.Header().Set("Content-Type", "text/plain") } func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Request) {