From 5cdaf5662bcae23b8bed0c71d5cfdf14b290516e Mon Sep 17 00:00:00 2001 From: Alexander Pashkov Date: Mon, 27 May 2024 14:10:45 +0300 Subject: [PATCH] Refactor Signed-off-by: Alexander Pashkov --- config/config.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++ prober/handler.go | 86 +++------------------------------------------- 2 files changed, 91 insertions(+), 82 deletions(-) diff --git a/config/config.go b/config/config.go index cddb4bb0..41b04d57 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,7 @@ import ( "fmt" "math" "net/textproto" + "net/url" "os" "regexp" "runtime" @@ -226,6 +227,92 @@ type HTTPProbe struct { EnableRegexpsFromParams bool `yaml:"enable_regexps_from_params,omitempty"` } +func (s *HTTPProbe) AddRegexpsFromParams(params url.Values) error { + if !s.EnableRegexpsFromParams { + return nil + } + paramRegexps, err := extractParamRegexps(params) + if err != nil { + return err + } + if paramRegexps.FailIfBodyMatchesRegexp != nil { + s.FailIfBodyMatchesRegexp = append(s.FailIfBodyMatchesRegexp, *paramRegexps.FailIfBodyMatchesRegexp) + } + if paramRegexps.FailIfBodyNotMatchesRegexp != nil { + s.FailIfBodyNotMatchesRegexp = append(s.FailIfBodyNotMatchesRegexp, *paramRegexps.FailIfBodyNotMatchesRegexp) + } + if paramRegexps.FailIfHeaderMatchesRegexp != nil { + s.FailIfHeaderMatchesRegexp = append(s.FailIfHeaderMatchesRegexp, *paramRegexps.FailIfHeaderMatchesRegexp) + } + if paramRegexps.FailIfHeaderNotMatchesRegexp != nil { + s.FailIfHeaderNotMatchesRegexp = append(s.FailIfHeaderNotMatchesRegexp, *paramRegexps.FailIfHeaderNotMatchesRegexp) + } + return nil +} + +func extractParamRegexps(params url.Values) (*HTTPRegexps, error) { + var ( + dynamicHTTPRegexps HTTPRegexps + err error + ) + + if re := params.Get("fail_if_body_matches_regexp"); re != "" { + dynamicHTTPRegexps.FailIfBodyMatchesRegexp, err = regexpFromURLEncodedString(re) + if err != nil { + return nil, fmt.Errorf("failed to parse fail_if_body_matches_regexp: %s", err) + } + } + if re := params.Get("fail_if_body_not_matches_regexp"); re != "" { + dynamicHTTPRegexps.FailIfBodyNotMatchesRegexp, err = regexpFromURLEncodedString(re) + if err != nil { + return nil, fmt.Errorf("failed to parse fail_if_body_not_matches_regexp: %s", err) + } + } + if re := params.Get("fail_if_header_matches_regexp"); re != "" { + dynamicHTTPRegexps.FailIfHeaderMatchesRegexp, err = extractHeaderMatch( + params.Get("fail_if_header_matches_regexp_header"), re) + if err != nil { + return nil, fmt.Errorf("failed to parse %s: %s", "fail_if_header_matches_regexp", err) + } + } + if re := params.Get("fail_if_header_not_matches_regexp"); re != "" { + dynamicHTTPRegexps.FailIfHeaderNotMatchesRegexp, err = extractHeaderMatch(re, "fail_if_header_not_matches_regexp") + if err != nil { + return nil, fmt.Errorf("failed to parse %s: %s", "fail_if_header_matches_regexp", err) + } + } + return &dynamicHTTPRegexps, nil +} + +func extractHeaderMatch(headerParam, headerRegexpParam string) (*HeaderMatch, error) { + var dynamicHeaderMatch HeaderMatch + var err error + + if headerParam == "" || headerRegexpParam == "" { + return nil, fmt.Errorf("both fail_if_header_matches_regexp and fail_if_header_matches_regexp_header must be specified") + } + + dynamicHeaderMatch.Header = headerParam + regexp, err := regexpFromURLEncodedString(headerRegexpParam) + if err != nil { + return nil, fmt.Errorf("failed to parse %s: %s", headerRegexpParam, err) + } + dynamicHeaderMatch.Regexp = *regexp + return &dynamicHeaderMatch, err +} + +func regexpFromURLEncodedString(a string) (*Regexp, error) { + re, err := url.QueryUnescape(a) + if err != nil { + return nil, fmt.Errorf("failed to unescape regexp: %s", err) + } + regexp, err := NewRegexp(re) + if err != nil { + return nil, fmt.Errorf("failed to compile regexp: %s", err) + } + return ®exp, nil +} + type HTTPRegexps struct { FailIfBodyMatchesRegexp *Regexp `yaml:"fail_if_body_matches_regexp,omitempty"` FailIfBodyNotMatchesRegexp *Regexp `yaml:"fail_if_body_not_matches_regexp,omitempty"` diff --git a/prober/handler.go b/prober/handler.go index ee79ea2e..9aa7b013 100644 --- a/prober/handler.go +++ b/prober/handler.go @@ -63,25 +63,10 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo return } - if module.HTTP.EnableRegexpsFromParams { - var err error - paramRegexps, err := extractHTTPParamRegexps(params) - if err != nil { - http.Error(w, fmt.Sprintf("Failed to extract regular expressions from params: %s", err), http.StatusBadRequest) - return - } - if paramRegexps.FailIfBodyMatchesRegexp != nil { - module.HTTP.FailIfBodyMatchesRegexp = append(module.HTTP.FailIfBodyMatchesRegexp, *paramRegexps.FailIfBodyMatchesRegexp) - } - if paramRegexps.FailIfBodyNotMatchesRegexp != nil { - module.HTTP.FailIfBodyNotMatchesRegexp = append(module.HTTP.FailIfBodyNotMatchesRegexp, *paramRegexps.FailIfBodyNotMatchesRegexp) - } - if paramRegexps.FailIfHeaderMatchesRegexp != nil { - module.HTTP.FailIfHeaderMatchesRegexp = append(module.HTTP.FailIfHeaderMatchesRegexp, *paramRegexps.FailIfHeaderMatchesRegexp) - } - if paramRegexps.FailIfHeaderNotMatchesRegexp != nil { - module.HTTP.FailIfHeaderNotMatchesRegexp = append(module.HTTP.FailIfHeaderNotMatchesRegexp, *paramRegexps.FailIfHeaderNotMatchesRegexp) - } + err := module.HTTP.AddRegexpsFromParams(params) + if err != nil { + http.Error(w, fmt.Sprintf("Failed to parse regexps from URL parameters: %s", err), http.StatusBadRequest) + return } timeoutSeconds, err := getTimeout(r, module, timeoutOffset) @@ -160,69 +145,6 @@ func Handler(w http.ResponseWriter, r *http.Request, c *config.Config, logger lo h.ServeHTTP(w, r) } -func extractHTTPParamRegexps(params url.Values) (*config.HTTPRegexps, error) { - var ( - dynamicHTTPRegexps config.HTTPRegexps - err error - ) - - if re := params.Get("fail_if_body_matches_regexp"); re != "" { - dynamicHTTPRegexps.FailIfBodyMatchesRegexp, err = regexpFromURLEncodedString(re) - if err != nil { - return nil, fmt.Errorf("failed to parse fail_if_body_matches_regexp: %s", err) - } - } - if re := params.Get("fail_if_body_not_matches_regexp"); re != "" { - dynamicHTTPRegexps.FailIfBodyNotMatchesRegexp, err = regexpFromURLEncodedString(re) - if err != nil { - return nil, fmt.Errorf("failed to parse fail_if_body_not_matches_regexp: %s", err) - } - } - if re := params.Get("fail_if_header_matches_regexp"); re != "" { - dynamicHTTPRegexps.FailIfHeaderMatchesRegexp, err = extractHeaderMatch( - params.Get("fail_if_header_matches_regexp_header"), re) - if err != nil { - return nil, fmt.Errorf("failed to parse %s: %s", "fail_if_header_matches_regexp", err) - } - } - if re := params.Get("fail_if_header_not_matches_regexp"); re != "" { - dynamicHTTPRegexps.FailIfHeaderNotMatchesRegexp, err = extractHeaderMatch(re, "fail_if_header_not_matches_regexp") - if err != nil { - return nil, fmt.Errorf("failed to parse %s: %s", "fail_if_header_matches_regexp", err) - } - } - return &dynamicHTTPRegexps, nil -} - -func extractHeaderMatch(headerParam, headerRegexpParam string) (*config.HeaderMatch, error) { - var dynamicHeaderMatch config.HeaderMatch - var err error - - if headerParam == "" || headerRegexpParam == "" { - return nil, fmt.Errorf("both fail_if_header_matches_regexp and fail_if_header_matches_regexp_header must be specified") - } - - dynamicHeaderMatch.Header = headerParam - regexp, err := regexpFromURLEncodedString(headerRegexpParam) - if err != nil { - return nil, fmt.Errorf("failed to parse %s: %s", headerRegexpParam, err) - } - dynamicHeaderMatch.Regexp = *regexp - return &dynamicHeaderMatch, err -} - -func regexpFromURLEncodedString(a string) (*config.Regexp, error) { - re, err := url.QueryUnescape(a) - if err != nil { - return nil, fmt.Errorf("failed to unescape regexp: %s", err) - } - regexp, err := config.NewRegexp(re) - if err != nil { - return nil, fmt.Errorf("failed to compile regexp: %s", err) - } - return ®exp, nil -} - func setHTTPHost(hostname string, module *config.Module) error { // By creating a new hashmap and copying values there we // ensure that the initial configuration remain intact.