Commit 1ef7f3c4 authored by Matthew Holt's avatar Matthew Holt

Remove path scoping for middleware slice

It was implemented for almost a year but we'll probably never use it, especially since we'll match more than the path in the future.
parent f25ae823
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"github.com/mholt/caddy/caddy/https" "github.com/mholt/caddy/caddy/https"
"github.com/mholt/caddy/caddy/parse" "github.com/mholt/caddy/caddy/parse"
"github.com/mholt/caddy/caddy/setup" "github.com/mholt/caddy/caddy/setup"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/server" "github.com/mholt/caddy/server"
) )
...@@ -55,7 +54,6 @@ func loadConfigsUpToIncludingTLS(filename string, input io.Reader) ([]server.Con ...@@ -55,7 +54,6 @@ func loadConfigsUpToIncludingTLS(filename string, input io.Reader) ([]server.Con
Port: addr.Port, Port: addr.Port,
Scheme: addr.Scheme, Scheme: addr.Scheme,
Root: Root, Root: Root,
Middleware: make(map[string][]middleware.Middleware),
ConfigFile: filename, ConfigFile: filename,
AppName: AppName, AppName: AppName,
AppVersion: AppVersion, AppVersion: AppVersion,
...@@ -89,8 +87,7 @@ func loadConfigsUpToIncludingTLS(filename string, input io.Reader) ([]server.Con ...@@ -89,8 +87,7 @@ func loadConfigsUpToIncludingTLS(filename string, input io.Reader) ([]server.Con
return nil, nil, lastDirectiveIndex, err return nil, nil, lastDirectiveIndex, err
} }
if midware != nil { if midware != nil {
// TODO: For now, we only support the default path scope / config.Middleware = append(config.Middleware, midware)
config.Middleware["/"] = append(config.Middleware["/"], midware)
} }
storages[dir.name] = controller.ServerBlockStorage // persist for this server block storages[dir.name] = controller.ServerBlockStorage // persist for this server block
} }
...@@ -171,8 +168,7 @@ func loadConfigs(filename string, input io.Reader) ([]server.Config, error) { ...@@ -171,8 +168,7 @@ func loadConfigs(filename string, input io.Reader) ([]server.Config, error) {
return nil, err return nil, err
} }
if midware != nil { if midware != nil {
// TODO: For now, we only support the default path scope / configs[configIndex].Middleware = append(configs[configIndex].Middleware, midware)
configs[configIndex].Middleware["/"] = append(configs[configIndex].Middleware["/"], midware)
} }
storages[dir.name] = controller.ServerBlockStorage // persist for this server block storages[dir.name] = controller.ServerBlockStorage // persist for this server block
} }
......
...@@ -332,12 +332,10 @@ func redirPlaintextHost(cfg server.Config) server.Config { ...@@ -332,12 +332,10 @@ func redirPlaintextHost(cfg server.Config) server.Config {
} }
return server.Config{ return server.Config{
Host: cfg.Host, Host: cfg.Host,
BindHost: cfg.BindHost, BindHost: cfg.BindHost,
Port: "80", Port: "80",
Middleware: map[string][]middleware.Middleware{ Middleware: []middleware.Middleware{redirMidware},
"/": {redirMidware},
},
} }
} }
......
...@@ -87,11 +87,11 @@ func TestRedirPlaintextHost(t *testing.T) { ...@@ -87,11 +87,11 @@ func TestRedirPlaintextHost(t *testing.T) {
} }
// Make sure redirect handler is set up properly // Make sure redirect handler is set up properly
if cfg.Middleware == nil || len(cfg.Middleware["/"]) != 1 { if cfg.Middleware == nil || len(cfg.Middleware) != 1 {
t.Fatalf("Redir config middleware not set up properly; got: %#v", cfg.Middleware) t.Fatalf("Redir config middleware not set up properly; got: %#v", cfg.Middleware)
} }
handler, ok := cfg.Middleware["/"][0](nil).(redirect.Redirect) handler, ok := cfg.Middleware[0](nil).(redirect.Redirect)
if !ok { if !ok {
t.Fatalf("Expected a redirect.Redirect middleware, but got: %#v", handler) t.Fatalf("Expected a redirect.Redirect middleware, but got: %#v", handler)
} }
...@@ -116,7 +116,7 @@ func TestRedirPlaintextHost(t *testing.T) { ...@@ -116,7 +116,7 @@ func TestRedirPlaintextHost(t *testing.T) {
// browsers can infer a default port from scheme, so make sure the port // browsers can infer a default port from scheme, so make sure the port
// doesn't get added in explicitly for default ports like 443 for https. // doesn't get added in explicitly for default ports like 443 for https.
cfg = redirPlaintextHost(server.Config{Host: "example.com", Port: "443"}) cfg = redirPlaintextHost(server.Config{Host: "example.com", Port: "443"})
handler, ok = cfg.Middleware["/"][0](nil).(redirect.Redirect) handler, ok = cfg.Middleware[0](nil).(redirect.Redirect)
if actual, expected := handler.Rules[0].To, "https://{host}{uri}"; actual != expected { if actual, expected := handler.Rules[0].To, "https://{host}{uri}"; actual != expected {
t.Errorf("(Default Port) Expected redirect rule to be to URL '%s' but is actually to '%s'", expected, actual) t.Errorf("(Default Port) Expected redirect rule to be to URL '%s' but is actually to '%s'", expected, actual)
} }
......
...@@ -26,8 +26,8 @@ type Config struct { ...@@ -26,8 +26,8 @@ type Config struct {
// HTTPS configuration // HTTPS configuration
TLS TLSConfig TLS TLSConfig
// Middleware stack; map of path scope to middleware -- TODO: Support path scope? // Middleware stack
Middleware map[string][]middleware.Middleware Middleware []middleware.Middleware
// Startup is a list of functions (or methods) to execute at // Startup is a list of functions (or methods) to execute at
// server startup and restart; these are executed before any // server startup and restart; these are executed before any
......
...@@ -21,13 +21,7 @@ type virtualHost struct { ...@@ -21,13 +21,7 @@ type virtualHost struct {
// ListenAndServe begins. // ListenAndServe begins.
func (vh *virtualHost) buildStack() error { func (vh *virtualHost) buildStack() error {
vh.fileServer = middleware.FileServer(http.Dir(vh.config.Root), []string{vh.config.ConfigFile}) vh.fileServer = middleware.FileServer(http.Dir(vh.config.Root), []string{vh.config.ConfigFile})
vh.compile(vh.config.Middleware)
// TODO: We only compile middleware for the "/" scope.
// Partial support for multiple location contexts already
// exists at the parser and config levels, but until full
// support is implemented, this is all we do right here.
vh.compile(vh.config.Middleware["/"])
return nil return nil
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment