From 6621406fa8b44826477ba7cbe2ff6c5462048f8e Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sun, 31 Mar 2019 20:41:29 -0600 Subject: Very basic middleware and route matching functionality --- caddy.go | 15 ++- cmd/caddy2/main.go | 2 + listeners.go | 6 +- modules.go | 42 ++++++- modules/caddyhttp/caddyhttp.go | 179 +++++++++++++++++++++++++-- modules/caddyhttp/caddylog/log.go | 37 ++++++ modules/caddyhttp/matchers.go | 102 +++++++++++++++ modules/caddyhttp/responsewriter.go | 68 ++++++++++ modules/caddyhttp/staticfiles/staticfiles.go | 28 +++++ 9 files changed, 454 insertions(+), 25 deletions(-) create mode 100644 modules/caddyhttp/caddylog/log.go create mode 100644 modules/caddyhttp/matchers.go create mode 100644 modules/caddyhttp/responsewriter.go create mode 100644 modules/caddyhttp/staticfiles/staticfiles.go diff --git a/caddy.go b/caddy.go index 3b7aee2..20e2d6c 100644 --- a/caddy.go +++ b/caddy.go @@ -17,11 +17,7 @@ func Start(cfg Config) error { cfg.runners = make(map[string]Runner) for modName, rawMsg := range cfg.Modules { - mod, ok := modules[modName] - if !ok { - return fmt.Errorf("unrecognized module: %s", modName) - } - val, err := LoadModule(mod, rawMsg) + val, err := LoadModule(modName, rawMsg) if err != nil { return fmt.Errorf("loading module '%s': %v", modName, err) } @@ -68,14 +64,17 @@ type Config struct { type Duration time.Duration // UnmarshalJSON satisfies json.Unmarshaler. -func (d *Duration) UnmarshalJSON(b []byte) (err error) { +func (d *Duration) UnmarshalJSON(b []byte) error { dd, err := time.ParseDuration(strings.Trim(string(b), `"`)) + if err != nil { + return err + } cd := Duration(dd) d = &cd - return + return nil } // MarshalJSON satisfies json.Marshaler. -func (d Duration) MarshalJSON() (b []byte, err error) { +func (d Duration) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`"%s"`, time.Duration(d).String())), nil } diff --git a/cmd/caddy2/main.go b/cmd/caddy2/main.go index 234a88d..9ec32f9 100644 --- a/cmd/caddy2/main.go +++ b/cmd/caddy2/main.go @@ -9,6 +9,8 @@ import ( // this is where modules get plugged in _ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp" + _ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp/caddylog" + _ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp/staticfiles" _ "bitbucket.org/lightcodelabs/dynamicconfig" ) diff --git a/listeners.go b/listeners.go index 0a2fe1c..a3efc9e 100644 --- a/listeners.go +++ b/listeners.go @@ -8,8 +8,8 @@ import ( ) // Listen returns a listener suitable for use in a Caddy module. -func Listen(proto, addr string) (net.Listener, error) { - lnKey := proto + "/" + addr +func Listen(network, addr string) (net.Listener, error) { + lnKey := network + "/" + addr listenersMu.Lock() defer listenersMu.Unlock() @@ -20,7 +20,7 @@ func Listen(proto, addr string) (net.Listener, error) { } // or, create new one and save it - ln, err := net.Listen(proto, addr) + ln, err := net.Listen(network, addr) if err != nil { return nil, err } diff --git a/modules.go b/modules.go index 4a396d4..efd5d4a 100644 --- a/modules.go +++ b/modules.go @@ -43,7 +43,7 @@ func GetModule(name string) (Module, error) { // GetModules returns all modules in the given scope/namespace. // For example, a scope of "foo" returns modules named "foo.bar", -// "foo.lor", but not "bar", "foo.bar.lor", etc. An empty scope +// "foo.lee", but not "bar", "foo.bar.lee", etc. An empty scope // returns top-level modules, for example "foo" or "bar". Partial // scopes are not matched (i.e. scope "foo.ba" does not match // name "foo.bar"). @@ -111,9 +111,16 @@ func Modules() []string { // value, it is converted to one so that it is unmarshaled // into the underlying concrete type. If mod.New is nil, an // error is returned. -func LoadModule(mod Module, rawMsg json.RawMessage) (interface{}, error) { +func LoadModule(name string, rawMsg json.RawMessage) (interface{}, error) { + modulesMu.Lock() + mod, ok := modules[name] + modulesMu.Unlock() + if !ok { + return nil, fmt.Errorf("unknown module: %s", name) + } + if mod.New == nil { - return nil, fmt.Errorf("no constructor") + return nil, fmt.Errorf("module '%s' has no constructor", mod.Name) } val, err := mod.New() @@ -134,6 +141,35 @@ func LoadModule(mod Module, rawMsg json.RawMessage) (interface{}, error) { return val, nil } +// LoadModuleInlineName loads a module from a JSON raw message which +// decodes to a map[string]interface{}, and where one of the keys is +// "_module", which indicates the module name and which be found in +// the given scope. +// +// This allows modules to be decoded into their concrete types and +// used when their names cannot be the unique key in a map, such as +// when there are multiple instances in the map or it appears in an +// array (where there are no custom keys). +func LoadModuleInlineName(moduleScope string, raw json.RawMessage) (interface{}, error) { + var tmp map[string]interface{} + err := json.Unmarshal(raw, &tmp) + if err != nil { + return nil, err + } + + moduleName, ok := tmp["_module"].(string) + if !ok || moduleName == "" { + return nil, fmt.Errorf("module name not specified") + } + + val, err := LoadModule(moduleScope+"."+moduleName, raw) + if err != nil { + return nil, fmt.Errorf("loading module '%s': %v", moduleName, err) + } + + return val, nil +} + var ( modules = make(map[string]Module) modulesMu sync.Mutex diff --git a/modules/caddyhttp/caddyhttp.go b/modules/caddyhttp/caddyhttp.go index 529c1f7..059af62 100644 --- a/modules/caddyhttp/caddyhttp.go +++ b/modules/caddyhttp/caddyhttp.go @@ -2,6 +2,7 @@ package caddyhttp import ( "context" + "encoding/json" "fmt" "log" "net" @@ -30,23 +31,56 @@ type httpModuleConfig struct { } func (hc *httpModuleConfig) Run() error { - // fmt.Printf("RUNNING: %#v\n", hc) + // TODO: Either prevent overlapping listeners on different servers, or combine them into one + + // TODO: A way to loop requests back through, so have them start the matching over again, but keeping any mutations for _, srv := range hc.Servers { + // set up the routes + for i, route := range srv.Routes { + // matchers + for modName, rawMsg := range route.Matchers { + val, err := caddy2.LoadModule("http.matchers."+modName, rawMsg) + if err != nil { + return fmt.Errorf("loading matcher module '%s': %v", modName, err) + } + srv.Routes[i].matchers = append(srv.Routes[i].matchers, val.(RouteMatcher)) + } + + // middleware + for j, rawMsg := range route.Apply { + mid, err := caddy2.LoadModuleInlineName("http.middleware", rawMsg) + if err != nil { + return fmt.Errorf("loading middleware module in position %d: %v", j, err) + } + srv.Routes[i].middleware = append(srv.Routes[i].middleware, mid.(MiddlewareHandler)) + } + + // responder + if route.Respond != nil { + resp, err := caddy2.LoadModuleInlineName("http.responders", route.Respond) + if err != nil { + return fmt.Errorf("loading responder module: %v", err) + } + srv.Routes[i].responder = resp.(Handler) + } + } + s := &http.Server{ ReadTimeout: time.Duration(srv.ReadTimeout), ReadHeaderTimeout: time.Duration(srv.ReadHeaderTimeout), + Handler: srv, } for _, lnAddr := range srv.Listen { - proto, addrs, err := parseListenAddr(lnAddr) + network, addrs, err := parseListenAddr(lnAddr) if err != nil { return fmt.Errorf("parsing listen address '%s': %v", lnAddr, err) } for _, addr := range addrs { - ln, err := caddy2.Listen(proto, addr) + ln, err := caddy2.Listen(network, addr) if err != nil { - return fmt.Errorf("%s: listening on %s: %v", proto, addr, err) + return fmt.Errorf("%s: listening on %s: %v", network, addr, err) } go s.Serve(ln) hc.servers = append(hc.servers, s) @@ -67,10 +101,117 @@ func (hc *httpModuleConfig) Cancel() error { return nil } -func parseListenAddr(a string) (proto string, addrs []string, err error) { - proto = "tcp" +type httpServerConfig struct { + Listen []string `json:"listen"` + ReadTimeout caddy2.Duration `json:"read_timeout"` + ReadHeaderTimeout caddy2.Duration `json:"read_header_timeout"` + HiddenFiles []string `json:"hidden_files"` // TODO:... experimenting with shared/common state + Routes []serverRoute `json:"routes"` +} + +func (s httpServerConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) { + var mid []Middleware // TODO: see about using make() for performance reasons + var responder Handler + mrw := &middlewareResponseWriter{ResponseWriterWrapper: &ResponseWriterWrapper{w}} + + for _, route := range s.Routes { + matched := len(route.matchers) == 0 + for _, m := range route.matchers { + if m.Match(r) { + matched = true + break + } + } + if !matched { + continue + } + for _, m := range route.middleware { + mid = append(mid, func(next HandlerFunc) HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) error { + return m.ServeHTTP(mrw, r, next) + } + }) + } + if responder == nil { + responder = route.responder + } + } + + // build the middleware stack, with the responder at the end + stack := HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { + if responder == nil { + return nil + } + mrw.allowWrites = true + return responder.ServeHTTP(w, r) + }) + for i := len(mid) - 1; i >= 0; i-- { + stack = mid[i](stack) + } + + err := stack.ServeHTTP(w, r) + if err != nil { + // TODO: error handling + log.Printf("[ERROR] TODO: error handling: %v", err) + } +} + +type serverRoute struct { + Matchers map[string]json.RawMessage `json:"match"` + Apply []json.RawMessage `json:"apply"` + Respond json.RawMessage `json:"respond"` + + // decoded values + matchers []RouteMatcher + middleware []MiddlewareHandler + responder Handler +} + +// RouteMatcher is a type that can match to a request. +// A route matcher MUST NOT modify the request. +type RouteMatcher interface { + Match(*http.Request) bool +} + +// Middleware chains one Handler to the next by being passed +// the next Handler in the chain. +type Middleware func(HandlerFunc) HandlerFunc + +// MiddlewareHandler is a Handler that includes a reference +// to the next middleware handler in the chain. Middleware +// handlers MUST NOT call Write() or WriteHeader() on the +// response writer; doing so will panic. See Handler godoc +// for more information. +type MiddlewareHandler interface { + ServeHTTP(http.ResponseWriter, *http.Request, Handler) error +} + +// Handler is like http.Handler except ServeHTTP may return an error. +// +// Middleware and responder handlers both implement this method. +// Middleware must not call Write or WriteHeader on the ResponseWriter; +// doing so will cause a panic. Responders should write to the response +// if there was not an error. +// +// If any handler encounters an error, it should be returned for proper +// handling. Return values should be propagated down the middleware chain +// by returning it unchanged. +type Handler interface { + ServeHTTP(http.ResponseWriter, *http.Request) error +} + +// HandlerFunc is a convenience type like http.HandlerFunc. +type HandlerFunc func(http.ResponseWriter, *http.Request) error + +// ServeHTTP implements the Handler interface. +func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error { + return f(w, r) +} + +func parseListenAddr(a string) (network string, addrs []string, err error) { + network = "tcp" if idx := strings.Index(a, "/"); idx >= 0 { - proto = strings.ToLower(strings.TrimSpace(a[:idx])) + network = strings.ToLower(strings.TrimSpace(a[:idx])) a = a[idx+1:] } var host, port string @@ -101,8 +242,24 @@ func parseListenAddr(a string) (proto string, addrs []string, err error) { return } -type httpServerConfig struct { - Listen []string `json:"listen"` - ReadTimeout caddy2.Duration `json:"read_timeout"` - ReadHeaderTimeout caddy2.Duration `json:"read_header_timeout"` +type middlewareResponseWriter struct { + *ResponseWriterWrapper + allowWrites bool } + +func (mrw middlewareResponseWriter) WriteHeader(statusCode int) { + if !mrw.allowWrites { + panic("WriteHeader: middleware cannot write to the response") + } + mrw.ResponseWriterWrapper.WriteHeader(statusCode) +} + +func (mrw middlewareResponseWriter) Write(b []byte) (int, error) { + if !mrw.allowWrites { + panic("Write: middleware cannot write to the response") + } + return mrw.ResponseWriterWrapper.Write(b) +} + +// Interface guards +var _ HTTPInterfaces = middlewareResponseWriter{} diff --git a/modules/caddyhttp/caddylog/log.go b/modules/caddyhttp/caddylog/log.go new file mode 100644 index 0000000..5bbab2a --- /dev/null +++ b/modules/caddyhttp/caddylog/log.go @@ -0,0 +1,37 @@ +package caddylog + +import ( + "log" + "net/http" + "time" + + "bitbucket.org/lightcodelabs/caddy2" + "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp" +) + +func init() { + caddy2.RegisterModule(caddy2.Module{ + Name: "http.middleware.log", + New: func() (interface{}, error) { return &Log{}, nil }, + }) +} + +// Log implements a simple logging middleware. +type Log struct { + Filename string +} + +func (l *Log) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { + start := time.Now() + + if err := next.ServeHTTP(w, r); err != nil { + return err + } + + log.Println("latency:", time.Now().Sub(start)) + + return nil +} + +// Interface guard +var _ caddyhttp.MiddlewareHandler = &Log{} diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go new file mode 100644 index 0000000..59f1838 --- /dev/null +++ b/modules/caddyhttp/matchers.go @@ -0,0 +1,102 @@ +package caddyhttp + +import ( + "net/http" + "strings" + + "bitbucket.org/lightcodelabs/caddy2" +) + +func init() { + caddy2.RegisterModule(caddy2.Module{ + Name: "http.matchers.host", + New: func() (interface{}, error) { return matchHost{}, nil }, + }) + caddy2.RegisterModule(caddy2.Module{ + Name: "http.matchers.path", + New: func() (interface{}, error) { return matchPath{}, nil }, + }) + caddy2.RegisterModule(caddy2.Module{ + Name: "http.matchers.method", + New: func() (interface{}, error) { return matchMethod{}, nil }, + }) + caddy2.RegisterModule(caddy2.Module{ + Name: "http.matchers.query", + New: func() (interface{}, error) { return matchQuery{}, nil }, + }) + caddy2.RegisterModule(caddy2.Module{ + Name: "http.matchers.header", + New: func() (interface{}, error) { return matchHeader{}, nil }, + }) +} + +// TODO: Matchers should probably support regex of some sort... performance trade-offs? + +type ( + matchHost []string + matchPath []string + matchMethod []string + matchQuery map[string][]string + matchHeader map[string][]string +) + +func (m matchHost) Match(r *http.Request) bool { + for _, host := range m { + if r.Host == host { + return true + } + } + return false +} + +func (m matchPath) Match(r *http.Request) bool { + for _, path := range m { + if strings.HasPrefix(r.URL.Path, path) { + return true + } + } + return false +} + +func (m matchMethod) Match(r *http.Request) bool { + for _, method := range m { + if r.Method == method { + return true + } + } + return false +} + +func (m matchQuery) Match(r *http.Request) bool { + for param, vals := range m { + paramVal := r.URL.Query().Get(param) + for _, v := range vals { + if paramVal == v { + return true + } + } + } + return false +} + +func (m matchHeader) Match(r *http.Request) bool { + for field, vals := range m { + fieldVals := r.Header[field] + for _, fieldVal := range fieldVals { + for _, v := range vals { + if fieldVal == v { + return true + } + } + } + } + return false +} + +var ( + _ RouteMatcher = matchHost{} + _ RouteMatcher = matchPath{} + _ RouteMatcher = matchMethod{} + _ RouteMatcher = matchQuery{} + _ RouteMatcher = matchHeader{} +) diff --git a/modules/caddyhttp/responsewriter.go b/modules/caddyhttp/responsewriter.go new file mode 100644 index 0000000..587d9f9 --- /dev/null +++ b/modules/caddyhttp/responsewriter.go @@ -0,0 +1,68 @@ +package caddyhttp + +import ( + "bufio" + "fmt" + "net" + "net/http" +) + +// ResponseWriterWrapper wraps an underlying ResponseWriter and +// promotes its Pusher/Flusher/CloseNotifier/Hijacker methods +// as well. To use this type, embed a pointer to it within your +// own struct type that implements the http.ResponseWriter +// interface, then call methods on the embedded value. You can +// make sure your type wraps correctly by asserting that it +// implements the HTTPInterfaces interface. +type ResponseWriterWrapper struct { + http.ResponseWriter +} + +// Hijack implements http.Hijacker. It simply calls the underlying +// ResponseWriter's Hijack method if there is one, or returns an error. +func (rww *ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) { + if hj, ok := rww.ResponseWriter.(http.Hijacker); ok { + return hj.Hijack() + } + return nil, nil, fmt.Errorf("not a hijacker") +} + +// Flush implements http.Flusher. It simply calls the underlying +// ResponseWriter's Flush method if there is one, or panics. +func (rww *ResponseWriterWrapper) Flush() { + if f, ok := rww.ResponseWriter.(http.Flusher); ok { + f.Flush() + } else { + panic("not a flusher") + } +} + +// CloseNotify implements http.CloseNotifier. It simply calls the underlying +// ResponseWriter's CloseNotify method if there is one, or panics. +func (rww *ResponseWriterWrapper) CloseNotify() <-chan bool { + if cn, ok := rww.ResponseWriter.(http.CloseNotifier); ok { + return cn.CloseNotify() + } + panic("not a close notifier") +} + +// Push implements http.Pusher. It simply calls the underlying +// ResponseWriter's Push method if there is one, or returns an error. +func (rww *ResponseWriterWrapper) Push(target string, opts *http.PushOptions) error { + if pusher, hasPusher := rww.ResponseWriter.(http.Pusher); hasPusher { + return pusher.Push(target, opts) + } + return fmt.Errorf("not a pusher") +} + +// HTTPInterfaces mix all the interfaces that middleware ResponseWriters need to support. +type HTTPInterfaces interface { + http.ResponseWriter + http.Pusher + http.Flusher + http.CloseNotifier + http.Hijacker +} + +// Interface guards +var _ HTTPInterfaces = (*ResponseWriterWrapper)(nil) diff --git a/modules/caddyhttp/staticfiles/staticfiles.go b/modules/caddyhttp/staticfiles/staticfiles.go new file mode 100644 index 0000000..d1a7a7e --- /dev/null +++ b/modules/caddyhttp/staticfiles/staticfiles.go @@ -0,0 +1,28 @@ +package staticfiles + +import ( + "net/http" + + "bitbucket.org/lightcodelabs/caddy2" + "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp" +) + +func init() { + caddy2.RegisterModule(caddy2.Module{ + Name: "http.responders.static_files", + New: func() (interface{}, error) { return &StaticFiles{}, nil }, + }) +} + +// StaticFiles implements a static file server responder for Caddy. +type StaticFiles struct { + Root string +} + +func (sf StaticFiles) ServeHTTP(w http.ResponseWriter, r *http.Request) error { + http.FileServer(http.Dir(sf.Root)).ServeHTTP(w, r) + return nil +} + +// Interface guard +var _ caddyhttp.Handler = StaticFiles{} -- cgit v1.2.3