Reducing code duplication relating to content type

master
Frederick F. Kautz IV 10 years ago
parent 96a5fc0ee7
commit c6d537e66e
  1. 41
      pkg/webapi/minioapi/minioapi.go

@ -29,8 +29,10 @@ import (
mstorage "github.com/minio-io/minio/pkg/storage" mstorage "github.com/minio-io/minio/pkg/storage"
) )
type contentType int
const ( const (
xmlType = iota xmlType contentType = iota
jsonType jsonType
) )
@ -74,12 +76,7 @@ func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Requ
prefix = "" prefix = ""
} }
contentType := xmlType acceptsContentType := getContentType(req)
if _, ok := req.Header["Accept"]; ok {
if req.Header["Accept"][0] == "application/json" {
contentType = jsonType
}
}
buckets, err := server.storage.ListBuckets(prefix) buckets, err := server.storage.ListBuckets(prefix)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -87,7 +84,7 @@ func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Requ
return return
} }
response := generateBucketsListResult(buckets) response := generateBucketsListResult(buckets)
w.Write(writeObjectHeadersAndResponse(w, response, contentType)) w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType))
} }
func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Request) { func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Request) {
@ -103,12 +100,7 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ
prefix = "" prefix = ""
} }
contentType := xmlType acceptsContentType := getContentType(req)
if _, ok := req.Header["Accept"]; ok {
if req.Header["Accept"][0] == "application/json" {
contentType = jsonType
}
}
objects, isTruncated, err := server.storage.ListObjects(bucket, prefix, 1000) objects, isTruncated, err := server.storage.ListObjects(bucket, prefix, 1000)
if err != nil { if err != nil {
@ -117,7 +109,7 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ
return return
} }
response := generateObjectsListResult(bucket, objects, isTruncated) response := generateObjectsListResult(bucket, objects, isTruncated)
w.Write(writeObjectHeadersAndResponse(w, response, contentType)) w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType))
} }
func (server *minioApi) putBucketHandler(w http.ResponseWriter, req *http.Request) { func (server *minioApi) putBucketHandler(w http.ResponseWriter, req *http.Request) {
@ -208,13 +200,13 @@ func (server *minioApi) putObjectHandler(w http.ResponseWriter, req *http.Reques
w.Header().Set("Connection", "close") w.Header().Set("Connection", "close")
} }
func writeObjectHeadersAndResponse(w http.ResponseWriter, response interface{}, contentType int) []byte { func writeObjectHeadersAndResponse(w http.ResponseWriter, response interface{}, acceptsType contentType) []byte {
var bytesBuffer bytes.Buffer var bytesBuffer bytes.Buffer
var encoder encoder var encoder encoder
if contentType == xmlType { if acceptsType == xmlType {
w.Header().Set("Content-Type", "application/xml") w.Header().Set("Content-Type", "application/xml")
encoder = xml.NewEncoder(&bytesBuffer) encoder = xml.NewEncoder(&bytesBuffer)
} else if contentType == jsonType { } else if acceptsType == jsonType {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
encoder = json.NewEncoder(&bytesBuffer) encoder = json.NewEncoder(&bytesBuffer)
} }
@ -256,7 +248,9 @@ func generateBucketsListResult(buckets []mstorage.BucketMetadata) BucketListResp
return data return data
} }
//// Unimplemented Resources //// helpers
// Checks requests for unimplemented resources
func (server *minioApi) ignoreUnImplementedBucketResources(req *http.Request) bool { func (server *minioApi) ignoreUnImplementedBucketResources(req *http.Request) bool {
q := req.URL.Query() q := req.URL.Query()
for name := range q { for name := range q {
@ -277,7 +271,14 @@ func (server *minioApi) ignoreUnImplementedObjectResources(req *http.Request) bo
return false return false
} }
//// helpers func getContentType(req *http.Request) contentType {
if _, ok := req.Header["Accept"]; ok {
if req.Header["Accept"][0] == "application/json" {
return jsonType
}
}
return xmlType
}
// takes a set of objects and prepares the objects for serialization // takes a set of objects and prepares the objects for serialization
// input: // input:

Loading…
Cancel
Save