/// <summary>
        /// 从参数字符串生成键或索引集合
        /// 如果参数未解码过,则将被解码再处理
        /// </summary>
        /// <param name="parameters">http请求参数</param>
        /// <param name="decoded">参数是否已解码过</param>
        /// <returns></returns>
        public static HttpNameValueCollection Parse(string parameters, bool decoded)
        {
            var collection = new HttpNameValueCollection();
            if (string.IsNullOrWhiteSpace(parameters) == true)
            {
                return collection;
            }

            if (decoded == false)
            {
                parameters = HttpUtility.UrlDecode(parameters, Encoding.UTF8);
            }

            var keyValues = parameters.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in keyValues)
            {
                var kv = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                if (kv.Length == 2)
                {
                    collection.Add(kv[0], kv[1]);
                }
            }
            return collection;
        }
 /// <summary>
 /// 调试视图
 /// </summary>
 /// <param name="view">查看的对象</param>
 public HttpNameValueCollectionView(HttpNameValueCollection view)
 {
     this.view = view;
 }
Example #3
0
        /// <summary>
        /// 生成表单和文件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="buffer"></param>   
        /// <param name="boundary">边界</param>
        private static void GenerateMultipartFormAndFiles(HttpRequest request, IReceiveBuffer buffer, string boundary)
        {
            var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary);
            var maxPosition = buffer.Length - Encoding.ASCII.GetBytes("--\r\n").Length;

            var files = new List<HttpFile>();
            var form = new HttpNameValueCollection();

            buffer.Position = buffer.Position + boundaryBytes.Length;
            while (buffer.Position < maxPosition)
            {
                var headLength = buffer.IndexOf(Protocol.DoubleCrlf) + Protocol.DoubleCrlf.Length;
                if (headLength < Protocol.DoubleCrlf.Length)
                {
                    break;
                }

                var head = buffer.ReadString(headLength, Encoding.UTF8);
                var bodyLength = buffer.IndexOf(boundaryBytes);
                if (bodyLength < 0)
                {
                    break;
                }

                var mHead = new MultipartHead(head);
                if (mHead.IsFile == true)
                {
                    var stream = buffer.ReadArray(bodyLength);
                    var file = new HttpFile(mHead, stream);
                    files.Add(file);
                }
                else
                {
                    var byes = buffer.ReadArray(bodyLength);
                    var value = HttpUtility.UrlDecode(byes, Encoding.UTF8);
                    form.Add(mHead.Name, value);
                }
                buffer.Position = buffer.Position + boundaryBytes.Length;
            }

            request.Form = form;
            request.Files = files.ToArray();
        }