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

Fixes format exception thrown when user agent header value is incorrect #283

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 27 additions & 7 deletions CloudinaryDotNet.Tests/Asset/UrlBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,23 +475,43 @@ public void TestExcludeEmptyTransformation()
Assert.AreEqual(TestConstants.DefaultImageUpPath + "c_fill,x_100,y_100/test", uri);
}

[Test]
public void TestAgentPlatformHeaders()
private HttpRequestMessage CreateRequest(string userPlatform)
{
var request = new HttpRequestMessage { RequestUri = new Uri("http://dummy.com") };
m_api.UserPlatform = "Test/1.0";
m_api.UserPlatform = userPlatform;

m_api.PrepareRequestBody(
request,
HttpMethod.GET,
new SortedDictionary<string, object>(),
new FileDescription(""));
return request;
}

[Test]
public void TestAgentPlatformHeaders()
{
var httpRequestMessage = CreateRequest("UserPlatform");

//Can't test the result, so we just verify the UserAgent parameter is sent to the server
StringAssert.AreEqualIgnoringCase($"{m_api.UserPlatform} {ApiShared.USER_AGENT}",
request.Headers.UserAgent.ToString());
StringAssert.IsMatch(@"Test\/1\.0 CloudinaryDotNet\/(\d+)\.(\d+)\.(\d+) \(.*\)",
request.Headers.UserAgent.ToString());
StringAssert.IsMatch(@"CloudinaryDotNet\/(\d+)\.(\d+)\.(\d+) \(" + ApiShared.USER_AGENT.Replace("(", "").Replace(")", "") + @"\) \(UserPlatform\)",
httpRequestMessage.Headers.UserAgent.ToString());
}

[Test]
public void UnityUserAgentShouldNotThrow()
{
var userAgent = "Mono 5.11.0 ((HEAD/768f1b247c6)";
var prevAgent = ApiShared.USER_AGENT;
ApiShared.USER_AGENT = userAgent;
try
{
var httpRequestMessage = CreateRequest("UserPlatform");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better also to check that it is serialized correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@const-cloudinary We think that such a check would be complicated and nonobvious (need to use regex etc. like in TestAgentPlatformHeaders). These tests were added to assert that "bad" data won't break anything — DoesNotThrow seems to be fine here.

}
finally
{
ApiShared.USER_AGENT = prevAgent;
}
}

[Test]
Expand Down
24 changes: 20 additions & 4 deletions CloudinaryDotNet/ApiShared.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ protected void HandleUnsignedParameters(IDictionary<string, object> parameters)
}
}

private static void AddCommentToUserAgent(
HttpHeaderValueCollection<ProductInfoHeaderValue> userAgentHeader,
string comment)
{
if (string.IsNullOrEmpty(comment))
{
return;
}

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

private static SortedDictionary<string, object> GetCallParams(HttpMethod method, BaseParams parameters)
{
parameters?.Check();
Expand Down Expand Up @@ -455,10 +470,11 @@ private void PrePrepareRequestBody(

// Add platform information to the USER_AGENT header
// This is intended for platform information and not individual applications!
var userPlatform = string.IsNullOrEmpty(UserPlatform)
? USER_AGENT
: string.Format(CultureInfo.InvariantCulture, "{0} {1}", UserPlatform, USER_AGENT);
request.Headers.Add("User-Agent", userPlatform);
var userAgentHeader = request.Headers.UserAgent;
userAgentHeader.Add(new ProductInfoHeaderValue("CloudinaryDotNet", CloudinaryVersion.Full));

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

byte[] authBytes = Encoding.ASCII.GetBytes(GetApiCredentials());
request.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", Convert.ToBase64String(authBytes)));
Expand Down
7 changes: 1 addition & 6 deletions CloudinaryDotNet/ApiShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public partial class ApiShared : ISignProvider
/// <summary>
/// User agent for cloudinary API requests.
/// </summary>
public static string USER_AGENT = BuildUserAgent();
public static string USER_AGENT = RuntimeInformation.FrameworkDescription;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of this constant does not correspond the content. Very confusing. Please refactor.


/// <summary>
/// Sends HTTP requests and receives HTTP responses.
Expand Down Expand Up @@ -803,10 +803,5 @@ public string BuildUploadFormShared(string field, string resourceType, SortedDic

return builder.ToString();
}

private static string BuildUserAgent()
{
return $"CloudinaryDotNet/{CloudinaryVersion.Full} ({RuntimeInformation.FrameworkDescription})";
}
}
}