diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2020-01-19 11:51:17 -0700 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-01-19 11:51:17 -0700 |
commit | aad9f90cad23d709075073fd59214a51249de386 (patch) | |
tree | e0283a010e379fd84e2f0af425ebc79afec637db /caddyconfig/httpcaddyfile/addresses.go | |
parent | 07ef4b0c7db356bfc1d334187c0920f85aa416f3 (diff) |
httpcaddyfile: Fix address parsing; don't infer port at parse-time
Before, listener ports could be wrong because ParseAddress doesn't know
about the user-configured HTTP/HTTPS ports, instead hard-coding port 80
or 443, which could be wrong if the user changed them to something else.
Now we defer port and scheme validation/inference to a later part of
building the output JSON.
Diffstat (limited to 'caddyconfig/httpcaddyfile/addresses.go')
-rw-r--r-- | caddyconfig/httpcaddyfile/addresses.go | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/caddyconfig/httpcaddyfile/addresses.go b/caddyconfig/httpcaddyfile/addresses.go index 431bb75..19ac197 100644 --- a/caddyconfig/httpcaddyfile/addresses.go +++ b/caddyconfig/httpcaddyfile/addresses.go @@ -72,7 +72,8 @@ import ( // repetition may be undesirable, so call consolidateAddrMappings() to map // multiple addresses to the same lists of server blocks (a many:many mapping). // (Doing this is essentially a map-reduce technique.) -func (st *ServerType) mapAddressToServerBlocks(originalServerBlocks []serverBlock) (map[string][]serverBlock, error) { +func (st *ServerType) mapAddressToServerBlocks(originalServerBlocks []serverBlock, + options map[string]interface{}) (map[string][]serverBlock, error) { sbmap := make(map[string][]serverBlock) for i, sblock := range originalServerBlocks { @@ -87,7 +88,7 @@ func (st *ServerType) mapAddressToServerBlocks(originalServerBlocks []serverBloc // arguments to the 'bind' directive (although they will all have // the same port, since the port is defined by the key or is implicit // through automatic HTTPS) - addrs, err := st.listenerAddrsForServerBlockKey(sblock, key) + addrs, err := st.listenerAddrsForServerBlockKey(sblock, key, options) if err != nil { return nil, fmt.Errorf("server block %d, key %d (%s): determining listener address: %v", i, j, key, err) } @@ -153,20 +154,43 @@ func (st *ServerType) consolidateAddrMappings(addrToServerBlocks map[string][]se return sbaddrs } -func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key string) ([]string, error) { +func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key string, + options map[string]interface{}) ([]string, error) { addr, err := ParseAddress(key) if err != nil { return nil, fmt.Errorf("parsing key: %v", err) } addr = addr.Normalize() + // figure out the HTTP and HTTPS ports; either + // use defaults, or override with user config + httpPort, httpsPort := strconv.Itoa(certmagic.HTTPPort), strconv.Itoa(certmagic.HTTPSPort) + if hport, ok := options["http_port"]; ok { + httpPort = strconv.Itoa(hport.(int)) + } + if hsport, ok := options["https_port"]; ok { + httpsPort = strconv.Itoa(hsport.(int)) + } + lnPort := DefaultPort if addr.Port != "" { // port explicitly defined lnPort = addr.Port + } else if addr.Scheme != "" { + // port inferred from scheme + if addr.Scheme == "http" { + lnPort = httpPort + } else if addr.Scheme == "https" { + lnPort = httpsPort + } } else if certmagic.HostQualifies(addr.Host) { // automatic HTTPS - lnPort = strconv.Itoa(certmagic.HTTPSPort) + lnPort = httpsPort + } + + // error if scheme and port combination violate convention + if (addr.Scheme == "http" && lnPort == httpsPort) || (addr.Scheme == "https" && lnPort == httpPort) { + return nil, fmt.Errorf("[%s] scheme and port violate convention", key) } // the bind directive specifies hosts, but is optional @@ -245,17 +269,6 @@ func ParseAddress(str string) (Address, error) { a.Path = "/" + hostSplit[1] } - httpPort, httpsPort := strconv.Itoa(certmagic.HTTPPort), strconv.Itoa(certmagic.HTTPSPort) - - // see if we can set port based off scheme - if a.Port == "" { - if a.Scheme == "http" { - a.Port = httpPort - } else if a.Scheme == "https" { - a.Port = httpsPort - } - } - // make sure port is valid if a.Port != "" { if portNum, err := strconv.Atoi(a.Port); err != nil { @@ -265,11 +278,6 @@ func ParseAddress(str string) (Address, error) { } } - // error if scheme and port combination violate convention - if (a.Scheme == "http" && a.Port == httpsPort) || (a.Scheme == "https" && a.Port == httpPort) { - return Address{}, fmt.Errorf("[%s] scheme and port violate convention", str) - } - return a, nil } |