httpcaddyfile: Add nil check to prevent panic, fix validation logic
Panic would happen if an automation policy was specified in a singular
server block that had no hostnames in its address. Definitely an edge
case.
Fixed a bug related to checking for server blocks with a host-less key
that tried to make an automation policy. Previously if you had only two
server blocks like ":443" and another one at ":80", the one at ":443"
could not create a TLS automation policy because it thought it would
interfere with TLS automation for the block at ":80", but obviously that
key doesn't enable TLS because it is on the HTTP port. So now we are a
little smarter and count only non-HTTP-empty-hostname keys.
Also fixed a bug so that a key like "https://:1234" is sure to have TLS
enabled by giving it a TLS connection policy. (Relaxed conditions
slightly; the previous conditions were too strict, requiring there to be
a TLS conn policy already or a default SNI to be non-empty.)
Also clarified a comment thanks to feedback from @Mohammed90
// this is an example of a poor mapping from Caddyfile to JSON but that's
// this is an example of a poor mapping from Caddyfile to JSON but that's
// the least-leaky abstraction I could figure out
// the least-leaky abstraction I could figure out
iflen(sblockHosts)==0{
iflen(sblockHosts)==0{
ifserverBlocksWithHostlessKey>1{
ifserverBlocksWithTLSHostlessKey>1{
// this server block and at least one other has a key with no host,
// this server block and at least one other has a key with no host,
// making the two indistinguishable; it is misleading to define such
// making the two indistinguishable; it is misleading to define such
// a policy within one server block since it actually will apply to
// a policy within one server block since it actually will apply to
// others as well
// others as well
returnnil,warnings,fmt.Errorf("cannot make a TLS automation policy from a server block that has a host-less address when there are other server block addresses lacking a host")
returnnil,warnings,fmt.Errorf("cannot make a TLS automation policy from a server block that has a host-less address when there are other TLS-enabled server block addresses lacking a host")
}
}
ifcatchAllAP==nil{
ifcatchAllAP==nil{
// this server block has a key with no hosts, but there is not yet
// this server block has a key with no hosts, but there is not yet