Browse code

api/server, dockerversion: modify context key

Signed-off-by: KimMachineGun <geon0250@gmail.com>

KimMachineGun authored on 2018/08/22 11:20:22
Showing 4 changed files
... ...
@@ -12,10 +12,8 @@ import (
12 12
 	"github.com/sirupsen/logrus"
13 13
 )
14 14
 
15
-type contextKey string
16
-
17 15
 // APIVersionKey is the client's requested API version.
18
-const APIVersionKey contextKey = "api-version"
16
+type APIVersionKey struct{}
19 17
 
20 18
 // APIFunc is an adapter to allow the use of ordinary functions as Docker API endpoints.
21 19
 // Any function that has the appropriate signature can be registered as an API endpoint (e.g. getVersion).
... ...
@@ -83,7 +81,7 @@ func VersionFromContext(ctx context.Context) string {
83 83
 		return ""
84 84
 	}
85 85
 
86
-	if val := ctx.Value(APIVersionKey); val != nil {
86
+	if val := ctx.Value(APIVersionKey{}); val != nil {
87 87
 		return val.(string)
88 88
 	}
89 89
 
... ...
@@ -58,7 +58,7 @@ func (v VersionMiddleware) WrapHandler(handler func(ctx context.Context, w http.
58 58
 		if versions.GreaterThan(apiVersion, v.defaultVersion) {
59 59
 			return versionUnsupportedError{version: apiVersion, maxVersion: v.defaultVersion}
60 60
 		}
61
-		ctx = context.WithValue(ctx, httputils.APIVersionKey, apiVersion)
61
+		ctx = context.WithValue(ctx, httputils.APIVersionKey{}, apiVersion)
62 62
 		return handler(ctx, w, r, vars)
63 63
 	}
64 64
 
... ...
@@ -129,8 +129,7 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
129 129
 
130 130
 		// use intermediate variable to prevent "should not use basic type
131 131
 		// string as key in context.WithValue" golint errors
132
-		var ki interface{} = dockerversion.UAStringKey
133
-		ctx := context.WithValue(context.Background(), ki, r.Header.Get("User-Agent"))
132
+		ctx := context.WithValue(context.Background(), dockerversion.UAStringKey{}, r.Header.Get("User-Agent"))
134 133
 		handlerFunc := s.handlerWithGlobalMiddlewares(handler)
135 134
 
136 135
 		vars := mux.Vars(r)
... ...
@@ -10,7 +10,7 @@ import (
10 10
 )
11 11
 
12 12
 // UAStringKey is used as key type for user-agent string in net/context struct
13
-const UAStringKey = "upstream-user-agent"
13
+type UAStringKey struct{}
14 14
 
15 15
 // DockerUserAgent is the User-Agent the Docker client uses to identify itself.
16 16
 // In accordance with RFC 7231 (5.5.3) is of the form:
... ...
@@ -39,9 +39,9 @@ func DockerUserAgent(ctx context.Context) string {
39 39
 func getUserAgentFromContext(ctx context.Context) string {
40 40
 	var upstreamUA string
41 41
 	if ctx != nil {
42
-		var ki interface{} = ctx.Value(UAStringKey)
42
+		var ki interface{} = ctx.Value(UAStringKey{})
43 43
 		if ki != nil {
44
-			upstreamUA = ctx.Value(UAStringKey).(string)
44
+			upstreamUA = ctx.Value(UAStringKey{}).(string)
45 45
 		}
46 46
 	}
47 47
 	return upstreamUA