docs: sync public link configuration reference
This commit is contained in:
parent
4390ebf5a9
commit
c0088f1160
13 changed files with 857 additions and 100 deletions
|
|
@ -6,9 +6,52 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
const DefaultPublicBaseURL = "https://telesrv.net"
|
||||
const (
|
||||
DefaultPublicBaseURL = "https://telesrv.net"
|
||||
DefaultWebBaseURL = "https://web.telesrv.net"
|
||||
DefaultAppScheme = "telesrv"
|
||||
DefaultAppName = "telesrv"
|
||||
)
|
||||
const MaxChatlistSlugBytes = 128
|
||||
|
||||
// ValidateAppScheme normalizes the client-visible custom URL scheme used by
|
||||
// public landing pages. Standard Web schemes and Telegram's official tg scheme
|
||||
// are deliberately rejected: the latter remains a manual compatibility link
|
||||
// and must never become the automatic open target.
|
||||
func ValidateAppScheme(raw string) (string, error) {
|
||||
scheme := strings.ToLower(strings.TrimSpace(raw))
|
||||
if scheme == "" {
|
||||
scheme = DefaultAppScheme
|
||||
}
|
||||
for i, r := range scheme {
|
||||
if (r >= 'a' && r <= 'z') || (i > 0 && ((r >= '0' && r <= '9') || r == '+' || r == '-' || r == '.')) {
|
||||
continue
|
||||
}
|
||||
return "", fmt.Errorf("must match [a-z][a-z0-9+.-]*")
|
||||
}
|
||||
switch scheme {
|
||||
case "http", "https", "tg":
|
||||
return "", fmt.Errorf("reserved scheme %q is not allowed", scheme)
|
||||
}
|
||||
return scheme, nil
|
||||
}
|
||||
|
||||
func ValidateAppName(raw string) (string, error) {
|
||||
name := strings.TrimSpace(raw)
|
||||
if name == "" {
|
||||
return "", fmt.Errorf("must not be empty")
|
||||
}
|
||||
if len([]rune(name)) > 64 {
|
||||
return "", fmt.Errorf("must not exceed 64 characters")
|
||||
}
|
||||
for _, r := range name {
|
||||
if r < 0x20 || r == 0x7f {
|
||||
return "", fmt.Errorf("must not contain control characters")
|
||||
}
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func NormalizeBaseURL(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package links
|
|||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -59,6 +60,45 @@ func TestValidateBaseURL(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestValidateAppScheme(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "default", raw: "", want: "telesrv"},
|
||||
{name: "normalized", raw: " My-App+Dev ", want: "my-app+dev"},
|
||||
{name: "starts with digit", raw: "1app", wantErr: true},
|
||||
{name: "colon", raw: "myapp:", wantErr: true},
|
||||
{name: "official tg", raw: "tg", wantErr: true},
|
||||
{name: "http", raw: "http", wantErr: true},
|
||||
{name: "https", raw: "https", wantErr: true},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := ValidateAppScheme(tc.raw)
|
||||
if (err != nil) != tc.wantErr {
|
||||
t.Fatalf("ValidateAppScheme(%q) error = %v, wantErr %v", tc.raw, err, tc.wantErr)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Fatalf("ValidateAppScheme(%q) = %q, want %q", tc.raw, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAppName(t *testing.T) {
|
||||
if got, err := ValidateAppName(" Example Chat "); err != nil || got != "Example Chat" {
|
||||
t.Fatalf("ValidateAppName valid = %q, %v", got, err)
|
||||
}
|
||||
for _, raw := range []string{"", " ", "bad\nname", strings.Repeat("x", 65)} {
|
||||
if got, err := ValidateAppName(raw); err == nil {
|
||||
t.Fatalf("ValidateAppName(%q) = %q, want error", raw, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPreservesBasePathAndQuery(t *testing.T) {
|
||||
got := Build("http://127.0.0.1:2401/root/", "/call/abc", url.Values{"slug": []string{"abc"}})
|
||||
if want := "http://127.0.0.1:2401/root/call/abc?slug=abc"; got != want {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue