2015-02-15 03:48:15 -05:00
|
|
|
/*
|
2015-07-24 20:51:40 -04:00
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
2015-02-15 03:48:15 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-09-19 03:52:01 -04:00
|
|
|
package main
|
2015-02-11 06:23:15 -05:00
|
|
|
|
|
|
|
import (
|
2015-04-27 06:54:49 -04:00
|
|
|
"errors"
|
2015-02-11 06:23:15 -05:00
|
|
|
"net/http"
|
2015-10-07 23:36:36 -04:00
|
|
|
"strings"
|
2015-04-27 06:54:49 -04:00
|
|
|
"time"
|
2015-02-11 06:23:15 -05:00
|
|
|
|
2015-10-08 22:56:41 -04:00
|
|
|
router "github.com/gorilla/mux"
|
2015-08-31 17:40:12 -04:00
|
|
|
"github.com/rs/cors"
|
2015-02-11 06:23:15 -05:00
|
|
|
)
|
|
|
|
|
2016-02-15 20:42:39 -05:00
|
|
|
const (
|
|
|
|
iso8601Format = "20060102T150405Z"
|
2016-02-16 21:50:36 -05:00
|
|
|
privateBucket = "/minio"
|
2016-02-15 20:42:39 -05:00
|
|
|
)
|
|
|
|
|
2016-02-04 17:57:20 -05:00
|
|
|
// HandlerFunc - useful to chain different middleware http.Handler
|
|
|
|
type HandlerFunc func(http.Handler) http.Handler
|
2015-09-15 19:59:43 -04:00
|
|
|
|
2016-02-04 17:57:20 -05:00
|
|
|
func registerHandlers(mux *router.Router, handlerFns ...HandlerFunc) http.Handler {
|
2015-10-08 22:56:41 -04:00
|
|
|
var f http.Handler
|
|
|
|
f = mux
|
2016-02-04 17:57:20 -05:00
|
|
|
for _, hFn := range handlerFns {
|
|
|
|
f = hFn(f)
|
2015-10-08 22:56:41 -04:00
|
|
|
}
|
|
|
|
return f
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
|
|
|
|
2016-02-16 21:50:36 -05:00
|
|
|
// Attempts to parse date string into known date layouts. Date layouts
|
|
|
|
// currently supported are ``time.RFC1123``, ``time.RFC1123Z`` and
|
|
|
|
// special ``iso8601Format``.
|
|
|
|
func parseKnownLayouts(date string) (time.Time, error) {
|
|
|
|
parsedTime, e := time.Parse(time.RFC1123, date)
|
|
|
|
if e == nil {
|
|
|
|
return parsedTime, nil
|
|
|
|
}
|
|
|
|
parsedTime, e = time.Parse(time.RFC1123Z, date)
|
|
|
|
if e == nil {
|
|
|
|
return parsedTime, nil
|
|
|
|
}
|
|
|
|
parsedTime, e = time.Parse(iso8601Format, date)
|
|
|
|
if e == nil {
|
|
|
|
return parsedTime, nil
|
|
|
|
}
|
|
|
|
return time.Time{}, e
|
2015-12-09 18:38:40 -05:00
|
|
|
}
|
|
|
|
|
2016-02-16 21:50:36 -05:00
|
|
|
// Parse date string from incoming header, current supports and verifies
|
|
|
|
// follow HTTP headers.
|
|
|
|
//
|
|
|
|
// - X-Amz-Date
|
|
|
|
// - X-Minio-Date
|
|
|
|
// - Date
|
|
|
|
//
|
|
|
|
// In following time layouts ``time.RFC1123``, ``time.RFC1123Z`` and ``iso8601Format``.
|
|
|
|
func parseDateHeader(req *http.Request) (time.Time, error) {
|
2015-07-10 20:21:53 -04:00
|
|
|
amzDate := req.Header.Get(http.CanonicalHeaderKey("x-amz-date"))
|
2016-02-16 21:50:36 -05:00
|
|
|
if amzDate != "" {
|
|
|
|
return parseKnownLayouts(amzDate)
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
2015-10-08 22:56:41 -04:00
|
|
|
minioDate := req.Header.Get(http.CanonicalHeaderKey("x-minio-date"))
|
2016-02-16 21:50:36 -05:00
|
|
|
if minioDate != "" {
|
|
|
|
return parseKnownLayouts(minioDate)
|
2015-10-08 22:56:41 -04:00
|
|
|
}
|
2016-02-16 21:50:36 -05:00
|
|
|
genericDate := req.Header.Get("Date")
|
|
|
|
if genericDate != "" {
|
|
|
|
return parseKnownLayouts(genericDate)
|
|
|
|
}
|
|
|
|
return time.Time{}, errors.New("Date header missing, invalid request.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds redirect rules for incoming requests.
|
|
|
|
type redirectHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
locationPrefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func setBrowserRedirectHandler(h http.Handler) http.Handler {
|
|
|
|
return redirectHandler{handler: h, locationPrefix: privateBucket}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Re-direction handled specifically for browsers.
|
|
|
|
if strings.Contains(r.Header.Get("User-Agent"), "Mozilla") {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/":
|
|
|
|
// This could be the default route for browser, redirect
|
|
|
|
// to 'locationPrefix/'.
|
|
|
|
fallthrough
|
|
|
|
case "/rpc":
|
|
|
|
// This is '/rpc' API route for browser, redirect to
|
|
|
|
// 'locationPrefix/rpc'.
|
|
|
|
fallthrough
|
|
|
|
case "/login":
|
|
|
|
// This is '/login' route for browser, redirect to
|
|
|
|
// 'locationPrefix/login'.
|
|
|
|
location := h.locationPrefix + r.URL.Path
|
|
|
|
// Redirect to new location.
|
|
|
|
http.Redirect(w, r, location, http.StatusTemporaryRedirect)
|
|
|
|
return
|
2015-04-30 19:29:03 -04:00
|
|
|
}
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
2016-02-16 21:50:36 -05:00
|
|
|
h.handler.ServeHTTP(w, r)
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:57:56 -05:00
|
|
|
// Adds Cache-Control header
|
|
|
|
type cacheControlHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:50:36 -05:00
|
|
|
func setBrowserCacheControlHandler(h http.Handler) http.Handler {
|
2016-02-04 16:57:56 -05:00
|
|
|
return cacheControlHandler{h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h cacheControlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2016-02-16 21:50:36 -05:00
|
|
|
if r.Method == "GET" && strings.Contains(r.Header.Get("User-Agent"), "Mozilla") {
|
|
|
|
// Expire cache in one hour for all browser requests.
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
}
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds verification for incoming paths.
|
|
|
|
type minioPrivateBucketHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
privateBucket string
|
|
|
|
}
|
|
|
|
|
|
|
|
func setPrivateBucketHandler(h http.Handler) http.Handler {
|
|
|
|
return minioPrivateBucketHandler{handler: h, privateBucket: privateBucket}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h minioPrivateBucketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// For all non browser requests, reject access to 'privateBucket'.
|
|
|
|
if !strings.Contains(r.Header.Get("User-Agent"), "Mozilla") && strings.HasPrefix(r.URL.Path, privateBucket) {
|
|
|
|
writeErrorResponse(w, r, AllAccessDisabled, r.URL.Path)
|
|
|
|
return
|
2016-02-04 16:57:56 -05:00
|
|
|
}
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:50:36 -05:00
|
|
|
type timeHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2016-02-04 17:57:20 -05:00
|
|
|
// setTimeValidityHandler to validate parsable time over http header
|
|
|
|
func setTimeValidityHandler(h http.Handler) http.Handler {
|
2015-05-13 15:19:41 -04:00
|
|
|
return timeHandler{h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-04-27 06:54:49 -04:00
|
|
|
// Verify if date headers are set, if not reject the request
|
2015-04-29 21:30:17 -04:00
|
|
|
if r.Header.Get("Authorization") != "" {
|
2016-02-16 21:50:36 -05:00
|
|
|
date, e := parseDateHeader(r)
|
|
|
|
if e != nil {
|
|
|
|
// All our internal APIs are sensitive towards Date
|
|
|
|
// header, for all requests where Date header is not
|
|
|
|
// present we will reject such clients.
|
2015-10-04 03:27:49 -04:00
|
|
|
writeErrorResponse(w, r, RequestTimeTooSkewed, r.URL.Path)
|
2015-04-29 21:30:17 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
duration := time.Since(date)
|
|
|
|
minutes := time.Duration(5) * time.Minute
|
2016-02-16 21:50:36 -05:00
|
|
|
// Verify if the request date header is more than 5minutes
|
|
|
|
// late, reject such clients.
|
2015-04-29 21:30:17 -04:00
|
|
|
if duration.Minutes() > minutes.Minutes() {
|
2015-10-04 03:27:49 -04:00
|
|
|
writeErrorResponse(w, r, RequestTimeTooSkewed, r.URL.Path)
|
2015-04-29 21:30:17 -04:00
|
|
|
return
|
|
|
|
}
|
2015-04-27 06:54:49 -04:00
|
|
|
}
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:50:36 -05:00
|
|
|
type resourceHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2016-02-04 17:57:20 -05:00
|
|
|
// setCorsHandler handler for CORS (Cross Origin Resource Sharing)
|
|
|
|
func setCorsHandler(h http.Handler) http.Handler {
|
2015-10-30 14:49:08 -04:00
|
|
|
c := cors.New(cors.Options{
|
|
|
|
AllowedOrigins: []string{"*"},
|
2016-01-28 12:23:05 -05:00
|
|
|
AllowedMethods: []string{"GET", "HEAD", "POST", "PUT"},
|
2015-10-30 14:49:08 -04:00
|
|
|
AllowedHeaders: []string{"*"},
|
|
|
|
})
|
|
|
|
return c.Handler(h)
|
2015-08-31 17:40:12 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 17:57:20 -05:00
|
|
|
// setIgnoreResourcesHandler -
|
2015-02-23 19:46:48 -05:00
|
|
|
// 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
|
2016-02-16 21:50:36 -05:00
|
|
|
// valid error message indicating that requested feature is not implemented.
|
2016-02-04 17:57:20 -05:00
|
|
|
func setIgnoreResourcesHandler(h http.Handler) http.Handler {
|
2015-04-27 06:54:49 -04:00
|
|
|
return resourceHandler{h}
|
2015-02-18 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
2015-03-03 05:36:12 -05:00
|
|
|
// Resource handler ServeHTTP() wrapper
|
2015-04-27 06:54:49 -04:00
|
|
|
func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2016-02-10 00:38:50 -05:00
|
|
|
// Skip the first element which is usually '/' and split the rest.
|
2015-12-07 14:57:33 -05:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
// If bucketName is present and not objectName check for bucket
|
|
|
|
// level resource queries.
|
|
|
|
if bucketName != "" && objectName == "" {
|
2015-10-07 23:36:36 -04:00
|
|
|
if ignoreNotImplementedBucketResources(r) {
|
|
|
|
writeErrorResponse(w, r, NotImplemented, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2015-12-07 14:57:33 -05:00
|
|
|
}
|
|
|
|
// If bucketName and objectName are present check for its resource queries.
|
|
|
|
if bucketName != "" && objectName != "" {
|
2015-10-07 23:36:36 -04:00
|
|
|
if ignoreNotImplementedObjectResources(r) {
|
|
|
|
writeErrorResponse(w, r, NotImplemented, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2015-02-18 15:15:33 -05:00
|
|
|
}
|
2015-12-07 14:57:33 -05:00
|
|
|
// A put method on path "/" doesn't make sense, ignore it.
|
|
|
|
if r.Method == "PUT" && r.URL.Path == "/" {
|
|
|
|
writeErrorResponse(w, r, NotImplemented, r.URL.Path)
|
|
|
|
return
|
|
|
|
}
|
2015-04-27 06:54:49 -04:00
|
|
|
h.handler.ServeHTTP(w, r)
|
2015-02-11 06:23:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//// helpers
|
|
|
|
|
2015-04-29 18:28:04 -04:00
|
|
|
// Checks requests for not implemented Bucket resources
|
|
|
|
func ignoreNotImplementedBucketResources(req *http.Request) bool {
|
2015-02-11 06:23:15 -05:00
|
|
|
q := req.URL.Query()
|
|
|
|
for name := range q {
|
2015-04-29 18:28:04 -04:00
|
|
|
if notimplementedBucketResourceNames[name] {
|
2015-02-11 06:23:15 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:28:04 -04:00
|
|
|
// Checks requests for not implemented Object resources
|
|
|
|
func ignoreNotImplementedObjectResources(req *http.Request) bool {
|
2015-02-11 06:23:15 -05:00
|
|
|
q := req.URL.Query()
|
|
|
|
for name := range q {
|
2015-04-29 18:28:04 -04:00
|
|
|
if notimplementedObjectResourceNames[name] {
|
2015-02-11 06:23:15 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|