From 5c5761ae1679dc7d35006977547197dc5e2c280d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 15 May 2015 19:23:30 -0700 Subject: [PATCH 1/2] Remove unnecessary GetObjectMetadata prefix argument --- pkg/api/api_object_handlers.go | 4 +- pkg/api/api_test.go | 62 ++++++++++++++-------------- pkg/storage/drivers/api_testsuite.go | 8 ++-- pkg/storage/drivers/donut/donut.go | 3 +- pkg/storage/drivers/driver.go | 2 +- pkg/storage/drivers/memory/memory.go | 4 +- pkg/storage/drivers/mocks/Driver.go | 4 +- 7 files changed, 43 insertions(+), 44 deletions(-) diff --git a/pkg/api/api_object_handlers.go b/pkg/api/api_object_handlers.go index fc105ac84..dbf3caabd 100644 --- a/pkg/api/api_object_handlers.go +++ b/pkg/api/api_object_handlers.go @@ -49,7 +49,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques bucket = vars["bucket"] object = vars["object"] - metadata, err := server.driver.GetObjectMetadata(bucket, object, "") + metadata, err := server.driver.GetObjectMetadata(bucket, object) switch err := iodine.ToError(err).(type) { case nil: // success { @@ -106,7 +106,7 @@ func (server *minioAPI) headObjectHandler(w http.ResponseWriter, req *http.Reque bucket = vars["bucket"] object = vars["object"] - metadata, err := server.driver.GetObjectMetadata(bucket, object, "") + metadata, err := server.driver.GetObjectMetadata(bucket, object) switch err := iodine.ToError(err).(type) { case nil: { diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index 569eaf774..d64e1821a 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -158,9 +158,9 @@ func (s *MySuite) TestEmptyObject(c *C) { typedDriver.On("CreateBucket", "bucket", "private").Return(nil).Once() typedDriver.On("CreateObject", "bucket", "object", "", "", 0, mock.Anything).Return(metadata.Md5, nil).Once() typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Twice() - typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(metadata, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object").Return(metadata, nil).Once() typedDriver.On("GetObject", mock.Anything, "bucket", "object").Return(int64(0), nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(metadata, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object").Return(metadata, nil).Once() httpHandler := HTTPHandler(driver) testServer := httptest.NewServer(httpHandler) defer testServer.Close() @@ -182,7 +182,7 @@ func (s *MySuite) TestEmptyObject(c *C) { c.Assert(err, IsNil) c.Assert(true, Equals, bytes.Equal(responseBody, buffer.Bytes())) - resMetadata, err := driver.GetObjectMetadata("bucket", "object", "") + resMetadata, err := driver.GetObjectMetadata("bucket", "object") c.Assert(err, IsNil) verifyHeaders(c, response.Header, resMetadata.Created, 0, "application/octet-stream", resMetadata.Md5) } @@ -240,7 +240,7 @@ func (s *MySuite) TestObject(c *C) { typedDriver.On("CreateBucket", "bucket", "private").Return(nil).Once() typedDriver.On("CreateObject", "bucket", "object", "", "", mock.Anything, mock.Anything).Return(metadata.Md5, nil).Once() typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Twice() - typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(metadata, nil).Twice() + typedDriver.On("GetObjectMetadata", "bucket", "object").Return(metadata, nil).Twice() typedDriver.SetGetObjectWriter("bucket", "object", []byte("hello world")) typedDriver.On("GetObject", mock.Anything, "bucket", "object").Return(int64(0), nil).Once() @@ -265,7 +265,7 @@ func (s *MySuite) TestObject(c *C) { c.Assert(err, IsNil) c.Assert(responseBody, DeepEquals, []byte("hello world")) - resMetadata, err := driver.GetObjectMetadata("bucket", "object", "") + resMetadata, err := driver.GetObjectMetadata("bucket", "object") c.Assert(err, IsNil) verifyHeaders(c, response.Header, resMetadata.Created, len("hello world"), "application/octet-stream", metadata.Md5) } @@ -322,7 +322,7 @@ func (s *MySuite) TestMultipleObjects(c *C) { // test non-existant object typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once() request, err := http.NewRequest("GET", testServer.URL+"/bucket/object", nil) c.Assert(err, IsNil) setDummyAuthHeader(request) @@ -336,7 +336,7 @@ func (s *MySuite) TestMultipleObjects(c *C) { // get object typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object1", "").Return(metadata1, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object1").Return(metadata1, nil).Once() typedDriver.SetGetObjectWriter("bucket", "object1", []byte("hello one")) typedDriver.On("GetObject", mock.Anything, "bucket", "object1").Return(int64(0), nil).Once() request, err = http.NewRequest("GET", testServer.URL+"/bucket/object1", nil) @@ -349,8 +349,8 @@ func (s *MySuite) TestMultipleObjects(c *C) { // get metadata typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object1", "").Return(metadata1, nil).Once() - metadata, err := driver.GetObjectMetadata("bucket", "object1", "") + typedDriver.On("GetObjectMetadata", "bucket", "object1").Return(metadata1, nil).Once() + metadata, err := driver.GetObjectMetadata("bucket", "object1") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -366,7 +366,7 @@ func (s *MySuite) TestMultipleObjects(c *C) { // test object 2 // get object typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object2", "").Return(metadata2, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object2").Return(metadata2, nil).Once() typedDriver.SetGetObjectWriter("bucket", "object2", []byte("hello two")) typedDriver.On("GetObject", mock.Anything, "bucket", "object2").Return(int64(0), nil).Once() request, err = http.NewRequest("GET", testServer.URL+"/bucket/object2", nil) @@ -379,8 +379,8 @@ func (s *MySuite) TestMultipleObjects(c *C) { // get metadata typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object2", "").Return(metadata2, nil).Once() - metadata, err = driver.GetObjectMetadata("bucket", "object2", "") + typedDriver.On("GetObjectMetadata", "bucket", "object2").Return(metadata2, nil).Once() + metadata, err = driver.GetObjectMetadata("bucket", "object2") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -396,7 +396,7 @@ func (s *MySuite) TestMultipleObjects(c *C) { // test object 3 // get object typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object3", "").Return(metadata3, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object3").Return(metadata3, nil).Once() typedDriver.SetGetObjectWriter("bucket", "object3", []byte("hello three")) typedDriver.On("GetObject", mock.Anything, "bucket", "object3").Return(int64(0), nil).Once() request, err = http.NewRequest("GET", testServer.URL+"/bucket/object3", nil) @@ -409,8 +409,8 @@ func (s *MySuite) TestMultipleObjects(c *C) { // get metadata typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object3", "").Return(metadata3, nil).Once() - metadata, err = driver.GetObjectMetadata("bucket", "object3", "") + typedDriver.On("GetObjectMetadata", "bucket", "object3").Return(metadata3, nil).Once() + metadata, err = driver.GetObjectMetadata("bucket", "object3") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -472,7 +472,7 @@ func (s *MySuite) TestHeader(c *C) { ACL: drivers.BucketACL("private"), } typedDriver.On("GetBucketMetadata", "bucket").Return(bucketMetadata, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once() request, err := http.NewRequest("GET", testServer.URL+"/bucket/object", nil) c.Assert(err, IsNil) setDummyAuthHeader(request) @@ -498,7 +498,7 @@ func (s *MySuite) TestHeader(c *C) { driver.CreateObject("bucket", "object", "", "", int64(buffer.Len()), buffer) typedDriver.On("GetBucketMetadata", "bucket").Return(bucketMetadata, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(objectMetadata, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "object").Return(objectMetadata, nil).Once() typedDriver.SetGetObjectWriter("", "", []byte("hello world")) typedDriver.On("GetObject", mock.Anything, "bucket", "object").Return(int64(0), nil).Once() request, err = http.NewRequest("GET", testServer.URL+"/bucket/object", nil) @@ -511,8 +511,8 @@ func (s *MySuite) TestHeader(c *C) { c.Assert(response.StatusCode, Equals, http.StatusOK) typedDriver.On("GetBucketMetadata", "bucket").Return(bucketMetadata, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "object", "").Return(objectMetadata, nil).Once() - metadata, err := driver.GetObjectMetadata("bucket", "object", "") + typedDriver.On("GetObjectMetadata", "bucket", "object").Return(objectMetadata, nil).Once() + metadata, err := driver.GetObjectMetadata("bucket", "object") c.Assert(err, IsNil) verifyHeaders(c, response.Header, metadata.Created, len("hello world"), "application/octet-stream", metadata.Md5) } @@ -629,14 +629,14 @@ func (s *MySuite) TestPutObject(c *C) { var writer bytes.Buffer - typedDriver.On("GetObjectMetadata", "bucket", "two", "").Return(twoMetadata, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "two").Return(twoMetadata, nil).Once() typedDriver.SetGetObjectWriter("bucket", "two", []byte("hello world")) typedDriver.On("GetObject", mock.Anything, "bucket", "two").Return(int64(11), nil).Once() driver.GetObject(&writer, "bucket", "two") c.Assert(bytes.Equal(writer.Bytes(), []byte("hello world")), Equals, true) - metadata, err := driver.GetObjectMetadata("bucket", "two", "") + metadata, err := driver.GetObjectMetadata("bucket", "two") c.Assert(err, IsNil) lastModified := metadata.Created @@ -905,7 +905,7 @@ func (s *MySuite) TestContentTypePersists(c *C) { c.Assert(response.StatusCode, Equals, http.StatusOK) typedDriver.On("GetBucketMetadata", "bucket").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "one", "").Return(oneMetadata, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "one").Return(oneMetadata, nil).Once() request, err = http.NewRequest("HEAD", testServer.URL+"/bucket/one", nil) c.Assert(err, IsNil) setDummyAuthHeader(request) @@ -917,7 +917,7 @@ func (s *MySuite) TestContentTypePersists(c *C) { // test get object typedDriver.SetGetObjectWriter("bucket", "once", []byte("")) typedDriver.On("GetBucketMetadata", "bucket").Return(metadata, nil).Twice() - typedDriver.On("GetObjectMetadata", "bucket", "one", "").Return(oneMetadata, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "one").Return(oneMetadata, nil).Once() typedDriver.On("GetObject", mock.Anything, "bucket", "one").Return(int64(0), nil).Once() request, err = http.NewRequest("GET", testServer.URL+"/bucket/one", nil) c.Assert(err, IsNil) @@ -952,7 +952,7 @@ func (s *MySuite) TestContentTypePersists(c *C) { c.Assert(response.StatusCode, Equals, http.StatusOK) typedDriver.On("GetBucketMetadata", "bucket").Return(metadata, nil).Once() - typedDriver.On("GetObjectMetadata", "bucket", "two", "").Return(twoMetadata, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "two").Return(twoMetadata, nil).Once() request, err = http.NewRequest("HEAD", testServer.URL+"/bucket/two", nil) c.Assert(err, IsNil) setDummyAuthHeader(request) @@ -963,7 +963,7 @@ func (s *MySuite) TestContentTypePersists(c *C) { // test get object typedDriver.On("GetBucketMetadata", "bucket").Return(metadata, nil).Twice() - typedDriver.On("GetObjectMetadata", "bucket", "two", "").Return(twoMetadata, nil).Once() + typedDriver.On("GetObjectMetadata", "bucket", "two").Return(twoMetadata, nil).Once() typedDriver.On("GetObject", mock.Anything, "bucket", "two").Return(int64(0), nil).Once() request, err = http.NewRequest("GET", testServer.URL+"/bucket/two", nil) c.Assert(err, IsNil) @@ -1008,7 +1008,7 @@ func (s *MySuite) TestPartialContent(c *C) { typedDriver.SetGetObjectWriter("foo", "bar", []byte("hello world")) typedDriver.On("GetBucketMetadata", "foo").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "foo", "bar", "").Return(metadata, nil).Once() + typedDriver.On("GetObjectMetadata", "foo", "bar").Return(metadata, nil).Once() typedDriver.On("GetPartialObject", mock.Anything, "foo", "bar", int64(6), int64(2)).Return(int64(2), nil).Once() // prepare request @@ -1217,7 +1217,7 @@ func (s *MySuite) TestGetObjectErrors(c *C) { ACL: drivers.BucketACL("private"), } typedDriver.On("GetBucketMetadata", "foo").Return(metadata, nil).Once() - typedDriver.On("GetObjectMetadata", "foo", "bar", "").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once() + typedDriver.On("GetObjectMetadata", "foo", "bar").Return(drivers.ObjectMetadata{}, drivers.ObjectNotFound{}).Once() request, err := http.NewRequest("GET", testServer.URL+"/foo/bar", nil) c.Assert(err, IsNil) setDummyAuthHeader(request) @@ -1236,7 +1236,7 @@ func (s *MySuite) TestGetObjectErrors(c *C) { verifyError(c, response, "NoSuchBucket", "The specified bucket does not exist.", http.StatusNotFound) typedDriver.On("GetBucketMetadata", "foo").Return(metadata, nil).Once() - typedDriver.On("GetObjectMetadata", "foo", "bar", "").Return(drivers.ObjectMetadata{}, drivers.ObjectNameInvalid{}).Once() + typedDriver.On("GetObjectMetadata", "foo", "bar").Return(drivers.ObjectMetadata{}, drivers.ObjectNameInvalid{}).Once() request, err = http.NewRequest("GET", testServer.URL+"/foo/bar", nil) c.Assert(err, IsNil) setDummyAuthHeader(request) @@ -1255,7 +1255,7 @@ func (s *MySuite) TestGetObjectErrors(c *C) { verifyError(c, response, "InvalidBucketName", "The specified bucket is not valid.", http.StatusBadRequest) typedDriver.On("GetBucketMetadata", "foo").Return(metadata, nil).Once() - typedDriver.On("GetObjectMetadata", "foo", "bar", "").Return(drivers.ObjectMetadata{}, drivers.BackendCorrupted{}).Once() + typedDriver.On("GetObjectMetadata", "foo", "bar").Return(drivers.ObjectMetadata{}, drivers.BackendCorrupted{}).Once() request, err = http.NewRequest("GET", testServer.URL+"/foo/bar", nil) c.Assert(err, IsNil) setDummyAuthHeader(request) @@ -1295,7 +1295,7 @@ func (s *MySuite) TestGetObjectRangeErrors(c *C) { } typedDriver.On("GetBucketMetadata", "foo").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "foo", "bar", "").Return(metadata, nil).Once() + typedDriver.On("GetObjectMetadata", "foo", "bar").Return(metadata, nil).Once() request, err := http.NewRequest("GET", testServer.URL+"/foo/bar", nil) request.Header.Add("Range", "bytes=7-6") c.Assert(err, IsNil) @@ -1670,7 +1670,7 @@ func (s *MySuite) TestObjectMultipart(c *C) { // get data typedDriver.On("GetBucketMetadata", "foo").Return(drivers.BucketMetadata{}, nil).Once() - typedDriver.On("GetObjectMetadata", "foo", "object", "").Return(drivers.ObjectMetadata{Size: 22}, nil).Once() + typedDriver.On("GetObjectMetadata", "foo", "object").Return(drivers.ObjectMetadata{Size: 22}, nil).Once() typedDriver.On("GetObject", mock.Anything, "foo", "object").Return(int64(22), nil).Once() typedDriver.SetGetObjectWriter("foo", "object", []byte("hello worldhello world")) request, err = http.NewRequest("GET", testServer.URL+"/foo/object", nil) diff --git a/pkg/storage/drivers/api_testsuite.go b/pkg/storage/drivers/api_testsuite.go index e9b980eae..63343c2a6 100644 --- a/pkg/storage/drivers/api_testsuite.go +++ b/pkg/storage/drivers/api_testsuite.go @@ -157,7 +157,7 @@ func testMultipleObjectCreation(c *check.C, create func() Driver) { c.Assert(err, check.IsNil) c.Assert(byteBuffer.Bytes(), check.DeepEquals, value) - metadata, err := drivers.GetObjectMetadata("bucket", key, "") + metadata, err := drivers.GetObjectMetadata("bucket", key) c.Assert(err, check.IsNil) c.Assert(metadata.Size, check.Equals, int64(len(value))) @@ -490,19 +490,19 @@ func testDefaultContentType(c *check.C, create func() Driver) { // test empty _, err = drivers.CreateObject("bucket", "one", "", "", int64(len("one")), bytes.NewBufferString("one")) - metadata, err := drivers.GetObjectMetadata("bucket", "one", "") + metadata, err := drivers.GetObjectMetadata("bucket", "one") c.Assert(err, check.IsNil) c.Assert(metadata.ContentType, check.Equals, "application/octet-stream") // test custom drivers.CreateObject("bucket", "two", "application/text", "", int64(len("two")), bytes.NewBufferString("two")) - metadata, err = drivers.GetObjectMetadata("bucket", "two", "") + metadata, err = drivers.GetObjectMetadata("bucket", "two") c.Assert(err, check.IsNil) c.Assert(metadata.ContentType, check.Equals, "application/text") // test trim space drivers.CreateObject("bucket", "three", "\tapplication/json ", "", int64(len("three")), bytes.NewBufferString("three")) - metadata, err = drivers.GetObjectMetadata("bucket", "three", "") + metadata, err = drivers.GetObjectMetadata("bucket", "three") c.Assert(err, check.IsNil) c.Assert(metadata.ContentType, check.Equals, "application/json") } diff --git a/pkg/storage/drivers/donut/donut.go b/pkg/storage/drivers/donut/donut.go index 87ba799b5..4696dae95 100644 --- a/pkg/storage/drivers/donut/donut.go +++ b/pkg/storage/drivers/donut/donut.go @@ -275,11 +275,10 @@ func (d donutDriver) GetPartialObject(w io.Writer, bucketName, objectName string } // GetObjectMetadata retrieves an object's metadata -func (d donutDriver) GetObjectMetadata(bucketName, objectName, prefixName string) (drivers.ObjectMetadata, error) { +func (d donutDriver) GetObjectMetadata(bucketName, objectName string) (drivers.ObjectMetadata, error) { errParams := map[string]string{ "bucketName": bucketName, "objectName": objectName, - "prefixName": prefixName, } if !drivers.IsValidBucket(bucketName) || strings.Contains(bucketName, ".") { return drivers.ObjectMetadata{}, iodine.New(drivers.BucketNameInvalid{Bucket: bucketName}, nil) diff --git a/pkg/storage/drivers/driver.go b/pkg/storage/drivers/driver.go index 610306a66..2728653b9 100644 --- a/pkg/storage/drivers/driver.go +++ b/pkg/storage/drivers/driver.go @@ -35,7 +35,7 @@ type Driver interface { // Object Operations GetObject(w io.Writer, bucket, object string) (int64, error) GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error) - GetObjectMetadata(bucket, key, prefix string) (ObjectMetadata, error) + GetObjectMetadata(bucket, key string) (ObjectMetadata, error) ListObjects(bucket string, resources BucketResourcesMetadata) ([]ObjectMetadata, BucketResourcesMetadata, error) CreateObject(bucket, key, contentType, md5sum string, size int64, data io.Reader) (string, error) diff --git a/pkg/storage/drivers/memory/memory.go b/pkg/storage/drivers/memory/memory.go index f8d941d08..43ecba5c9 100644 --- a/pkg/storage/drivers/memory/memory.go +++ b/pkg/storage/drivers/memory/memory.go @@ -478,14 +478,14 @@ func (memory *memoryDriver) ListBuckets() ([]drivers.BucketMetadata, error) { } // GetObjectMetadata - get object metadata from memory -func (memory *memoryDriver) GetObjectMetadata(bucket, key, prefix string) (drivers.ObjectMetadata, error) { +func (memory *memoryDriver) GetObjectMetadata(bucket, key string) (drivers.ObjectMetadata, error) { memory.lock.RLock() defer memory.lock.RUnlock() // check if bucket exists if !drivers.IsValidBucket(bucket) { return drivers.ObjectMetadata{}, iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil) } - if !drivers.IsValidObjectName(key) || !drivers.IsValidObjectName(prefix) { + if !drivers.IsValidObjectName(key) { return drivers.ObjectMetadata{}, iodine.New(drivers.ObjectNameInvalid{Object: key}, nil) } if _, ok := memory.storedBuckets[bucket]; ok == false { diff --git a/pkg/storage/drivers/mocks/Driver.go b/pkg/storage/drivers/mocks/Driver.go index b212844d6..fc70f5ef7 100644 --- a/pkg/storage/drivers/mocks/Driver.go +++ b/pkg/storage/drivers/mocks/Driver.go @@ -97,8 +97,8 @@ func (m *Driver) GetPartialObject(w io.Writer, bucket, object string, start int6 } // GetObjectMetadata is a mock -func (m *Driver) GetObjectMetadata(bucket, object, prefix string) (drivers.ObjectMetadata, error) { - ret := m.Called(bucket, object, prefix) +func (m *Driver) GetObjectMetadata(bucket, object string) (drivers.ObjectMetadata, error) { + ret := m.Called(bucket, object) r0 := ret.Get(0).(drivers.ObjectMetadata) r1 := ret.Error(1) From 317637fcb868e8e670b3d124eeb4fd1be703c4a3 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 15 May 2015 20:44:42 -0700 Subject: [PATCH 2/2] Remove redundant WriteHeader calls --- pkg/api/api_bucket_handlers.go | 16 +++------------- pkg/api/api_generic_handlers.go | 5 +++-- pkg/api/api_object_handlers.go | 17 ++++------------- pkg/api/api_response.go | 11 ++++------- pkg/api/headers.go | 10 +++++----- pkg/api/logging/logging.go | 17 +++++++++++++---- 6 files changed, 32 insertions(+), 44 deletions(-) diff --git a/pkg/api/api_bucket_handlers.go b/pkg/api/api_bucket_handlers.go index 1a9d358f3..8e0f9cf0e 100644 --- a/pkg/api/api_bucket_handlers.go +++ b/pkg/api/api_bucket_handlers.go @@ -18,7 +18,6 @@ package api import ( "net/http" - "strconv" "github.com/gorilla/mux" "github.com/minio/minio/pkg/iodine" @@ -87,10 +86,7 @@ func (server *minioAPI) listMultipartUploadsHandler(w http.ResponseWriter, req * response := generateListMultipartUploadsResult(bucket, resources) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) // write headers - setCommonHeaders(w, getContentTypeString(acceptsContentType)) - // set content-length to the size of the body - w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse))) - w.WriteHeader(http.StatusOK) + setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) // write body w.Write(encodedSuccessResponse) } @@ -141,10 +137,7 @@ func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Requ response := generateListObjectsResponse(bucket, objects, resources) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) // write headers - setCommonHeaders(w, getContentTypeString(acceptsContentType)) - // set content-length to the size of the body - w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse))) - w.WriteHeader(http.StatusOK) + setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) // write body w.Write(encodedSuccessResponse) } @@ -184,10 +177,7 @@ func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Requ response := generateListBucketsResponse(buckets) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) // write headers - setCommonHeaders(w, getContentTypeString(acceptsContentType)) - // set content-length to the size of the body - w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse))) - w.WriteHeader(http.StatusOK) + setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) // write response w.Write(encodedSuccessResponse) } diff --git a/pkg/api/api_generic_handlers.go b/pkg/api/api_generic_handlers.go index 16378da81..2da6ef53f 100644 --- a/pkg/api/api_generic_handlers.go +++ b/pkg/api/api_generic_handlers.go @@ -202,9 +202,10 @@ func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if ignoreNotImplementedObjectResources(r) || ignoreNotImplementedBucketResources(r) { error := getErrorCode(NotImplemented) errorResponse := getErrorResponse(error, "") - setCommonHeaders(w, getContentTypeString(acceptsContentType)) + encodeErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) + setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodeErrorResponse)) w.WriteHeader(error.HTTPStatusCode) - w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse) return } h.handler.ServeHTTP(w, r) diff --git a/pkg/api/api_object_handlers.go b/pkg/api/api_object_handlers.go index dbf3caabd..8676a50d4 100644 --- a/pkg/api/api_object_handlers.go +++ b/pkg/api/api_object_handlers.go @@ -230,10 +230,7 @@ func (server *minioAPI) newMultipartUploadHandler(w http.ResponseWriter, req *ht response := generateInitiateMultipartUploadResult(bucket, object, uploadID) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) // write headers - setCommonHeaders(w, getContentTypeString(acceptsContentType)) - // set content-length to the size of the body - w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse))) - w.WriteHeader(http.StatusOK) + setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) // write body w.Write(encodedSuccessResponse) case drivers.ObjectExists: @@ -349,7 +346,7 @@ func (server *minioAPI) abortMultipartUploadHandler(w http.ResponseWriter, req * err := server.driver.AbortMultipartUpload(bucket, object, objectResourcesMetadata.UploadID) switch err := iodine.ToError(err).(type) { case nil: - setCommonHeaders(w, getContentTypeString(acceptsContentType)) + setCommonHeaders(w, getContentTypeString(acceptsContentType), 0) w.WriteHeader(http.StatusNoContent) case drivers.InvalidUploadID: writeErrorResponse(w, req, NoSuchUpload, acceptsContentType, req.URL.Path) @@ -381,10 +378,7 @@ func (server *minioAPI) listObjectPartsHandler(w http.ResponseWriter, req *http. response := generateListPartsResult(objectResourcesMetadata) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) // write headers - setCommonHeaders(w, getContentTypeString(acceptsContentType)) - // set content-length to the size of the body - w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse))) - w.WriteHeader(http.StatusOK) + setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) // write body w.Write(encodedSuccessResponse) case drivers.InvalidUploadID: @@ -431,10 +425,7 @@ func (server *minioAPI) completeMultipartUploadHandler(w http.ResponseWriter, re response := generateCompleteMultpartUploadResult(bucket, object, "", etag) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) // write headers - setCommonHeaders(w, getContentTypeString(acceptsContentType)) - // set content-length to the size of the body - w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse))) - w.WriteHeader(http.StatusOK) + setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) // write body w.Write(encodedSuccessResponse) case drivers.InvalidUploadID: diff --git a/pkg/api/api_response.go b/pkg/api/api_response.go index d3b5e5781..b17eb2d74 100644 --- a/pkg/api/api_response.go +++ b/pkg/api/api_response.go @@ -19,7 +19,6 @@ package api import ( "net/http" "sort" - "strconv" "github.com/minio/minio/pkg/storage/drivers" ) @@ -188,7 +187,7 @@ func generateListMultipartUploadsResult(bucket string, metadata drivers.BucketMu // writeSuccessResponse write success headers func writeSuccessResponse(w http.ResponseWriter, acceptsContentType contentType) { - setCommonHeaders(w, getContentTypeString(acceptsContentType)) + setCommonHeaders(w, getContentTypeString(acceptsContentType), 0) w.WriteHeader(http.StatusOK) } @@ -199,11 +198,9 @@ func writeErrorResponse(w http.ResponseWriter, req *http.Request, errorType int, errorResponse := getErrorResponse(error, resource) encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) // set common headers - setCommonHeaders(w, getContentTypeString(acceptsContentType)) - // set content-length to size of error response - w.Header().Set("Content-Length", strconv.Itoa(len(encodedErrorResponse))) - // set headers + setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedErrorResponse)) + // write Header w.WriteHeader(error.HTTPStatusCode) - // write body + // write error body w.Write(encodedErrorResponse) } diff --git a/pkg/api/headers.go b/pkg/api/headers.go index 9595a4349..a80ded041 100644 --- a/pkg/api/headers.go +++ b/pkg/api/headers.go @@ -35,13 +35,13 @@ type encoder interface { //// helpers // Write http common headers -func setCommonHeaders(w http.ResponseWriter, acceptsType string) { +func setCommonHeaders(w http.ResponseWriter, acceptsType string, contentLength int) { w.Header().Set("Server", "Minio") w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Content-Type", acceptsType) w.Header().Set("Connection", "close") // should be set to '0' by default - w.Header().Set("Content-Length", "0") + w.Header().Set("Content-Length", strconv.Itoa(contentLength)) } // Write error response headers @@ -66,16 +66,16 @@ func encodeErrorResponse(response interface{}, acceptsType contentType) []byte { func setObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata) { lastModified := metadata.Created.Format(time.RFC1123) // common headers - setCommonHeaders(w, metadata.ContentType) + setCommonHeaders(w, metadata.ContentType, int(metadata.Size)) + // object related headers w.Header().Set("ETag", metadata.Md5) w.Header().Set("Last-Modified", lastModified) - w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10)) } // Write range object header func setRangeObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata, contentRange *httpRange) { // set common headers - setCommonHeaders(w, metadata.ContentType) + setCommonHeaders(w, metadata.ContentType, int(metadata.Size)) // set object headers setObjectHeaders(w, metadata) // set content range diff --git a/pkg/api/logging/logging.go b/pkg/api/logging/logging.go index 5df34d122..b66270b2b 100644 --- a/pkg/api/logging/logging.go +++ b/pkg/api/logging/logging.go @@ -44,15 +44,24 @@ type LogMessage struct { // LogWriter is used to capture status for log messages type LogWriter struct { - http.ResponseWriter - LogMessage *LogMessage + ResponseWriter http.ResponseWriter + LogMessage *LogMessage } // WriteHeader writes headers and stores status in LogMessage func (w *LogWriter) WriteHeader(status int) { w.LogMessage.Status = status w.ResponseWriter.WriteHeader(status) - w.ResponseWriter.Header() +} + +// Header Dummy wrapper for LogWriter +func (w *LogWriter) Header() http.Header { + return w.ResponseWriter.Header() +} + +// Write Dummy wrapper for LogWriter +func (w *LogWriter) Write(data []byte) (int, error) { + return w.ResponseWriter.Write(data) } func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { @@ -60,12 +69,12 @@ func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { StartTime: time.Now().UTC(), } logWriter := &LogWriter{ResponseWriter: w, LogMessage: logMessage} - h.Handler.ServeHTTP(logWriter, req) logMessage.ResponseHeaders = w.Header() logMessage.Request = req logMessage.Duration = time.Now().UTC().Sub(logMessage.StartTime) js, _ := json.Marshal(logMessage) h.Logger <- string(js) + h.Handler.ServeHTTP(logWriter, req) } // LogHandler logs requests