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.
177 lines
5.1 KiB
177 lines
5.1 KiB
![]()
10 years ago
|
/*
|
||
![]()
9 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
![]()
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
![]()
9 years ago
|
package main
|
||
![]()
10 years ago
|
|
||
|
import (
|
||
![]()
9 years ago
|
"errors"
|
||
![]()
10 years ago
|
"net/http"
|
||
![]()
9 years ago
|
"strings"
|
||
![]()
9 years ago
|
"time"
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
router "github.com/gorilla/mux"
|
||
![]()
9 years ago
|
"github.com/rs/cors"
|
||
![]()
10 years ago
|
)
|
||
|
|
||
![]()
9 years ago
|
// MiddlewareHandler - useful to chain different middleware http.Handler
|
||
![]()
9 years ago
|
type MiddlewareHandler func(http.Handler) http.Handler
|
||
|
|
||
![]()
9 years ago
|
func registerCustomMiddleware(mux *router.Router, mwHandlers ...MiddlewareHandler) http.Handler {
|
||
|
var f http.Handler
|
||
|
f = mux
|
||
|
for _, mw := range mwHandlers {
|
||
|
f = mw(f)
|
||
|
}
|
||
|
return f
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
type timeHandler struct {
|
||
![]()
10 years ago
|
handler http.Handler
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
type resourceHandler struct {
|
||
![]()
10 years ago
|
handler http.Handler
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
func parseDate(req *http.Request) (time.Time, error) {
|
||
![]()
9 years ago
|
amzDate := req.Header.Get(http.CanonicalHeaderKey("x-amz-date"))
|
||
![]()
9 years ago
|
switch {
|
||
|
case amzDate != "":
|
||
|
if _, err := time.Parse(time.RFC1123, amzDate); err == nil {
|
||
|
return time.Parse(time.RFC1123, amzDate)
|
||
|
}
|
||
|
if _, err := time.Parse(time.RFC1123Z, amzDate); err == nil {
|
||
|
return time.Parse(time.RFC1123Z, amzDate)
|
||
|
}
|
||
![]()
9 years ago
|
if _, err := time.Parse(iso8601Format, amzDate); err == nil {
|
||
|
return time.Parse(iso8601Format, amzDate)
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
minioDate := req.Header.Get(http.CanonicalHeaderKey("x-minio-date"))
|
||
|
switch {
|
||
|
case minioDate != "":
|
||
|
if _, err := time.Parse(time.RFC1123, minioDate); err == nil {
|
||
|
return time.Parse(time.RFC1123, minioDate)
|
||
|
}
|
||
|
if _, err := time.Parse(time.RFC1123Z, minioDate); err == nil {
|
||
|
return time.Parse(time.RFC1123Z, minioDate)
|
||
|
}
|
||
|
if _, err := time.Parse(iso8601Format, minioDate); err == nil {
|
||
|
return time.Parse(iso8601Format, minioDate)
|
||
|
}
|
||
|
}
|
||
![]()
9 years ago
|
date := req.Header.Get("Date")
|
||
|
switch {
|
||
|
case date != "":
|
||
|
if _, err := time.Parse(time.RFC1123, date); err == nil {
|
||
|
return time.Parse(time.RFC1123, date)
|
||
|
}
|
||
|
if _, err := time.Parse(time.RFC1123Z, date); err == nil {
|
||
|
return time.Parse(time.RFC1123Z, date)
|
||
|
}
|
||
![]()
9 years ago
|
if _, err := time.Parse(iso8601Format, amzDate); err == nil {
|
||
|
return time.Parse(iso8601Format, amzDate)
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
}
|
||
|
return time.Time{}, errors.New("invalid request")
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// TimeValidityHandler to validate parsable time over http header
|
||
![]()
9 years ago
|
func TimeValidityHandler(h http.Handler) http.Handler {
|
||
![]()
9 years ago
|
return timeHandler{h}
|
||
|
}
|
||
|
|
||
|
func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
![]()
9 years ago
|
// Verify if date headers are set, if not reject the request
|
||
![]()
9 years ago
|
if r.Header.Get("Authorization") != "" {
|
||
![]()
9 years ago
|
if r.Header.Get(http.CanonicalHeaderKey("x-amz-date")) == "" && r.Header.Get(http.CanonicalHeaderKey("x-minio-date")) == "" && r.Header.Get("Date") == "" {
|
||
![]()
9 years ago
|
// there is no way to knowing if this is a valid request, could be a attack reject such clients
|
||
![]()
9 years ago
|
writeErrorResponse(w, r, RequestTimeTooSkewed, r.URL.Path)
|
||
![]()
9 years ago
|
return
|
||
|
}
|
||
![]()
9 years ago
|
date, err := parseDate(r)
|
||
![]()
9 years ago
|
if err != nil {
|
||
|
// there is no way to knowing if this is a valid request, could be a attack reject such clients
|
||
![]()
9 years ago
|
writeErrorResponse(w, r, RequestTimeTooSkewed, r.URL.Path)
|
||
![]()
9 years ago
|
return
|
||
|
}
|
||
|
duration := time.Since(date)
|
||
|
minutes := time.Duration(5) * time.Minute
|
||
|
if duration.Minutes() > minutes.Minutes() {
|
||
![]()
9 years ago
|
writeErrorResponse(w, r, RequestTimeTooSkewed, r.URL.Path)
|
||
![]()
9 years ago
|
return
|
||
|
}
|
||
![]()
9 years ago
|
}
|
||
|
h.handler.ServeHTTP(w, r)
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// CorsHandler handler for CORS (Cross Origin Resource Sharing)
|
||
![]()
9 years ago
|
func CorsHandler(h http.Handler) http.Handler {
|
||
|
return cors.Default().Handler(h)
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// IgnoreResourcesHandler -
|
||
![]()
10 years ago
|
// Ignore resources handler is wrapper handler used for API request resource validation
|
||
|
// Since we do not support all the S3 queries, it is necessary for us to throw back a
|
||
![]()
9 years ago
|
// valid error message indicating such a feature is not implemented.
|
||
![]()
9 years ago
|
func IgnoreResourcesHandler(h http.Handler) http.Handler {
|
||
![]()
9 years ago
|
return resourceHandler{h}
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
// Resource handler ServeHTTP() wrapper
|
||
![]()
9 years ago
|
func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
![]()
9 years ago
|
splits := strings.SplitN(r.URL.Path, "/", 3)
|
||
|
switch len(splits) {
|
||
|
// bucket exists
|
||
|
case 2:
|
||
|
if ignoreNotImplementedBucketResources(r) {
|
||
|
writeErrorResponse(w, r, NotImplemented, r.URL.Path)
|
||
|
return
|
||
|
}
|
||
|
// object exists
|
||
|
case 3:
|
||
|
if ignoreNotImplementedObjectResources(r) {
|
||
|
writeErrorResponse(w, r, NotImplemented, r.URL.Path)
|
||
|
return
|
||
|
}
|
||
![]()
10 years ago
|
}
|
||
![]()
9 years ago
|
h.handler.ServeHTTP(w, r)
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
//// helpers
|
||
|
|
||
![]()
9 years ago
|
// Checks requests for not implemented Bucket resources
|
||
|
func ignoreNotImplementedBucketResources(req *http.Request) bool {
|
||
![]()
10 years ago
|
q := req.URL.Query()
|
||
|
for name := range q {
|
||
![]()
9 years ago
|
if notimplementedBucketResourceNames[name] {
|
||
![]()
10 years ago
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
// Checks requests for not implemented Object resources
|
||
|
func ignoreNotImplementedObjectResources(req *http.Request) bool {
|
||
![]()
10 years ago
|
q := req.URL.Query()
|
||
|
for name := range q {
|
||
![]()
9 years ago
|
if notimplementedObjectResourceNames[name] {
|
||
![]()
10 years ago
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|