Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor User-Agent construction in Marshaller.cs #3423

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"core": {
"changeLogMessages": [
"Improve user agent string construction in Marshaller.cs"
],
"type": "patch",
"updateMinimum": true
}
}
31 changes: 22 additions & 9 deletions sdk/src/Core/Amazon.Runtime/Pipeline/Handlers/Marshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using Amazon.Runtime.Telemetry;
Expand Down Expand Up @@ -106,29 +107,28 @@ private static void SetRecursionDetectionHeader(IDictionary<string, string> head

private static void SetUserAgentHeader(IRequestContext requestContext)
{
var sb = new StringBuilder(requestContext.ClientConfig.UserAgent);
var sb = new StringBuilder(256);

sb.Append(InternalSDKUtils.ReplaceInvalidUserAgentCharacters(requestContext.ClientConfig.UserAgent));

var clientAppId = requestContext.ClientConfig.ClientAppId;
if (!string.IsNullOrEmpty(clientAppId))
sb.AppendFormat(" app/{0}", clientAppId);
sb.Append(" app/").Append(InternalSDKUtils.ReplaceInvalidUserAgentCharacters(clientAppId));

var retryMode = requestContext.ClientConfig.RetryMode.ToString().ToLower();
sb.AppendFormat(" cfg/retry-mode#{0}", retryMode);
sb.Append(" cfg/retry-mode#}").Append(ToUserAgentHeaderString(requestContext.ClientConfig.RetryMode));

sb.AppendFormat(" md/{0}", requestContext.IsAsync ? "ClientAsync" : "ClientSync");
sb.Append(" md/").Append(requestContext.IsAsync ? "ClientAsync" : "ClientSync");

sb.AppendFormat(" cfg/init-coll#{0}", AWSConfigs.InitializeCollections ? "1" : "0");
sb.Append(" cfg/init-coll#").Append(AWSConfigs.InitializeCollections ? '1' : '0');

var userAgentAddition = requestContext.OriginalRequest.UserAgentAddition;
if (!string.IsNullOrEmpty(userAgentAddition))
{
sb.AppendFormat(" {0}", userAgentAddition);
sb.Append(' ').Append(InternalSDKUtils.ReplaceInvalidUserAgentCharacters(userAgentAddition));
}

var userAgent = sb.ToString();

userAgent = InternalSDKUtils.ReplaceInvalidUserAgentCharacters(userAgent);

if (requestContext.ClientConfig.UseAlternateUserAgentHeader)
{
requestContext.Request.Headers[HeaderKeys.XAmzUserAgentHeader] = userAgent;
Expand All @@ -138,5 +138,18 @@ private static void SetUserAgentHeader(IRequestContext requestContext)
requestContext.Request.Headers[HeaderKeys.UserAgentHeader] = userAgent;
}
}

private static string ToUserAgentHeaderString(RequestRetryMode requestRetryMode)
{
switch (requestRetryMode)
{
case RequestRetryMode.Standard:
return "standard";
case RequestRetryMode.Adaptive:
return "adaptive";
default:
return requestRetryMode.ToString().ToLowerInvariant();
}
}
}
}
2 changes: 1 addition & 1 deletion sdk/src/Core/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.AWS4Signer.#CanonicalizeHeaders(System.Collections.Generic.ICollection`1<System.Collections.Generic.KeyValuePair`2<System.String,System.String>>)")]
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.S3Signer.#BuildCanonicalizedHeaders(System.Collections.Generic.IDictionary`2<System.String,System.String>)")]
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.AWS4Signer.#DetermineSigningRegion(Amazon.Runtime.IClientConfig,System.String,Amazon.RegionEndpoint,Amazon.Runtime.Internal.IRequest)")]

[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.AWS4Signer.#CanonicalizeHeaders(System.Collections.Generic.IDictionary`2<System.String,System.String>)")]
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Auth.AWS4Signer.#CanonicalizeHeaderNames(System.Collections.Generic.IDictionary`2<System.String,System.String>)")]
[module: SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Scope = "member", Target = "Amazon.Runtime.Internal.Marshaller.#ToUserAgentHeaderString(Amazon.Runtime.RequestRetryMode)")]

// Types names matching namespaces
[module: SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", Scope = "type", Target = "Amazon.Auth.AccessControlPolicy.Policy")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,15 @@ public void EnsureCredentialProfileDetectorSetup()
// the properties are used at least once.
Assert.IsTrue(profileOptionsProperties.SetEquals(referencedProfileOptionsProperties));
}

[TestMethod]
public void LookForRequestRetryModeChanges()
{
var expectedHash = "2FC699DB6A4284C5D53F3B7FA4E64165576FD1F6AE458BC737AE503F06DFBE4C";
AssertExtensions.AssertEnumUnchanged(
typeof(RequestRetryMode),
expectedHash,
"The Amazon.Runtime.Internal.Marshaller.ToUserAgentHeaderString method implementation may need to be updated.");
}
}
}