From f8aef51374a1571dbdc42248dfc039d57629b9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Thu, 18 Oct 2018 11:42:35 +0800 Subject: [PATCH 1/4] server: allow cors http request --- config/config.go | 2 ++ server/http_status.go | 2 +- server/util.go | 15 +++++++++++++++ tidb-server/main.go | 5 +++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 9e9ce88c2e17e..f5f174045e6b1 100644 --- a/config/config.go +++ b/config/config.go @@ -47,6 +47,7 @@ type Config struct { Host string `toml:"host" json:"host"` AdvertiseAddress string `toml:"advertise-address" json:"advertise-address"` Port uint `toml:"port" json:"port"` + Cors bool `toml:"cors" json:"cors"` Store string `toml:"store" json:"store"` Path string `toml:"path" json:"path"` Socket string `toml:"socket" json:"socket"` @@ -252,6 +253,7 @@ var defaultConf = Config{ Host: "0.0.0.0", AdvertiseAddress: "", Port: 4000, + Cors: false, Store: "mocktikv", Path: "/tmp/tidb", RunDDL: true, diff --git a/server/http_status.go b/server/http_status.go index c48d1bc342036..47470f2318028 100644 --- a/server/http_status.go +++ b/server/http_status.go @@ -117,7 +117,7 @@ func (s *Server) startHTTPServer() { }) log.Infof("Listening on %v for status and metrics report.", addr) - s.statusServer = &http.Server{Addr: addr, Handler: serverMux} + s.statusServer = &http.Server{Addr: addr, Handler: CorsHandler{handler: serverMux, cfg: s.cfg}} if len(s.cfg.Security.ClusterSSLCA) != 0 { err = s.statusServer.ListenAndServeTLS(s.cfg.Security.ClusterSSLCert, s.cfg.Security.ClusterSSLKey) diff --git a/server/util.go b/server/util.go index 352fc9e75c12e..cd000129a661f 100644 --- a/server/util.go +++ b/server/util.go @@ -37,8 +37,10 @@ package server import ( "bytes" "encoding/binary" + "github.com/pingcap/tidb/config" "io" "math" + "net/http" "strconv" "time" @@ -354,3 +356,16 @@ func appendFormatFloat(in []byte, fVal float64, prec, bitSize int) []byte { } return out } + +type CorsHandler struct { + handler http.Handler + cfg *config.Config +} + +func (h CorsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + if h.cfg.Cors { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET") + } + h.handler.ServeHTTP(w, req) +} diff --git a/tidb-server/main.go b/tidb-server/main.go index 44bddf593e4e0..579b692d15e8b 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -64,6 +64,7 @@ const ( nmHost = "host" nmAdvertiseAddress = "advertise-address" nmPort = "P" + nmCors = "cors" nmSocket = "socket" nmBinlogSocket = "binlog-socket" nmRunDDL = "run-ddl" @@ -91,6 +92,7 @@ var ( host = flag.String(nmHost, "0.0.0.0", "tidb server host") advertiseAddress = flag.String(nmAdvertiseAddress, "", "tidb server advertise IP") port = flag.String(nmPort, "4000", "tidb server port") + cors = flagBoolean(nmCors, false, "tidb server allow cors request") socket = flag.String(nmSocket, "", "The socket file to use for connection.") binlogSocket = flag.String(nmBinlogSocket, "", "socket file to write binlog") runDDL = flagBoolean(nmRunDDL, true, "run ddl worker on this tidb-server") @@ -278,6 +280,9 @@ func overrideConfig() { terror.MustNil(err) cfg.Port = uint(p) } + if actualFlags[nmCors] { + cfg.Cors = *cors + } if actualFlags[nmStore] { cfg.Store = *store } From 6eaaa806f31bb759b86e3493af6a1def4d60890b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Thu, 18 Oct 2018 12:02:36 +0800 Subject: [PATCH 2/4] add comment --- server/util.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/util.go b/server/util.go index cd000129a661f..1dd710edf0ec8 100644 --- a/server/util.go +++ b/server/util.go @@ -357,6 +357,7 @@ func appendFormatFloat(in []byte, fVal float64, prec, bitSize int) []byte { return out } +// CorsHandler add Cors Header if `cors` config is setted type CorsHandler struct { handler http.Handler cfg *config.Config From 4053552bed966dd6ccc0ef6494af2d4aa5afaff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Fri, 19 Oct 2018 10:40:52 +0800 Subject: [PATCH 3/4] update comment --- server/util.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/util.go b/server/util.go index 1dd710edf0ec8..57211a9763bdc 100644 --- a/server/util.go +++ b/server/util.go @@ -37,13 +37,13 @@ package server import ( "bytes" "encoding/binary" - "github.com/pingcap/tidb/config" "io" "math" "net/http" "strconv" "time" + "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/chunk" @@ -357,7 +357,7 @@ func appendFormatFloat(in []byte, fVal float64, prec, bitSize int) []byte { return out } -// CorsHandler add Cors Header if `cors` config is setted +// CorsHandler adds Cors Header if `cors` config is set. type CorsHandler struct { handler http.Handler cfg *config.Config From 9141d04a2f1b23ca2abacc0eff121609baea07de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Fri, 19 Oct 2018 10:50:10 +0800 Subject: [PATCH 4/4] update cors config --- config/config.go | 4 ++-- server/util.go | 4 ++-- tidb-server/main.go | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index f5f174045e6b1..258c9a5fcaa64 100644 --- a/config/config.go +++ b/config/config.go @@ -47,7 +47,7 @@ type Config struct { Host string `toml:"host" json:"host"` AdvertiseAddress string `toml:"advertise-address" json:"advertise-address"` Port uint `toml:"port" json:"port"` - Cors bool `toml:"cors" json:"cors"` + Cors string `toml:"cors" json:"cors"` Store string `toml:"store" json:"store"` Path string `toml:"path" json:"path"` Socket string `toml:"socket" json:"socket"` @@ -253,7 +253,7 @@ var defaultConf = Config{ Host: "0.0.0.0", AdvertiseAddress: "", Port: 4000, - Cors: false, + Cors: "", Store: "mocktikv", Path: "/tmp/tidb", RunDDL: true, diff --git a/server/util.go b/server/util.go index 57211a9763bdc..12037a4301259 100644 --- a/server/util.go +++ b/server/util.go @@ -364,8 +364,8 @@ type CorsHandler struct { } func (h CorsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - if h.cfg.Cors { - w.Header().Set("Access-Control-Allow-Origin", "*") + if h.cfg.Cors != "" { + w.Header().Set("Access-Control-Allow-Origin", h.cfg.Cors) w.Header().Set("Access-Control-Allow-Methods", "GET") } h.handler.ServeHTTP(w, req) diff --git a/tidb-server/main.go b/tidb-server/main.go index 579b692d15e8b..2e3f7eebf13ae 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -92,7 +92,7 @@ var ( host = flag.String(nmHost, "0.0.0.0", "tidb server host") advertiseAddress = flag.String(nmAdvertiseAddress, "", "tidb server advertise IP") port = flag.String(nmPort, "4000", "tidb server port") - cors = flagBoolean(nmCors, false, "tidb server allow cors request") + cors = flag.String(nmCors, "", "tidb server allow cors origin") socket = flag.String(nmSocket, "", "The socket file to use for connection.") binlogSocket = flag.String(nmBinlogSocket, "", "socket file to write binlog") runDDL = flagBoolean(nmRunDDL, true, "run ddl worker on this tidb-server") @@ -281,6 +281,7 @@ func overrideConfig() { cfg.Port = uint(p) } if actualFlags[nmCors] { + fmt.Println(cors) cfg.Cors = *cors } if actualFlags[nmStore] {