mirror of
https://github.com/minio/minio.git
synced 2024-12-24 22:25:54 -05:00
Merge pull request #406 from fkautz/pr_out_simplfying_code_in_api_round_1_decoupling_functions_which_both_write_headers_and_data
This commit is contained in:
commit
c728798137
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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]
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user