diff options
| author | Matthew Holt <mholt@users.noreply.github.com> | 2020-04-02 14:20:30 -0600 | 
|---|---|---|
| committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-04-02 14:24:53 -0600 | 
| commit | 1c190b001b95e57d21dc63c01ae3c6de2a3fec57 (patch) | |
| tree | 762fca56efffb3cedc7de7128f42c6c479e3a34f /caddyconfig/httpcaddyfile/directives.go | |
| parent | 3634c4593f2d9999dca2d7a02e23edc29bf7bd11 (diff) | |
httpcaddyfile: Refactor site key parsing; detect conflicting schemes
We now store the parsed site/server block keys with the server block,
rather than parsing the addresses every time we read them.
Also detect conflicting schemes, i.e. TLS and non-TLS cannot be served
from the same server (natively -- modules could be built for it).
Also do not add site subroutes (subroutes generated specifically from
site blocks in the Caddyfile) that are empty.
Diffstat (limited to 'caddyconfig/httpcaddyfile/directives.go')
| -rw-r--r-- | caddyconfig/httpcaddyfile/directives.go | 45 | 
1 files changed, 42 insertions, 3 deletions
diff --git a/caddyconfig/httpcaddyfile/directives.go b/caddyconfig/httpcaddyfile/directives.go index 4c2b2d9..8fa48cd 100644 --- a/caddyconfig/httpcaddyfile/directives.go +++ b/caddyconfig/httpcaddyfile/directives.go @@ -16,7 +16,9 @@ package httpcaddyfile  import (  	"encoding/json" +	"net"  	"sort" +	"strconv"  	"strings"  	"github.com/caddyserver/caddy/v2" @@ -381,12 +383,49 @@ func parseSegmentAsSubroute(h Helper) (caddyhttp.MiddlewareHandler, error) {  	return buildSubroute(allResults, h.groupCounter)  } -// serverBlock pairs a Caddyfile server block -// with a "pile" of config values, keyed by class -// name. +// serverBlock pairs a Caddyfile server block with +// a "pile" of config values, keyed by class name, +// as well as its parsed keys for convenience.  type serverBlock struct {  	block caddyfile.ServerBlock  	pile  map[string][]ConfigValue // config values obtained from directives +	keys  []Address +} + +// hostsFromKeys returns a list of all the non-empty hostnames found in +// the keys of the server block sb, unless allowEmpty is true, in which +// case a key with no host (e.g. ":443") will be added to the list as an +// empty string. Otherwise, if allowEmpty is false, and if sb has a key +// that omits the hostname (i.e. is a catch-all/empty host), then the returned +// list is empty, because the server block effectively matches ALL hosts. +// The list may not be in a consistent order. If includePorts is true, then +// any non-empty, non-standard ports will be included. +func (sb serverBlock) hostsFromKeys(allowEmpty, includePorts bool) []string { +	// first get each unique hostname +	hostMap := make(map[string]struct{}) +	for _, addr := range sb.keys { +		if addr.Host == "" && !allowEmpty { +			// server block contains a key like ":443", i.e. the host portion +			// is empty / catch-all, which means to match all hosts +			return []string{} +		} +		if includePorts && +			addr.Port != "" && +			addr.Port != strconv.Itoa(caddyhttp.DefaultHTTPPort) && +			addr.Port != strconv.Itoa(caddyhttp.DefaultHTTPSPort) { +			hostMap[net.JoinHostPort(addr.Host, addr.Port)] = struct{}{} +		} else { +			hostMap[addr.Host] = struct{}{} +		} +	} + +	// convert map to slice +	sblockHosts := make([]string, 0, len(hostMap)) +	for host := range hostMap { +		sblockHosts = append(sblockHosts, host) +	} + +	return sblockHosts  }  type (  | 
