@marcel91200 / caddy Mirror · read-only / commits / 0182fb87
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 07:00 UTC

core: addresses.go funcs renames (#6622)

* right side in tls ln * remove ParseNetworkAddressFromHostPort * ignore placeholder port * remove println * update test cases (!!!) * [] * comment * Trim * Update addresses.go

Aaron Paterson committed Oct 21, 2024 at 10:02 UTC 0182fb87fa7276463086c2360431a1c0dc797edf
3 files changed +31 -41
caddyconfig/httpcaddyfile/addresses.go
+6 -2
@@ -329,8 +329,12 @@ func (st *ServerType) listenersForServerBlockAddress(sblock serverBlock, addr Ad
329 // use a map to prevent duplication
330 listeners := map[string]map[string]struct{}{}
331 for _, lnCfgVal := range lnCfgVals {
332 - for _, lnHost := range lnCfgVal.addresses {
333 - networkAddr, err := caddy.ParseNetworkAddressFromHostPort(lnHost, lnPort)
332 + for _, lnAddr := range lnCfgVal.addresses {
333 + lnNetw, lnHost, _, err := caddy.SplitNetworkAddress(lnAddr)
334 + if err != nil {
335 + return nil, fmt.Errorf("splitting listener address: %v", err)
336 + }
337 + networkAddr, err := caddy.ParseNetworkAddress(caddy.JoinNetworkAddress(lnNetw, lnHost, lnPort))
338 if err != nil {
339 return nil, fmt.Errorf("parsing network address: %v", err)
340 }
listeners.go
+18 -34
@@ -305,25 +305,6 @@ func IsFdNetwork(netw string) bool {
305 return strings.HasPrefix(netw, "fd")
306 }
307
308 -// normally we would simply append the port,
309 -// but if host is IPv6, we need to ensure it
310 -// is enclosed in [ ]; net.JoinHostPort does
311 -// this for us, but host might also have a
312 -// network type in front (e.g. "tcp/") leading
313 -// to "[tcp/::1]" which causes parsing failures
314 -// later; what we need is "tcp/[::1]", so we have
315 -// to split the network and host, then re-combine
316 -func ParseNetworkAddressFromHostPort(host, port string) (NetworkAddress, error) {
317 - network, addr, ok := strings.Cut(host, "/")
318 - if !ok {
319 - addr = network
320 - network = ""
321 - }
322 - addr = strings.Trim(addr, "[]") // IPv6
323 - networkAddr := JoinNetworkAddress(network, addr, port)
324 - return ParseNetworkAddress(networkAddr)
325 -}
326 -
308 // ParseNetworkAddress parses addr into its individual
309 // components. The input string is expected to be of
310 // the form "network/host:port-range" where any part is
@@ -399,25 +380,28 @@ func SplitNetworkAddress(a string) (network, host, port string, err error) {
380 if slashFound {
381 network = strings.ToLower(strings.TrimSpace(beforeSlash))
382 a = afterSlash
383 + if IsUnixNetwork(network) || IsFdNetwork(network) {
384 + host = a
385 + return
386 + }
387 }
403 - if IsUnixNetwork(network) || IsFdNetwork(network) {
404 - host = a
405 - return
406 - }
388 +
389 host, port, err = net.SplitHostPort(a)
408 - if err == nil || a == "" {
409 - return
410 - }
411 - // in general, if there was an error, it was likely "missing port",
412 - // so try adding a bogus port to take advantage of standard library's
413 - // robust parser, then strip the artificial port before returning
414 - // (don't overwrite original error though; might still be relevant)
415 - var err2 error
416 - host, port, err2 = net.SplitHostPort(a + ":0")
417 - if err2 == nil {
418 - err = nil
390 + firstErr := err
391 +
392 + if err != nil {
393 + // in general, if there was an error, it was likely "missing port",
394 + // so try removing square brackets around an IPv6 host, adding a bogus
395 + // port to take advantage of standard library's robust parser, then
396 + // strip the artificial port.
397 + host, _, err = net.SplitHostPort(net.JoinHostPort(strings.Trim(a, "[]"), "0"))
398 port = ""
399 }
400 +
401 + if err != nil {
402 + err = errors.Join(firstErr, err)
403 + }
404 +
405 return
406 }
407
listeners_test.go
+7 -5
@@ -31,7 +31,7 @@ func TestSplitNetworkAddress(t *testing.T) {
31 }{
32 {
33 input: "",
34 - expectErr: true,
34 + expectHost: "",
35 },
36 {
37 input: "foo",
@@ -42,7 +42,7 @@ func TestSplitNetworkAddress(t *testing.T) {
42 },
43 {
44 input: "::",
45 - expectErr: true,
45 + expectHost: "::",
46 },
47 {
48 input: "[::]",
@@ -77,7 +77,7 @@ func TestSplitNetworkAddress(t *testing.T) {
77 {
78 input: "udp/",
79 expectNetwork: "udp",
80 - expectErr: true,
80 + expectHost: "",
81 },
82 {
83 input: "unix//foo/bar",
@@ -185,7 +185,8 @@ func TestParseNetworkAddress(t *testing.T) {
185 }{
186 {
187 input: "",
188 - expectErr: true,
188 + expectAddr: NetworkAddress{
189 + },
190 },
191 {
192 input: ":",
@@ -311,7 +312,8 @@ func TestParseNetworkAddressWithDefaults(t *testing.T) {
312 }{
313 {
314 input: "",
314 - expectErr: true,
315 + expectAddr: NetworkAddress{
316 + },
317 },
318 {
319 input: ":",