From 05acc3329411d50e62a973cb9e93edfa7216995f Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 31 Oct 2020 16:31:39 -0700 Subject: [PATCH] volume: add CORS support --- weed/server/volume_server_handlers.go | 17 ++++++++++++++++- weed/server/volume_server_handlers_read.go | 7 +++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/weed/server/volume_server_handlers.go b/weed/server/volume_server_handlers.go index ad13cdf3b..6f44b9a0a 100644 --- a/weed/server/volume_server_handlers.go +++ b/weed/server/volume_server_handlers.go @@ -1,10 +1,11 @@ package weed_server import ( - "github.com/chrislusf/seaweedfs/weed/util" "net/http" "strings" + "github.com/chrislusf/seaweedfs/weed/util" + "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/security" "github.com/chrislusf/seaweedfs/weed/stats" @@ -27,6 +28,10 @@ security settings: func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION) + if r.Header.Get("Origin") != "" { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Credentials", "true") + } switch r.Method { case "GET", "HEAD": stats.ReadRequest() @@ -37,11 +42,18 @@ func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Reque case "PUT", "POST": stats.WriteRequest() vs.guard.WhiteList(vs.PostHandler)(w, r) + case "OPTIONS": + stats.ReadRequest() + vs.guard.WhiteList(vs.OptionsHandler)(w, r) } } func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Server", "SeaweedFS Volume "+util.VERSION) + if r.Header.Get("Origin") != "" { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Credentials", "true") + } switch r.Method { case "GET": stats.ReadRequest() @@ -49,6 +61,9 @@ func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Req case "HEAD": stats.ReadRequest() vs.GetOrHeadHandler(w, r) + case "OPTIONS": + stats.ReadRequest() + vs.OptionsHandler(w, r) } } diff --git a/weed/server/volume_server_handlers_read.go b/weed/server/volume_server_handlers_read.go index 15fd446e7..909b674a4 100644 --- a/weed/server/volume_server_handlers_read.go +++ b/weed/server/volume_server_handlers_read.go @@ -270,3 +270,10 @@ func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.Re }) return nil } + +func (vs *VolumeServer) OptionsHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Access-Control-Allow-Origin", "*") + w.Header().Add("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS") + w.Header().Add("Access-Control-Allow-Headers", "Content-Type") + w.Header().Add("Access-Control-Allow-Credentials", "true") +} \ No newline at end of file