splunk / splunk-sdk-csharp-pcl

Splunk's next generation C# SDK
https://dev.splunk.com/enterprise/docs/csharp
Apache License 2.0
64 stars 46 forks source link

Long argument value causes UriFormatException Exception #100

Closed ewan-derivco closed 2 years ago

ewan-derivco commented 4 years ago

Uri.EscapeDataString fails if the string is longer than 32k characters (depending on framework?)

This causes an exception to be thrown in Splunk.Client.Context.CreateStringContent while the API can accept the long value.

Fix by splitting the string and looping?

        /// <summary>
        /// deal with long searches
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static string EscapeDataString(string value)
        {
            int limit = 20000;

            StringBuilder sb = new StringBuilder();
            int loops = value.Length / limit;

            for (int i = 0; i <= loops; i++)
            {
                if (i < loops)
                {
                    sb.Append(Uri.EscapeDataString(value.Substring(limit * i, limit)));
                }
                else
                {
                    sb.Append(Uri.EscapeDataString(value.Substring(limit * i)));
                }
            }
            return sb.ToString();
        }

https://docs.microsoft.com/en-us/dotnet/api/system.uri.escapedatastring?view=netframework-4.8

ncanumalla-splunk commented 2 years ago

This SDK is deprecated and no longer under active development.