diff --git a/internal/provider/opencode_at_test.go b/internal/provider/opencode_at_test.go new file mode 100644 index 000000000..aba0aa1fe --- /dev/null +++ b/internal/provider/opencode_at_test.go @@ -0,0 +1,22 @@ +package provider + +import "testing" + +// IsOpenCode reads its host as OpenCodeURL reads a URL, with no URL +// parsed for it: it is asked for every model of the providers' page. +func TestOpenCodeAtReadsTheHostAsAURLWould(t *testing.T) { + for _, h := range []string{ + "opencode.ai", "OpenCode.AI", "opencode.ai.", "opencode.ai:443", + "zen.opencode.ai", "api.opencode.ai:8443", "u:p@opencode.ai", + "u@zen.opencode.ai:1", "notopencode.ai", "opencode.ai.evil.com", + "evil.com", "evil.com@", "opencode.ai@evil.com", "[::1]:443", + "[::1]", "127.0.0.1:3425", "", "localhost", + } { + if got, want := openCodeAt(h), OpenCodeURL("//"+h); got != want { + t.Errorf("%q: %v, a URL says %v", h, got, want) + } + } + if n := testing.AllocsPerRun(100, func() { openCodeAt("u@zen.opencode.ai:8443") }); n != 0 { + t.Fatalf("a look allocated %v times, want 0", n) + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index a65e0bc1c..ebb06da04 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1878,8 +1878,23 @@ func (p Provider) Where() string { // IsOpenCode reports whether the provider is OpenCode's gateway (Zen or Go), // which routes and caches by conversation and turns away requests that do // not name one in x-opencode-session. -func (p Provider) IsOpenCode() bool { - return OpenCodeURL("//" + p.Host()) // its port, if it names one, aside +func (p Provider) IsOpenCode() bool { return openCodeAt(p.Host()) } + +// openCodeAt is OpenCodeURL for a host as Host gives it, its port and +// any user aside, read without parsing a URL: the providers' page asks +// IsOpenCode for each model, and url.Parse there was 7 MB of what twenty +// window polls allocated. +func openCodeAt(h string) bool { + if i := strings.LastIndexByte(h, '@'); i >= 0 { + h = h[i+1:] + } + if strings.HasPrefix(h, "[") { + return false // an IPv6 address + } + if i := strings.IndexByte(h, ':'); i >= 0 { + h = h[:i] + } + return openCodeHost(h) } // IsBedrock reports whether the provider is Amazon Bedrock: its runtime