Ejemplo n.º 1
0
        /// <summary>
        /// Gets the qualified name of the given method, consisting of
        /// fully qualified interface/class name + "." method name.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <returns>qualified name of the method.</returns>
        public static string GetQualifiedMethodName(MethodInfo method)
        {
            AssertUtil.ArgumentNotNull(method, nameof(method));

            return(method.DeclaringType.FullName + "." + method.Name);
        }
Ejemplo n.º 2
0
        public static Stream BuildMultipartData(IDictionary formData, string boundary, Encoding encoding = null)
        {
            AssertUtil.ArgumentNotNull(formData, nameof(formData));
            AssertUtil.ArgumentNotEmpty(boundary, nameof(boundary));

            encoding = encoding == null ? Encoding.UTF8 : encoding;

            var stream = new MultiStream();

            string keyValTemplate = $"--{boundary}\r\n"
                                    + "Content-Disposition: form-data; name=\"{0}\"\r\n"
                                    + "\r\n";

            string fileValTemplate = $"--{boundary}\r\n"
                                     + "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n"
                                     + "Content-Type: application/octet-stream\r\n"
                                     + "\r\n";

            foreach (var key in formData.Keys)
            {
                if (!(key is string))
                {
                    throw new ArgumentException("Key isnot typeof string.");
                }

                if (string.IsNullOrWhiteSpace((string)key))
                {
                    throw new ArgumentException("Key cannot be null or empty.");
                }

                if (formData[key] is IEnumerable)
                {
                    foreach (var value in (IEnumerable)formData[key])
                    {
                        if (value is FileStream)
                        {
                            var fileValStrm = new MemoryStream();
                            using (var writer = new SmartStream(fileValStrm, false))
                            {
                                writer.Encoding = encoding;

                                writer.Write(string.Format(fileValTemplate, ((string)key).Trim(), Path.GetFileName(((FileStream)value).Name)));
                                writer.Flush();
                            }

                            fileValStrm.Position = 0;
                            stream.AppendStream(fileValStrm);

                            stream.AppendStream((FileStream)value);
                            stream.AppendStream(new MemoryStream(new byte[] { (byte)'\r', (byte)'\n' }));
                        }
                        else
                        {
                            var keyValStrm = new MemoryStream();
                            using (var writer = new SmartStream(keyValStrm, false))
                            {
                                writer.Encoding = encoding;

                                writer.Write(string.Format(keyValTemplate, ((string)key).Trim()));
                                writer.Write(value.ToString());
                                writer.Write("\r\n");
                                writer.Flush();
                            }

                            keyValStrm.Position = 0;
                            stream.AppendStream(keyValStrm);
                        }
                    }
                }
                else
                {
                    if (formData[key] is FileStream)
                    {
                        var fileValStrm = new MemoryStream();
                        using (var writer = new SmartStream(fileValStrm, false))
                        {
                            writer.Encoding = encoding;

                            writer.Write(string.Format(fileValTemplate, ((string)key).Trim(), Path.GetFileName(((FileStream)formData[key]).Name)));
                            writer.Flush();
                        }

                        fileValStrm.Position = 0;
                        stream.AppendStream(fileValStrm);

                        stream.AppendStream((FileStream)formData[key]);
                        stream.AppendStream(new MemoryStream(new byte[] { (byte)'\r', (byte)'\n' }));
                    }
                    else
                    {
                        var keyValStrm = new MemoryStream();
                        using (var writer = new SmartStream(keyValStrm, false))
                        {
                            writer.Encoding = encoding;

                            writer.Write(string.Format(keyValTemplate, ((string)key).Trim()));
                            writer.Write(formData[key]?.ToString());
                            writer.Write("\r\n");
                            writer.Flush();
                        }

                        keyValStrm.Position = 0;
                        stream.AppendStream(keyValStrm);
                    }
                }
            }

            // add end boundary string line.
            stream.AppendStream(new MemoryStream(encoding.GetBytes($"--{boundary}--\r\n")));

            return(stream);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Splits string into string arrays. This split method won't split qouted strings, but only text outside of qouted string.
        /// For example: '"text1, text2",text3' will be 2 parts: "text1, text2" and text3.
        /// </summary>
        /// <param name="text">Text to split.</param>
        /// <param name="splitChar">Char that splits text.</param>
        /// <param name="unquote">If true, splitted parst will be unqouted if they are qouted.</param>
        /// <param name="count">Maximum number of substrings to return.</param>
        /// <returns>Returns splitted string.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>text</b> is null reference.</exception>
        public static string[] SplitQuotedString(string text, char splitChar, bool unquote, int count)
        {
            AssertUtil.ArgumentNotNull(text, nameof(text));

            var  splitParts     = new List <string>();     // Holds splitted parts
            int  startPos       = 0;
            bool inQuotedString = false;                   // Holds flag if position is quoted string or not
            char lastChar       = '0';

            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];

                // We have exceeded maximum allowed splitted parts.
                if ((splitParts.Count + 1) >= count)
                {
                    break;
                }

                // We have quoted string start/end.
                if (lastChar != '\\' && c == '\"')
                {
                    inQuotedString = !inQuotedString;
                }
                // We have escaped or normal char.
                //else{

                // We igonre split char in quoted-string.
                if (!inQuotedString)
                {
                    // We have split char, do split.
                    if (c == splitChar)
                    {
                        if (unquote)
                        {
                            splitParts.Add(UnQuoteString(text.Substring(startPos, i - startPos)));
                        }
                        else
                        {
                            splitParts.Add(text.Substring(startPos, i - startPos));
                        }

                        // Store new split part start position.
                        startPos = i + 1;
                    }
                }
                //else{

                lastChar = c;
            }

            // Add last split part to splitted parts list
            if (unquote)
            {
                splitParts.Add(UnQuoteString(text.Substring(startPos, text.Length - startPos)));
            }
            else
            {
                splitParts.Add(text.Substring(startPos, text.Length - startPos));
            }

            return(splitParts.ToArray());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets if the specified IP address is in the range.
        /// </summary>
        /// <param name="ip">The IP address to check.</param>
        /// <param name="range">The IP address range.</param>
        /// <returns>Returns true if the IP address is in the range.</returns>
        public static bool IsIPAddressInRange(IPAddress ip, string range)
        {
            AssertUtil.ArgumentNotNull(ip, nameof(ip));
            AssertUtil.ArgumentNotEmpty(range, nameof(range));

            string ipString = ip.ToString();

            if (ipString == range)
            {
                return(true);
            }

            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                if (range.IndexOf('-') > 0)
                {
                    string[] items = range.Split(new char[] { '-' }, 2);

                    if (!IPAddress.TryParse(items[0], out IPAddress ipStart))
                    {
                        throw new ArgumentException($"Agument '{range}' value is not valid ip range.");
                    }

                    if (!int.TryParse(items[1], out int endValue) || endValue > 255)
                    {
                        throw new ArgumentException($"Agument '{range}' value is not valid ip range.");
                    }

                    byte[] ipBytes    = ip.GetAddressBytes();
                    byte[] startBytes = ipStart.GetAddressBytes();
                    for (int i = 0; i < 4; i++)
                    {
                        if (i == 3)
                        {
                            return(ipBytes[i] >= startBytes[i] && ipBytes[i] <= endValue);
                        }
                        else if (ipBytes[i] != startBytes[i])
                        {
                            return(false);
                        }
                    }
                }
                else if (range.IndexOf('/') > 0)
                {
                    string[] items = range.Split(new char[] { '/' }, 2);

                    if (!IPAddress.TryParse(items[0], out IPAddress ipStart))
                    {
                        throw new ArgumentException($"Agument '{range}' value is not valid ip range.");
                    }

                    if (!int.TryParse(items[1], out int maskValue) || maskValue > 32)
                    {
                        throw new ArgumentException($"Agument '{range}' value is not valid ip range.");
                    }

                    byte[] ipBytes    = ip.GetAddressBytes();
                    byte[] startBytes = ipStart.GetAddressBytes();
                    for (int i = 0; i < 4; i++)
                    {
                        int endValue = startBytes[i];
                        if (((i + 1) * 8 - maskValue) > 0)
                        {
                            endValue += (int)Math.Pow(2, (i + 1) * 8 - maskValue) - 1;
                        }

                        if (ipBytes[i] < startBytes[i] || ipBytes[i] > endValue)
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }
            else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
            {
                if (range.IndexOf('-') > 0)
                {
                    string[] items = range.Split(new char[] { '-' }, 2);

                    if (!IPAddress.TryParse(items[0], out IPAddress ipStart))
                    {
                        throw new ArgumentException($"Agument '{range}' value is not valid ip range.");
                    }

                    if (items[1].Length > 4)
                    {
                        throw new ArgumentException($"Agument '{range}' value is not valid ip range.");
                    }

                    byte[] last2Bytes = items[1].PadLeft(4, '0').FromHex();

                    byte[] ipBytes    = ip.GetAddressBytes();
                    byte[] startBytes = ipStart.GetAddressBytes();
                    for (int i = 0; i < 16; i++)
                    {
                        if (i >= 14)
                        {
                            if (ipBytes[i] < startBytes[i] || ipBytes[i] > last2Bytes[i - 14])
                            {
                                return(false);
                            }
                        }
                        else if (ipBytes[i] != startBytes[i])
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }

            return(false);
        }