-
Notifications
You must be signed in to change notification settings - Fork 69
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
ed41a79
0b2e134
faa37a1
a1e6328
387a487
31a9e26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,6 +248,26 @@ protected void HandleUnsignedParameters(IDictionary<string, object> parameters) | |
} | ||
} | ||
|
||
private static void AddCommentToUserAgent( | ||
HttpHeaderValueCollection<ProductInfoHeaderValue> userAgentHeader, | ||
string comment) | ||
{ | ||
if (string.IsNullOrEmpty(comment)) | ||
{ | ||
return; | ||
} | ||
|
||
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(); | ||
|
@@ -455,10 +475,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); | ||
SetUserPlatform(userAgentHeader); | ||
|
||
byte[] authBytes = Encoding.ASCII.GetBytes(GetApiCredentials()); | ||
request.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", Convert.ToBase64String(authBytes))); | ||
|
@@ -478,6 +499,36 @@ private void PrePrepareRequestBody( | |
} | ||
} | ||
|
||
private void SetUserPlatform(HttpHeaderValueCollection<ProductInfoHeaderValue> userAgentHeader) | ||
{ | ||
Console.WriteLine($"UserPlatform: [{UserPlatform}] ======"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove debug printing. |
||
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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "0.1" ? is there any special meaning? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @const-cloudinary No special meaning here, just a default value. |
||
} | ||
|
||
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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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})"; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain the problem we are solving here. In a year nobody will remember why we remove brackets from headers.