Remote magpie and System One addresses got "http://" put in front by several normalisers that each tested for "://" their own way, and one saved "http://http://host" stayed that way until the address was deleted and added again. provider.WithScheme (new, internal/provider/scheme.go) is the one rule: trim, add the default scheme when none is there, lowercase http/https, and collapse a repeated http/https prefix to the innermost one. Path and trailing slash are left as written; "host:3425/x?u=http://y" has no scheme. It is used by provider.normalize (every provider URL, on read), remoteMagpieEndpoints, DetectDecide, parseAddress and gui remoteAddr. app.js has withScheme as its mirror, used by hostOf, localAddress, vertexRoot and respellURL (whose gemini/openai regexes now match any letter case). Old data: an address saved with a doubled scheme is repaired when it is read (provider.normalize for providers and System One addresses, loadRemote for remote-magpie.json). The file is written out correctly on the next save; nobody has to delete and re-add. Reading alone does not rewrite the file. app.js: the core script budget is met without a raise; withScheme was cut down to fit (final package run after the rebase onfd9033cbf: gui and provider ok). Run: - go test ./internal/provider ./internal/gui ./internal/settings ./internal/cli on mote: ok, except TestCoreScriptBudget (284 over), fixed by shortening the JS and re-run alone: ok. - gofmt -l, go vet darwin, GOOS=windows, GOOS=linux -tags nogui: clean. - GUI on mica, chromium and webkit: scheme-once, local-address, custom-systemone-url, decide-endpoint-label, azure-endpoint, remote-*, i18n-keys, connect-address-kept: 158 of 164 pass; the 6 reds are "Remote magpie asks for the other magpie's address", red the same at origin/main5e933e153(#6099). - add-sheet-custom, provider-fetch-again, provider-gemini: 14 of 54 red on mica, the same 14 at origin/mainab98228cc(#5232, #6209). Red on the old code: scheme_test.go, remote_test.go (Go) and scheme-once.test.cjs (withScheme undefined) were run against an origin/main copy and failed there. Not tried: the place that produced "http://http://" was not found by reading (every guard used Contains("://")), so this hardens every normaliser instead of one site; no real remote magpie was used; the full GUI suite was not run.
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package provider
|
|
|
|
import "strings"
|
|
|
|
// WithScheme is a typed address with its scheme made certain: one that is
|
|
// there already, http or https in any letter case, stays (its scheme in
|
|
// lower case) and is never given a second; one with none gets def. A
|
|
// scheme saved twice ("http://http://host", from before this was
|
|
// checked) is one again. Other schemes are left as they are. The rest of
|
|
// the address, its path and trailing slash, is not touched.
|
|
func WithScheme(raw, def string) string {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return ""
|
|
}
|
|
scheme, rest := splitScheme(raw)
|
|
if scheme == "" {
|
|
return def + "://" + raw
|
|
}
|
|
// the repeats: "http://https://host" is host, at the last scheme said
|
|
for {
|
|
inner, more := splitScheme(rest)
|
|
if inner != "http" && inner != "https" || scheme != "http" && scheme != "https" {
|
|
break
|
|
}
|
|
scheme, rest = inner, more
|
|
}
|
|
return scheme + "://" + rest
|
|
}
|
|
|
|
// splitScheme cuts a leading "http://" or "https://" (any case) or another
|
|
// "scheme://" off s; scheme is lower case, "" when s has none.
|
|
func splitScheme(s string) (scheme, rest string) {
|
|
i := strings.Index(s, "://")
|
|
if i <= 0 {
|
|
return "", s
|
|
}
|
|
for _, c := range s[:i] {
|
|
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '+' || c == '-' || c == '.') {
|
|
return "", s
|
|
}
|
|
}
|
|
return strings.ToLower(s[:i]), s[i+3:]
|
|
}
|