From 6f5af4a5fb2dcec5bbfda5259ccc98d2bb4fe537 Mon Sep 17 00:00:00 2001 From: "m.scharrig" Date: Thu, 17 May 2018 14:51:06 +0200 Subject: [PATCH 1/2] Fixes #1104 --- RestSharp.IntegrationTests/oAuth1Tests.cs | 2 +- RestSharp/Authenticators/OAuth/OAuthTools.cs | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/RestSharp.IntegrationTests/oAuth1Tests.cs b/RestSharp.IntegrationTests/oAuth1Tests.cs index 76e79852e..5acef5299 100644 --- a/RestSharp.IntegrationTests/oAuth1Tests.cs +++ b/RestSharp.IntegrationTests/oAuth1Tests.cs @@ -388,7 +388,7 @@ public void Use_RFC_3986_Encoding_For_Auth_Signature_Base() var escapedString = OAuthTools.UrlEncodeRelaxed(reservedCharacterString); // assert - Assert.AreEqual("%3B%2F%3F%3A%40%26%3D%2B%24%2C%21%2A%27%28%29", escapedString); + Assert.AreEqual("%3B%2F%3F%3A%40%26%3D%2B%24%2C%2521%252A%2527%2528%2529", escapedString); } } } \ No newline at end of file diff --git a/RestSharp/Authenticators/OAuth/OAuthTools.cs b/RestSharp/Authenticators/OAuth/OAuthTools.cs index cd41b84bb..811ca34ae 100644 --- a/RestSharp/Authenticators/OAuth/OAuthTools.cs +++ b/RestSharp/Authenticators/OAuth/OAuthTools.cs @@ -110,20 +110,17 @@ public static string GetTimestamp(DateTime dateTime) /// public static string UrlEncodeRelaxed(string value) { - // Start with RFC 2396 escaping by calling the .NET method to do the work. - // This MAY sometimes exhibit RFC 3986 behavior (according to the documentation). - // If it does, the escaping we do that follows it will be a no-op since the - // characters we search for to replace can't possibly exist in the string. - var escaped = new StringBuilder(Uri.EscapeDataString(value)); - - // Upgrade the escaping to RFC 3986, if necessary. + // Escape RFC 3986 chars first. for (var i = 0; i < uriRfc3986CharsToEscape.Length; i++) { var t = uriRfc3986CharsToEscape[i]; - escaped.Replace(t, uriRfc3968EscapedHex[i]); + value = value.Replace(t, uriRfc3968EscapedHex[i]); } + // Do RFC 2396 escaping by calling the .NET method to do the work. + var escaped = new StringBuilder(Uri.EscapeDataString(value)); + // Return the fully-RFC3986-escaped string. return escaped.ToString(); } From c3e0e2f3a59292bc3580691bdb58def7f5a3ccc5 Mon Sep 17 00:00:00 2001 From: "m.scharrig" Date: Thu, 17 May 2018 15:32:08 +0200 Subject: [PATCH 2/2] fix Codacy issue --- RestSharp/Authenticators/OAuth/OAuthTools.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/RestSharp/Authenticators/OAuth/OAuthTools.cs b/RestSharp/Authenticators/OAuth/OAuthTools.cs index 811ca34ae..a72ff0dcf 100644 --- a/RestSharp/Authenticators/OAuth/OAuthTools.cs +++ b/RestSharp/Authenticators/OAuth/OAuthTools.cs @@ -111,18 +111,19 @@ public static string GetTimestamp(DateTime dateTime) public static string UrlEncodeRelaxed(string value) { // Escape RFC 3986 chars first. + var escapedRfc3986 = new StringBuilder(value); for (var i = 0; i < uriRfc3986CharsToEscape.Length; i++) { var t = uriRfc3986CharsToEscape[i]; - value = value.Replace(t, uriRfc3968EscapedHex[i]); + escapedRfc3986.Replace(t, uriRfc3968EscapedHex[i]); } // Do RFC 2396 escaping by calling the .NET method to do the work. - var escaped = new StringBuilder(Uri.EscapeDataString(value)); + string escapedRfc2396 = Uri.EscapeDataString(escapedRfc3986.ToString()); // Return the fully-RFC3986-escaped string. - return escaped.ToString(); + return escapedRfc2396; } ///