mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-28 14:43:48 +03:00
Set host and port on address if specified via flag (fixes #888)
Also fixed a few typos and renamed caddyfile.ServerBlocks() to caddyfile.Parse().
This commit is contained in:
parent
33aba7eb91
commit
937654d1e0
8 changed files with 54 additions and 15 deletions
4
caddy.go
4
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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
//
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue