Skip to content

Commit 34540e9

Browse files
authored
Merge pull request #2154 from aws/feat-ua
Modify user agent header syntax and add support for app ID
2 parents 5f1a82d + 5734243 commit 34540e9

File tree

14,303 files changed

+19374
-14736
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

14,303 files changed

+19374
-14736
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": "750daed9-0e25-4d42-b503-83de16cf6852",
3+
"type": "feature",
4+
"collapse": true,
5+
"description": "Modify user agent syntax and introduce support for optional app identifier in UA header",
6+
"modules": [
7+
"."
8+
]
9+
}

aws/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ type Config struct {
132132
// `config.LoadDefaultConfig`. You should not populate this structure
133133
// programmatically, or rely on the values here within your applications.
134134
RuntimeEnvironment RuntimeEnvironment
135+
136+
// AppId is an optional application specific identifier that can be set.
137+
// When set it will be appended to the User-Agent header of every request
138+
// in the form of App/{AppId}. This variable is sourced from environment
139+
// variable AWS_SDK_UA_APP_ID or the shared config profile attribute sdk_ua_app_id.
140+
// See https://docs.aws.amazon.com/sdkref/latest/guide/settings-reference.html for
141+
// more information on environment variables and shared config settings.
142+
AppID string
135143
}
136144

137145
// NewConfig returns a new Config pointer that can be chained with builder

aws/middleware/user_agent.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (k SDKAgentKeyType) string() string {
5959

6060
const execEnvVar = `AWS_EXECUTION_ENV`
6161

62+
var validChars = map[rune]bool{
63+
'!': true, '#': true, '$': true, '%': true, '&': true, '\'': true, '*': true, '+': true,
64+
'-': true, '.': true, '^': true, '_': true, '`': true, '|': true, '~': true,
65+
}
66+
6267
// requestUserAgent is a build middleware that set the User-Agent for the request.
6368
type requestUserAgent struct {
6469
sdkAgent, userAgent *smithyhttp.UserAgentBuilder
@@ -178,24 +183,24 @@ func getOrAddRequestUserAgent(stack *middleware.Stack) (*requestUserAgent, error
178183

179184
// AddUserAgentKey adds the component identified by name to the User-Agent string.
180185
func (u *requestUserAgent) AddUserAgentKey(key string) {
181-
u.userAgent.AddKey(key)
186+
u.userAgent.AddKey(strings.Map(rules, key))
182187
}
183188

184189
// AddUserAgentKeyValue adds the key identified by the given name and value to the User-Agent string.
185190
func (u *requestUserAgent) AddUserAgentKeyValue(key, value string) {
186-
u.userAgent.AddKeyValue(key, value)
191+
u.userAgent.AddKeyValue(strings.Map(rules, key), strings.Map(rules, value))
187192
}
188193

189194
// AddUserAgentKey adds the component identified by name to the User-Agent string.
190195
func (u *requestUserAgent) AddSDKAgentKey(keyType SDKAgentKeyType, key string) {
191196
// TODO: should target sdkAgent
192-
u.userAgent.AddKey(keyType.string() + "/" + key)
197+
u.userAgent.AddKey(keyType.string() + "/" + strings.Map(rules, key))
193198
}
194199

195200
// AddUserAgentKeyValue adds the key identified by the given name and value to the User-Agent string.
196201
func (u *requestUserAgent) AddSDKAgentKeyValue(keyType SDKAgentKeyType, key, value string) {
197202
// TODO: should target sdkAgent
198-
u.userAgent.AddKeyValue(keyType.string()+"/"+key, value)
203+
u.userAgent.AddKeyValue(keyType.string(), strings.Map(rules, key)+"#"+strings.Map(rules, value))
199204
}
200205

201206
// ID the name of the middleware.
@@ -241,3 +246,16 @@ func updateHTTPHeader(request *smithyhttp.Request, header string, value string)
241246
}
242247
request.Header[header] = append(request.Header[header][:0], current)
243248
}
249+
250+
func rules(r rune) rune {
251+
switch {
252+
case r >= '0' && r <= '9':
253+
return r
254+
case r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z':
255+
return r
256+
case validChars[r]:
257+
return r
258+
default:
259+
return '-'
260+
}
261+
}

0 commit comments

Comments
 (0)