Skip to content

Commit 54a4d15

Browse files
committed
UrlEncode fix for input strings > 32766 limit
Initial code by @jeffthieleke-wf with my additions of null parameter check, correct maxLength of 32766, and matching indentations per @haacked.
1 parent 4f90124 commit 54a4d15

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

RestSharp/Extensions/StringExtensions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,24 @@ public static string UrlDecode(this string input)
4949
/// </summary>
5050
public static string UrlEncode(this string input)
5151
{
52-
return Uri.EscapeDataString(input);
52+
const int maxLength = 32766;
53+
if (input == null)
54+
throw new ArgumentNullException("input");
55+
56+
if (input.Length <= maxLength)
57+
return Uri.EscapeDataString(input);
58+
59+
StringBuilder sb = new StringBuilder(input.Length * 2);
60+
int index = 0;
61+
while (index < input.Length)
62+
{
63+
int length = Math.Min(input.Length - index, maxLength);
64+
string subString = input.Substring(index, length);
65+
sb.Append(Uri.EscapeDataString(subString));
66+
index += subString.Length;
67+
}
68+
69+
return sb.ToString();
5370
}
5471

5572
public static string HtmlDecode(this string input)

0 commit comments

Comments
 (0)