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

Function ParseAddress

caddyconfig/httpcaddyfile/addresses.go:373–421  ·  view source on GitHub ↗

ParseAddress parses an address string into a structured format with separate scheme, host, port, and path portions, as well as the original input string.

(str string)

Source from the content-addressed store, hash-verified

371// ParseAddress parses an address string into a structured format with separate
372// scheme, host, port, and path portions, as well as the original input string.
373func ParseAddress(str string) (Address, error) {
374 const maxLen = 4096
375 if len(str) > maxLen {
376 str = str[:maxLen]
377 }
378 remaining := strings.TrimSpace(str)
379 a := Address{Original: remaining}
380
381 // extract scheme
382 splitScheme := strings.SplitN(remaining, "://", 2)
383 switch len(splitScheme) {
384 case 0:
385 return a, nil
386 case 1:
387 remaining = splitScheme[0]
388 case 2:
389 a.Scheme = splitScheme[0]
390 remaining = splitScheme[1]
391 }
392
393 // extract host and port
394 hostSplit := strings.SplitN(remaining, "/", 2)
395 if len(hostSplit) > 0 {
396 host, port, err := net.SplitHostPort(hostSplit[0])
397 if err != nil {
398 host, port, err = net.SplitHostPort(hostSplit[0] + ":")
399 if err != nil {
400 host = hostSplit[0]
401 }
402 }
403 a.Host = host
404 a.Port = port
405 }
406 if len(hostSplit) == 2 {
407 // all that remains is the path
408 a.Path = "/" + hostSplit[1]
409 }
410
411 // make sure port is valid
412 if a.Port != "" {
413 if portNum, err := strconv.Atoi(a.Port); err != nil {
414 return Address{}, fmt.Errorf("invalid port '%s': %v", a.Port, err)
415 } else if portNum < 0 || portNum > 65535 {
416 return Address{}, fmt.Errorf("port %d is out of range", portNum)
417 }
418 }
419
420 return a, nil
421}
422
423// String returns a human-readable form of a. It will
424// be a cleaned-up and filled-out URL string.

Callers 5

cmdReverseProxyFunction · 0.92
TestParseAddressFunction · 0.85
TestKeyNormalizationFunction · 0.85
FuzzParseAddressFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestParseAddressFunction · 0.68
TestKeyNormalizationFunction · 0.68