mirror of
https://github.com/caddyserver/caddy.git
synced 2026-01-17 01:30:34 +00:00
Some minor updates, and get rid of OnLoad/OnUnload
This commit is contained in:
@@ -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
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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, "<empty>")
|
||||
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, "<empty>")
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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, ",")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user