MCPcopy
hub / github.com/gofiber/fiber / classifyHostPort

Function classifyHostPort

internal/schemehost/schemehost.go:64–88  ·  view source on GitHub ↗

classifyHostPort reports whether host is a plain "<reg-name-or-IPv4>" or "<reg-name-or-IPv4>:<port>" value (clean) and, if so, whether it carries an explicit numeric port. The accepted character set is deliberately narrow (lowercase ASCII letters, digits, '.', '-', and a single ':'); anything else,

(host string)

Source from the content-addressed store, hash-verified

62// including bracketed IPv6 literals, returns clean=false and is handled by the
63// url.Parse fallback so behavior stays identical to the legacy implementation.
64func classifyHostPort(host string) (hasPort, clean bool) { //nolint:nonamedreturns // names document the two booleans
65 colon := -1
66 for i := 0; i < len(host); i++ {
67 c := host[i]
68 switch {
69 case c >= 'a' && c <= 'z', c >= '0' && c <= '9', c == '.', c == '-':
70 // safe reg-name / IPv4 character
71 case c == ':':
72 if colon >= 0 {
73 return false, false // more than one colon -> not a clean host:port
74 }
75 colon = i
76 default:
77 return false, false // brackets, control chars, anything else
78 }
79 }
80
81 if colon < 0 {
82 return false, host != "" // no port; empty host falls back to url.Parse
83 }
84 if !allDigits(host[colon+1:]) {
85 return false, false // "host:" or "host:abc" -> let url.Parse decide
86 }
87 return true, true
88}
89
90// allDigits reports whether s is non-empty and all ASCII digits.
91func allDigits(s string) bool {

Callers 1

normalizeSchemeHostFunction · 0.85

Calls 1

allDigitsFunction · 0.85

Tested by

no test coverage detected