| 1 | package fichas |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/url" |
| 6 | "strings" |
| 7 | "unicode" |
| 8 | ) |
| 9 | |
| 10 | // KindURL is a web address. Paul, 2026-09-30: the Domain field "should be |
| 11 | // URL type and very forgiving whether I type the protocol or not." |
| 12 | // |
| 13 | // Forgiving is exactly what <input type="url"> is not: browsers reject |
| 14 | // "example.com" without a scheme. So the control is a plain text input with |
| 15 | // the URL keyboard (inputmode="url"), and all the forgiveness is here, on the |
| 16 | // server, where it applies to every way a value can arrive. |
| 17 | const KindURL = "url" |
| 18 | |
| 19 | var errNotAURL = errors.New("not a web address") |
| 20 | |
| 21 | // normaliseURL accepts what a person types for a web address and returns the |
| 22 | // stored form: an http(s) URL with a lowercase host and no bare trailing |
| 23 | // slash. "example.com", "WWW.Example.com/", "https://example.com" and |
| 24 | // "http://example.com/pricing" are all fine. Anything whose scheme is not |
| 25 | // http or https — javascript:, mailto:, data: — is refused, because the |
| 26 | // stored value becomes a link. |
| 27 | func normaliseURL(v string) (string, error) { |
| 28 | v = strings.TrimSpace(v) |
| 29 | if v == "" { |
| 30 | return "", nil |
| 31 | } |
| 32 | if strings.IndexFunc(v, unicode.IsSpace) >= 0 { |
| 33 | return "", errNotAURL |
| 34 | } |
| 35 | if !strings.Contains(v, "://") { |
| 36 | // "mailto:x" and "javascript:x" have no "://" either; with https:// |
| 37 | // in front the part before the colon parses as a host with a |
| 38 | // non-numeric port, and Parse refuses it below. |
| 39 | v = "https://" + v |
| 40 | } |
| 41 | u, err := url.Parse(v) |
| 42 | if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.User != nil { |
| 43 | return "", errNotAURL |
| 44 | } |
| 45 | host := strings.ToLower(u.Hostname()) |
| 46 | if host == "" || !strings.Contains(host, ".") || strings.HasPrefix(host, ".") || strings.HasSuffix(host, ".") { |
| 47 | return "", errNotAURL |
| 48 | } |
| 49 | for _, r := range host { |
| 50 | if !(unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-' || r == '.') { |
| 51 | return "", errNotAURL |
| 52 | } |
| 53 | } |
| 54 | u.Scheme = strings.ToLower(u.Scheme) |
| 55 | if p := u.Port(); p != "" { |
| 56 | u.Host = host + ":" + p |
| 57 | } else { |
| 58 | u.Host = host |
| 59 | } |
| 60 | if u.Path == "/" && u.RawQuery == "" && u.Fragment == "" { |
| 61 | u.Path = "" |
| 62 | } |
| 63 | return u.String(), nil |
| 64 | } |
| 65 | |
| 66 | // displayURL is how a stored address reads: no scheme, no bare trailing |
| 67 | // slash — "brightwater.example", not "https://brightwater.example/". |
| 68 | func displayURL(v string) string { |
| 69 | s := strings.TrimPrefix(strings.TrimPrefix(v, "https://"), "http://") |
| 70 | return strings.TrimSuffix(s, "/") |
| 71 | } |
| 72 | |
| 73 | // safeHref is the stored address as a link target, or "" if it is not an |
| 74 | // http(s) URL — a value that predates validation, say. Never anything a |
| 75 | // browser would execute. |
| 76 | func safeHref(v string) string { |
| 77 | u, err := url.Parse(v) |
| 78 | if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" { |
| 79 | return "" |
| 80 | } |
| 81 | return u.String() |
| 82 | } |
| 83 | |
| 84 | // canonicalURL is the identity of an address for duplicate-checking: host |
| 85 | // without "www.", plus path, no scheme, no trailing slash, lowercase. So |
| 86 | // "https://www.Brightwater.example/" and "brightwater.example" are the same |
| 87 | // company. Deliberately looser than the stored form, which keeps what was |
| 88 | // typed. |
| 89 | func canonicalURL(v string) string { |
| 90 | n, err := normaliseURL(v) |
| 91 | if err != nil { |
| 92 | return strings.ToLower(strings.TrimSpace(v)) |
| 93 | } |
| 94 | u, _ := url.Parse(n) |
| 95 | host := strings.TrimPrefix(strings.ToLower(u.Hostname()), "www.") |
| 96 | return strings.TrimSuffix(host+strings.ToLower(u.EscapedPath()), "/") |
| 97 | } |
| 98 | |