2016-03-27 15:37:21 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2016 MinIO, Inc.
|
2016-03-27 15:37:21 -04: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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-03-27 15:37:21 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-08 14:26:43 -05:00
|
|
|
"io/fs"
|
2016-03-27 15:37:21 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/handlers"
|
2018-04-21 22:23:54 -04:00
|
|
|
"github.com/gorilla/mux"
|
2021-03-08 14:26:43 -05:00
|
|
|
|
2017-01-23 21:07:22 -05:00
|
|
|
"github.com/minio/minio/browser"
|
2020-11-21 01:52:17 -05:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
|
|
|
jsonrpc "github.com/minio/minio/pkg/rpc"
|
|
|
|
"github.com/minio/minio/pkg/rpc/json2"
|
2016-03-27 15:37:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// webAPI container for Web API.
|
2016-04-12 15:45:15 -04:00
|
|
|
type webAPIHandlers struct {
|
2016-07-31 17:11:14 -04:00
|
|
|
ObjectAPI func() ObjectLayer
|
2018-03-28 17:14:06 -04:00
|
|
|
CacheAPI func() CacheObjectLayer
|
2016-03-27 15:37:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// indexHandler - Handler to serve index.html
|
|
|
|
type indexHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-08-06 15:08:58 -04:00
|
|
|
r.URL.Path = minioReservedBucketPath + SlashSeparator
|
2016-03-27 15:37:21 -04:00
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
const assetPrefix = "production"
|
|
|
|
|
|
|
|
// specialAssets are files which are unique files not embedded inside index_bundle.js.
|
2019-08-12 01:17:02 -04:00
|
|
|
const specialAssets = "index_bundle.*.js|loader.css|logo.svg|firefox.png|safari.png|chrome.png|favicon-16x16.png|favicon-32x32.png|favicon-96x96.png"
|
2016-03-27 15:37:21 -04:00
|
|
|
|
|
|
|
// registerWebRouter - registers web router for serving minio browser.
|
2018-04-21 22:23:54 -04:00
|
|
|
func registerWebRouter(router *mux.Router) error {
|
2016-10-05 15:48:07 -04:00
|
|
|
// Initialize Web.
|
|
|
|
web := &webAPIHandlers{
|
2019-11-09 12:27:23 -05:00
|
|
|
ObjectAPI: newObjectLayerFn,
|
|
|
|
CacheAPI: newCachedObjectLayerFn,
|
2016-10-05 15:48:07 -04:00
|
|
|
}
|
|
|
|
|
2016-03-27 15:37:21 -04:00
|
|
|
// Initialize a new json2 codec.
|
|
|
|
codec := json2.NewCodec()
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// MinIO browser router.
|
2019-08-12 20:05:30 -04:00
|
|
|
webBrowserRouter := router.PathPrefix(minioReservedBucketPath).HeadersRegexp("User-Agent", ".*Mozilla.*").Subrouter()
|
2016-03-27 15:37:21 -04:00
|
|
|
|
|
|
|
// Initialize json rpc handlers.
|
|
|
|
webRPC := jsonrpc.NewServer()
|
|
|
|
webRPC.RegisterCodec(codec, "application/json")
|
|
|
|
webRPC.RegisterCodec(codec, "application/json; charset=UTF-8")
|
2020-11-21 01:52:17 -05:00
|
|
|
webRPC.RegisterAfterFunc(func(ri *jsonrpc.RequestInfo) {
|
|
|
|
if ri != nil {
|
|
|
|
claims, _, _ := webRequestAuthenticate(ri.Request)
|
|
|
|
bucketName, objectName := extractBucketObject(ri.Args)
|
|
|
|
ri.Request = mux.SetURLVars(ri.Request, map[string]string{
|
|
|
|
"bucket": bucketName,
|
|
|
|
"object": objectName,
|
|
|
|
})
|
2021-01-04 12:40:30 -05:00
|
|
|
if globalHTTPTrace.NumSubscribers() > 0 {
|
2020-11-21 01:52:17 -05:00
|
|
|
globalHTTPTrace.Publish(WebTrace(ri))
|
|
|
|
}
|
2021-01-26 16:21:51 -05:00
|
|
|
ctx := newContext(ri.Request, ri.ResponseWriter, ri.Method)
|
|
|
|
logger.AuditLog(ctx, ri.ResponseWriter, ri.Request, claims.Map())
|
2020-11-21 01:52:17 -05:00
|
|
|
}
|
|
|
|
})
|
2016-10-13 20:34:10 -04:00
|
|
|
|
|
|
|
// Register RPC handlers with server
|
2020-11-21 01:52:17 -05:00
|
|
|
if err := webRPC.RegisterService(web, "web"); err != nil {
|
2016-10-13 20:34:10 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-03-27 15:37:21 -04:00
|
|
|
|
2016-04-08 02:44:08 -04:00
|
|
|
// RPC handler at URI - /minio/webrpc
|
2020-07-20 15:52:49 -04:00
|
|
|
webBrowserRouter.Methods(http.MethodPost).Path("/webrpc").Handler(webRPC)
|
|
|
|
webBrowserRouter.Methods(http.MethodPut).Path("/upload/{bucket}/{object:.+}").HandlerFunc(httpTraceHdrs(web.Upload))
|
2017-07-24 15:46:37 -04:00
|
|
|
|
|
|
|
// These methods use short-expiry tokens in the URLs. These tokens may unintentionally
|
|
|
|
// be logged, so a new one must be generated for each request.
|
2020-07-20 15:52:49 -04:00
|
|
|
webBrowserRouter.Methods(http.MethodGet).Path("/download/{bucket}/{object:.+}").Queries("token", "{token:.*}").HandlerFunc(httpTraceHdrs(web.Download))
|
|
|
|
webBrowserRouter.Methods(http.MethodPost).Path("/zip").Queries("token", "{token:.*}").HandlerFunc(httpTraceHdrs(web.DownloadZip))
|
2016-03-27 15:37:21 -04:00
|
|
|
|
2019-01-22 13:58:28 -05:00
|
|
|
// Create compressed assets handler
|
2021-03-08 14:26:43 -05:00
|
|
|
assetFS, err := fs.Sub(browser.GetStaticAssets(), assetPrefix)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
compressAssets := handlers.CompressHandler(http.StripPrefix(minioReservedBucketPath, http.FileServer(http.FS(assetFS))))
|
2016-03-27 15:37:21 -04:00
|
|
|
|
|
|
|
// Serve javascript files and favicon from assets.
|
2019-01-22 13:58:28 -05:00
|
|
|
webBrowserRouter.Path(fmt.Sprintf("/{assets:%s}", specialAssets)).Handler(compressAssets)
|
2016-03-27 15:37:21 -04:00
|
|
|
|
2019-01-22 13:58:28 -05:00
|
|
|
// Serve index.html from assets for rest of the requests.
|
|
|
|
webBrowserRouter.Path("/{index:.*}").Handler(indexHandler{compressAssets})
|
2016-10-13 20:34:10 -04:00
|
|
|
|
|
|
|
return nil
|
2016-03-27 15:37:21 -04:00
|
|
|
}
|