Skip to content

Commit 0b2e134

Browse files
committed
Change processing of User Platform header
1 parent ed41a79 commit 0b2e134

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

CloudinaryDotNet.Tests/Asset/UrlBuilderTest.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,22 +491,36 @@ private HttpRequestMessage CreateRequest(string userPlatform)
491491
[Test]
492492
public void TestAgentPlatformHeaders()
493493
{
494-
var httpRequestMessage = CreateRequest("UserPlatform");
494+
var httpRequestMessage = CreateRequest("UserPlatform/2.3");
495495

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

501501
[Test]
502-
public void UnityUserAgentShouldNotThrow()
502+
[TestCase(null)]
503+
[TestCase("")]
504+
[TestCase(" ")]
505+
[TestCase("UserPlatform/")]
506+
[TestCase("UserPlatform/ ")]
507+
[TestCase("UserPlatform / 1.2")]
508+
public void UnexpectedUserPlatformShouldNotThrow(string userPlatorm)
509+
{
510+
Assert.DoesNotThrow(() => CreateRequest(userPlatorm));
511+
}
512+
513+
[Test]
514+
[TestCase("Mono 5.11.0 ((HEAD/768f1b247c6)")]
515+
[TestCase("(")]
516+
[TestCase(")")]
517+
public void MalformedUserAgentShouldNotThrow(string userAgent)
503518
{
504-
var userAgent = "Mono 5.11.0 ((HEAD/768f1b247c6)";
505519
var prevAgent = ApiShared.USER_AGENT;
506520
ApiShared.USER_AGENT = userAgent;
507521
try
508522
{
509-
var httpRequestMessage = CreateRequest("UserPlatform");
523+
Assert.DoesNotThrow(() => CreateRequest("UserPlatform"));
510524
}
511525
finally
512526
{

CloudinaryDotNet/ApiShared.Internal.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,17 @@ private static void AddCommentToUserAgent(
257257
return;
258258
}
259259

260-
var normalizedComment = comment
261-
.Replace(")", string.Empty)
262-
.Replace("(", string.Empty);
260+
var normalizedComment = RemoveBracketsFrom(comment);
263261
userAgentHeader.Add(new ProductInfoHeaderValue($"({normalizedComment})"));
264262
}
265263

264+
private static string RemoveBracketsFrom(string comment)
265+
{
266+
return comment
267+
.Replace(")", string.Empty)
268+
.Replace("(", string.Empty);
269+
}
270+
266271
private static SortedDictionary<string, object> GetCallParams(HttpMethod method, BaseParams parameters)
267272
{
268273
parameters?.Check();
@@ -474,7 +479,7 @@ private void PrePrepareRequestBody(
474479
userAgentHeader.Add(new ProductInfoHeaderValue("CloudinaryDotNet", CloudinaryVersion.Full));
475480

476481
AddCommentToUserAgent(userAgentHeader, USER_AGENT);
477-
AddCommentToUserAgent(userAgentHeader, UserPlatform);
482+
SetUserPlatform(userAgentHeader);
478483

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

502+
private void SetUserPlatform(HttpHeaderValueCollection<ProductInfoHeaderValue> userAgentHeader)
503+
{
504+
Console.WriteLine($"UserPlatform: [{UserPlatform}] ======");
505+
var up = UserPlatform?.Trim();
506+
if (string.IsNullOrEmpty(up))
507+
{
508+
return;
509+
}
510+
511+
var upp = up.Split('/');
512+
var productName = GetElement(0);
513+
if (string.IsNullOrEmpty(productName))
514+
{
515+
return;
516+
}
517+
518+
var productVersion = GetElement(1);
519+
if (string.IsNullOrEmpty(productVersion))
520+
{
521+
productVersion = "0.1";
522+
}
523+
524+
userAgentHeader.Add(new ProductInfoHeaderValue(productName, productVersion));
525+
526+
string GetElement(int index)
527+
{
528+
return upp.ElementAtOrDefault(index)?.Trim();
529+
}
530+
}
531+
497532
private async Task PrepareRequestContentAsync(
498533
HttpRequestMessage request,
499534
SortedDictionary<string, object> parameters,

0 commit comments

Comments
 (0)