2015-02-23 19:46:48 -05:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
2015-02-23 19:46:48 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-18 20:49:33 -04:00
|
|
|
package api
|
2015-02-15 20:03:27 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2015-04-08 21:02:03 -04:00
|
|
|
"github.com/minio-io/minio/pkg/iodine"
|
2015-04-08 19:28:14 -04:00
|
|
|
"github.com/minio-io/minio/pkg/storage/drivers"
|
2015-03-23 21:12:20 -04:00
|
|
|
"github.com/minio-io/minio/pkg/utils/log"
|
2015-02-15 20:03:27 -05:00
|
|
|
)
|
|
|
|
|
2015-04-23 04:20:03 -04:00
|
|
|
func (server *minioAPI) isValidOp(w http.ResponseWriter, req *http.Request, acceptsContentType contentType) bool {
|
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
bucketMetadata, err := server.driver.GetBucketMetadata(bucket)
|
|
|
|
switch iodine.ToError(err).(type) {
|
|
|
|
case drivers.BucketNotFound:
|
|
|
|
{
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path)
|
2015-04-23 04:20:03 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case drivers.BucketNameInvalid:
|
|
|
|
{
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path)
|
2015-04-23 04:20:03 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case nil:
|
2015-04-23 06:21:08 -04:00
|
|
|
|
2015-04-23 04:20:03 -04:00
|
|
|
if stripAccessKey(req) == "" && bucketMetadata.ACL.IsPrivate() {
|
2015-04-23 06:21:08 -04:00
|
|
|
return true
|
|
|
|
// Uncomment this when we have webCli
|
|
|
|
// writeErrorResponse(w, req, AccessDenied, acceptsContentType, req.URL.Path)
|
|
|
|
// return false
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
|
|
|
if bucketMetadata.ACL.IsPublicRead() && req.Method == "PUT" {
|
2015-04-23 06:21:08 -04:00
|
|
|
return true
|
|
|
|
// Uncomment this when we have webCli
|
|
|
|
// writeErrorResponse(w, req, AccessDenied, acceptsContentType, req.URL.Path)
|
|
|
|
// return false
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// GET Bucket (List Objects)
|
|
|
|
// -------------------------
|
|
|
|
// This implementation of the GET operation returns some or all (up to 1000)
|
|
|
|
// of the objects in a bucket. You can use the request parameters as selection
|
|
|
|
// criteria to return a subset of the objects in a bucket.
|
|
|
|
//
|
2015-03-06 00:07:19 -05:00
|
|
|
func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Request) {
|
2015-04-22 19:28:13 -04:00
|
|
|
acceptsContentType := getContentType(req)
|
|
|
|
if acceptsContentType == unknownContentType {
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, NotAcceptable, acceptsContentType, req.URL.Path)
|
2015-04-22 19:28:13 -04:00
|
|
|
return
|
|
|
|
}
|
2015-04-23 04:20:03 -04:00
|
|
|
// verify if bucket allows this operation
|
|
|
|
if !server.isValidOp(w, req, acceptsContentType) {
|
|
|
|
return
|
|
|
|
}
|
2015-02-15 20:03:27 -05:00
|
|
|
|
|
|
|
resources := getBucketResources(req.URL.Query())
|
2015-03-02 21:03:01 -05:00
|
|
|
if resources.Maxkeys == 0 {
|
|
|
|
resources.Maxkeys = maxObjectList
|
|
|
|
}
|
2015-04-22 19:28:13 -04:00
|
|
|
|
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
2015-04-22 22:29:39 -04:00
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
objects, resources, err := server.driver.ListObjects(bucket, resources)
|
2015-04-23 04:20:03 -04:00
|
|
|
switch err := iodine.ToError(err).(type) {
|
2015-02-15 20:03:27 -05:00
|
|
|
case nil: // success
|
|
|
|
{
|
2015-03-28 21:00:55 -04:00
|
|
|
// write headers
|
|
|
|
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
// write body
|
2015-02-28 17:44:26 -05:00
|
|
|
response := generateObjectsListResult(bucket, objects, resources)
|
2015-04-22 19:28:13 -04:00
|
|
|
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
|
|
|
|
w.Write(encodedSuccessResponse)
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
2015-04-23 04:20:03 -04:00
|
|
|
case drivers.ObjectNotFound:
|
2015-02-15 20:03:27 -05:00
|
|
|
{
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path)
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
case drivers.ObjectNameInvalid:
|
2015-02-15 20:03:27 -05:00
|
|
|
{
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path)
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
2015-03-31 23:26:14 -04:00
|
|
|
default:
|
|
|
|
{
|
2015-04-03 21:06:30 -04:00
|
|
|
log.Error.Println(iodine.New(err, nil))
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
|
2015-03-31 23:26:14 -04:00
|
|
|
}
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// GET Service
|
|
|
|
// -----------
|
|
|
|
// This implementation of the GET operation returns a list of all buckets
|
|
|
|
// owned by the authenticated sender of the request.
|
2015-03-06 00:07:19 -05:00
|
|
|
func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Request) {
|
2015-02-15 20:03:27 -05:00
|
|
|
acceptsContentType := getContentType(req)
|
2015-04-22 19:28:13 -04:00
|
|
|
if acceptsContentType == unknownContentType {
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, NotAcceptable, acceptsContentType, req.URL.Path)
|
2015-04-22 19:28:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
buckets, err := server.driver.ListBuckets()
|
2015-03-31 23:26:14 -04:00
|
|
|
// cannot fallthrough in (type) switch :(
|
2015-04-23 04:20:03 -04:00
|
|
|
switch err := iodine.ToError(err).(type) {
|
2015-02-15 20:03:27 -05:00
|
|
|
case nil:
|
|
|
|
{
|
|
|
|
response := generateBucketsListResult(buckets)
|
2015-03-28 21:00:55 -04:00
|
|
|
// write headers
|
|
|
|
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
// write response
|
2015-04-22 19:28:13 -04:00
|
|
|
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
|
|
|
|
w.Write(encodedSuccessResponse)
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
2015-03-28 21:00:55 -04:00
|
|
|
default:
|
2015-02-15 20:03:27 -05:00
|
|
|
{
|
2015-04-03 21:29:27 -04:00
|
|
|
log.Error.Println(iodine.New(err, nil))
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 19:46:48 -05:00
|
|
|
// PUT Bucket
|
|
|
|
// ----------
|
|
|
|
// This implementation of the PUT operation creates a new bucket for authenticated request
|
2015-03-06 00:07:19 -05:00
|
|
|
func (server *minioAPI) putBucketHandler(w http.ResponseWriter, req *http.Request) {
|
2015-04-22 19:28:13 -04:00
|
|
|
acceptsContentType := getContentType(req)
|
|
|
|
if acceptsContentType == unknownContentType {
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, NotAcceptable, acceptsContentType, req.URL.Path)
|
2015-04-22 19:28:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// read from 'x-amz-acl'
|
|
|
|
aclType := getACLType(req)
|
|
|
|
if aclType == unsupportedACLType {
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, NotImplemented, acceptsContentType, req.URL.Path)
|
2015-04-22 19:28:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-15 20:03:27 -05:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
bucket := vars["bucket"]
|
2015-04-22 21:19:20 -04:00
|
|
|
err := server.driver.CreateBucket(bucket, getACLTypeString(aclType))
|
2015-04-23 04:20:03 -04:00
|
|
|
switch iodine.ToError(err).(type) {
|
2015-02-15 20:03:27 -05:00
|
|
|
case nil:
|
|
|
|
{
|
|
|
|
w.Header().Set("Server", "Minio")
|
|
|
|
w.Header().Set("Connection", "close")
|
2015-03-30 14:40:21 -04:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
2015-04-27 00:15:43 -04:00
|
|
|
case drivers.TooManyBuckets:
|
|
|
|
{
|
|
|
|
writeErrorResponse(w, req, TooManyBuckets, acceptsContentType, req.URL.Path)
|
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
case drivers.BucketNameInvalid:
|
2015-02-15 20:03:27 -05:00
|
|
|
{
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path)
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
case drivers.BucketExists:
|
2015-02-15 20:03:27 -05:00
|
|
|
{
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, BucketAlreadyExists, acceptsContentType, req.URL.Path)
|
2015-03-31 23:26:14 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2015-04-03 21:37:36 -04:00
|
|
|
log.Error.Println(iodine.New(err, nil))
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path)
|
2015-02-15 20:03:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 03:44:01 -04:00
|
|
|
|
|
|
|
// HEAD Bucket
|
|
|
|
// ----------
|
|
|
|
// This operation is useful to determine if a bucket exists.
|
|
|
|
// The operation returns a 200 OK if the bucket exists and you
|
|
|
|
// have permission to access it. Otherwise, the operation might
|
|
|
|
// return responses such as 404 Not Found and 403 Forbidden.
|
|
|
|
func (server *minioAPI) headBucketHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
acceptsContentType := getContentType(req)
|
2015-04-22 19:28:13 -04:00
|
|
|
if acceptsContentType == unknownContentType {
|
2015-04-26 20:02:49 -04:00
|
|
|
writeErrorResponse(w, req, NotAcceptable, acceptsContentType, req.URL.Path)
|
2015-04-22 19:28:13 -04:00
|
|
|
return
|
|
|
|
}
|
2015-04-07 03:44:01 -04:00
|
|
|
|
2015-04-23 04:20:03 -04:00
|
|
|
// verify if bucket allows this operation
|
|
|
|
if !server.isValidOp(w, req, acceptsContentType) {
|
|
|
|
return
|
2015-04-07 03:44:01 -04:00
|
|
|
}
|
2015-04-23 04:20:03 -04:00
|
|
|
|
|
|
|
// Always a success if isValidOp succeeds
|
|
|
|
w.Header().Set("Server", "Minio")
|
|
|
|
w.Header().Set("Connection", "close")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2015-04-07 03:44:01 -04:00
|
|
|
}
|