Skip to content

Commit

Permalink
Change processing of User Platform header
Browse files Browse the repository at this point in the history
  • Loading branch information
YomesInc committed Jul 28, 2021
1 parent ed41a79 commit 0b2e134
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
24 changes: 19 additions & 5 deletions CloudinaryDotNet.Tests/Asset/UrlBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,22 +491,36 @@ private HttpRequestMessage CreateRequest(string userPlatform)
[Test]
public void TestAgentPlatformHeaders()
{
var httpRequestMessage = CreateRequest("UserPlatform");
var httpRequestMessage = CreateRequest("UserPlatform/2.3");

//Can't test the result, so we just verify the UserAgent parameter is sent to the server
StringAssert.IsMatch(@"CloudinaryDotNet\/(\d+)\.(\d+)\.(\d+) \(" + ApiShared.USER_AGENT.Replace("(", "").Replace(")", "") + @"\) \(UserPlatform\)",
StringAssert.IsMatch(@"CloudinaryDotNet\/(\d+)\.(\d+)\.(\d+) \(" + ApiShared.USER_AGENT.Replace("(", "").Replace(")", "") + @"\) UserPlatform/2\.3",
httpRequestMessage.Headers.UserAgent.ToString());
}

[Test]
public void UnityUserAgentShouldNotThrow()
[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
[TestCase("UserPlatform/")]
[TestCase("UserPlatform/ ")]
[TestCase("UserPlatform / 1.2")]
public void UnexpectedUserPlatformShouldNotThrow(string userPlatorm)
{
Assert.DoesNotThrow(() => CreateRequest(userPlatorm));
}

[Test]
[TestCase("Mono 5.11.0 ((HEAD/768f1b247c6)")]
[TestCase("(")]
[TestCase(")")]
public void MalformedUserAgentShouldNotThrow(string userAgent)
{
var userAgent = "Mono 5.11.0 ((HEAD/768f1b247c6)";
var prevAgent = ApiShared.USER_AGENT;
ApiShared.USER_AGENT = userAgent;
try
{
var httpRequestMessage = CreateRequest("UserPlatform");
Assert.DoesNotThrow(() => CreateRequest("UserPlatform"));
}
finally
{
Expand Down
43 changes: 39 additions & 4 deletions CloudinaryDotNet/ApiShared.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,17 @@ private static void AddCommentToUserAgent(
return;
}

var normalizedComment = comment
.Replace(")", string.Empty)
.Replace("(", string.Empty);
var normalizedComment = RemoveBracketsFrom(comment);
userAgentHeader.Add(new ProductInfoHeaderValue($"({normalizedComment})"));
}

private static string RemoveBracketsFrom(string comment)
{
return comment
.Replace(")", string.Empty)
.Replace("(", string.Empty);
}

private static SortedDictionary<string, object> GetCallParams(HttpMethod method, BaseParams parameters)
{
parameters?.Check();
Expand Down Expand Up @@ -474,7 +479,7 @@ private void PrePrepareRequestBody(
userAgentHeader.Add(new ProductInfoHeaderValue("CloudinaryDotNet", CloudinaryVersion.Full));

AddCommentToUserAgent(userAgentHeader, USER_AGENT);
AddCommentToUserAgent(userAgentHeader, UserPlatform);
SetUserPlatform(userAgentHeader);

byte[] authBytes = Encoding.ASCII.GetBytes(GetApiCredentials());
request.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", Convert.ToBase64String(authBytes)));
Expand All @@ -494,6 +499,36 @@ private void PrePrepareRequestBody(
}
}

private void SetUserPlatform(HttpHeaderValueCollection<ProductInfoHeaderValue> userAgentHeader)
{
Console.WriteLine($"UserPlatform: [{UserPlatform}] ======");
var up = UserPlatform?.Trim();
if (string.IsNullOrEmpty(up))
{
return;
}

var upp = up.Split('/');
var productName = GetElement(0);
if (string.IsNullOrEmpty(productName))
{
return;
}

var productVersion = GetElement(1);
if (string.IsNullOrEmpty(productVersion))
{
productVersion = "0.1";
}

userAgentHeader.Add(new ProductInfoHeaderValue(productName, productVersion));

string GetElement(int index)
{
return upp.ElementAtOrDefault(index)?.Trim();
}
}

private async Task PrepareRequestContentAsync(
HttpRequestMessage request,
SortedDictionary<string, object> parameters,
Expand Down

0 comments on commit 0b2e134

Please sign in to comment.