1package config
2
3import (
4 "strings"
5 "unicode"
6 "unicode/utf8"
7)
8
9const (
10 siteNameMaxBytes = 64
11 reasonSiteName = "must be at most 64 bytes of UTF-8 text with a visible character and no control characters (the display-name rule, ยง3.8)"
12)
13
14func siteNameValid(value string) bool {
15 if value == "" || len(value) > siteNameMaxBytes || !utf8.ValidString(value) {
16 return false
17 }
18 for _, r := range value {
19 if unicode.IsControl(r) {
20 return false
21 }
22 }
23 return strings.IndexFunc(value, siteNameVisible) >= 0
24}
25
26func siteNameVisible(r rune) bool {
27 return !unicode.IsSpace(r) && !unicode.In(r, unicode.Cf, unicode.Variation_Selector, unicode.Other_Default_Ignorable_Code_Point)
28}
29
30func (v *validator) siteName(value string) string {
31 if !v.idx.has("server.site_name") {
32 return ""
33 }
34 switch {
35 case value == "":
36 v.add("server.site_name", reasonEmpty)
37 case !siteNameValid(value):
38 v.add("server.site_name", reasonSiteName)
39 }
40 return value
41}