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)
| 560 | // |
| 561 | // The resulting slice is not sorted but will never have duplicates. |
| 562 | func (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 | |
| 594 | func (sb serverBlock) hostsFromKeysNotHTTP(httpPort string) []string { |
| 595 | // ensure each entry in our list is unique |