From 2638e271fed1937ceafaefeefe8086cb249eaf72 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Thu, 4 Feb 2021 14:15:34 +0300 Subject: [PATCH] Pull request: all: use http method constants Merge in DNS/adguard-home from method-const to master Squashed commit of the following: commit ae6d6699a25ca04ba92aa53258d46e50233a9e00 Author: Ainar Garipov Date: Fri Jan 29 22:03:59 2021 +0300 all: use http method constants --- internal/dnsfilter/blocked.go | 4 ++-- internal/dnsfilter/rewrites.go | 6 +++--- internal/dnsfilter/safebrowsing.go | 18 +++++++++--------- internal/home/clientshttp.go | 10 +++++----- internal/home/controlfiltering.go | 16 ++++++++-------- internal/home/tls.go | 6 +++--- internal/querylog/http.go | 8 ++++---- internal/stats/http.go | 8 ++++---- scripts/make/go-lint.sh | 24 ++++++++++++++++++++++++ 9 files changed, 62 insertions(+), 38 deletions(-) diff --git a/internal/dnsfilter/blocked.go b/internal/dnsfilter/blocked.go index 48b02932..473010c0 100644 --- a/internal/dnsfilter/blocked.go +++ b/internal/dnsfilter/blocked.go @@ -242,6 +242,6 @@ func (d *DNSFilter) handleBlockedServicesSet(w http.ResponseWriter, r *http.Requ // registerBlockedServicesHandlers - register HTTP handlers func (d *DNSFilter) registerBlockedServicesHandlers() { - d.Config.HTTPRegister("GET", "/control/blocked_services/list", d.handleBlockedServicesList) - d.Config.HTTPRegister("POST", "/control/blocked_services/set", d.handleBlockedServicesSet) + d.Config.HTTPRegister(http.MethodGet, "/control/blocked_services/list", d.handleBlockedServicesList) + d.Config.HTTPRegister(http.MethodPost, "/control/blocked_services/set", d.handleBlockedServicesSet) } diff --git a/internal/dnsfilter/rewrites.go b/internal/dnsfilter/rewrites.go index 8db1fd0b..146be60a 100644 --- a/internal/dnsfilter/rewrites.go +++ b/internal/dnsfilter/rewrites.go @@ -219,7 +219,7 @@ func (d *DNSFilter) handleRewriteDelete(w http.ResponseWriter, r *http.Request) } func (d *DNSFilter) registerRewritesHandlers() { - d.Config.HTTPRegister("GET", "/control/rewrite/list", d.handleRewriteList) - d.Config.HTTPRegister("POST", "/control/rewrite/add", d.handleRewriteAdd) - d.Config.HTTPRegister("POST", "/control/rewrite/delete", d.handleRewriteDelete) + d.Config.HTTPRegister(http.MethodGet, "/control/rewrite/list", d.handleRewriteList) + d.Config.HTTPRegister(http.MethodPost, "/control/rewrite/add", d.handleRewriteAdd) + d.Config.HTTPRegister(http.MethodPost, "/control/rewrite/delete", d.handleRewriteDelete) } diff --git a/internal/dnsfilter/safebrowsing.go b/internal/dnsfilter/safebrowsing.go index ad99bf80..9a14b584 100644 --- a/internal/dnsfilter/safebrowsing.go +++ b/internal/dnsfilter/safebrowsing.go @@ -381,15 +381,15 @@ func (d *DNSFilter) handleParentalStatus(w http.ResponseWriter, r *http.Request) } func (d *DNSFilter) registerSecurityHandlers() { - d.Config.HTTPRegister("POST", "/control/safebrowsing/enable", d.handleSafeBrowsingEnable) - d.Config.HTTPRegister("POST", "/control/safebrowsing/disable", d.handleSafeBrowsingDisable) - d.Config.HTTPRegister("GET", "/control/safebrowsing/status", d.handleSafeBrowsingStatus) + d.Config.HTTPRegister(http.MethodPost, "/control/safebrowsing/enable", d.handleSafeBrowsingEnable) + d.Config.HTTPRegister(http.MethodPost, "/control/safebrowsing/disable", d.handleSafeBrowsingDisable) + d.Config.HTTPRegister(http.MethodGet, "/control/safebrowsing/status", d.handleSafeBrowsingStatus) - d.Config.HTTPRegister("POST", "/control/parental/enable", d.handleParentalEnable) - d.Config.HTTPRegister("POST", "/control/parental/disable", d.handleParentalDisable) - d.Config.HTTPRegister("GET", "/control/parental/status", d.handleParentalStatus) + d.Config.HTTPRegister(http.MethodPost, "/control/parental/enable", d.handleParentalEnable) + d.Config.HTTPRegister(http.MethodPost, "/control/parental/disable", d.handleParentalDisable) + d.Config.HTTPRegister(http.MethodGet, "/control/parental/status", d.handleParentalStatus) - d.Config.HTTPRegister("POST", "/control/safesearch/enable", d.handleSafeSearchEnable) - d.Config.HTTPRegister("POST", "/control/safesearch/disable", d.handleSafeSearchDisable) - d.Config.HTTPRegister("GET", "/control/safesearch/status", d.handleSafeSearchStatus) + d.Config.HTTPRegister(http.MethodPost, "/control/safesearch/enable", d.handleSafeSearchEnable) + d.Config.HTTPRegister(http.MethodPost, "/control/safesearch/disable", d.handleSafeSearchDisable) + d.Config.HTTPRegister(http.MethodGet, "/control/safesearch/status", d.handleSafeSearchStatus) } diff --git a/internal/home/clientshttp.go b/internal/home/clientshttp.go index edeaf585..23930411 100644 --- a/internal/home/clientshttp.go +++ b/internal/home/clientshttp.go @@ -297,9 +297,9 @@ func (clients *clientsContainer) findTemporary(ip net.IP, idStr string) (cj clie // RegisterClientsHandlers registers HTTP handlers func (clients *clientsContainer) registerWebHandlers() { - httpRegister("GET", "/control/clients", clients.handleGetClients) - httpRegister("POST", "/control/clients/add", clients.handleAddClient) - httpRegister("POST", "/control/clients/delete", clients.handleDelClient) - httpRegister("POST", "/control/clients/update", clients.handleUpdateClient) - httpRegister("GET", "/control/clients/find", clients.handleFindClient) + httpRegister(http.MethodGet, "/control/clients", clients.handleGetClients) + httpRegister(http.MethodPost, "/control/clients/add", clients.handleAddClient) + httpRegister(http.MethodPost, "/control/clients/delete", clients.handleDelClient) + httpRegister(http.MethodPost, "/control/clients/update", clients.handleUpdateClient) + httpRegister(http.MethodGet, "/control/clients/find", clients.handleFindClient) } diff --git a/internal/home/controlfiltering.go b/internal/home/controlfiltering.go index 53ca0445..b64423d1 100644 --- a/internal/home/controlfiltering.go +++ b/internal/home/controlfiltering.go @@ -417,14 +417,14 @@ func (f *Filtering) handleCheckHost(w http.ResponseWriter, r *http.Request) { // RegisterFilteringHandlers - register handlers func (f *Filtering) RegisterFilteringHandlers() { - httpRegister("GET", "/control/filtering/status", f.handleFilteringStatus) - httpRegister("POST", "/control/filtering/config", f.handleFilteringConfig) - httpRegister("POST", "/control/filtering/add_url", f.handleFilteringAddURL) - httpRegister("POST", "/control/filtering/remove_url", f.handleFilteringRemoveURL) - httpRegister("POST", "/control/filtering/set_url", f.handleFilteringSetURL) - httpRegister("POST", "/control/filtering/refresh", f.handleFilteringRefresh) - httpRegister("POST", "/control/filtering/set_rules", f.handleFilteringSetRules) - httpRegister("GET", "/control/filtering/check_host", f.handleCheckHost) + httpRegister(http.MethodGet, "/control/filtering/status", f.handleFilteringStatus) + httpRegister(http.MethodPost, "/control/filtering/config", f.handleFilteringConfig) + httpRegister(http.MethodPost, "/control/filtering/add_url", f.handleFilteringAddURL) + httpRegister(http.MethodPost, "/control/filtering/remove_url", f.handleFilteringRemoveURL) + httpRegister(http.MethodPost, "/control/filtering/set_url", f.handleFilteringSetURL) + httpRegister(http.MethodPost, "/control/filtering/refresh", f.handleFilteringRefresh) + httpRegister(http.MethodPost, "/control/filtering/set_rules", f.handleFilteringSetRules) + httpRegister(http.MethodGet, "/control/filtering/check_host", f.handleCheckHost) } func checkFiltersUpdateIntervalHours(i uint32) bool { diff --git a/internal/home/tls.go b/internal/home/tls.go index 1f68cc39..d97207ee 100644 --- a/internal/home/tls.go +++ b/internal/home/tls.go @@ -544,7 +544,7 @@ func marshalTLS(w http.ResponseWriter, data tlsConfig) { // registerWebHandlers registers HTTP handlers for TLS configuration func (t *TLSMod) registerWebHandlers() { - httpRegister("GET", "/control/tls/status", t.handleTLSStatus) - httpRegister("POST", "/control/tls/configure", t.handleTLSConfigure) - httpRegister("POST", "/control/tls/validate", t.handleTLSValidate) + httpRegister(http.MethodGet, "/control/tls/status", t.handleTLSStatus) + httpRegister(http.MethodPost, "/control/tls/configure", t.handleTLSConfigure) + httpRegister(http.MethodPost, "/control/tls/validate", t.handleTLSValidate) } diff --git a/internal/querylog/http.go b/internal/querylog/http.go index 9bc63b7e..8948f9f6 100644 --- a/internal/querylog/http.go +++ b/internal/querylog/http.go @@ -22,10 +22,10 @@ type qlogConfig struct { // Register web handlers func (l *queryLog) initWeb() { - l.conf.HTTPRegister("GET", "/control/querylog", l.handleQueryLog) - l.conf.HTTPRegister("GET", "/control/querylog_info", l.handleQueryLogInfo) - l.conf.HTTPRegister("POST", "/control/querylog_clear", l.handleQueryLogClear) - l.conf.HTTPRegister("POST", "/control/querylog_config", l.handleQueryLogConfig) + l.conf.HTTPRegister(http.MethodGet, "/control/querylog", l.handleQueryLog) + l.conf.HTTPRegister(http.MethodGet, "/control/querylog_info", l.handleQueryLogInfo) + l.conf.HTTPRegister(http.MethodPost, "/control/querylog_clear", l.handleQueryLogClear) + l.conf.HTTPRegister(http.MethodPost, "/control/querylog_config", l.handleQueryLogConfig) } func httpError(r *http.Request, w http.ResponseWriter, code int, format string, args ...interface{}) { diff --git a/internal/stats/http.go b/internal/stats/http.go index 4d7c0b15..1580174a 100644 --- a/internal/stats/http.go +++ b/internal/stats/http.go @@ -113,8 +113,8 @@ func (s *statsCtx) initWeb() { return } - s.conf.HTTPRegister("GET", "/control/stats", s.handleStats) - s.conf.HTTPRegister("POST", "/control/stats_reset", s.handleStatsReset) - s.conf.HTTPRegister("POST", "/control/stats_config", s.handleStatsConfig) - s.conf.HTTPRegister("GET", "/control/stats_info", s.handleStatsInfo) + s.conf.HTTPRegister(http.MethodGet, "/control/stats", s.handleStats) + s.conf.HTTPRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset) + s.conf.HTTPRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig) + s.conf.HTTPRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo) } diff --git a/scripts/make/go-lint.sh b/scripts/make/go-lint.sh index 391e8c51..59c4cbf1 100644 --- a/scripts/make/go-lint.sh +++ b/scripts/make/go-lint.sh @@ -23,6 +23,10 @@ fi # variables. set -f -u + + +# Deferred Helpers + not_found_msg=' looks like a binary not found error. make sure you have installed the linter binaries using: @@ -43,6 +47,10 @@ not_found() { } trap not_found EXIT + + +# Simple Analyzers + # blocklist_imports is a simple check against unwanted packages. # Currently it only looks for package log which is replaced by our own # package github.com/AdguardTeam/golibs/log. @@ -50,6 +58,12 @@ blocklist_imports() { git grep -F -e '"log"' -- '*.go' || exit 0; } +# method_const is a simple check against the usage of some raw strings +# and numbers where one should use named constants. +method_const() { + git grep -F -e '"GET"' -e '"POST"' -- '*.go' || exit 0; +} + # underscores is a simple check against Go filenames with underscores. underscores() { git ls-files '*_*.go' | { grep -F -e '_darwin.go' \ @@ -58,6 +72,10 @@ underscores() { -v || exit 0; } } + + +# Helpers + # exit_on_output exits with a nonzero exit code if there is anything in # the command's combined output. exit_on_output() ( @@ -98,8 +116,14 @@ exit_on_output() ( return "$exitcode" ) + + +# Checks + exit_on_output blocklist_imports +exit_on_output method_const + exit_on_output underscores exit_on_output gofumpt --extra -l -s .