serversFromPairings creates the servers for each pairing of addresses to server blocks. Each pairing is essentially a server definition.
( pairings []sbAddrAssociation, options map[string]any, warnings *[]caddyconfig.Warning, groupCounter counter, )
| 533 | // serversFromPairings creates the servers for each pairing of addresses |
| 534 | // to server blocks. Each pairing is essentially a server definition. |
| 535 | func (st *ServerType) serversFromPairings( |
| 536 | pairings []sbAddrAssociation, |
| 537 | options map[string]any, |
| 538 | warnings *[]caddyconfig.Warning, |
| 539 | groupCounter counter, |
| 540 | ) (map[string]*caddyhttp.Server, error) { |
| 541 | servers := make(map[string]*caddyhttp.Server) |
| 542 | defaultSNI := tryString(options["default_sni"], warnings) |
| 543 | fallbackSNI := tryString(options["fallback_sni"], warnings) |
| 544 | |
| 545 | httpPort := strconv.Itoa(caddyhttp.DefaultHTTPPort) |
| 546 | if hp, ok := options["http_port"].(int); ok { |
| 547 | httpPort = strconv.Itoa(hp) |
| 548 | } |
| 549 | httpsPort := strconv.Itoa(caddyhttp.DefaultHTTPSPort) |
| 550 | if hsp, ok := options["https_port"].(int); ok { |
| 551 | httpsPort = strconv.Itoa(hsp) |
| 552 | } |
| 553 | autoHTTPS := []string{} |
| 554 | if ah, ok := options["auto_https"].([]string); ok { |
| 555 | autoHTTPS = ah |
| 556 | } |
| 557 | |
| 558 | for i, p := range pairings { |
| 559 | // detect ambiguous site definitions: server blocks which |
| 560 | // have the same host bound to the same interface (listener |
| 561 | // address), otherwise their routes will improperly be added |
| 562 | // to the same server (see issue #4635) |
| 563 | for j, sblock1 := range p.serverBlocks { |
| 564 | for _, key := range sblock1.block.GetKeysText() { |
| 565 | for k, sblock2 := range p.serverBlocks { |
| 566 | if k == j { |
| 567 | continue |
| 568 | } |
| 569 | if slices.Contains(sblock2.block.GetKeysText(), key) { |
| 570 | return nil, fmt.Errorf("ambiguous site definition: %s", key) |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | var ( |
| 577 | addresses []string |
| 578 | protocols [][]string |
| 579 | ) |
| 580 | |
| 581 | for _, addressWithProtocols := range p.addressesWithProtocols { |
| 582 | addresses = append(addresses, addressWithProtocols.address) |
| 583 | protocols = append(protocols, addressWithProtocols.protocols) |
| 584 | } |
| 585 | |
| 586 | srv := &caddyhttp.Server{ |
| 587 | Listen: addresses, |
| 588 | ListenProtocols: protocols, |
| 589 | } |
| 590 | |
| 591 | // remove srv.ListenProtocols[j] if it only contains the default protocols |
| 592 | for j, lnProtocols := range srv.ListenProtocols { |
no test coverage detected