Merge pull request #37679 from zdj6373/proxy-accept

Automatic merge from submit-queue

Code optimization

Accept function, the same sentence information repeated three times, under the optimization, recorded in the call function
This commit is contained in:
Kubernetes Submit Queue 2016-12-04 17:16:08 -08:00 committed by GitHub
commit 81177226a8

View File

@ -97,18 +97,14 @@ func matchesRegexp(str string, regexps []*regexp.Regexp) bool {
func (f *FilterServer) accept(method, path, host string) bool { func (f *FilterServer) accept(method, path, host string) bool {
if matchesRegexp(path, f.RejectPaths) { if matchesRegexp(path, f.RejectPaths) {
glog.V(3).Infof("Filter rejecting %v %v %v", method, path, host)
return false return false
} }
if matchesRegexp(method, f.RejectMethods) { if matchesRegexp(method, f.RejectMethods) {
glog.V(3).Infof("Filter rejecting %v %v %v", method, path, host)
return false return false
} }
if matchesRegexp(path, f.AcceptPaths) && matchesRegexp(host, f.AcceptHosts) { if matchesRegexp(path, f.AcceptPaths) && matchesRegexp(host, f.AcceptHosts) {
glog.V(3).Infof("Filter accepting %v %v %v", method, path, host)
return true return true
} }
glog.V(3).Infof("Filter rejecting %v %v %v", method, path, host)
return false return false
} }
@ -131,9 +127,11 @@ func extractHost(header string) (host string) {
func (f *FilterServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (f *FilterServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
host := extractHost(req.Host) host := extractHost(req.Host)
if f.accept(req.Method, req.URL.Path, host) { if f.accept(req.Method, req.URL.Path, host) {
glog.V(3).Infof("Filter accepting %v %v %v", req.Method, req.URL.Path, host)
f.delegate.ServeHTTP(rw, req) f.delegate.ServeHTTP(rw, req)
return return
} }
glog.V(3).Infof("Filter rejecting %v %v %v", req.Method, req.URL.Path, host)
rw.WriteHeader(http.StatusForbidden) rw.WriteHeader(http.StatusForbidden)
rw.Write([]byte("<h3>Unauthorized</h3>")) rw.Write([]byte("<h3>Unauthorized</h3>"))
} }