Browse code

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>

Sebastiaan van Stijn authored on 2023/06/01 20:50:30
Showing 1 changed files
... ...
@@ -18,11 +18,11 @@ type UAStringKey struct{}
18 18
 //
19 19
 //	[docker client's UA] UpstreamClient([upstream client's UA])
20 20
 func DockerUserAgent(ctx context.Context) string {
21
-	daemonUA := getDaemonUserAgent()
22
-	if upstreamUA := getUserAgentFromContext(ctx); len(upstreamUA) > 0 {
23
-		return insertUpstreamUserAgent(upstreamUA, daemonUA)
21
+	ua := getDaemonUserAgent()
22
+	if upstreamUA := getUpstreamUserAgent(ctx); upstreamUA != "" {
23
+		ua += " " + upstreamUA
24 24
 	}
25
-	return daemonUA
25
+	return ua
26 26
 }
27 27
 
28 28
 var (
... ...
@@ -57,16 +57,23 @@ func getDaemonUserAgent() string {
57 57
 	return daemonUA
58 58
 }
59 59
 
60
-// getUserAgentFromContext returns the previously saved user-agent context stored in ctx, if one exists
61
-func getUserAgentFromContext(ctx context.Context) string {
60
+// getUpstreamUserAgent returns the previously saved user-agent context stored
61
+// in ctx, if one exists, and formats it as:
62
+//
63
+//	UpstreamClient(<upstream user agent string>)
64
+//
65
+// It returns an empty string if no user-agent is present in the context.
66
+func getUpstreamUserAgent(ctx context.Context) string {
62 67
 	var upstreamUA string
63 68
 	if ctx != nil {
64
-		var ki interface{} = ctx.Value(UAStringKey{})
65
-		if ki != nil {
69
+		if ki := ctx.Value(UAStringKey{}); ki != nil {
66 70
 			upstreamUA = ctx.Value(UAStringKey{}).(string)
67 71
 		}
68 72
 	}
69
-	return upstreamUA
73
+	if upstreamUA == "" {
74
+		return ""
75
+	}
76
+	return fmt.Sprintf("UpstreamClient(%s)", escapeStr(upstreamUA))
70 77
 }
71 78
 
72 79
 const charsToEscape = `();\`
... ...
@@ -89,11 +96,3 @@ func escapeStr(s string) string {
89 89
 	}
90 90
 	return ret
91 91
 }
92
-
93
-// insertUpstreamUserAgent adds the upstream client useragent to create a user-agent
94
-// string of the form:
95
-//
96
-//	$dockerUA UpstreamClient($upstreamUA)
97
-func insertUpstreamUserAgent(upstreamUA string, dockerUA string) string {
98
-	return fmt.Sprintf("%s UpstreamClient(%s)", dockerUA, escapeStr(upstreamUA))
99
-}