MCPcopy
hub / github.com/caddyserver/caddy / hostsFromKeys

Method hostsFromKeys

caddyconfig/httpcaddyfile/directives.go:562–592  ·  view source on GitHub ↗

hostsFromKeys returns a list of all the non-empty hostnames found in the keys of the server block sb. If logger mode is false, a key with an empty hostname portion will return an empty slice, since that server block is interpreted to effectively match all hosts. An empty string is never added to the

(loggerMode bool)

Source from the content-addressed store, hash-verified

560//
561// The resulting slice is not sorted but will never have duplicates.
562func (sb serverBlock) hostsFromKeys(loggerMode bool) []string {
563 // ensure each entry in our list is unique
564 hostMap := make(map[string]struct{})
565 for _, addr := range sb.parsedKeys {
566 if addr.Host == "" {
567 if !loggerMode {
568 // server block contains a key like ":443", i.e. the host portion
569 // is empty / catch-all, which means to match all hosts
570 return []string{}
571 }
572 // never append an empty string
573 continue
574 }
575 if loggerMode &&
576 addr.Port != "" &&
577 addr.Port != strconv.Itoa(caddyhttp.DefaultHTTPPort) &&
578 addr.Port != strconv.Itoa(caddyhttp.DefaultHTTPSPort) {
579 hostMap[net.JoinHostPort(addr.Host, addr.Port)] = struct{}{}
580 } else {
581 hostMap[addr.Host] = struct{}{}
582 }
583 }
584
585 // convert map to slice
586 sblockHosts := make([]string, 0, len(hostMap))
587 for host := range hostMap {
588 sblockHosts = append(sblockHosts, host)
589 }
590
591 return sblockHosts
592}
593
594func (sb serverBlock) hostsFromKeysNotHTTP(httpPort string) []string {
595 // ensure each entry in our list is unique

Callers 3

TestHostsFromKeysFunction · 0.95
serversFromPairingsMethod · 0.80
buildTLSAppMethod · 0.80

Calls 1

JoinHostPortMethod · 0.80

Tested by 1

TestHostsFromKeysFunction · 0.76