From 3f404bc37e14bbd781f6feb62502a44188b4c7ef Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Mon, 15 Apr 2019 16:21:12 +0300 Subject: [PATCH] + http server: enable gzip compression for /control/querylog --- control.go | 3 ++- helpers.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/control.go b/control.go index 9a0e7fa0..ba7b1838 100644 --- a/control.go +++ b/control.go @@ -19,6 +19,7 @@ import ( "github.com/AdguardTeam/dnsproxy/upstream" "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/utils" + "github.com/NYTimes/gziphandler" "github.com/miekg/dns" govalidator "gopkg.in/asaskevich/govalidator.v4" ) @@ -1100,7 +1101,7 @@ func registerControlHandlers() { http.HandleFunc("/control/status", postInstall(optionalAuth(ensureGET(handleStatus)))) http.HandleFunc("/control/enable_protection", postInstall(optionalAuth(ensurePOST(handleProtectionEnable)))) http.HandleFunc("/control/disable_protection", postInstall(optionalAuth(ensurePOST(handleProtectionDisable)))) - http.HandleFunc("/control/querylog", postInstall(optionalAuth(ensureGET(handleQueryLog)))) + http.Handle("/control/querylog", postInstallHandler(optionalAuthHandler(gziphandler.GzipHandler(ensureGETHandler(handleQueryLog))))) http.HandleFunc("/control/querylog_enable", postInstall(optionalAuth(ensurePOST(handleQueryLogEnable)))) http.HandleFunc("/control/querylog_disable", postInstall(optionalAuth(ensurePOST(handleQueryLogDisable)))) http.HandleFunc("/control/set_upstreams_config", postInstall(optionalAuth(ensurePOST(handleSetUpstreamConfig)))) diff --git a/helpers.go b/helpers.go index 9d02ad3c..b0bd3754 100644 --- a/helpers.go +++ b/helpers.go @@ -45,6 +45,21 @@ func ensureGET(handler func(http.ResponseWriter, *http.Request)) func(http.Respo return ensure("GET", handler) } +// Bridge between http.Handler object and Go function +type httpHandler struct { + handler func(http.ResponseWriter, *http.Request) +} + +func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + h.handler(w, r) +} + +func ensureGETHandler(handler func(http.ResponseWriter, *http.Request)) http.Handler { + h := httpHandler{} + h.handler = ensure("GET", handler) + return &h +} + func ensurePUT(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) { return ensure("PUT", handler) }