mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-29 21:29:24 +00:00
Move RecoverPanics to be the top level wrapped handler. Add new method to be sure a logger has been generated instead of assuming one has. Move regexp list compilation into a utility and pass regexp list into CORS.
This commit is contained in:
@@ -55,7 +55,7 @@ func Handle(storage map[string]RESTStorage, codec runtime.Codec, prefix string)
|
||||
mux := http.NewServeMux()
|
||||
group.InstallREST(mux, prefix)
|
||||
InstallSupport(mux)
|
||||
return &defaultAPIServer{RecoverPanics(mux), group}
|
||||
return &defaultAPIServer{mux, group}
|
||||
}
|
||||
|
||||
// APIGroup is a http.Handler that exposes multiple RESTStorage objects
|
||||
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -733,7 +734,7 @@ func TestSyncCreateTimeout(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCORSAllowedOrigin(t *testing.T) {
|
||||
handler := CORS(Handle(map[string]RESTStorage{}, codec, "/prefix/version"), []string{"example.com"}, nil, nil, "true")
|
||||
handler := CORS(Handle(map[string]RESTStorage{}, codec, "/prefix/version"), []*regexp.Regexp{regexp.MustCompile("example.com")}, nil, nil, "true")
|
||||
server := httptest.NewServer(handler)
|
||||
client := http.Client{}
|
||||
|
||||
@@ -766,7 +767,7 @@ func TestCORSAllowedOrigin(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCORSUnallowedOrigin(t *testing.T) {
|
||||
handler := CORS(Handle(map[string]RESTStorage{}, codec, "/prefix/version"), []string{"example.com"}, nil, nil, "true")
|
||||
handler := CORS(Handle(map[string]RESTStorage{}, codec, "/prefix/version"), []*regexp.Regexp{regexp.MustCompile("example.com")}, nil, nil, "true")
|
||||
server := httptest.NewServer(handler)
|
||||
client := http.Client{}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
@@ -59,22 +58,12 @@ func RecoverPanics(handler http.Handler) http.Handler {
|
||||
// For a more detailed implementation use https://github.com/martini-contrib/cors
|
||||
// or implement CORS at your proxy layer
|
||||
// Pass nil for allowedMethods and allowedHeaders to use the defaults
|
||||
func CORS(handler http.Handler, allowedOriginPatterns util.StringList, allowedMethods []string, allowedHeaders []string, allowCredentials string) http.Handler {
|
||||
// Compile the regular expressions once upfront
|
||||
allowedOriginRegexps := []*regexp.Regexp{}
|
||||
for _, allowedOrigin := range allowedOriginPatterns {
|
||||
allowedOriginRegexp, err := regexp.Compile(allowedOrigin)
|
||||
if err != nil {
|
||||
glog.Fatalf("Invalid CORS allowed origin regexp: %v", err)
|
||||
}
|
||||
allowedOriginRegexps = append(allowedOriginRegexps, allowedOriginRegexp)
|
||||
}
|
||||
|
||||
func CORS(handler http.Handler, allowedOriginPatterns []*regexp.Regexp, allowedMethods []string, allowedHeaders []string, allowCredentials string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
origin := req.Header.Get("Origin")
|
||||
if origin != "" {
|
||||
allowed := false
|
||||
for _, pattern := range allowedOriginRegexps {
|
||||
for _, pattern := range allowedOriginPatterns {
|
||||
if allowed = pattern.MatchString(origin); allowed {
|
||||
break
|
||||
}
|
||||
@@ -86,7 +75,7 @@ func CORS(handler http.Handler, allowedOriginPatterns util.StringList, allowedMe
|
||||
allowedMethods = []string{"POST", "GET", "OPTIONS", "PUT", "DELETE"}
|
||||
}
|
||||
if allowedHeaders == nil {
|
||||
allowedHeaders = []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"}
|
||||
allowedHeaders = []string{"Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization", "X-Requested-With", "If-Modified-Since"}
|
||||
}
|
||||
w.Header().Set("Access-Control-Allow-Methods", strings.Join(allowedMethods, ", "))
|
||||
w.Header().Set("Access-Control-Allow-Headers", strings.Join(allowedHeaders, ", "))
|
||||
|
||||
@@ -42,7 +42,7 @@ func (h *RESTHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
storage := h.storage[parts[0]]
|
||||
if storage == nil {
|
||||
httplog.LogOf(w).Addf("'%v' has no storage object", parts[0])
|
||||
httplog.FindOrCreateLogOf(req, &w).Addf("'%v' has no storage object", parts[0])
|
||||
notFound(w, req)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ func (w *WatchServer) HandleWS(ws *websocket.Conn) {
|
||||
// ServeHTTP serves a series of JSON encoded events via straight HTTP with
|
||||
// Transfer-Encoding: chunked.
|
||||
func (self *WatchServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
loggedW := httplog.LogOf(w)
|
||||
loggedW := httplog.FindOrCreateLogOf(req, &w)
|
||||
w = httplog.Unlogged(w)
|
||||
|
||||
cn, ok := w.(http.CloseNotifier)
|
||||
|
||||
Reference in New Issue
Block a user