Skip to content

Commit

Permalink
internal/cueversion: convert spaces to underscore in Go version
Browse files Browse the repository at this point in the history
A Component/Version pair inside a User-Agent header
should not contain spaces, but `runtime.Version` can
return a version containing spaces when using a development
version (for example "devel go1.23-2ab9218c86 2024-03-11 22:10:38 +0000").

So convert spaces to underscores before putting that string
in the User-Agent header.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I03e433429db80ccadf44983b4938fbdf14b08673
Dispatch-Trailer: {"type":"trybot","CL":1178179,"patchset":2,"ref":"refs/changes/79/1178179/2","targetBranch":"master"}
  • Loading branch information
rogpeppe authored and cueckoo committed Mar 12, 2024
1 parent 6db0b7f commit 4fce5ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion internal/cueversion/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"runtime"
"runtime/debug"
"strings"
"sync"
)

Expand Down Expand Up @@ -51,5 +52,10 @@ func UserAgent(clientType string) string {
if clientType == "" {
clientType = "cuelang.org/go"
}
return fmt.Sprintf("Cue/%s (%s) Go/%s (%s/%s)", Version(), clientType, runtime.Version(), runtime.GOOS, runtime.GOARCH)
// The Go version can contain spaces, but we don't want spaces inside
// Component/Version pair, so replace them with underscores.
// As the runtime version won't contain underscores itself, this
// is reversible.
goVersion := strings.ReplaceAll(runtime.Version(), "_", " ")
return fmt.Sprintf("Cue/%s (%s) Go/%s (%s/%s)", Version(), clientType, goVersion, runtime.GOOS, runtime.GOARCH)
}
2 changes: 1 addition & 1 deletion internal/cueversion/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestVersion(t *testing.T) {
func TestUserAgent(t *testing.T) {
agent := UserAgent("custom")
qt.Assert(t, qt.Matches(agent,
`Cue/v[^ ]+ \(custom\) Go/go1\.[^ ]+ \([^/]+/[^/]+\)`,
`Cue/v[^ ]+ \(custom\) Go/[^ ]+ \([^/]+/[^/]+\)`,
))
}

Expand Down

0 comments on commit 4fce5ba

Please sign in to comment.