From 1152886216a58b3a278a4af4b72cee99d457ad5e Mon Sep 17 00:00:00 2001 From: Philip O Toole Date: Mon, 29 Aug 2016 13:07:59 -0700 Subject: [PATCH] Reduce Go cyclo complexity --- CHANGELOG.md | 1 + http/service.go | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1118fd47..d34f5f14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 3.4.1 (unreleased) - [PR #170](https://github.com/rqlite/rqlite/pull/170): Log any failure to call `Serve()` on HTTP service. - Go lint fixes. +- Go cyclo complexity changes. ## 3.4.0 (July 7th 2016) - [PR #159](https://github.com/rqlite/rqlite/pull/159): All HTTP responses set X-RQLITE-VERSION. diff --git a/http/service.go b/http/service.go index 1f1b2674..d6b6bb3e 100644 --- a/http/service.go +++ b/http/service.go @@ -543,14 +543,9 @@ func (s *Service) handleQuery(w http.ResponseWriter, r *http.Request) { } // Validate queries, unless disabled. - if !s.NoVerifySelect { - for _, q := range queries { - u := strings.ToUpper(strings.TrimSpace(q)) - if !strings.HasPrefix(u, "SELECT ") { - w.WriteHeader(http.StatusForbidden) - return - } - } + if !s.NoVerifySelect && !queriesValid(queries) { + w.WriteHeader(http.StatusForbidden) + return } results, err := s.store.Query(queries, timings, isTx, lvl) @@ -602,6 +597,17 @@ func (s *Service) CheckRequestPerm(r *http.Request, perm string) bool { return s.credentialStore.HasPerm(username, PermAll) || s.credentialStore.HasPerm(username, perm) } +// queriesValid returns whether the slice of queries is valid. +func queriesValid(queries []string) bool { + for _, q := range queries { + u := strings.ToUpper(strings.TrimSpace(q)) + if !strings.HasPrefix(u, "SELECT ") { + return false + } + } + return true +} + // serveExpvar serves registered expvar information over HTTP. func serveExpvar(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=utf-8")