mirror of
https://github.com/caddyserver/caddy.git
synced 2025-03-30 17:09:05 +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) {
|
func loadServerBlocks(serverType, filename string, input io.Reader) ([]caddyfile.ServerBlock, error) {
|
||||||
validDirectives := ValidDirectives(serverType)
|
validDirectives := ValidDirectives(serverType)
|
||||||
serverBlocks, err := caddyfile.ServerBlocks(filename, input, validDirectives)
|
serverBlocks, err := caddyfile.Parse(filename, input, validDirectives)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(serverBlocks) == 0 && serverTypes[serverType].DefaultInput != nil {
|
if len(serverBlocks) == 0 && serverTypes[serverType].DefaultInput != nil {
|
||||||
newInput := serverTypes[serverType].DefaultInput()
|
newInput := serverTypes[serverType].DefaultInput()
|
||||||
serverBlocks, err = caddyfile.ServerBlocks(newInput.Path(),
|
serverBlocks, err = caddyfile.Parse(newInput.Path(),
|
||||||
bytes.NewReader(newInput.Body()), validDirectives)
|
bytes.NewReader(newInput.Body()), validDirectives)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -15,7 +15,7 @@ const filename = "Caddyfile"
|
||||||
func ToJSON(caddyfile []byte) ([]byte, error) {
|
func ToJSON(caddyfile []byte) ([]byte, error) {
|
||||||
var j EncodedCaddyfile
|
var j EncodedCaddyfile
|
||||||
|
|
||||||
serverBlocks, err := ServerBlocks(filename, bytes.NewReader(caddyfile), nil)
|
serverBlocks, err := Parse(filename, bytes.NewReader(caddyfile), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,13 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ServerBlocks parses the input just enough to group tokens,
|
// Parse parses the input just enough to group tokens, in
|
||||||
// in order, by server block. No further parsing is performed.
|
// order, by server block. No further parsing is performed.
|
||||||
// Server blocks are returned in the order in which they appear.
|
// Server blocks are returned in the order in which they appear.
|
||||||
// Directives that do not appear in validDirectives will cause
|
// Directives that do not appear in validDirectives will cause
|
||||||
// an error. If you do not want to check for valid directives,
|
// an error. If you do not want to check for valid directives,
|
||||||
// pass in nil instead.
|
// 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}
|
p := parser{Dispenser: NewDispenser(filename, input), validDirectives: validDirectives}
|
||||||
blocks, err := p.parseAll()
|
blocks, err := p.parseAll()
|
||||||
return blocks, err
|
return blocks, err
|
||||||
|
|
|
@ -82,6 +82,16 @@ func (h *httpContext) InspectServerBlocks(sourceFile string, serverBlocks []cadd
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serverBlocks, err
|
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
|
// Save the config to our master list, and key it for lookups
|
||||||
cfg := &SiteConfig{
|
cfg := &SiteConfig{
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
|
@ -222,7 +232,9 @@ func (sc *SiteConfig) AddMiddleware(m Middleware) {
|
||||||
|
|
||||||
// Address represents a site address. It contains
|
// Address represents a site address. It contains
|
||||||
// the original input value, and the component
|
// 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 {
|
type Address struct {
|
||||||
Original, Scheme, Host, Port, Path string
|
Original, Scheme, Host, Port, Path string
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
package httpserver
|
package httpserver
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mholt/caddy/caddyfile"
|
||||||
|
)
|
||||||
|
|
||||||
func TestStandardizeAddress(t *testing.T) {
|
func TestStandardizeAddress(t *testing.T) {
|
||||||
for i, test := range []struct {
|
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
|
// test case #1 tests the recognition of an invalid HTTP status code defined outside of block statement
|
||||||
{"redir 9000 {\n/ /foo\n}", true, []Rule{{}}},
|
{"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{{}}},
|
{"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{{}}},
|
{"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
|
// 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
|
// GetCertificate gets a certificate to satisfy clientHello. In getting
|
||||||
// the certificate, it abides the rules and settings defined in the
|
// the certificate, it abides the rules and settings defined in the
|
||||||
// Config that matches clientHello.ServerName. It first checks the in-
|
// 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
|
// disk, then accesses the network if it must obtain a new certificate
|
||||||
// via ACME.
|
// via ACME.
|
||||||
//
|
//
|
||||||
|
|
|
@ -78,11 +78,14 @@ func (c *Controller) Context() Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTestController creates a new Controller for
|
// NewTestController creates a new Controller for
|
||||||
// the input specified, with a filename of "Testfile".
|
// the server type and input specified. The filename
|
||||||
// The Config is bare, consisting only of a Root of cwd.
|
// 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
|
// Used only for testing, but exported so plugins can
|
||||||
// add-ons can use this as a convenience.
|
// use this for convenience.
|
||||||
func NewTestController(serverType, input string) *Controller {
|
func NewTestController(serverType, input string) *Controller {
|
||||||
var ctx Context
|
var ctx Context
|
||||||
if stype, err := getServerType(serverType); err == nil {
|
if stype, err := getServerType(serverType); err == nil {
|
||||||
|
|
Loading…
Reference in a new issue