You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.2 KiB
123 lines
3.2 KiB
10 years ago
|
/*
|
||
|
* Mini Object 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.
|
||
|
*/
|
||
|
|
||
10 years ago
|
package minioapi
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/minio-io/minio/pkg/utils/config"
|
||
|
"github.com/minio-io/minio/pkg/utils/crypto/signers"
|
||
|
)
|
||
|
|
||
|
type vHandler struct {
|
||
|
conf config.Config
|
||
|
handler http.Handler
|
||
|
}
|
||
|
|
||
10 years ago
|
type rHandler struct {
|
||
|
handler http.Handler
|
||
|
}
|
||
|
|
||
10 years ago
|
// grab AccessKey from authorization header
|
||
|
func stripAccessKey(r *http.Request) string {
|
||
|
fields := strings.Fields(r.Header.Get("Authorization"))
|
||
|
if len(fields) < 2 {
|
||
|
return ""
|
||
|
}
|
||
|
splits := strings.Split(fields[1], ":")
|
||
|
if len(splits) < 2 {
|
||
|
return ""
|
||
|
}
|
||
|
return splits[0]
|
||
|
}
|
||
|
|
||
|
func validateHandler(conf config.Config, h http.Handler) http.Handler {
|
||
|
return vHandler{conf, h}
|
||
|
}
|
||
|
|
||
|
func (h vHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
|
accessKey := stripAccessKey(r)
|
||
10 years ago
|
acceptsContentType := getContentType(r)
|
||
10 years ago
|
if accessKey != "" {
|
||
|
if err := h.conf.ReadConfig(); err != nil {
|
||
10 years ago
|
error := errorCodeError(InternalError)
|
||
|
errorResponse := getErrorResponse(error, "")
|
||
|
w.WriteHeader(error.HttpStatusCode)
|
||
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
||
10 years ago
|
} else {
|
||
|
user := h.conf.GetKey(accessKey)
|
||
10 years ago
|
ok, _ := signers.ValidateRequest(user, r)
|
||
10 years ago
|
if ok {
|
||
|
h.handler.ServeHTTP(w, r)
|
||
|
} else {
|
||
10 years ago
|
error := errorCodeError(AccessDenied)
|
||
|
errorResponse := getErrorResponse(error, "")
|
||
|
w.WriteHeader(error.HttpStatusCode)
|
||
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
||
10 years ago
|
}
|
||
|
}
|
||
|
} else {
|
||
|
//No access key found, handle this more appropriately
|
||
|
//TODO: Remove this after adding tests to support signature
|
||
|
//request
|
||
|
h.handler.ServeHTTP(w, r)
|
||
|
//Add this line, to reply back for invalid requests
|
||
|
//w.WriteHeader(http.StatusUnauthorized)
|
||
|
//w.Write([]byte("Authorization header malformed")
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
func ignoreResourcesHandler(h http.Handler) http.Handler {
|
||
|
return rHandler{h}
|
||
|
}
|
||
|
|
||
|
func (h rHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
|
acceptsContentType := getContentType(r)
|
||
|
if ignoreUnImplementedObjectResources(r) || ignoreUnImplementedBucketResources(r) {
|
||
|
error := errorCodeError(NotImplemented)
|
||
|
errorResponse := getErrorResponse(error, "")
|
||
|
w.WriteHeader(error.HttpStatusCode)
|
||
|
w.Write(writeErrorResponse(w, errorResponse, acceptsContentType))
|
||
|
} else {
|
||
|
h.handler.ServeHTTP(w, r)
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
//// helpers
|
||
|
|
||
|
// Checks requests for unimplemented resources
|
||
|
func ignoreUnImplementedBucketResources(req *http.Request) bool {
|
||
|
q := req.URL.Query()
|
||
|
for name := range q {
|
||
|
if unimplementedBucketResourceNames[name] {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func ignoreUnImplementedObjectResources(req *http.Request) bool {
|
||
|
q := req.URL.Query()
|
||
|
for name := range q {
|
||
|
if unimplementedObjectResourceNames[name] {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|