From d5db891094725bc85f4576f3ebbb623e51124b05 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sat, 28 Mar 2015 18:00:55 -0700 Subject: [PATCH] Simplfying code in api, round 1. Decoupling functions which both write headers and data. --- pkg/api/api_bucket_handlers.go | 120 +++++++++++++++++++++++--------- pkg/api/api_generic_handlers.go | 20 +++--- pkg/api/api_object_handlers.go | 85 +++++++++++++--------- pkg/api/api_policy_handlers.go | 59 +++++++++------- pkg/api/contenttype.go | 40 ++++++----- pkg/api/errors.go | 2 +- pkg/api/headers.go | 27 +++---- pkg/utils/log/definitions.go | 3 + 8 files changed, 221 insertions(+), 135 deletions(-) diff --git a/pkg/api/api_bucket_handlers.go b/pkg/api/api_bucket_handlers.go index 7ffed9c09..eb26f3a3b 100644 --- a/pkg/api/api_bucket_handlers.go +++ b/pkg/api/api_bucket_handlers.go @@ -46,41 +46,70 @@ func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Requ acceptsContentType := getContentType(req) objects, resources, err := server.driver.ListObjects(bucket, resources) - switch err := err.(type) { + switch err.(type) { case nil: // success { + // write headers + setCommonHeaders(w, getContentTypeString(acceptsContentType)) + w.WriteHeader(http.StatusOK) + // write body response := generateObjectsListResult(bucket, objects, resources) - w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType)) + encodedResponse := encodeResponse(response, acceptsContentType) + w.Write(encodedResponse) } case drivers.BucketNotFound: { - error := errorCodeError(NoSuchBucket) + // get error + error := getErrorCode(NoSuchBucket) errorResponse := getErrorResponse(error, bucket) + + // write headers + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + + // write body + encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedErrorResponse) } case drivers.ImplementationError: { - // Embed error log on server side - log.Error.Println(err) - error := errorCodeError(InternalError) + // get error + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, bucket) + + // write headers + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + + // write body + encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedErrorResponse) } case drivers.BucketNameInvalid: { - error := errorCodeError(InvalidBucketName) + error := getErrorCode(InvalidBucketName) errorResponse := getErrorResponse(error, bucket) + + // write headers + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + + // write body + encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedErrorResponse) } case drivers.ObjectNameInvalid: { - error := errorCodeError(NoSuchKey) + error := getErrorCode(NoSuchKey) errorResponse := getErrorResponse(error, resources.Prefix) + + // write headers + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + + // write body + encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedErrorResponse) } } } @@ -96,23 +125,37 @@ func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Requ case nil: { response := generateBucketsListResult(buckets) - w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType)) - } - case drivers.ImplementationError: - { - log.Error.Println(err) - error := errorCodeError(InternalError) - errorResponse := getErrorResponse(error, "") - w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + // write headers + setCommonHeaders(w, getContentTypeString(acceptsContentType)) + w.WriteHeader(http.StatusOK) + // write response + encodedResponse := encodeResponse(response, acceptsContentType) + w.Write(encodedResponse) } case drivers.BackendCorrupted: { log.Error.Println(err) - error := errorCodeError(InternalError) - errorResponse := getErrorResponse(error, "") - w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + // get error + errorCode := getErrorCode(InternalError) + errorResponse := getErrorResponse(errorCode, "") + // write headers + setCommonHeaders(w, getContentTypeString(acceptsContentType)) + w.WriteHeader(errorCode.HTTPStatusCode) + // write + encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedErrorResponse) + } + default: + { + log.Trace.Println(err) + // get error + errorCode := getErrorCode(InternalError) + errorResponse := getErrorResponse(errorCode, "") + // write headers + w.WriteHeader(errorCode.HTTPStatusCode) + // write body + encodedError := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedError) } } } @@ -132,7 +175,7 @@ func (server *minioAPI) putBucketHandler(w http.ResponseWriter, req *http.Reques } acceptsContentType := getContentType(req) - switch err := err.(type) { + switch err.(type) { case nil: { w.Header().Set("Server", "Minio") @@ -140,26 +183,37 @@ func (server *minioAPI) putBucketHandler(w http.ResponseWriter, req *http.Reques } case drivers.BucketNameInvalid: { - error := errorCodeError(InvalidBucketName) + error := getErrorCode(InvalidBucketName) errorResponse := getErrorResponse(error, bucket) + // write header + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + // write object + encodedResponse := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedResponse) } case drivers.BucketExists: { - error := errorCodeError(BucketAlreadyExists) + error := getErrorCode(BucketAlreadyExists) errorResponse := getErrorResponse(error, bucket) + // write header + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + // write object + encodedResponse := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedResponse) } case drivers.ImplementationError: { // Embed errors log on server side - log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, bucket) + // write header + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + // write object + encodedResponse := encodeErrorResponse(errorResponse, acceptsContentType) + w.Write(encodedResponse) } } } diff --git a/pkg/api/api_generic_handlers.go b/pkg/api/api_generic_handlers.go index f27726920..d60decf4e 100644 --- a/pkg/api/api_generic_handlers.go +++ b/pkg/api/api_generic_handlers.go @@ -57,26 +57,29 @@ func (h vHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { acceptsContentType := getContentType(r) if accessKey != "" { if err := h.conf.ReadConfig(); err != nil { - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, "") + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } else { user, ok := h.conf.Users[accessKey] if ok == false { - error := errorCodeError(AccessDenied) + error := getErrorCode(AccessDenied) errorResponse := getErrorResponse(error, "") + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } else { ok, _ = ValidateRequest(user, r) if ok { h.handler.ServeHTTP(w, r) } else { - error := errorCodeError(AccessDenied) + error := getErrorCode(AccessDenied) errorResponse := getErrorResponse(error, "") + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } } } @@ -106,10 +109,11 @@ func ignoreResourcesHandler(h http.Handler) http.Handler { func (h rHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { acceptsContentType := getContentType(r) if ignoreUnImplementedObjectResources(r) || ignoreUnImplementedBucketResources(r) { - error := errorCodeError(NotImplemented) + error := getErrorCode(NotImplemented) errorResponse := getErrorResponse(error, "") + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } else { h.handler.ServeHTTP(w, r) } diff --git a/pkg/api/api_object_handlers.go b/pkg/api/api_object_handlers.go index 7d38e7bd0..ead70cd5c 100644 --- a/pkg/api/api_object_handlers.go +++ b/pkg/api/api_object_handlers.go @@ -43,10 +43,11 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques httpRange, err := newRange(req, metadata.Size) if err != nil { log.Error.Println(err) - error := errorCodeError(InvalidRange) + error := getErrorCode(InvalidRange) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) return } switch httpRange.start == 0 && httpRange.length == 0 { @@ -54,10 +55,11 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques writeObjectHeaders(w, metadata) if _, err := server.driver.GetObject(w, bucket, object); err != nil { log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) return } case false: @@ -67,10 +69,11 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques _, err := server.driver.GetPartialObject(w, bucket, object, httpRange.start, httpRange.length) if err != nil { log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) return } @@ -78,40 +81,45 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques } case drivers.ObjectNotFound: { - error := errorCodeError(NoSuchKey) + error := getErrorCode(NoSuchKey) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BucketNotFound: { - error := errorCodeError(NoSuchBucket) + error := getErrorCode(NoSuchBucket) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.ObjectNameInvalid: { - error := errorCodeError(NoSuchKey) + error := getErrorCode(NoSuchKey) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BucketNameInvalid: { - error := errorCodeError(InvalidBucketName) + error := getErrorCode(InvalidBucketName) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } default: { // Embed errors log on serve side log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } } } @@ -132,26 +140,29 @@ func (server *minioAPI) headObjectHandler(w http.ResponseWriter, req *http.Reque writeObjectHeaders(w, metadata) case drivers.ObjectNotFound: { - error := errorCodeError(NoSuchKey) + error := getErrorCode(NoSuchKey) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.ObjectNameInvalid: { - error := errorCodeError(NoSuchKey) + error := getErrorCode(NoSuchKey) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.ImplementationError: { // Embed error log on server side log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } } } @@ -183,45 +194,51 @@ func (server *minioAPI) putObjectHandler(w http.ResponseWriter, req *http.Reques { // Embed error log on server side log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BucketNotFound: { - error := errorCodeError(NoSuchBucket) + error := getErrorCode(NoSuchBucket) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BucketNameInvalid: { - error := errorCodeError(InvalidBucketName) + error := getErrorCode(InvalidBucketName) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.ObjectExists: { - error := errorCodeError(NotImplemented) + error := getErrorCode(NotImplemented) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BadDigest: { - error := errorCodeError(BadDigest) + error := getErrorCode(BadDigest) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.InvalidDigest: { - error := errorCodeError(InvalidDigest) + error := getErrorCode(InvalidDigest) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } } diff --git a/pkg/api/api_policy_handlers.go b/pkg/api/api_policy_handlers.go index ba95d726f..3a689e299 100644 --- a/pkg/api/api_policy_handlers.go +++ b/pkg/api/api_policy_handlers.go @@ -36,10 +36,11 @@ func (server *minioAPI) putBucketPolicyHandler(w http.ResponseWriter, req *http. policy, ok := drivers.Parsepolicy(req.Body) if ok == false { - error := errorCodeError(InvalidPolicyDocument) + error := getErrorCode(InvalidPolicyDocument) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) return } @@ -48,38 +49,42 @@ func (server *minioAPI) putBucketPolicyHandler(w http.ResponseWriter, req *http. case nil: { w.WriteHeader(http.StatusNoContent) - writeCommonHeaders(w, getContentString(acceptsContentType)) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.Header().Set("Connection", "keep-alive") } case drivers.BucketNameInvalid: { - error := errorCodeError(InvalidBucketName) + error := getErrorCode(InvalidBucketName) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BucketNotFound: { - error := errorCodeError(NoSuchBucket) + error := getErrorCode(NoSuchBucket) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BackendCorrupted: { log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.ImplementationError: { log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } } } @@ -99,51 +104,57 @@ func (server *minioAPI) getBucketPolicyHandler(w http.ResponseWriter, req *http. { responsePolicy, ret := json.Marshal(p) if ret != nil { - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } - writeCommonHeaders(w, getContentString(acceptsContentType)) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.Header().Set("Connection", "keep-alive") w.Write(responsePolicy) } case drivers.BucketNameInvalid: { - error := errorCodeError(InvalidBucketName) + error := getErrorCode(InvalidBucketName) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BucketNotFound: { - error := errorCodeError(NoSuchBucket) + error := getErrorCode(NoSuchBucket) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BucketPolicyNotFound: { - error := errorCodeError(NoSuchBucketPolicy) + error := getErrorCode(NoSuchBucketPolicy) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.BackendCorrupted: { log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } case drivers.ImplementationError: { log.Error.Println(err) - error := errorCodeError(InternalError) + error := getErrorCode(InternalError) errorResponse := getErrorResponse(error, bucket) + setCommonHeaders(w, getContentTypeString(acceptsContentType)) w.WriteHeader(error.HTTPStatusCode) - w.Write(writeErrorResponse(w, errorResponse, acceptsContentType)) + w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) } } } diff --git a/pkg/api/contenttype.go b/pkg/api/contenttype.go index 406ef5d41..44921f46e 100644 --- a/pkg/api/contenttype.go +++ b/pkg/api/contenttype.go @@ -18,36 +18,38 @@ package api import ( "net/http" + "strings" ) type contentType int const ( - xmlType contentType = iota - jsonType + xmlContentType contentType = iota + jsonContentType ) -// content-type to human readable map -var typeToString = map[contentType]string{ - xmlType: "application/xml", - jsonType: "application/json", -} - -// human readbale to content-type map -var acceptToType = map[string]contentType{ - "application/xml": xmlType, - "application/json": jsonType, -} - // Get content type requested from 'Accept' header func getContentType(req *http.Request) contentType { - if accept := req.Header.Get("Accept"); accept != "" { - return acceptToType[accept] + acceptHeader := req.Header.Get("Accept") + switch { + case strings.HasPrefix(acceptHeader, "application/json"): + return jsonContentType + default: + return xmlContentType } - return xmlType } // Content type to human readable string -func getContentString(content contentType) string { - return typeToString[content] +func getContentTypeString(content contentType) string { + switch content { + case jsonContentType: + { + + return "application/json" + } + default: + { + return "application/xml" + } + } } diff --git a/pkg/api/errors.go b/pkg/api/errors.go index 64ccf59c6..d5cdea3db 100644 --- a/pkg/api/errors.go +++ b/pkg/api/errors.go @@ -185,7 +185,7 @@ var errorCodeResponse = map[int]Error{ } // errorCodeError provides errorCode to Error. It returns empty if the code provided is unknown -func errorCodeError(code int) Error { +func getErrorCode(code int) Error { return errorCodeResponse[code] } diff --git a/pkg/api/headers.go b/pkg/api/headers.go index 085c8f78e..c064541cb 100644 --- a/pkg/api/headers.go +++ b/pkg/api/headers.go @@ -35,22 +35,22 @@ type encoder interface { //// helpers // Write http common headers -func writeCommonHeaders(w http.ResponseWriter, acceptsType string) { +func setCommonHeaders(w http.ResponseWriter, acceptsType string) { w.Header().Set("Server", "Minio") w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Content-Type", acceptsType) + w.Header().Set("Connection", "close") } // Write error response headers -func writeErrorResponse(w http.ResponseWriter, response interface{}, acceptsType contentType) []byte { +func encodeErrorResponse(response interface{}, acceptsType contentType) []byte { var bytesBuffer bytes.Buffer var encoder encoder // write common headers - writeCommonHeaders(w, getContentString(acceptsType)) switch acceptsType { - case xmlType: + case xmlContentType: encoder = xml.NewEncoder(&bytesBuffer) - case jsonType: + case jsonContentType: encoder = json.NewEncoder(&bytesBuffer) } encoder.Encode(response) @@ -61,7 +61,7 @@ func writeErrorResponse(w http.ResponseWriter, response interface{}, acceptsType func writeObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata) { lastModified := metadata.Created.Format(time.RFC1123) // common headers - writeCommonHeaders(w, metadata.ContentType) + setCommonHeaders(w, metadata.ContentType) w.Header().Set("ETag", metadata.Md5) w.Header().Set("Last-Modified", lastModified) w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10)) @@ -72,27 +72,22 @@ func writeObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata) func writeRangeObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata, ra string) { lastModified := metadata.Created.Format(time.RFC1123) // common headers - writeCommonHeaders(w, metadata.ContentType) + setCommonHeaders(w, metadata.ContentType) w.Header().Set("ETag", metadata.Md5) w.Header().Set("Last-Modified", lastModified) w.Header().Set("Content-Range", ra) w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10)) } -// Write object header and response -func writeObjectHeadersAndResponse(w http.ResponseWriter, response interface{}, acceptsType contentType) []byte { - var bytesBuffer bytes.Buffer +func encodeResponse(response interface{}, acceptsType contentType) []byte { var encoder encoder - // common headers - writeCommonHeaders(w, getContentString(acceptsType)) + var bytesBuffer bytes.Buffer switch acceptsType { - case xmlType: + case xmlContentType: encoder = xml.NewEncoder(&bytesBuffer) - case jsonType: + case jsonContentType: encoder = json.NewEncoder(&bytesBuffer) } - - w.Header().Set("Connection", "close") encoder.Encode(response) return bytesBuffer.Bytes() } diff --git a/pkg/utils/log/definitions.go b/pkg/utils/log/definitions.go index a2ec93882..48d8c631b 100644 --- a/pkg/utils/log/definitions.go +++ b/pkg/utils/log/definitions.go @@ -60,6 +60,9 @@ var std = New(os.Stderr, "", LstdFlags) // Error is an error logger var Error = New(os.Stderr, "", 0) +// Trace is an error logger +var Trace = New(os.Stderr, "", 0) + // Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. // Knows the buffer has capacity. func itoa(buf *[]byte, i int, wid int) {