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.
315 lines
9.0 KiB
315 lines
9.0 KiB
10 years ago
|
/*
|
||
10 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
10 years ago
|
|
||
|
import (
|
||
|
"net/http"
|
||
9 years ago
|
"path"
|
||
9 years ago
|
"regexp"
|
||
9 years ago
|
"strings"
|
||
10 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
|
// HandlerFunc - useful to chain different middleware http.Handler
|
||
|
type HandlerFunc func(http.Handler) http.Handler
|
||
9 years ago
|
|
||
9 years ago
|
func registerHandlers(mux *router.Router, handlerFns ...HandlerFunc) http.Handler {
|
||
9 years ago
|
var f http.Handler
|
||
|
f = mux
|
||
9 years ago
|
for _, hFn := range handlerFns {
|
||
|
f = hFn(f)
|
||
9 years ago
|
}
|
||
|
return f
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// Adds limiting body size middleware
|
||
|
|
||
|
// Set the body size limit to 6 Gb = Maximum object size + other possible data
|
||
|
// in the same request
|
||
|
const requestMaxBodySize = 1024 * 1024 * 1024 * (5 + 1)
|
||
|
|
||
|
type requestSizeLimitHandler struct {
|
||
|
handler http.Handler
|
||
|
maxBodySize int64
|
||
|
}
|
||
|
|
||
|
func setRequestSizeLimitHandler(h http.Handler) http.Handler {
|
||
|
return requestSizeLimitHandler{handler: h, maxBodySize: requestMaxBodySize}
|
||
|
}
|
||
|
|
||
|
func (h requestSizeLimitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
|
// Restricting read data to a given maximum length
|
||
|
r.Body = http.MaxBytesReader(w, r.Body, h.maxBodySize)
|
||
|
h.handler.ServeHTTP(w, r)
|
||
|
}
|
||
|
|
||
9 years ago
|
// Adds redirect rules for incoming requests.
|
||
|
type redirectHandler struct {
|
||
|
handler http.Handler
|
||
|
locationPrefix string
|
||
|
}
|
||
|
|
||
9 years ago
|
// Reserved bucket.
|
||
9 years ago
|
const (
|
||
9 years ago
|
reservedBucket = "/minio"
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
func setBrowserRedirectHandler(h http.Handler) http.Handler {
|
||
9 years ago
|
return redirectHandler{handler: h, locationPrefix: reservedBucket}
|
||
9 years ago
|
}
|
||
|
|
||
|
func (h redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
|
// Re-direction handled specifically for browsers.
|
||
8 years ago
|
if strings.Contains(r.Header.Get("User-Agent"), "Mozilla") && !isRequestSignatureV4(r) {
|
||
9 years ago
|
// '/' is redirected to 'locationPrefix/'
|
||
9 years ago
|
// '/webrpc' is redirected to 'locationPrefix/webrpc'
|
||
9 years ago
|
// '/login' is redirected to 'locationPrefix/login'
|
||
9 years ago
|
switch r.URL.Path {
|
||
9 years ago
|
case "/", "/webrpc", "/login", "/favicon.ico":
|
||
9 years ago
|
location := h.locationPrefix + r.URL.Path
|
||
9 years ago
|
// Redirect to new location.
|
||
|
http.Redirect(w, r, location, http.StatusTemporaryRedirect)
|
||
|
return
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
h.handler.ServeHTTP(w, r)
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// Adds Cache-Control header
|
||
|
type cacheControlHandler struct {
|
||
|
handler http.Handler
|
||
|
}
|
||
|
|
||
9 years ago
|
func setBrowserCacheControlHandler(h http.Handler) http.Handler {
|
||
9 years ago
|
return cacheControlHandler{h}
|
||
|
}
|
||
|
|
||
|
func (h cacheControlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
9 years ago
|
if r.Method == "GET" && strings.Contains(r.Header.Get("User-Agent"), "Mozilla") {
|
||
9 years ago
|
// For all browser requests set appropriate Cache-Control policies
|
||
9 years ago
|
match, e := regexp.MatchString(reservedBucket+`/([^/]+\.js|favicon.ico)`, r.URL.Path)
|
||
9 years ago
|
if e != nil {
|
||
9 years ago
|
writeErrorResponse(w, r, ErrInternalError, r.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
if match {
|
||
|
// For assets set cache expiry of one year. For each release, the name
|
||
|
// of the asset name will change and hence it can not be served from cache.
|
||
|
w.Header().Set("Cache-Control", "max-age=31536000")
|
||
9 years ago
|
} else if strings.HasPrefix(r.URL.Path, reservedBucket+"/") {
|
||
9 years ago
|
// For non asset requests we serve index.html which will never be cached.
|
||
|
w.Header().Set("Cache-Control", "no-store")
|
||
|
}
|
||
9 years ago
|
}
|
||
|
h.handler.ServeHTTP(w, r)
|
||
|
}
|
||
|
|
||
|
// Adds verification for incoming paths.
|
||
|
type minioPrivateBucketHandler struct {
|
||
9 years ago
|
handler http.Handler
|
||
|
reservedBucket string
|
||
9 years ago
|
}
|
||
|
|
||
|
func setPrivateBucketHandler(h http.Handler) http.Handler {
|
||
9 years ago
|
return minioPrivateBucketHandler{handler: h, reservedBucket: reservedBucket}
|
||
9 years ago
|
}
|
||
|
|
||
|
func (h minioPrivateBucketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
9 years ago
|
// For all non browser requests, reject access to 'reservedBucket'.
|
||
|
if !strings.Contains(r.Header.Get("User-Agent"), "Mozilla") && path.Clean(r.URL.Path) == reservedBucket {
|
||
9 years ago
|
writeErrorResponse(w, r, ErrAllAccessDisabled, r.URL.Path)
|
||
9 years ago
|
return
|
||
9 years ago
|
}
|
||
|
h.handler.ServeHTTP(w, r)
|
||
|
}
|
||
|
|
||
9 years ago
|
// Supported Amz date formats.
|
||
|
var amzDateFormats = []string{
|
||
9 years ago
|
time.RFC1123,
|
||
|
time.RFC1123Z,
|
||
|
iso8601Format,
|
||
9 years ago
|
// Add new AMZ date formats here.
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// parseAmzDate - parses date string into supported amz date formats.
|
||
|
func parseAmzDate(amzDateStr string) (amzDate time.Time, apiErr APIErrorCode) {
|
||
|
for _, dateFormat := range amzDateFormats {
|
||
|
amzDate, e := time.Parse(dateFormat, amzDateStr)
|
||
9 years ago
|
if e == nil {
|
||
9 years ago
|
return amzDate, ErrNone
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
return time.Time{}, ErrMalformedDate
|
||
|
}
|
||
|
|
||
|
// Supported Amz date headers.
|
||
|
var amzDateHeaders = []string{
|
||
9 years ago
|
"x-amz-date",
|
||
|
"date",
|
||
|
}
|
||
|
|
||
9 years ago
|
// parseAmzDateHeader - parses supported amz date headers, in
|
||
|
// supported amz date formats.
|
||
|
func parseAmzDateHeader(req *http.Request) (time.Time, APIErrorCode) {
|
||
|
for _, amzDateHeader := range amzDateHeaders {
|
||
|
amzDateStr := req.Header.Get(http.CanonicalHeaderKey(amzDateHeader))
|
||
|
if amzDateStr != "" {
|
||
|
return parseAmzDate(amzDateStr)
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
// Date header missing.
|
||
|
return time.Time{}, ErrMissingDateHeader
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
type timeHandler struct {
|
||
|
handler http.Handler
|
||
|
}
|
||
|
|
||
9 years ago
|
// setTimeValidityHandler to validate parsable time over http header
|
||
|
func setTimeValidityHandler(h http.Handler) http.Handler {
|
||
10 years ago
|
return timeHandler{h}
|
||
|
}
|
||
|
|
||
|
func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
10 years ago
|
// Verify if date headers are set, if not reject the request
|
||
9 years ago
|
if _, ok := r.Header["Authorization"]; ok {
|
||
9 years ago
|
amzDate, apiErr := parseAmzDateHeader(r)
|
||
|
if apiErr != ErrNone {
|
||
9 years ago
|
// All our internal APIs are sensitive towards Date
|
||
|
// header, for all requests where Date header is not
|
||
|
// present we will reject such clients.
|
||
9 years ago
|
writeErrorResponse(w, r, apiErr, r.URL.Path)
|
||
10 years ago
|
return
|
||
|
}
|
||
9 years ago
|
// Verify if the request date header is more than 5minutes
|
||
|
// late, reject such clients.
|
||
9 years ago
|
if time.Now().UTC().Sub(amzDate)/time.Minute > time.Duration(5)*time.Minute {
|
||
9 years ago
|
writeErrorResponse(w, r, ErrRequestTimeTooSkewed, r.URL.Path)
|
||
10 years ago
|
return
|
||
|
}
|
||
10 years ago
|
}
|
||
|
h.handler.ServeHTTP(w, r)
|
||
|
}
|
||
|
|
||
9 years ago
|
type resourceHandler struct {
|
||
|
handler http.Handler
|
||
|
}
|
||
|
|
||
9 years ago
|
// setCorsHandler handler for CORS (Cross Origin Resource Sharing)
|
||
|
func setCorsHandler(h http.Handler) http.Handler {
|
||
9 years ago
|
c := cors.New(cors.Options{
|
||
|
AllowedOrigins: []string{"*"},
|
||
9 years ago
|
AllowedMethods: []string{"GET", "HEAD", "POST", "PUT"},
|
||
9 years ago
|
AllowedHeaders: []string{"*"},
|
||
9 years ago
|
ExposedHeaders: []string{"ETag"},
|
||
9 years ago
|
})
|
||
|
return c.Handler(h)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// setIgnoreResourcesHandler -
|
||
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 that requested feature is not implemented.
|
||
9 years ago
|
func setIgnoreResourcesHandler(h http.Handler) http.Handler {
|
||
10 years ago
|
return resourceHandler{h}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// Resource handler ServeHTTP() wrapper
|
||
10 years ago
|
func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
9 years ago
|
// Skip the first element which is usually '/' and split the rest.
|
||
9 years ago
|
splits := strings.SplitN(r.URL.Path[1:], "/", 2)
|
||
|
|
||
|
// Save bucketName and objectName extracted from url Path.
|
||
|
var bucketName, objectName string
|
||
|
if len(splits) == 1 {
|
||
|
bucketName = splits[0]
|
||
|
}
|
||
|
if len(splits) == 2 {
|
||
|
bucketName = splits[0]
|
||
|
objectName = splits[1]
|
||
|
}
|
||
8 years ago
|
|
||
|
// If bucketName is present and not objectName check for bucket level resource queries.
|
||
9 years ago
|
if bucketName != "" && objectName == "" {
|
||
9 years ago
|
if ignoreNotImplementedBucketResources(r) {
|
||
9 years ago
|
writeErrorResponse(w, r, ErrNotImplemented, r.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
}
|
||
|
// If bucketName and objectName are present check for its resource queries.
|
||
|
if bucketName != "" && objectName != "" {
|
||
9 years ago
|
if ignoreNotImplementedObjectResources(r) {
|
||
9 years ago
|
writeErrorResponse(w, r, ErrNotImplemented, r.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
// A put method on path "/" doesn't make sense, ignore it.
|
||
|
if r.Method == "PUT" && r.URL.Path == "/" {
|
||
9 years ago
|
writeErrorResponse(w, r, ErrNotImplemented, r.URL.Path)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
|
||
|
// Serve HTTP.
|
||
10 years ago
|
h.handler.ServeHTTP(w, r)
|
||
10 years ago
|
}
|
||
|
|
||
|
//// helpers
|
||
|
|
||
10 years ago
|
// Checks requests for not implemented Bucket resources
|
||
|
func ignoreNotImplementedBucketResources(req *http.Request) bool {
|
||
9 years ago
|
for name := range req.URL.Query() {
|
||
10 years ago
|
if notimplementedBucketResourceNames[name] {
|
||
10 years ago
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
10 years ago
|
// Checks requests for not implemented Object resources
|
||
|
func ignoreNotImplementedObjectResources(req *http.Request) bool {
|
||
9 years ago
|
for name := range req.URL.Query() {
|
||
10 years ago
|
if notimplementedObjectResourceNames[name] {
|
||
10 years ago
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
9 years ago
|
|
||
|
// List of not implemented bucket queries
|
||
|
var notimplementedBucketResourceNames = map[string]bool{
|
||
9 years ago
|
"acl": true,
|
||
9 years ago
|
"cors": true,
|
||
|
"lifecycle": true,
|
||
|
"logging": true,
|
||
|
"replication": true,
|
||
|
"tagging": true,
|
||
|
"versions": true,
|
||
|
"requestPayment": true,
|
||
|
"versioning": true,
|
||
|
"website": true,
|
||
|
}
|
||
|
|
||
|
// List of not implemented object queries
|
||
|
var notimplementedObjectResourceNames = map[string]bool{
|
||
|
"torrent": true,
|
||
|
"acl": true,
|
||
|
"policy": true,
|
||
|
}
|