Skip to content

Commit

Permalink
Merge pull request #2154 from aws/feat-ua
Browse files Browse the repository at this point in the history
Modify user agent header syntax and add support for app ID
  • Loading branch information
wty-Bryant authored Jul 12, 2023
2 parents 5f1a82d + 5734243 commit 34540e9
Show file tree
Hide file tree
Showing 14,303 changed files with 19,374 additions and 14,736 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
9 changes: 9 additions & 0 deletions .changelog/750daed90e254d42b50383de16cf6852.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "750daed9-0e25-4d42-b503-83de16cf6852",
"type": "feature",
"collapse": true,
"description": "Modify user agent syntax and introduce support for optional app identifier in UA header",
"modules": [
"."
]
}
8 changes: 8 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ type Config struct {
// `config.LoadDefaultConfig`. You should not populate this structure
// programmatically, or rely on the values here within your applications.
RuntimeEnvironment RuntimeEnvironment

// AppId is an optional application specific identifier that can be set.
// When set it will be appended to the User-Agent header of every request
// in the form of App/{AppId}. This variable is sourced from environment
// variable AWS_SDK_UA_APP_ID or the shared config profile attribute sdk_ua_app_id.
// See https://docs.aws.amazon.com/sdkref/latest/guide/settings-reference.html for
// more information on environment variables and shared config settings.
AppID string
}

// NewConfig returns a new Config pointer that can be chained with builder
Expand Down
26 changes: 22 additions & 4 deletions aws/middleware/user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func (k SDKAgentKeyType) string() string {

const execEnvVar = `AWS_EXECUTION_ENV`

var validChars = map[rune]bool{
'!': true, '#': true, '$': true, '%': true, '&': true, '\'': true, '*': true, '+': true,
'-': true, '.': true, '^': true, '_': true, '`': true, '|': true, '~': true,
}

// requestUserAgent is a build middleware that set the User-Agent for the request.
type requestUserAgent struct {
sdkAgent, userAgent *smithyhttp.UserAgentBuilder
Expand Down Expand Up @@ -178,24 +183,24 @@ func getOrAddRequestUserAgent(stack *middleware.Stack) (*requestUserAgent, error

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

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

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

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

// ID the name of the middleware.
Expand Down Expand Up @@ -241,3 +246,16 @@ func updateHTTPHeader(request *smithyhttp.Request, header string, value string)
}
request.Header[header] = append(request.Header[header][:0], current)
}

func rules(r rune) rune {
switch {
case r >= '0' && r <= '9':
return r
case r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z':
return r
case validChars[r]:
return r
default:
return '-'
}
}
Loading

0 comments on commit 34540e9

Please sign in to comment.