dockerversion: remove insertUpstreamUserAgent()

It was not really "inserting" anything, just formatting and appending.
Simplify this by changing this in to a `getUpstreamUserAgent()` function
which returns the upstream User-Agent (if any) into a `UpstreamClient()`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 9a1f2e6d7c)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-06-01 13:50:30 +02:00
parent 1d45ea52f4
commit ed376a603f
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -18,11 +18,11 @@ type UAStringKey struct{}
//
// [docker client's UA] UpstreamClient([upstream client's UA])
func DockerUserAgent(ctx context.Context) string {
daemonUA := getDaemonUserAgent()
if upstreamUA := getUserAgentFromContext(ctx); len(upstreamUA) > 0 {
return insertUpstreamUserAgent(upstreamUA, daemonUA)
ua := getDaemonUserAgent()
if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" {
ua += " " + upstreamUA
}
return daemonUA
return ua
}
var (
@ -57,16 +57,23 @@ func getDaemonUserAgent() string {
return daemonUA
}
// getUserAgentFromContext returns the previously saved user-agent context stored in ctx, if one exists
func getUserAgentFromContext(ctx context.Context) string {
// getUpstreamUserAgent returns the previously saved user-agent context stored
// in ctx, if one exists, and formats it as:
//
// UpstreamClient(<upstream user agent string>)
//
// It returns an empty string if no user-agent is present in the context.
func getUpstreamUserAgent(ctx context.Context) string {
var upstreamUA string
if ctx != nil {
var ki interface{} = ctx.Value(UAStringKey{})
if ki != nil {
if ki := ctx.Value(UAStringKey{}); ki != nil {
upstreamUA = ctx.Value(UAStringKey{}).(string)
}
}
return upstreamUA
if upstreamUA == "" {
return ""
}
return fmt.Sprintf("UpstreamClient(%s)", escapeStr(upstreamUA))
}
const charsToEscape = `();\`
@ -89,11 +96,3 @@ func escapeStr(s string) string {
}
return ret
}
// insertUpstreamUserAgent adds the upstream client useragent to create a user-agent
// string of the form:
//
// $dockerUA UpstreamClient($upstreamUA)
func insertUpstreamUserAgent(upstreamUA string, dockerUA string) string {
return fmt.Sprintf("%s UpstreamClient(%s)", dockerUA, escapeStr(upstreamUA))
}