2015-01-13 14:43:45 -05:00
|
|
|
package config
|
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
import (
|
2015-05-21 01:06:53 -05:00
|
|
|
"fmt"
|
2015-05-04 07:53:54 -05:00
|
|
|
"io"
|
2015-04-16 00:17:56 -05:00
|
|
|
"log"
|
2015-05-21 01:06:53 -05:00
|
|
|
"net"
|
2015-10-15 00:45:28 -05:00
|
|
|
"sync"
|
2015-01-19 01:11:21 -05:00
|
|
|
|
2015-05-21 01:06:53 -05:00
|
|
|
"github.com/mholt/caddy/app"
|
2015-05-04 07:53:54 -05:00
|
|
|
"github.com/mholt/caddy/config/parse"
|
|
|
|
"github.com/mholt/caddy/config/setup"
|
2015-01-19 01:11:21 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-04 07:53:54 -05:00
|
|
|
"github.com/mholt/caddy/server"
|
2015-01-19 01:11:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-04-29 23:30:03 -05:00
|
|
|
DefaultHost = "0.0.0.0"
|
2015-04-28 23:13:00 -05:00
|
|
|
DefaultPort = "2015"
|
|
|
|
DefaultRoot = "."
|
2015-04-15 15:11:32 -05:00
|
|
|
|
2015-05-24 21:52:34 -05:00
|
|
|
// DefaultConfigFile is the name of the configuration file that is loaded
|
|
|
|
// by default if no other file is specified.
|
2015-04-15 15:11:32 -05:00
|
|
|
DefaultConfigFile = "Caddyfile"
|
2015-01-19 01:11:21 -05:00
|
|
|
)
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
// Load reads input (named filename) and parses it, returning server
|
|
|
|
// configurations grouped by listening address.
|
|
|
|
func Load(filename string, input io.Reader) (Group, error) {
|
2015-05-04 07:53:54 -05:00
|
|
|
var configs []server.Config
|
2015-01-21 15:19:25 -05:00
|
|
|
|
2015-04-16 00:17:56 -05:00
|
|
|
// turn off timestamp for parsing
|
|
|
|
flags := log.Flags()
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
serverBlocks, err := parse.ServerBlocks(filename, input)
|
2015-01-21 15:19:25 -05:00
|
|
|
if err != nil {
|
2015-08-01 14:08:31 -05:00
|
|
|
return nil, err
|
2015-01-21 15:19:25 -05:00
|
|
|
}
|
2015-08-03 18:31:10 -05:00
|
|
|
if len(serverBlocks) == 0 {
|
|
|
|
return Default()
|
|
|
|
}
|
2015-01-21 15:19:25 -05:00
|
|
|
|
2015-10-15 00:45:28 -05:00
|
|
|
// Each server block represents similar hosts/addresses.
|
2015-05-04 07:53:54 -05:00
|
|
|
// Iterate each server block and make a config for each one,
|
|
|
|
// executing the directives that were parsed.
|
2015-10-16 00:34:54 -05:00
|
|
|
for i, sb := range serverBlocks {
|
2015-10-15 01:07:26 -05:00
|
|
|
onces := makeOnces()
|
2015-10-17 15:11:32 -05:00
|
|
|
storages := makeStorages()
|
2015-10-15 00:45:28 -05:00
|
|
|
|
2015-10-17 15:11:32 -05:00
|
|
|
for j, addr := range sb.Addresses {
|
2015-10-15 00:45:28 -05:00
|
|
|
config := server.Config{
|
|
|
|
Host: addr.Host,
|
|
|
|
Port: addr.Port,
|
|
|
|
Root: Root,
|
|
|
|
Middleware: make(map[string][]middleware.Middleware),
|
|
|
|
ConfigFile: filename,
|
|
|
|
AppName: app.Name,
|
|
|
|
AppVersion: app.Version,
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is crucial that directives are executed in the proper order.
|
|
|
|
for _, dir := range directiveOrder {
|
|
|
|
// Execute directive if it is in the server block
|
|
|
|
if tokens, ok := sb.Tokens[dir.name]; ok {
|
|
|
|
// Each setup function gets a controller, which is the
|
|
|
|
// server config and the dispenser containing only
|
|
|
|
// this directive's tokens.
|
|
|
|
controller := &setup.Controller{
|
2015-10-15 12:38:17 -05:00
|
|
|
Config: &config,
|
|
|
|
Dispenser: parse.NewDispenserTokens(filename, tokens),
|
|
|
|
OncePerServerBlock: func(f func() error) error {
|
|
|
|
var err error
|
|
|
|
onces[dir.name].Do(func() {
|
|
|
|
err = f()
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
2015-10-17 15:11:32 -05:00
|
|
|
ServerBlockIndex: i,
|
|
|
|
ServerBlockHostIndex: j,
|
|
|
|
ServerBlockHosts: sb.HostList(),
|
|
|
|
ServerBlockStorage: storages[dir.name],
|
2015-10-15 00:45:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
midware, err := dir.setup(controller)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if midware != nil {
|
|
|
|
// TODO: For now, we only support the default path scope /
|
|
|
|
config.Middleware["/"] = append(config.Middleware["/"], midware)
|
|
|
|
}
|
2015-10-18 20:27:51 -05:00
|
|
|
storages[dir.name] = controller.ServerBlockStorage // persist for this server block
|
2015-10-15 00:45:28 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 07:53:54 -05:00
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
if config.Port == "" {
|
|
|
|
config.Port = Port
|
2015-05-04 07:53:54 -05:00
|
|
|
}
|
2015-10-15 00:45:28 -05:00
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
configs = append(configs, config)
|
2015-05-04 07:53:54 -05:00
|
|
|
}
|
2015-04-12 18:44:02 -05:00
|
|
|
}
|
|
|
|
|
2015-04-16 00:17:56 -05:00
|
|
|
// restore logging settings
|
|
|
|
log.SetFlags(flags)
|
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
return arrangeBindings(configs)
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-10-15 01:07:26 -05:00
|
|
|
// makeOnces makes a map of directive name to sync.Once
|
|
|
|
// instance. This is intended to be called once per server
|
|
|
|
// block when setting up configs so that Setup functions
|
|
|
|
// for each directive can perform a task just once per
|
|
|
|
// server block, even if there are multiple hosts on the block.
|
|
|
|
//
|
|
|
|
// We need one Once per directive, otherwise the first
|
|
|
|
// directive to use it would exclude other directives from
|
|
|
|
// using it at all, which would be a bug.
|
|
|
|
func makeOnces() map[string]*sync.Once {
|
|
|
|
onces := make(map[string]*sync.Once)
|
|
|
|
for _, dir := range directiveOrder {
|
|
|
|
onces[dir.name] = new(sync.Once)
|
|
|
|
}
|
|
|
|
return onces
|
|
|
|
}
|
|
|
|
|
2015-10-17 15:11:32 -05:00
|
|
|
// makeStorages makes a map of directive name to interface{}
|
|
|
|
// so that directives' setup functions can persist state
|
|
|
|
// between different hosts on the same server block during the
|
|
|
|
// setup phase.
|
|
|
|
func makeStorages() map[string]interface{} {
|
|
|
|
storages := make(map[string]interface{})
|
|
|
|
for _, dir := range directiveOrder {
|
|
|
|
storages[dir.name] = nil
|
|
|
|
}
|
|
|
|
return storages
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
// arrangeBindings groups configurations by their bind address. For example,
|
2015-05-21 01:06:53 -05:00
|
|
|
// a server that should listen on localhost and another on 127.0.0.1 will
|
|
|
|
// be grouped into the same address: 127.0.0.1. It will return an error
|
2015-06-23 23:00:55 -05:00
|
|
|
// if an address is malformed or a TLS listener is configured on the
|
2015-05-21 01:06:53 -05:00
|
|
|
// same address as a plaintext HTTP listener. The return value is a map of
|
|
|
|
// bind address to list of configs that would become VirtualHosts on that
|
2015-06-23 23:00:55 -05:00
|
|
|
// server. Use the keys of the returned map to create listeners, and use
|
|
|
|
// the associated values to set up the virtualhosts.
|
2015-10-15 00:45:28 -05:00
|
|
|
func arrangeBindings(allConfigs []server.Config) (map[*net.TCPAddr][]server.Config, error) {
|
|
|
|
addresses := make(map[*net.TCPAddr][]server.Config)
|
2015-05-21 01:06:53 -05:00
|
|
|
|
|
|
|
// Group configs by bind address
|
|
|
|
for _, conf := range allConfigs {
|
2015-06-23 23:00:55 -05:00
|
|
|
newAddr, warnErr, fatalErr := resolveAddr(conf)
|
|
|
|
if fatalErr != nil {
|
|
|
|
return addresses, fatalErr
|
|
|
|
}
|
|
|
|
if warnErr != nil {
|
|
|
|
log.Println("[Warning]", warnErr)
|
2015-05-21 01:06:53 -05:00
|
|
|
}
|
2015-05-21 01:40:05 -05:00
|
|
|
|
|
|
|
// Make sure to compare the string representation of the address,
|
|
|
|
// not the pointer, since a new *TCPAddr is created each time.
|
|
|
|
var existing bool
|
|
|
|
for addr := range addresses {
|
|
|
|
if addr.String() == newAddr.String() {
|
|
|
|
addresses[addr] = append(addresses[addr], conf)
|
|
|
|
existing = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !existing {
|
|
|
|
addresses[newAddr] = append(addresses[newAddr], conf)
|
|
|
|
}
|
2015-05-21 01:06:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow HTTP and HTTPS to be served on the same address
|
|
|
|
for _, configs := range addresses {
|
|
|
|
isTLS := configs[0].TLS.Enabled
|
|
|
|
for _, config := range configs {
|
|
|
|
if config.TLS.Enabled != isTLS {
|
|
|
|
thisConfigProto, otherConfigProto := "HTTP", "HTTP"
|
|
|
|
if config.TLS.Enabled {
|
|
|
|
thisConfigProto = "HTTPS"
|
|
|
|
}
|
|
|
|
if configs[0].TLS.Enabled {
|
|
|
|
otherConfigProto = "HTTPS"
|
|
|
|
}
|
2015-06-07 19:49:17 -05:00
|
|
|
return addresses, fmt.Errorf("configuration error: Cannot multiplex %s (%s) and %s (%s) on same address",
|
2015-05-21 01:06:53 -05:00
|
|
|
configs[0].Address(), otherConfigProto, config.Address(), thisConfigProto)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses, nil
|
|
|
|
}
|
|
|
|
|
2015-06-23 23:00:55 -05:00
|
|
|
// resolveAddr determines the address (host and port) that a config will
|
|
|
|
// bind to. The returned address, resolvAddr, should be used to bind the
|
|
|
|
// listener or group the config with other configs using the same address.
|
|
|
|
// The first error, if not nil, is just a warning and should be reported
|
|
|
|
// but execution may continue. The second error, if not nil, is a real
|
|
|
|
// problem and the server should not be started.
|
|
|
|
//
|
|
|
|
// This function handles edge cases gracefully. If a port name like
|
|
|
|
// "http" or "https" is unknown to the system, this function will
|
|
|
|
// change them to 80 or 443 respectively. If a hostname fails to
|
|
|
|
// resolve, that host can still be served but will be listening on
|
|
|
|
// the wildcard host instead. This function takes care of this for you.
|
|
|
|
func resolveAddr(conf server.Config) (resolvAddr *net.TCPAddr, warnErr error, fatalErr error) {
|
|
|
|
bindHost := conf.BindHost
|
|
|
|
|
|
|
|
resolvAddr, warnErr = net.ResolveTCPAddr("tcp", net.JoinHostPort(bindHost, conf.Port))
|
|
|
|
if warnErr != nil {
|
|
|
|
// Most likely the host lookup failed or the port is unknown
|
|
|
|
tryPort := conf.Port
|
|
|
|
|
|
|
|
switch errVal := warnErr.(type) {
|
|
|
|
case *net.AddrError:
|
|
|
|
if errVal.Err == "unknown port" {
|
|
|
|
// some odd Linux machines don't support these port names; see issue #136
|
|
|
|
switch conf.Port {
|
|
|
|
case "http":
|
|
|
|
tryPort = "80"
|
|
|
|
case "https":
|
|
|
|
tryPort = "443"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolvAddr, fatalErr = net.ResolveTCPAddr("tcp", net.JoinHostPort(bindHost, tryPort))
|
|
|
|
if fatalErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// the hostname probably couldn't be resolved, just bind to wildcard then
|
|
|
|
resolvAddr, fatalErr = net.ResolveTCPAddr("tcp", net.JoinHostPort("0.0.0.0", tryPort))
|
|
|
|
if fatalErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-04 07:53:54 -05:00
|
|
|
// validDirective returns true if d is a valid
|
|
|
|
// directive; false otherwise.
|
|
|
|
func validDirective(d string) bool {
|
|
|
|
for _, dir := range directiveOrder {
|
|
|
|
if dir.name == d {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
|
2015-10-15 00:45:28 -05:00
|
|
|
// NewDefault makes a default configuration, which
|
|
|
|
// is empty except for root, host, and port,
|
|
|
|
// which are essentials for serving the cwd.
|
2015-08-01 14:08:31 -05:00
|
|
|
func NewDefault() server.Config {
|
2015-05-04 07:53:54 -05:00
|
|
|
return server.Config{
|
|
|
|
Root: Root,
|
|
|
|
Host: Host,
|
|
|
|
Port: Port,
|
2015-01-13 14:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 17:23:16 -05:00
|
|
|
|
2015-10-15 00:45:28 -05:00
|
|
|
// Default obtains a default config and arranges
|
|
|
|
// bindings so it's ready to use.
|
2015-08-01 14:08:31 -05:00
|
|
|
func Default() (Group, error) {
|
|
|
|
return arrangeBindings([]server.Config{NewDefault()})
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:23:16 -05:00
|
|
|
// These three defaults are configurable through the command line
|
|
|
|
var (
|
|
|
|
Root = DefaultRoot
|
|
|
|
Host = DefaultHost
|
|
|
|
Port = DefaultPort
|
|
|
|
)
|
2015-08-01 14:08:31 -05:00
|
|
|
|
2015-10-09 17:35:34 -05:00
|
|
|
// Group maps network addresses to their configurations.
|
2015-08-01 14:08:31 -05:00
|
|
|
type Group map[*net.TCPAddr][]server.Config
|