tlsrp

TLS reverse proxy
git clone git://git.rr3.xyz/tlsrp
Log | Files | Refs | README | LICENSE

commit fb0eaf15250c7aaf738c723d8b860ce3f55d27a0
parent 5f3c7bb1cdf0cd76d4716d158aa205bc5a3418e2
Author: Robert Russell <robertrussell.72001@gmail.com>
Date:   Tue, 16 Jul 2024 16:45:07 -0700

Clean up hostname.go

Diffstat:
Mhostname.go | 47++++++++++++++++++++++-------------------------
1 file changed, 22 insertions(+), 25 deletions(-)

diff --git a/hostname.go b/hostname.go @@ -6,19 +6,18 @@ import ( "strings" ) +// TODO: Add support for more than just alternation in patterns. We should at +// least support leading wildcards, but maybe we should support full regex. + type label string -func parseLabel(s string) (label, error) { - if len(s) == 0 { +func parseLabel(labelStr string) (label, error) { + if len(labelStr) == 0 { return "", fmt.Errorf("empty label") } - buf := make([]byte, 0, len(s)) - - for i, r := range s { - first := i == 0 - last := i == len(s) - 1 - + buf := make([]byte, 0, len(labelStr)) + for _, r := range labelStr { switch { case 'A' <= r && r <= 'Z': r += 'a' - 'A' @@ -26,12 +25,8 @@ func parseLabel(s string) (label, error) { // Ok case '0' <= r && r <= '9': // Ok - case r == '-' && (!first && !last): - // Ok - case r == '-' && first: - return "", fmt.Errorf("hyphen at start of label") - case r == '-' && last: - return "", fmt.Errorf("hyphen at end of label") + case r == '-': + // Ok, as long as test after loop passes default: return "", fmt.Errorf("illegal rune in label: %q", r) } @@ -39,6 +34,10 @@ func parseLabel(s string) (label, error) { buf = append(buf, byte(r)) } + if buf[0] == '-' || buf[len(buf) - 1] == '-' { + return fmt.Errorf("hyphen at start or end of label") + } + return label(buf), nil } @@ -63,14 +62,13 @@ func (hostname0 hostname) equal(hostname1 hostname) bool { return slices.Equal(hostname0, hostname1) } -func parseHostname(s string) (hostname, error) { - if len(s) == 0 { - return nil, nil +func parseHostname(hostnameStr string) (hostname, error) { + labelStrs := strings.Split(hostnameStr, ".") + if len(labelStrs) < 2 { + return nil, fmt.Errorf("illegal hostname: expected 2 or more labels") } - labelStrs := strings.Split(s, ".") labels := make([]label, 0, len(labelStrs)) - for _, labelStr := range labelStrs { label, err := parseLabel(labelStr) if err != nil { @@ -84,20 +82,19 @@ func parseHostname(s string) (hostname, error) { type pattern []hostname -func (pat pattern) matches(hostname hostname) bool { - return slices.ContainsFunc(pat, hostname.equal) +func (pattern pattern) matches(hostname hostname) bool { + return slices.ContainsFunc(pattern, hostname.equal) } func parsePattern(hostnameStrs []string) (pattern, error) { - pat := make(pattern, 0, len(hostnameStrs)) - + pattern := make(pattern, 0, len(hostnameStrs)) for _, hostnameStr := range hostnameStrs { hostname, err := parseHostname(hostnameStr) if err != nil { return nil, err } - pat = append(pat, hostname) + pattern = append(pattern, hostname) } - return pat, nil + return pattern, nil }