Merge pull request #882 from harshavardhana/remove-http-responses-injson

Remove using HTTP responses in json reply always in application/xml
This commit is contained in:
Harshavardhana 2015-10-04 01:25:49 -07:00
commit 3de10f9472
7 changed files with 181 additions and 297 deletions

View File

@ -25,7 +25,7 @@ import (
signv4 "github.com/minio/minio/pkg/signature" signv4 "github.com/minio/minio/pkg/signature"
) )
func (api API) isValidOp(w http.ResponseWriter, req *http.Request, acceptsContentType contentType) bool { func (api API) isValidOp(w http.ResponseWriter, req *http.Request) bool {
vars := mux.Vars(req) vars := mux.Vars(req)
bucket := vars["bucket"] bucket := vars["bucket"]
@ -34,13 +34,13 @@ func (api API) isValidOp(w http.ResponseWriter, req *http.Request, acceptsConten
errorIf(err.Trace(), "GetBucketMetadata failed.", nil) errorIf(err.Trace(), "GetBucketMetadata failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
return false return false
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
return false return false
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return false return false
} }
} }
@ -48,13 +48,13 @@ func (api API) isValidOp(w http.ResponseWriter, req *http.Request, acceptsConten
if bucketMetadata.ACL.IsPrivate() { if bucketMetadata.ACL.IsPrivate() {
return true return true
//uncomment this when we have webcli //uncomment this when we have webcli
//writeErrorResponse(w, req, AccessDenied, acceptsContentType, req.URL.Path) //writeErrorResponse(w, req, AccessDenied, req.URL.Path)
//return false //return false
} }
if bucketMetadata.ACL.IsPublicRead() && req.Method == "PUT" { if bucketMetadata.ACL.IsPublicRead() && req.Method == "PUT" {
return true return true
//uncomment this when we have webcli //uncomment this when we have webcli
//writeErrorResponse(w, req, AccessDenied, acceptsContentType, req.URL.Path) //writeErrorResponse(w, req, AccessDenied, req.URL.Path)
//return false //return false
} }
} }
@ -78,14 +78,13 @@ func (api API) ListMultipartUploadsHandler(w http.ResponseWriter, req *http.Requ
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
resources := getBucketMultipartResources(req.URL.Query()) resources := getBucketMultipartResources(req.URL.Query())
if resources.MaxUploads < 0 { if resources.MaxUploads < 0 {
writeErrorResponse(w, req, InvalidMaxUploads, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidMaxUploads, req.URL.Path)
return return
} }
if resources.MaxUploads == 0 { if resources.MaxUploads == 0 {
@ -102,7 +101,7 @@ func (api API) ListMultipartUploadsHandler(w http.ResponseWriter, req *http.Requ
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -112,19 +111,19 @@ func (api API) ListMultipartUploadsHandler(w http.ResponseWriter, req *http.Requ
errorIf(err.Trace(), "ListMultipartUploads failed.", nil) errorIf(err.Trace(), "ListMultipartUploads failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
// generate response // generate response
response := generateListMultipartUploadsResponse(bucket, resources) response := generateListMultipartUploadsResponse(bucket, resources)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) setCommonHeaders(w, len(encodedSuccessResponse))
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
} }
@ -145,8 +144,7 @@ func (api API) ListObjectsHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
@ -157,7 +155,7 @@ func (api API) ListObjectsHandler(w http.ResponseWriter, req *http.Request) {
resources := getBucketResources(req.URL.Query()) resources := getBucketResources(req.URL.Query())
if resources.Maxkeys < 0 { if resources.Maxkeys < 0 {
writeErrorResponse(w, req, InvalidMaxKeys, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidMaxKeys, req.URL.Path)
return return
} }
if resources.Maxkeys == 0 { if resources.Maxkeys == 0 {
@ -174,7 +172,7 @@ func (api API) ListObjectsHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -183,27 +181,27 @@ func (api API) ListObjectsHandler(w http.ResponseWriter, req *http.Request) {
if err == nil { if err == nil {
// generate response // generate response
response := generateListObjectsResponse(bucket, objects, resources) response := generateListObjectsResponse(bucket, objects, resources)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) setCommonHeaders(w, len(encodedSuccessResponse))
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
return return
} }
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
case donut.ObjectNotFound: case donut.ObjectNotFound:
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
case donut.ObjectNameInvalid: case donut.ObjectNameInvalid:
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
default: default:
errorIf(err.Trace(), "ListObjects failed.", nil) errorIf(err.Trace(), "ListObjects failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
} }
@ -221,11 +219,10 @@ func (api API) ListBucketsHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req)
// uncomment this when we have webcli // uncomment this when we have webcli
// without access key credentials one cannot list buckets // without access key credentials one cannot list buckets
// if _, err := StripAccessKeyID(req); err != nil { // if _, err := StripAccessKeyID(req); err != nil {
// writeErrorResponse(w, req, AccessDenied, acceptsContentType, req.URL.Path) // writeErrorResponse(w, req, AccessDenied, req.URL.Path)
// return // return
// } // }
@ -236,7 +233,7 @@ func (api API) ListBucketsHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -245,19 +242,19 @@ func (api API) ListBucketsHandler(w http.ResponseWriter, req *http.Request) {
if err == nil { if err == nil {
// generate response // generate response
response := generateListBucketsResponse(buckets) response := generateListBucketsResponse(buckets)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) setCommonHeaders(w, len(encodedSuccessResponse))
// write response // write response
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
return return
} }
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
default: default:
errorIf(err.Trace(), "ListBuckets failed.", nil) errorIf(err.Trace(), "ListBuckets failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
} }
@ -274,11 +271,10 @@ func (api API) PutBucketHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req)
// uncomment this when we have webcli // uncomment this when we have webcli
// without access key credentials one cannot create a bucket // without access key credentials one cannot create a bucket
// if _, err := StripAccessKeyID(req); err != nil { // if _, err := StripAccessKeyID(req); err != nil {
// writeErrorResponse(w, req, AccessDenied, acceptsContentType, req.URL.Path) // writeErrorResponse(w, req, AccessDenied, req.URL.Path)
// return // return
// } // }
@ -289,7 +285,7 @@ func (api API) PutBucketHandler(w http.ResponseWriter, req *http.Request) {
// read from 'x-amz-acl' // read from 'x-amz-acl'
aclType := getACLType(req) aclType := getACLType(req)
if aclType == unsupportedACLType { if aclType == unsupportedACLType {
writeErrorResponse(w, req, NotImplemented, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NotImplemented, req.URL.Path)
return return
} }
@ -303,7 +299,7 @@ func (api API) PutBucketHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -313,7 +309,7 @@ func (api API) PutBucketHandler(w http.ResponseWriter, req *http.Request) {
/// if Content-Length missing, deny the request /// if Content-Length missing, deny the request
size := req.Header.Get("Content-Length") size := req.Header.Get("Content-Length")
if size == "" { if size == "" {
writeErrorResponse(w, req, MissingContentLength, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, MissingContentLength, req.URL.Path)
return return
} }
} }
@ -323,21 +319,21 @@ func (api API) PutBucketHandler(w http.ResponseWriter, req *http.Request) {
errorIf(err.Trace(), "MakeBucket failed.", nil) errorIf(err.Trace(), "MakeBucket failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.TooManyBuckets: case donut.TooManyBuckets:
writeErrorResponse(w, req, TooManyBuckets, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, TooManyBuckets, req.URL.Path)
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
case donut.BucketExists: case donut.BucketExists:
writeErrorResponse(w, req, BucketAlreadyExists, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, BucketAlreadyExists, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
// Make sure to add Location information here only for bucket // Make sure to add Location information here only for bucket
w.Header().Set("Location", "/"+bucket) w.Header().Set("Location", "/"+bucket)
writeSuccessResponse(w, acceptsContentType) writeSuccessResponse(w)
} }
// PostPolicyBucketHandler - POST policy // PostPolicyBucketHandler - POST policy
@ -360,14 +356,14 @@ func (api API) PostPolicyBucketHandler(w http.ResponseWriter, req *http.Request)
reader, err := req.MultipartReader() reader, err := req.MultipartReader()
if err != nil { if err != nil {
errorIf(probe.NewError(err), "Unable to initialize multipart reader.", nil) errorIf(probe.NewError(err), "Unable to initialize multipart reader.", nil)
writeErrorResponse(w, req, MalformedPOSTRequest, 1, req.URL.Path) writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
return return
} }
fileBody, formValues, perr := extractHTTPFormValues(reader) fileBody, formValues, perr := extractHTTPFormValues(reader)
if perr != nil { if perr != nil {
errorIf(perr.Trace(), "Unable to parse form values.", nil) errorIf(perr.Trace(), "Unable to parse form values.", nil)
writeErrorResponse(w, req, MalformedPOSTRequest, 1, req.URL.Path) writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
return return
} }
bucket := mux.Vars(req)["bucket"] bucket := mux.Vars(req)["bucket"]
@ -376,22 +372,22 @@ func (api API) PostPolicyBucketHandler(w http.ResponseWriter, req *http.Request)
signature, perr := initPostPresignedPolicyV4(formValues) signature, perr := initPostPresignedPolicyV4(formValues)
if perr != nil { if perr != nil {
errorIf(perr.Trace(), "Unable to initialize post policy presigned.", nil) errorIf(perr.Trace(), "Unable to initialize post policy presigned.", nil)
writeErrorResponse(w, req, MalformedPOSTRequest, 1, req.URL.Path) writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
return return
} }
if perr = applyPolicy(formValues, signature.PresignedPolicy); perr != nil { if perr = applyPolicy(formValues, signature.PresignedPolicy); perr != nil {
errorIf(perr.Trace(), "Invalid request, policy doesn't match with the endpoint.", nil) errorIf(perr.Trace(), "Invalid request, policy doesn't match with the endpoint.", nil)
writeErrorResponse(w, req, MalformedPOSTRequest, 1, req.URL.Path) writeErrorResponse(w, req, MalformedPOSTRequest, req.URL.Path)
return return
} }
var ok bool var ok bool
if ok, perr = signature.DoesPolicySignatureMatch(formValues["X-Amz-Date"]); perr != nil { if ok, perr = signature.DoesPolicySignatureMatch(formValues["X-Amz-Date"]); perr != nil {
errorIf(perr.Trace(), "Unable to verify signature.", nil) errorIf(perr.Trace(), "Unable to verify signature.", nil)
writeErrorResponse(w, req, SignatureDoesNotMatch, 1, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
return return
} }
if ok == false { if ok == false {
writeErrorResponse(w, req, SignatureDoesNotMatch, 1, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
return return
} }
metadata, perr := api.Donut.CreateObject(bucket, object, "", 0, fileBody, nil, nil) metadata, perr := api.Donut.CreateObject(bucket, object, "", 0, fileBody, nil, nil)
@ -399,30 +395,30 @@ func (api API) PostPolicyBucketHandler(w http.ResponseWriter, req *http.Request)
errorIf(perr.Trace(), "CreateObject failed.", nil) errorIf(perr.Trace(), "CreateObject failed.", nil)
switch perr.ToGoError().(type) { switch perr.ToGoError().(type) {
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, 1, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, 1, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
case donut.ObjectExists: case donut.ObjectExists:
writeErrorResponse(w, req, MethodNotAllowed, 1, req.URL.Path) writeErrorResponse(w, req, MethodNotAllowed, req.URL.Path)
case donut.BadDigest: case donut.BadDigest:
writeErrorResponse(w, req, BadDigest, 1, req.URL.Path) writeErrorResponse(w, req, BadDigest, req.URL.Path)
case signv4.MissingDateHeader: case signv4.MissingDateHeader:
writeErrorResponse(w, req, RequestTimeTooSkewed, 1, req.URL.Path) writeErrorResponse(w, req, RequestTimeTooSkewed, req.URL.Path)
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, 1, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.IncompleteBody: case donut.IncompleteBody:
writeErrorResponse(w, req, IncompleteBody, 1, req.URL.Path) writeErrorResponse(w, req, IncompleteBody, req.URL.Path)
case donut.EntityTooLarge: case donut.EntityTooLarge:
writeErrorResponse(w, req, EntityTooLarge, 1, req.URL.Path) writeErrorResponse(w, req, EntityTooLarge, req.URL.Path)
case donut.InvalidDigest: case donut.InvalidDigest:
writeErrorResponse(w, req, InvalidDigest, 1, req.URL.Path) writeErrorResponse(w, req, InvalidDigest, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, 1, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
w.Header().Set("ETag", metadata.MD5Sum) w.Header().Set("ETag", metadata.MD5Sum)
writeSuccessResponse(w, 1) writeSuccessResponse(w)
} }
// PutBucketACLHandler - PUT Bucket ACL // PutBucketACLHandler - PUT Bucket ACL
@ -438,12 +434,10 @@ func (api API) PutBucketACLHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req)
// read from 'x-amz-acl' // read from 'x-amz-acl'
aclType := getACLType(req) aclType := getACLType(req)
if aclType == unsupportedACLType { if aclType == unsupportedACLType {
writeErrorResponse(w, req, NotImplemented, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NotImplemented, req.URL.Path)
return return
} }
@ -457,7 +451,7 @@ func (api API) PutBucketACLHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -467,17 +461,17 @@ func (api API) PutBucketACLHandler(w http.ResponseWriter, req *http.Request) {
errorIf(err.Trace(), "PutBucketACL failed.", nil) errorIf(err.Trace(), "PutBucketACL failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
writeSuccessResponse(w, acceptsContentType) writeSuccessResponse(w)
} }
// HeadBucketHandler - HEAD Bucket // HeadBucketHandler - HEAD Bucket
@ -496,8 +490,6 @@ func (api API) HeadBucketHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req)
vars := mux.Vars(req) vars := mux.Vars(req)
bucket := vars["bucket"] bucket := vars["bucket"]
@ -508,7 +500,7 @@ func (api API) HeadBucketHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -518,15 +510,15 @@ func (api API) HeadBucketHandler(w http.ResponseWriter, req *http.Request) {
errorIf(err.Trace(), "GetBucketMetadata failed.", nil) errorIf(err.Trace(), "GetBucketMetadata failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
writeSuccessResponse(w, acceptsContentType) writeSuccessResponse(w)
} }

View File

@ -1,56 +0,0 @@
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* 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.
*/
package main
import "net/http"
type contentType int
const (
unknownContentType contentType = iota
xmlContentType
jsonContentType
)
// Get content type requested from 'Accept' header
func getContentType(req *http.Request) contentType {
acceptHeader := req.Header.Get("Accept")
switch {
case acceptHeader == "application/json":
return jsonContentType
default:
return xmlContentType
}
}
// Content type to human readable string
func getContentTypeString(content contentType) string {
switch content {
case jsonContentType:
{
return "application/json"
}
case xmlContentType:
{
return "application/xml"
}
default:
{
return "application/xml"
}
}
}

View File

@ -28,10 +28,6 @@ import (
// MiddlewareHandler - useful to chain different middleware http.Handler // MiddlewareHandler - useful to chain different middleware http.Handler
type MiddlewareHandler func(http.Handler) http.Handler type MiddlewareHandler func(http.Handler) http.Handler
type contentTypeHandler struct {
handler http.Handler
}
type timeHandler struct { type timeHandler struct {
handler http.Handler handler http.Handler
} }
@ -74,44 +70,29 @@ func parseDate(req *http.Request) (time.Time, error) {
return time.Time{}, errors.New("invalid request") return time.Time{}, errors.New("invalid request")
} }
// ValidContentTypeHandler to validate Accept type
func ValidContentTypeHandler(h http.Handler) http.Handler {
return contentTypeHandler{h}
}
func (h contentTypeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
acceptsContentType := getContentType(r)
if acceptsContentType == unknownContentType {
writeErrorResponse(w, r, NotAcceptable, acceptsContentType, r.URL.Path)
return
}
h.handler.ServeHTTP(w, r)
}
// TimeValidityHandler to validate parsable time over http header // TimeValidityHandler to validate parsable time over http header
func TimeValidityHandler(h http.Handler) http.Handler { func TimeValidityHandler(h http.Handler) http.Handler {
return timeHandler{h} return timeHandler{h}
} }
func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
acceptsContentType := getContentType(r)
// Verify if date headers are set, if not reject the request // Verify if date headers are set, if not reject the request
if r.Header.Get("Authorization") != "" { if r.Header.Get("Authorization") != "" {
if r.Header.Get(http.CanonicalHeaderKey("x-amz-date")) == "" && r.Header.Get("Date") == "" { if r.Header.Get(http.CanonicalHeaderKey("x-amz-date")) == "" && r.Header.Get("Date") == "" {
// there is no way to knowing if this is a valid request, could be a attack reject such clients // there is no way to knowing if this is a valid request, could be a attack reject such clients
writeErrorResponse(w, r, RequestTimeTooSkewed, acceptsContentType, r.URL.Path) writeErrorResponse(w, r, RequestTimeTooSkewed, r.URL.Path)
return return
} }
date, err := parseDate(r) date, err := parseDate(r)
if err != nil { if err != nil {
// there is no way to knowing if this is a valid request, could be a attack reject such clients // there is no way to knowing if this is a valid request, could be a attack reject such clients
writeErrorResponse(w, r, RequestTimeTooSkewed, acceptsContentType, r.URL.Path) writeErrorResponse(w, r, RequestTimeTooSkewed, r.URL.Path)
return return
} }
duration := time.Since(date) duration := time.Since(date)
minutes := time.Duration(5) * time.Minute minutes := time.Duration(5) * time.Minute
if duration.Minutes() > minutes.Minutes() { if duration.Minutes() > minutes.Minutes() {
writeErrorResponse(w, r, RequestTimeTooSkewed, acceptsContentType, r.URL.Path) writeErrorResponse(w, r, RequestTimeTooSkewed, r.URL.Path)
return return
} }
} }
@ -127,20 +108,19 @@ func ValidateAuthHeaderHandler(h http.Handler) http.Handler {
// validate auth header handler ServeHTTP() wrapper // validate auth header handler ServeHTTP() wrapper
func (h validateAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h validateAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
acceptsContentType := getContentType(r)
accessKeyID, err := stripAccessKeyID(r.Header.Get("Authorization")) accessKeyID, err := stripAccessKeyID(r.Header.Get("Authorization"))
switch err.ToGoError() { switch err.ToGoError() {
case errInvalidRegion: case errInvalidRegion:
writeErrorResponse(w, r, AuthorizationHeaderMalformed, acceptsContentType, r.URL.Path) writeErrorResponse(w, r, AuthorizationHeaderMalformed, r.URL.Path)
return return
case errAccessKeyIDInvalid: case errAccessKeyIDInvalid:
writeErrorResponse(w, r, InvalidAccessKeyID, acceptsContentType, r.URL.Path) writeErrorResponse(w, r, InvalidAccessKeyID, r.URL.Path)
return return
case nil: case nil:
// load auth config // load auth config
authConfig, err := auth.LoadConfig() authConfig, err := auth.LoadConfig()
if err != nil { if err != nil {
writeErrorResponse(w, r, InternalError, acceptsContentType, r.URL.Path) writeErrorResponse(w, r, InternalError, r.URL.Path)
return return
} }
// Access key not found // Access key not found
@ -150,7 +130,7 @@ func (h validateAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
} }
writeErrorResponse(w, r, InvalidAccessKeyID, acceptsContentType, r.URL.Path) writeErrorResponse(w, r, InvalidAccessKeyID, r.URL.Path)
return return
// All other errors for now, serve them // All other errors for now, serve them
default: default:
@ -175,9 +155,8 @@ func IgnoreResourcesHandler(h http.Handler) http.Handler {
// Resource handler ServeHTTP() wrapper // Resource handler ServeHTTP() wrapper
func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
acceptsContentType := getContentType(r)
if ignoreNotImplementedObjectResources(r) || ignoreNotImplementedBucketResources(r) { if ignoreNotImplementedObjectResources(r) || ignoreNotImplementedBucketResources(r) {
writeErrorResponse(w, r, NotImplemented, acceptsContentType, r.URL.Path) writeErrorResponse(w, r, NotImplemented, r.URL.Path)
return return
} }
h.handler.ServeHTTP(w, r) h.handler.ServeHTTP(w, r)

View File

@ -19,7 +19,6 @@ package main
import ( import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"encoding/json"
"encoding/xml" "encoding/xml"
"net/http" "net/http"
"runtime" "runtime"
@ -28,11 +27,6 @@ import (
"github.com/minio/minio/pkg/donut" "github.com/minio/minio/pkg/donut"
) )
// No encoder interface exists, so we create one.
type encoder interface {
Encode(v interface{}) error
}
//// helpers //// helpers
// Static alphaNumeric table used for generating unique request ids // Static alphaNumeric table used for generating unique request ids
@ -49,32 +43,21 @@ func generateRequestID() []byte {
} }
// Write http common headers // Write http common headers
func setCommonHeaders(w http.ResponseWriter, acceptsType string, contentLength int) { func setCommonHeaders(w http.ResponseWriter, contentLength int) {
// set unique request ID for each reply // set unique request ID for each reply
w.Header().Set("X-Amz-Request-Id", string(generateRequestID())) w.Header().Set("X-Amz-Request-Id", string(generateRequestID()))
w.Header().Set("Server", ("Minio/" + minioReleaseTag + " (" + runtime.GOOS + ";" + runtime.GOARCH + ")")) w.Header().Set("Server", ("Minio/" + minioReleaseTag + " (" + runtime.GOOS + ";" + runtime.GOARCH + ")"))
w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Content-Type", acceptsType)
w.Header().Set("Connection", "close") w.Header().Set("Connection", "close")
// should be set to '0' by default // should be set to '0' by default
w.Header().Set("Content-Length", strconv.Itoa(contentLength)) w.Header().Set("Content-Length", strconv.Itoa(contentLength))
} }
// Write error response headers // Write error response headers
func encodeErrorResponse(response interface{}, acceptsType contentType) []byte { func encodeErrorResponse(response interface{}) []byte {
var bytesBuffer bytes.Buffer var bytesBuffer bytes.Buffer
var e encoder
// write common headers // write common headers
switch acceptsType { e := xml.NewEncoder(&bytesBuffer)
case xmlContentType:
e = xml.NewEncoder(&bytesBuffer)
case jsonContentType:
e = json.NewEncoder(&bytesBuffer)
// by default even if unknown Accept header received handle it by sending XML contenttype response
default:
e = xml.NewEncoder(&bytesBuffer)
}
e.Encode(response) e.Encode(response)
return bytesBuffer.Bytes() return bytesBuffer.Bytes()
} }
@ -84,16 +67,17 @@ func setObjectHeaders(w http.ResponseWriter, metadata donut.ObjectMetadata, cont
// set common headers // set common headers
if contentRange != nil { if contentRange != nil {
if contentRange.length > 0 { if contentRange.length > 0 {
setCommonHeaders(w, metadata.Metadata["contentType"], int(contentRange.length)) setCommonHeaders(w, int(contentRange.length))
} else { } else {
setCommonHeaders(w, metadata.Metadata["contentType"], int(metadata.Size)) setCommonHeaders(w, int(metadata.Size))
} }
} else { } else {
setCommonHeaders(w, metadata.Metadata["contentType"], int(metadata.Size)) setCommonHeaders(w, int(metadata.Size))
} }
// set object headers // set object headers
lastModified := metadata.Created.Format(http.TimeFormat) lastModified := metadata.Created.Format(http.TimeFormat)
// object related headers // object related headers
w.Header().Set("Content-Type", metadata.Metadata["contentType"])
w.Header().Set("ETag", "\""+metadata.MD5Sum+"\"") w.Header().Set("ETag", "\""+metadata.MD5Sum+"\"")
w.Header().Set("Last-Modified", lastModified) w.Header().Set("Last-Modified", lastModified)
@ -106,15 +90,9 @@ func setObjectHeaders(w http.ResponseWriter, metadata donut.ObjectMetadata, cont
} }
} }
func encodeSuccessResponse(response interface{}, acceptsType contentType) []byte { func encodeSuccessResponse(response interface{}) []byte {
var e encoder
var bytesBuffer bytes.Buffer var bytesBuffer bytes.Buffer
switch acceptsType { e := xml.NewEncoder(&bytesBuffer)
case xmlContentType:
e = xml.NewEncoder(&bytesBuffer)
case jsonContentType:
e = json.NewEncoder(&bytesBuffer)
}
e.Encode(response) e.Encode(response)
return bytesBuffer.Bytes() return bytesBuffer.Bytes()
} }

View File

@ -44,8 +44,7 @@ func (api API) GetObjectHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
@ -61,7 +60,7 @@ func (api API) GetObjectHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} else { } else {
@ -72,11 +71,11 @@ func (api API) GetObjectHandler(w http.ResponseWriter, req *http.Request) {
switch err.ToGoError() { switch err.ToGoError() {
case errAccessKeyIDInvalid: case errAccessKeyIDInvalid:
errorIf(err.Trace(), "Invalid access key id requested.", nil) errorIf(err.Trace(), "Invalid access key id requested.", nil)
writeErrorResponse(w, req, InvalidAccessKeyID, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidAccessKeyID, req.URL.Path)
return return
default: default:
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -87,24 +86,24 @@ func (api API) GetObjectHandler(w http.ResponseWriter, req *http.Request) {
errorIf(err.Trace(), "GetObject failed.", nil) errorIf(err.Trace(), "GetObject failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
case donut.ObjectNotFound: case donut.ObjectNotFound:
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
case donut.ObjectNameInvalid: case donut.ObjectNameInvalid:
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
var hrange *httpRange var hrange *httpRange
hrange, err = getRequestedRange(req.Header.Get("Range"), metadata.Size) hrange, err = getRequestedRange(req.Header.Get("Range"), metadata.Size)
if err != nil { if err != nil {
writeErrorResponse(w, req, InvalidRange, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidRange, req.URL.Path)
return return
} }
setObjectHeaders(w, metadata, hrange) setObjectHeaders(w, metadata, hrange)
@ -127,8 +126,7 @@ func (api API) HeadObjectHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
@ -144,7 +142,7 @@ func (api API) HeadObjectHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -154,17 +152,17 @@ func (api API) HeadObjectHandler(w http.ResponseWriter, req *http.Request) {
errorIf(err.Trace(), "GetObjectMetadata failed.", nil) errorIf(err.Trace(), "GetObjectMetadata failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
case donut.ObjectNotFound: case donut.ObjectNotFound:
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
case donut.ObjectNameInvalid: case donut.ObjectNameInvalid:
writeErrorResponse(w, req, NoSuchKey, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchKey, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
@ -185,8 +183,7 @@ func (api API) PutObjectHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
@ -198,18 +195,18 @@ func (api API) PutObjectHandler(w http.ResponseWriter, req *http.Request) {
// get Content-MD5 sent by client and verify if valid // get Content-MD5 sent by client and verify if valid
md5 := req.Header.Get("Content-MD5") md5 := req.Header.Get("Content-MD5")
if !isValidMD5(md5) { if !isValidMD5(md5) {
writeErrorResponse(w, req, InvalidDigest, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidDigest, req.URL.Path)
return return
} }
/// if Content-Length missing, deny the request /// if Content-Length missing, deny the request
size := req.Header.Get("Content-Length") size := req.Header.Get("Content-Length")
if size == "" { if size == "" {
writeErrorResponse(w, req, MissingContentLength, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, MissingContentLength, req.URL.Path)
return return
} }
/// maximum Upload size for objects in a single operation /// maximum Upload size for objects in a single operation
if isMaxObjectSize(size) { if isMaxObjectSize(size) {
writeErrorResponse(w, req, EntityTooLarge, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, EntityTooLarge, req.URL.Path)
return return
} }
/// minimum Upload size for objects in a single operation /// minimum Upload size for objects in a single operation
@ -219,7 +216,7 @@ func (api API) PutObjectHandler(w http.ResponseWriter, req *http.Request) {
// create a 0byte file using a regular putObject() operation // create a 0byte file using a regular putObject() operation
// //
// if isMinObjectSize(size) { // if isMinObjectSize(size) {
// writeErrorResponse(w, req, EntityTooSmall, acceptsContentType, req.URL.Path) // writeErrorResponse(w, req, EntityTooSmall, req.URL.Path)
// return // return
// } // }
var sizeInt64 int64 var sizeInt64 int64
@ -227,7 +224,7 @@ func (api API) PutObjectHandler(w http.ResponseWriter, req *http.Request) {
var err error var err error
sizeInt64, err = strconv.ParseInt(size, 10, 64) sizeInt64, err = strconv.ParseInt(size, 10, 64)
if err != nil { if err != nil {
writeErrorResponse(w, req, InvalidRequest, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidRequest, req.URL.Path)
return return
} }
} }
@ -239,7 +236,7 @@ func (api API) PutObjectHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -249,30 +246,30 @@ func (api API) PutObjectHandler(w http.ResponseWriter, req *http.Request) {
errorIf(err.Trace(), "CreateObject failed.", nil) errorIf(err.Trace(), "CreateObject failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case donut.BucketNotFound: case donut.BucketNotFound:
writeErrorResponse(w, req, NoSuchBucket, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchBucket, req.URL.Path)
case donut.BucketNameInvalid: case donut.BucketNameInvalid:
writeErrorResponse(w, req, InvalidBucketName, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidBucketName, req.URL.Path)
case donut.ObjectExists: case donut.ObjectExists:
writeErrorResponse(w, req, MethodNotAllowed, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, MethodNotAllowed, req.URL.Path)
case donut.BadDigest: case donut.BadDigest:
writeErrorResponse(w, req, BadDigest, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, BadDigest, req.URL.Path)
case signv4.MissingDateHeader: case signv4.MissingDateHeader:
writeErrorResponse(w, req, RequestTimeTooSkewed, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, RequestTimeTooSkewed, req.URL.Path)
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.IncompleteBody: case donut.IncompleteBody:
writeErrorResponse(w, req, IncompleteBody, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, IncompleteBody, req.URL.Path)
case donut.EntityTooLarge: case donut.EntityTooLarge:
writeErrorResponse(w, req, EntityTooLarge, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, EntityTooLarge, req.URL.Path)
case donut.InvalidDigest: case donut.InvalidDigest:
writeErrorResponse(w, req, InvalidDigest, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidDigest, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
w.Header().Set("ETag", metadata.MD5Sum) w.Header().Set("ETag", metadata.MD5Sum)
writeSuccessResponse(w, acceptsContentType) writeSuccessResponse(w)
} }
/// Multipart API /// Multipart API
@ -288,13 +285,12 @@ func (api API) NewMultipartUploadHandler(w http.ResponseWriter, req *http.Reques
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
if !isRequestUploads(req.URL.Query()) { if !isRequestUploads(req.URL.Query()) {
writeErrorResponse(w, req, MethodNotAllowed, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, MethodNotAllowed, req.URL.Path)
return return
} }
@ -310,7 +306,7 @@ func (api API) NewMultipartUploadHandler(w http.ResponseWriter, req *http.Reques
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -320,19 +316,19 @@ func (api API) NewMultipartUploadHandler(w http.ResponseWriter, req *http.Reques
errorIf(err.Trace(), "NewMultipartUpload failed.", nil) errorIf(err.Trace(), "NewMultipartUpload failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.ObjectExists: case donut.ObjectExists:
writeErrorResponse(w, req, MethodNotAllowed, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, MethodNotAllowed, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
response := generateInitiateMultipartUploadResponse(bucket, object, uploadID) response := generateInitiateMultipartUploadResponse(bucket, object, uploadID)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) setCommonHeaders(w, len(encodedSuccessResponse))
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
} }
@ -348,28 +344,27 @@ func (api API) PutObjectPartHandler(w http.ResponseWriter, req *http.Request) {
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
// get Content-MD5 sent by client and verify if valid // get Content-MD5 sent by client and verify if valid
md5 := req.Header.Get("Content-MD5") md5 := req.Header.Get("Content-MD5")
if !isValidMD5(md5) { if !isValidMD5(md5) {
writeErrorResponse(w, req, InvalidDigest, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidDigest, req.URL.Path)
return return
} }
/// if Content-Length missing, throw away /// if Content-Length missing, throw away
size := req.Header.Get("Content-Length") size := req.Header.Get("Content-Length")
if size == "" { if size == "" {
writeErrorResponse(w, req, MissingContentLength, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, MissingContentLength, req.URL.Path)
return return
} }
/// maximum Upload size for multipart objects in a single operation /// maximum Upload size for multipart objects in a single operation
if isMaxObjectSize(size) { if isMaxObjectSize(size) {
writeErrorResponse(w, req, EntityTooLarge, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, EntityTooLarge, req.URL.Path)
return return
} }
@ -378,7 +373,7 @@ func (api API) PutObjectPartHandler(w http.ResponseWriter, req *http.Request) {
var err error var err error
sizeInt64, err = strconv.ParseInt(size, 10, 64) sizeInt64, err = strconv.ParseInt(size, 10, 64)
if err != nil { if err != nil {
writeErrorResponse(w, req, InvalidRequest, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidRequest, req.URL.Path)
return return
} }
} }
@ -395,7 +390,7 @@ func (api API) PutObjectPartHandler(w http.ResponseWriter, req *http.Request) {
var err error var err error
partID, err = strconv.Atoi(partIDString) partID, err = strconv.Atoi(partIDString)
if err != nil { if err != nil {
writeErrorResponse(w, req, InvalidPart, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidPart, req.URL.Path)
return return
} }
} }
@ -407,7 +402,7 @@ func (api API) PutObjectPartHandler(w http.ResponseWriter, req *http.Request) {
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -417,26 +412,26 @@ func (api API) PutObjectPartHandler(w http.ResponseWriter, req *http.Request) {
errorIf(err.Trace(), "CreateObjectPart failed.", nil) errorIf(err.Trace(), "CreateObjectPart failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case donut.InvalidUploadID: case donut.InvalidUploadID:
writeErrorResponse(w, req, NoSuchUpload, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchUpload, req.URL.Path)
case donut.ObjectExists: case donut.ObjectExists:
writeErrorResponse(w, req, MethodNotAllowed, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, MethodNotAllowed, req.URL.Path)
case donut.BadDigest: case donut.BadDigest:
writeErrorResponse(w, req, BadDigest, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, BadDigest, req.URL.Path)
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.IncompleteBody: case donut.IncompleteBody:
writeErrorResponse(w, req, IncompleteBody, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, IncompleteBody, req.URL.Path)
case donut.EntityTooLarge: case donut.EntityTooLarge:
writeErrorResponse(w, req, EntityTooLarge, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, EntityTooLarge, req.URL.Path)
case donut.InvalidDigest: case donut.InvalidDigest:
writeErrorResponse(w, req, InvalidDigest, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidDigest, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
w.Header().Set("ETag", calculatedMD5) w.Header().Set("ETag", calculatedMD5)
writeSuccessResponse(w, acceptsContentType) writeSuccessResponse(w)
} }
// AbortMultipartUploadHandler - Abort multipart upload // AbortMultipartUploadHandler - Abort multipart upload
@ -450,8 +445,7 @@ func (api API) AbortMultipartUploadHandler(w http.ResponseWriter, req *http.Requ
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
@ -468,7 +462,7 @@ func (api API) AbortMultipartUploadHandler(w http.ResponseWriter, req *http.Requ
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -478,15 +472,15 @@ func (api API) AbortMultipartUploadHandler(w http.ResponseWriter, req *http.Requ
errorIf(err.Trace(), "AbortMutlipartUpload failed.", nil) errorIf(err.Trace(), "AbortMutlipartUpload failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.InvalidUploadID: case donut.InvalidUploadID:
writeErrorResponse(w, req, NoSuchUpload, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchUpload, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
setCommonHeaders(w, getContentTypeString(acceptsContentType), 0) setCommonHeaders(w, 0)
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} }
@ -501,18 +495,17 @@ func (api API) ListObjectPartsHandler(w http.ResponseWriter, req *http.Request)
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
objectResourcesMetadata := getObjectResources(req.URL.Query()) objectResourcesMetadata := getObjectResources(req.URL.Query())
if objectResourcesMetadata.PartNumberMarker < 0 { if objectResourcesMetadata.PartNumberMarker < 0 {
writeErrorResponse(w, req, InvalidPartNumberMarker, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidPartNumberMarker, req.URL.Path)
return return
} }
if objectResourcesMetadata.MaxParts < 0 { if objectResourcesMetadata.MaxParts < 0 {
writeErrorResponse(w, req, InvalidMaxParts, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidMaxParts, req.URL.Path)
return return
} }
if objectResourcesMetadata.MaxParts == 0 { if objectResourcesMetadata.MaxParts == 0 {
@ -530,7 +523,7 @@ func (api API) ListObjectPartsHandler(w http.ResponseWriter, req *http.Request)
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -540,18 +533,18 @@ func (api API) ListObjectPartsHandler(w http.ResponseWriter, req *http.Request)
errorIf(err.Trace(), "ListObjectParts failed.", nil) errorIf(err.Trace(), "ListObjectParts failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.InvalidUploadID: case donut.InvalidUploadID:
writeErrorResponse(w, req, NoSuchUpload, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchUpload, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
response := generateListPartsResponse(objectResourcesMetadata) response := generateListPartsResponse(objectResourcesMetadata)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) setCommonHeaders(w, len(encodedSuccessResponse))
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
} }
@ -567,8 +560,7 @@ func (api API) CompleteMultipartUploadHandler(w http.ResponseWriter, req *http.R
<-op.ProceedCh <-op.ProceedCh
} }
acceptsContentType := getContentType(req) if !api.isValidOp(w, req) {
if !api.isValidOp(w, req, acceptsContentType) {
return return
} }
@ -585,7 +577,7 @@ func (api API) CompleteMultipartUploadHandler(w http.ResponseWriter, req *http.R
signature, err = initSignatureV4(req) signature, err = initSignatureV4(req)
if err != nil { if err != nil {
errorIf(err.Trace(), "Initializing signature v4 failed.", nil) errorIf(err.Trace(), "Initializing signature v4 failed.", nil)
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
return return
} }
} }
@ -594,28 +586,28 @@ func (api API) CompleteMultipartUploadHandler(w http.ResponseWriter, req *http.R
errorIf(err.Trace(), "CompleteMultipartUpload failed.", nil) errorIf(err.Trace(), "CompleteMultipartUpload failed.", nil)
switch err.ToGoError().(type) { switch err.ToGoError().(type) {
case donut.InvalidUploadID: case donut.InvalidUploadID:
writeErrorResponse(w, req, NoSuchUpload, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchUpload, req.URL.Path)
case donut.InvalidPart: case donut.InvalidPart:
writeErrorResponse(w, req, InvalidPart, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidPart, req.URL.Path)
case donut.InvalidPartOrder: case donut.InvalidPartOrder:
writeErrorResponse(w, req, InvalidPartOrder, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InvalidPartOrder, req.URL.Path)
case signv4.MissingDateHeader: case signv4.MissingDateHeader:
writeErrorResponse(w, req, RequestTimeTooSkewed, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, RequestTimeTooSkewed, req.URL.Path)
case signv4.DoesNotMatch: case signv4.DoesNotMatch:
writeErrorResponse(w, req, SignatureDoesNotMatch, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, SignatureDoesNotMatch, req.URL.Path)
case donut.IncompleteBody: case donut.IncompleteBody:
writeErrorResponse(w, req, IncompleteBody, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, IncompleteBody, req.URL.Path)
case donut.MalformedXML: case donut.MalformedXML:
writeErrorResponse(w, req, MalformedXML, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, MalformedXML, req.URL.Path)
default: default:
writeErrorResponse(w, req, InternalError, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, InternalError, req.URL.Path)
} }
return return
} }
response := generateCompleteMultpartUploadResponse(bucket, object, "", metadata.MD5Sum) response := generateCompleteMultpartUploadResponse(bucket, object, "", metadata.MD5Sum)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse)) setCommonHeaders(w, len(encodedSuccessResponse))
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
} }

View File

@ -177,19 +177,19 @@ func generateListMultipartUploadsResponse(bucket string, metadata donut.BucketMu
} }
// writeSuccessResponse write success headers // writeSuccessResponse write success headers
func writeSuccessResponse(w http.ResponseWriter, acceptsContentType contentType) { func writeSuccessResponse(w http.ResponseWriter) {
setCommonHeaders(w, getContentTypeString(acceptsContentType), 0) setCommonHeaders(w, 0)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// writeErrorRespone write error headers // writeErrorRespone write error headers
func writeErrorResponse(w http.ResponseWriter, req *http.Request, errorType int, acceptsContentType contentType, resource string) { func writeErrorResponse(w http.ResponseWriter, req *http.Request, errorType int, resource string) {
error := getErrorCode(errorType) error := getErrorCode(errorType)
// generate error response // generate error response
errorResponse := getErrorResponse(error, resource) errorResponse := getErrorResponse(error, resource)
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) encodedErrorResponse := encodeErrorResponse(errorResponse)
// set common headers // set common headers
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedErrorResponse)) setCommonHeaders(w, len(encodedErrorResponse))
// write Header // write Header
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
// HEAD should have no body, do not attempt to write to it // HEAD should have no body, do not attempt to write to it

View File

@ -83,7 +83,6 @@ func getNewAPI() API {
// getAPIHandler api handler // getAPIHandler api handler
func getAPIHandler(api API) http.Handler { func getAPIHandler(api API) http.Handler {
var mwHandlers = []MiddlewareHandler{ var mwHandlers = []MiddlewareHandler{
ValidContentTypeHandler,
TimeValidityHandler, TimeValidityHandler,
IgnoreResourcesHandler, IgnoreResourcesHandler,
ValidateAuthHeaderHandler, ValidateAuthHeaderHandler,