From ff5b4639d597203f8aec43e5eae8fe3774976d32 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 16 May 2019 11:46:17 -0600 Subject: Some minor updates, and get rid of OnLoad/OnUnload --- modules/caddyhttp/caddylog/log.go | 18 ------------------ modules/caddyhttp/matchers.go | 4 ++-- modules/caddyhttp/matchers_test.go | 8 ++++---- modules/caddyhttp/replacer.go | 17 +++++++++-------- modules/caddyhttp/staticresp.go | 2 +- 5 files changed, 16 insertions(+), 33 deletions(-) (limited to 'modules') diff --git a/modules/caddyhttp/caddylog/log.go b/modules/caddyhttp/caddylog/log.go index dfc9da5..d3110cc 100644 --- a/modules/caddyhttp/caddylog/log.go +++ b/modules/caddyhttp/caddylog/log.go @@ -13,24 +13,6 @@ func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.middleware.log", New: func() (interface{}, error) { return new(Log), nil }, - // TODO: Examples of OnLoad and OnUnload. - OnLoad: func(instances []interface{}, priorState interface{}) (interface{}, error) { - var counter int - if priorState != nil { - counter = priorState.(int) - } - counter++ - for _, inst := range instances { - logInst := inst.(*Log) - logInst.counter = counter - } - log.Println("State is now:", counter) - return counter, nil - }, - OnUnload: func(state interface{}) error { - log.Println("Closing log files, state:", state) - return nil - }, }) } diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index 0179dd7..7df4d8f 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -233,14 +233,14 @@ func (mre *matchRegexp) match(input string, repl *Replacer, scope string) bool { // save all capture groups, first by index for i, match := range matches { - key := fmt.Sprintf("http.matchers.%s.%s.%d", scope, mre.Name, i) + key := fmt.Sprintf("matchers.%s.%s.%d", scope, mre.Name, i) repl.Map(key, match) } // then by name for i, name := range mre.compiled.SubexpNames() { if i != 0 && name != "" { - key := fmt.Sprintf("http.matchers.%s.%s.%s", scope, mre.Name, name) + key := fmt.Sprintf("matchers.%s.%s.%s", scope, mre.Name, name) repl.Map(key, matches[i]) } } diff --git a/modules/caddyhttp/matchers_test.go b/modules/caddyhttp/matchers_test.go index 7da09a6..f23efab 100644 --- a/modules/caddyhttp/matchers_test.go +++ b/modules/caddyhttp/matchers_test.go @@ -217,10 +217,10 @@ func TestPathREMatcher(t *testing.T) { } for key, expectVal := range tc.expectRepl { - placeholder := fmt.Sprintf("{http.matchers.path_regexp.%s}", key) + placeholder := fmt.Sprintf("{matchers.path_regexp.%s}", key) actualVal := repl.Replace(placeholder, "") if actualVal != expectVal { - t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.path_regexp.%s} to be '%s' but got '%s'", + t.Errorf("Test %d [%v]: Expected placeholder {matchers.path_regexp.%s} to be '%s' but got '%s'", i, tc.match.Pattern, key, expectVal, actualVal) continue } @@ -334,10 +334,10 @@ func TestHeaderREMatcher(t *testing.T) { } for key, expectVal := range tc.expectRepl { - placeholder := fmt.Sprintf("{http.matchers.header_regexp.%s}", key) + placeholder := fmt.Sprintf("{matchers.header_regexp.%s}", key) actualVal := repl.Replace(placeholder, "") if actualVal != expectVal { - t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.header_regexp.%s} to be '%s' but got '%s'", + t.Errorf("Test %d [%v]: Expected placeholder {matchers.header_regexp.%s} to be '%s' but got '%s'", i, tc.match, key, expectVal, actualVal) continue } diff --git a/modules/caddyhttp/replacer.go b/modules/caddyhttp/replacer.go index 1fd3428..e7aa250 100644 --- a/modules/caddyhttp/replacer.go +++ b/modules/caddyhttp/replacer.go @@ -93,19 +93,20 @@ func (r *Replacer) defaults() map[string]string { m["request.uri"] = r.req.URL.RequestURI() m["request.uri.path"] = r.req.URL.Path - // TODO: why should header fields, cookies, and query params get special treatment like this? - // maybe they should be scoped by words like "request.header." just like everything else. for field, vals := range r.req.Header { - m[">"+strings.ToLower(field)] = strings.Join(vals, ",") - } - for field, vals := range r.resp.Header() { - m["<"+strings.ToLower(field)] = strings.Join(vals, ",") + m["request.header."+strings.ToLower(field)] = strings.Join(vals, ",") } for _, cookie := range r.req.Cookies() { - m["~"+cookie.Name] = cookie.Value + m["request.cookie."+cookie.Name] = cookie.Value } for param, vals := range r.req.URL.Query() { - m["?"+param] = strings.Join(vals, ",") + m["request.uri.query."+param] = strings.Join(vals, ",") + } + } + + if r.resp != nil { + for field, vals := range r.resp.Header() { + m["response.header."+strings.ToLower(field)] = strings.Join(vals, ",") } } diff --git a/modules/caddyhttp/staticresp.go b/modules/caddyhttp/staticresp.go index 408de60..506689a 100644 --- a/modules/caddyhttp/staticresp.go +++ b/modules/caddyhttp/staticresp.go @@ -25,7 +25,7 @@ type Static struct { func (s Static) ServeHTTP(w http.ResponseWriter, r *http.Request) error { repl := r.Context().Value(ReplacerCtxKey).(*Replacer) - // close the connection + // close the connection after responding r.Close = s.Close // set all headers, with replacements -- cgit v1.2.3