From 937654d1e0d39731a20844ce33c6336813117306 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 20 Jun 2016 18:25:31 -0600 Subject: [PATCH] Set host and port on address if specified via flag (fixes #888) Also fixed a few typos and renamed caddyfile.ServerBlocks() to caddyfile.Parse(). --- caddy.go | 4 ++-- caddyfile/json.go | 2 +- caddyfile/parse.go | 6 +++--- caddyhttp/httpserver/plugin.go | 14 +++++++++++++- caddyhttp/httpserver/plugin_test.go | 26 +++++++++++++++++++++++++- caddyhttp/redirect/setup_test.go | 4 ++-- caddytls/handshake.go | 2 +- controller.go | 11 +++++++---- 8 files changed, 54 insertions(+), 15 deletions(-) diff --git a/caddy.go b/caddy.go index e011be8e..c52891af 100644 --- a/caddy.go +++ b/caddy.go @@ -591,13 +591,13 @@ func getServerType(serverType string) (ServerType, error) { func loadServerBlocks(serverType, filename string, input io.Reader) ([]caddyfile.ServerBlock, error) { validDirectives := ValidDirectives(serverType) - serverBlocks, err := caddyfile.ServerBlocks(filename, input, validDirectives) + serverBlocks, err := caddyfile.Parse(filename, input, validDirectives) if err != nil { return nil, err } if len(serverBlocks) == 0 && serverTypes[serverType].DefaultInput != nil { newInput := serverTypes[serverType].DefaultInput() - serverBlocks, err = caddyfile.ServerBlocks(newInput.Path(), + serverBlocks, err = caddyfile.Parse(newInput.Path(), bytes.NewReader(newInput.Body()), validDirectives) if err != nil { return nil, err diff --git a/caddyfile/json.go b/caddyfile/json.go index 52c7b90f..16aab4e3 100644 --- a/caddyfile/json.go +++ b/caddyfile/json.go @@ -15,7 +15,7 @@ const filename = "Caddyfile" func ToJSON(caddyfile []byte) ([]byte, error) { var j EncodedCaddyfile - serverBlocks, err := ServerBlocks(filename, bytes.NewReader(caddyfile), nil) + serverBlocks, err := Parse(filename, bytes.NewReader(caddyfile), nil) if err != nil { return nil, err } diff --git a/caddyfile/parse.go b/caddyfile/parse.go index b3b851d5..6d8f1588 100644 --- a/caddyfile/parse.go +++ b/caddyfile/parse.go @@ -7,13 +7,13 @@ import ( "strings" ) -// ServerBlocks parses the input just enough to group tokens, -// in order, by server block. No further parsing is performed. +// Parse parses the input just enough to group tokens, in +// order, by server block. No further parsing is performed. // Server blocks are returned in the order in which they appear. // Directives that do not appear in validDirectives will cause // an error. If you do not want to check for valid directives, // pass in nil instead. -func ServerBlocks(filename string, input io.Reader, validDirectives []string) ([]ServerBlock, error) { +func Parse(filename string, input io.Reader, validDirectives []string) ([]ServerBlock, error) { p := parser{Dispenser: NewDispenser(filename, input), validDirectives: validDirectives} blocks, err := p.parseAll() return blocks, err diff --git a/caddyhttp/httpserver/plugin.go b/caddyhttp/httpserver/plugin.go index 5965930a..47ce4a00 100644 --- a/caddyhttp/httpserver/plugin.go +++ b/caddyhttp/httpserver/plugin.go @@ -82,6 +82,16 @@ func (h *httpContext) InspectServerBlocks(sourceFile string, serverBlocks []cadd if err != nil { return serverBlocks, err } + + // Fill in address components from command line so that middleware + // have access to the correct information during setup + if addr.Host == "" && Host != DefaultHost { + addr.Host = Host + } + if addr.Port == "" && Port != DefaultPort { + addr.Port = Port + } + // Save the config to our master list, and key it for lookups cfg := &SiteConfig{ Addr: addr, @@ -222,7 +232,9 @@ func (sc *SiteConfig) AddMiddleware(m Middleware) { // Address represents a site address. It contains // the original input value, and the component -// parts of an address. +// parts of an address. The component parts may be +// updated to the correct values as setup proceeds, +// but the original value should never be changed. type Address struct { Original, Scheme, Host, Port, Path string } diff --git a/caddyhttp/httpserver/plugin_test.go b/caddyhttp/httpserver/plugin_test.go index 9959ce9b..d5a7bf7e 100644 --- a/caddyhttp/httpserver/plugin_test.go +++ b/caddyhttp/httpserver/plugin_test.go @@ -1,6 +1,11 @@ package httpserver -import "testing" +import ( + "strings" + "testing" + + "github.com/mholt/caddy/caddyfile" +) func TestStandardizeAddress(t *testing.T) { for i, test := range []struct { @@ -112,3 +117,22 @@ func TestAddressString(t *testing.T) { } } } + +func TestInspectServerBlocksWithCustomDefaultPort(t *testing.T) { + Port = "9999" + filename := "Testfile" + ctx := newContext().(*httpContext) + input := strings.NewReader(`localhost`) + sblocks, err := caddyfile.Parse(filename, input, nil) + if err != nil { + t.Fatalf("Expected no error setting up test, got: %v", err) + } + _, err = ctx.InspectServerBlocks(filename, sblocks) + if err != nil { + t.Fatalf("Didn't expect an error, but got: %v", err) + } + addr := ctx.keysToSiteConfigs["localhost"].Addr + if addr.Port != Port { + t.Errorf("Expected the port on the address to be set, but got: %#v", addr) + } +} diff --git a/caddyhttp/redirect/setup_test.go b/caddyhttp/redirect/setup_test.go index e0c5fde9..4ebea3e8 100644 --- a/caddyhttp/redirect/setup_test.go +++ b/caddyhttp/redirect/setup_test.go @@ -20,10 +20,10 @@ func TestSetup(t *testing.T) { // test case #1 tests the recognition of an invalid HTTP status code defined outside of block statement {"redir 9000 {\n/ /foo\n}", true, []Rule{{}}}, - // test case #2 tests the detection of a valid HTTP status code outside of a block statement being overriden by an invalid HTTP status code inside statement of a block statement + // test case #2 tests the detection of a valid HTTP status code outside of a block statement being overridden by an invalid HTTP status code inside statement of a block statement {"redir 300 {\n/ /foo 9000\n}", true, []Rule{{}}}, - // test case #3 tests the detection of an invalid HTTP status code outside of a block statement being overriden by a valid HTTP status code inside statement of a block statement + // test case #3 tests the detection of an invalid HTTP status code outside of a block statement being overridden by a valid HTTP status code inside statement of a block statement {"redir 9000 {\n/ /foo 300\n}", true, []Rule{{}}}, // test case #4 tests the recognition of a TO redirection in a block statement.The HTTP status code is set to the default of 301 - MovedPermanently diff --git a/caddytls/handshake.go b/caddytls/handshake.go index f389dd7a..65041b88 100644 --- a/caddytls/handshake.go +++ b/caddytls/handshake.go @@ -53,7 +53,7 @@ func (cg configGroup) getConfig(name string) *Config { // GetCertificate gets a certificate to satisfy clientHello. In getting // the certificate, it abides the rules and settings defined in the // Config that matches clientHello.ServerName. It first checks the in- -// memory cache, then, if the config enables "OnDemand", it accessses +// memory cache, then, if the config enables "OnDemand", it accesses // disk, then accesses the network if it must obtain a new certificate // via ACME. // diff --git a/controller.go b/controller.go index 0b8b7bc8..3c25e487 100644 --- a/controller.go +++ b/controller.go @@ -78,11 +78,14 @@ func (c *Controller) Context() Context { } // NewTestController creates a new Controller for -// the input specified, with a filename of "Testfile". -// The Config is bare, consisting only of a Root of cwd. +// the server type and input specified. The filename +// is "Testfile". If the server type is not empty and +// is plugged in, a context will be created so that +// the results of setup functions can be checked for +// correctness. // -// Used primarily for testing but needs to be exported so -// add-ons can use this as a convenience. +// Used only for testing, but exported so plugins can +// use this for convenience. func NewTestController(serverType, input string) *Controller { var ctx Context if stype, err := getServerType(serverType); err == nil {