Exemple #1
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());
        }
Exemple #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);
        }