| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2016 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08: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-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 13:59:13 +08:00
										 |  |  | 	assetfs "github.com/elazarl/go-bindata-assetfs" | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 	"github.com/gorilla/handlers" | 
					
						
							| 
									
										
										
										
											2018-04-22 10:23:54 +08:00
										 |  |  | 	"github.com/gorilla/mux" | 
					
						
							| 
									
										
										
										
											2017-01-24 10:07:22 +08:00
										 |  |  | 	"github.com/minio/minio/browser" | 
					
						
							| 
									
										
										
										
											2020-11-21 14:52:17 +08:00
										 |  |  | 	"github.com/minio/minio/cmd/logger" | 
					
						
							|  |  |  | 	jsonrpc "github.com/minio/minio/pkg/rpc" | 
					
						
							|  |  |  | 	"github.com/minio/minio/pkg/rpc/json2" | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // webAPI container for Web API.
 | 
					
						
							| 
									
										
										
										
											2016-04-13 03:45:15 +08:00
										 |  |  | type webAPIHandlers struct { | 
					
						
							| 
									
										
										
										
											2016-08-01 05:11:14 +08:00
										 |  |  | 	ObjectAPI func() ObjectLayer | 
					
						
							| 
									
										
										
										
											2018-03-29 05:14:06 +08:00
										 |  |  | 	CacheAPI  func() CacheObjectLayer | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08: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-07 03:08:58 +08:00
										 |  |  | 	r.URL.Path = minioReservedBucketPath + SlashSeparator | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 	h.handler.ServeHTTP(w, r) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const assetPrefix = "production" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func assetFS() *assetfs.AssetFS { | 
					
						
							|  |  |  | 	return &assetfs.AssetFS{ | 
					
						
							| 
									
										
										
										
											2017-02-23 09:27:26 +08:00
										 |  |  | 		Asset:     browser.Asset, | 
					
						
							|  |  |  | 		AssetDir:  browser.AssetDir, | 
					
						
							|  |  |  | 		AssetInfo: browser.AssetInfo, | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 		Prefix:    assetPrefix, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // specialAssets are files which are unique files not embedded inside index_bundle.js.
 | 
					
						
							| 
									
										
										
										
											2019-08-12 13:17:02 +08: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-28 03:37:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // registerWebRouter - registers web router for serving minio browser.
 | 
					
						
							| 
									
										
										
										
											2018-04-22 10:23:54 +08:00
										 |  |  | func registerWebRouter(router *mux.Router) error { | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 	// Initialize Web.
 | 
					
						
							|  |  |  | 	web := &webAPIHandlers{ | 
					
						
							| 
									
										
										
										
											2019-11-10 01:27:23 +08:00
										 |  |  | 		ObjectAPI: newObjectLayerFn, | 
					
						
							|  |  |  | 		CacheAPI:  newCachedObjectLayerFn, | 
					
						
							| 
									
										
										
										
											2016-10-06 03:48:07 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 	// Initialize a new json2 codec.
 | 
					
						
							|  |  |  | 	codec := json2.NewCodec() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  | 	// MinIO browser router.
 | 
					
						
							| 
									
										
										
										
											2019-08-13 08:05:30 +08:00
										 |  |  | 	webBrowserRouter := router.PathPrefix(minioReservedBucketPath).HeadersRegexp("User-Agent", ".*Mozilla.*").Subrouter() | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize json rpc handlers.
 | 
					
						
							|  |  |  | 	webRPC := jsonrpc.NewServer() | 
					
						
							|  |  |  | 	webRPC.RegisterCodec(codec, "application/json") | 
					
						
							|  |  |  | 	webRPC.RegisterCodec(codec, "application/json; charset=UTF-8") | 
					
						
							| 
									
										
										
										
											2020-11-21 14:52:17 +08: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-05 01:40:30 +08:00
										 |  |  | 			if globalHTTPTrace.NumSubscribers() > 0 { | 
					
						
							| 
									
										
										
										
											2020-11-21 14:52:17 +08:00
										 |  |  | 				globalHTTPTrace.Publish(WebTrace(ri)) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-27 05:21:51 +08:00
										 |  |  | 			ctx := newContext(ri.Request, ri.ResponseWriter, ri.Method) | 
					
						
							|  |  |  | 			logger.AuditLog(ctx, ri.ResponseWriter, ri.Request, claims.Map()) | 
					
						
							| 
									
										
										
										
											2020-11-21 14:52:17 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2016-10-14 08:34:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Register RPC handlers with server
 | 
					
						
							| 
									
										
										
										
											2020-11-21 14:52:17 +08:00
										 |  |  | 	if err := webRPC.RegisterService(web, "web"); err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-14 08:34:10 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-08 14:44:08 +08:00
										 |  |  | 	// RPC handler at URI - /minio/webrpc
 | 
					
						
							| 
									
										
										
										
											2020-07-21 03:52:49 +08:00
										 |  |  | 	webBrowserRouter.Methods(http.MethodPost).Path("/webrpc").Handler(webRPC) | 
					
						
							|  |  |  | 	webBrowserRouter.Methods(http.MethodPut).Path("/upload/{bucket}/{object:.+}").HandlerFunc(httpTraceHdrs(web.Upload)) | 
					
						
							| 
									
										
										
										
											2017-07-25 03:46:37 +08: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-21 03:52:49 +08: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-28 03:37:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 02:58:28 +08:00
										 |  |  | 	// Create compressed assets handler
 | 
					
						
							|  |  |  | 	compressAssets := handlers.CompressHandler(http.StripPrefix(minioReservedBucketPath, http.FileServer(assetFS()))) | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Serve javascript files and favicon from assets.
 | 
					
						
							| 
									
										
										
										
											2019-01-23 02:58:28 +08:00
										 |  |  | 	webBrowserRouter.Path(fmt.Sprintf("/{assets:%s}", specialAssets)).Handler(compressAssets) | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 02:58:28 +08:00
										 |  |  | 	// Serve index.html from assets for rest of the requests.
 | 
					
						
							|  |  |  | 	webBrowserRouter.Path("/{index:.*}").Handler(indexHandler{compressAssets}) | 
					
						
							| 
									
										
										
										
											2016-10-14 08:34:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | } |