Exemplo n.º 1
0
        /// <summary>
        /// 从Owin中提取所需的请求信息
        /// </summary>
        /// <param name="environment">Owin参数</param>
        public static NFinal.Owin.Request GetRequest(this IDictionary <string, object> environment)
        {
            NFinal.Owin.Request request = new Request();
            request.environment = environment;
            request.headers     = (IDictionary <string, string[]>)environment[OwinKeys.RequestHeaders];
            request.cookies     = environment.GetCookies();
            request.stream      = (Stream)environment[OwinKeys.RequestBody];
            //请求参数
            request.requestPath = (string)environment[OwinKeys.RequestPath];
            request.parameters  = new NFinal.NameValueCollection();
            request.files       = null;
            //获取POST方法
            string methodTypeTemp = (string)environment[OwinKeys.RequestMethod];

            switch (methodTypeTemp)
            {
            case NFinal.Constant.MethodTypePOST: request.methodType = MethodType.POST; break;

            case NFinal.Constant.MethodTypeGET: request.methodType = MethodType.GET; break;

            case NFinal.Constant.MethodTypeDELETE: request.methodType = MethodType.DELETE; break;

            case NFinal.Constant.MethodTypePUT: request.methodType = MethodType.PUT; break;

            case NFinal.Constant.MethodTypeAJAX: request.methodType = MethodType.AJAX; break;

            default: request.methodType = MethodType.NONE; break;
            }
            //提取内容类型
            if (request.methodType == MethodType.POST)
            {
                ContentType contentType       = ContentType.NONE;
                string      contentTypeString = null;
                if (request.headers.ContainsKey(NFinal.Constant.HeaderContentType))
                {
                    contentTypeString = request.headers[NFinal.Constant.HeaderContentType][0];
                    switch (contentTypeString.Split(NFinal.Constant.CharSemicolon)[0])
                    {
                    case NFinal.Constant.ContentType_Multipart_form_data: contentType = ContentType.Multipart_form_data; break;

                    case NFinal.Constant.ContentType_Text_json:
                    case NFinal.Constant.ContentType_Application_json: contentType = ContentType.Application_json; break;

                    case NFinal.Constant.ContentType_Application_x_www_form_urlencoded: contentType = ContentType.Application_x_www_form_urlencoded; break;

                    case NFinal.Constant.ContentType_Application_xml:
                    case NFinal.Constant.ContentType_Text_xml: contentType = ContentType.Text_xml; break;

                    default: contentType = ContentType.NONE; break;
                    }
                }
                //提取form参数
                if (request.stream != System.IO.Stream.Null)
                {
                    if (contentType == ContentType.Application_x_www_form_urlencoded)
                    {
                        System.IO.StreamReader sr = new System.IO.StreamReader(request.stream);
                        string body = sr.ReadToEnd();
                        sr.Dispose();
                        if (!string.IsNullOrEmpty(body))
                        {
                            string[] formArray = body.Split(NFinal.Constant.CharAnd, NFinal.Constant.CharEqual);
                            if (formArray.Length > 1 && (formArray.Length & 1) == 0)
                            {
                                for (int i = 0; i < formArray.Length; i += 2)
                                {
                                    request.parameters.Add(formArray[i], formArray[i + 1].UrlDecode());
                                }
                            }
                        }
                    }
                    //else if (contentType == NFinal.ContentType.Text_xml)
                    //{
                    //    System.IO.StreamReader sr = new System.IO.StreamReader(stream);
                    //    string body = sr.ReadToEnd();
                    //    sr.Dispose();
                    //    if (!string.IsNullOrEmpty(body))
                    //    {
                    //        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    //        doc.LoadXml(body);
                    //        if (doc.DocumentElement != null)
                    //        {
                    //            foreach (System.Xml.XmlElement xmlNode in doc.DocumentElement.ChildNodes)
                    //            {
                    //                get.Add(xmlNode.Name, xmlNode.Value);
                    //            }
                    //        }
                    //    }
                    //}
                    else if (contentType == ContentType.Application_json)
                    {
                        System.IO.StreamReader sr = new System.IO.StreamReader(request.stream);
                        string body = sr.ReadToEnd();
                        sr.Dispose();
                        NFinal.Json.IJsonSerialize   serializable = new NFinal.Json.NewtonsoftJsonSerialize();
                        IDictionary <string, object> data         = serializable.DeserializeObject <IDictionary <string, object> >(body);
                        foreach (var ele in data)
                        {
                            request.parameters.Add(ele.Key, ele.Value.ToString());
                        }
                    }
                    else if (contentType == ContentType.Multipart_form_data)
                    {
                        //multipart/form-data
                        string boundary  = NFinal.Http.HttpMultipart.HttpMultipart.boundaryReg.Match(contentTypeString).Value;
                        var    multipart = new NFinal.Http.HttpMultipart.HttpMultipart(request.stream, boundary);
                        request.files = new Dictionary <string, NFinal.Http.HttpMultipart.HttpFile>(StringComparer.Ordinal);
                        foreach (var httpMultipartBoundary in multipart.GetBoundaries())
                        {
                            if (string.IsNullOrEmpty(httpMultipartBoundary.Filename))
                            {
                                string name = httpMultipartBoundary.Name;
                                if (!string.IsNullOrEmpty(name))
                                {
                                    string value = new System.IO.StreamReader(httpMultipartBoundary.Value).ReadToEnd();
                                    request.parameters.Add(name, value);
                                }
                            }
                            else
                            {
                                request.files.Add(httpMultipartBoundary.Name, new NFinal.Http.HttpMultipart.HttpFile(httpMultipartBoundary));
                            }
                        }
                    }
                }
            }
            request.queryString = (string)environment[OwinKeys.RequestQueryString];
            //提取URL?后的参数
            if (request.queryString.Length > 0)
            {
                string[] queryArray = request.queryString.Split(NFinal.Constant.CharAnd, NFinal.Constant.CharEqual);
                if (queryArray.Length > 1 && (queryArray.Length & 1) == 0)
                {
                    for (int i = 0; i < queryArray.Length; i += 2)
                    {
                        request.parameters.Add(queryArray[i], queryArray[i + 1].UrlDecode());
                    }
                }
            }
            return(request);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Http参数解析函数
        /// </summary>
        /// <param name="requestMethod"></param>
        /// <param name="contentTypeString"></param>
        /// <param name="requestQueryString"></param>
        /// <param name="requestBody"></param>
        /// <returns></returns>
        public NameValueCollection GetParameters(string requestMethod, string contentTypeString, string requestQueryString, Stream requestBody)
        {
            //请求参数
            var        parameters = new NFinal.NameValueCollection();
            MethodType methodType = MethodType.NONE;

            //获取POST方法
            switch (requestMethod)
            {
            case NFinal.Constant.MethodTypePOST: methodType = MethodType.POST; break;

            case NFinal.Constant.MethodTypeGET: methodType = MethodType.GET; break;

            case NFinal.Constant.MethodTypeDELETE: methodType = MethodType.DELETE; break;

            case NFinal.Constant.MethodTypePUT: methodType = MethodType.PUT; break;

            case NFinal.Constant.MethodTypeAJAX: methodType = MethodType.AJAX; break;

            default: methodType = MethodType.NONE; break;
            }
            //提取内容类型
            if (methodType == MethodType.POST)
            {
                ContentType contentType = ContentType.NONE;
                switch (contentTypeString.Split(NFinal.Constant.CharSemicolon)[0])
                {
                case NFinal.Constant.ContentType_Multipart_form_data: contentType = ContentType.Multipart_form_data; break;

                case NFinal.Constant.ContentType_Text_json:
                case NFinal.Constant.ContentType_Application_json: contentType = ContentType.Application_json; break;

                case NFinal.Constant.ContentType_Application_x_www_form_urlencoded: contentType = ContentType.Application_x_www_form_urlencoded; break;

                case NFinal.Constant.ContentType_Application_xml:
                case NFinal.Constant.ContentType_Text_xml: contentType = ContentType.Text_xml; break;

                default: contentType = ContentType.NONE; break;
                }
                //提取form参数
                if (requestBody != System.IO.Stream.Null)
                {
                    if (contentType == ContentType.Application_x_www_form_urlencoded)
                    {
                        System.IO.StreamReader sr = new System.IO.StreamReader(requestBody);
                        string body = sr.ReadToEnd();
                        sr.Dispose();
                        if (!string.IsNullOrEmpty(body))
                        {
                            string[] formArray = body.Split(NFinal.Constant.CharAnd, NFinal.Constant.CharEqual);
                            if (formArray.Length > 1 && (formArray.Length & 1) == 0)
                            {
                                for (int i = 0; i < formArray.Length; i += 2)
                                {
                                    parameters.Add(formArray[i], formArray[i + 1].UrlDecode());
                                }
                            }
                        }
                    }
                    //else if (contentType == NFinal.ContentType.Text_xml)
                    //{
                    //    System.IO.StreamReader sr = new System.IO.StreamReader(stream);
                    //    string body = sr.ReadToEnd();
                    //    sr.Dispose();
                    //    if (!string.IsNullOrEmpty(body))
                    //    {
                    //        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    //        doc.LoadXml(body);
                    //        if (doc.DocumentElement != null)
                    //        {
                    //            foreach (System.Xml.XmlElement xmlNode in doc.DocumentElement.ChildNodes)
                    //            {
                    //                get.Add(xmlNode.Name, xmlNode.Value);
                    //            }
                    //        }
                    //    }
                    //}
                    else if (contentType == ContentType.Application_json)
                    {
                        System.IO.StreamReader sr = new System.IO.StreamReader(requestBody);
                        string body = sr.ReadToEnd();
                        sr.Dispose();
                        NFinal.Json.IJsonSerialize   serializable = new NFinal.Json.NewtonsoftJsonSerialize();
                        IDictionary <string, object> data         = serializable.DeserializeObject <IDictionary <string, object> >(body);
                        foreach (var ele in data)
                        {
                            parameters.Add(ele.Key, ele.Value.ToString());
                        }
                    }
                    //else if (contentType == ContentType.Multipart_form_data)
                    //{
                    //    //multipart/form-data
                    //    string boundary = NFinal.Http.HttpMultipart.HttpMultipart.boundaryReg.Match(contentTypeString).Value;
                    //    var multipart = new NFinal.Http.HttpMultipart.HttpMultipart(requestBody, boundary);
                    //    files = new NFinal.Collections.FastDictionary<string, NFinal.Http.HttpMultipart.HttpFile>(StringComparer.Ordinal);
                    //    foreach (var httpMultipartBoundary in multipart.GetBoundaries())
                    //    {
                    //        if (string.IsNullOrEmpty(httpMultipartBoundary.Filename))
                    //        {
                    //            string name = httpMultipartBoundary.Name;
                    //            if (!string.IsNullOrEmpty(name))
                    //            {
                    //                string value = new System.IO.StreamReader(httpMultipartBoundary.Value).ReadToEnd();
                    //                parameters.Add(name, value);
                    //            }
                    //        }
                    //        else
                    //        {
                    //            files.Add(httpMultipartBoundary.Name, new NFinal.Http.HttpMultipart.HttpFile(httpMultipartBoundary));
                    //        }
                    //    }
                    //}
                    //提取URL?后的参数
                    if (requestQueryString.Length > 0)
                    {
                        string[] queryArray = requestQueryString.Split(NFinal.Constant.CharAnd, NFinal.Constant.CharEqual);
                        if (queryArray.Length > 1 && (queryArray.Length & 1) == 0)
                        {
                            for (int i = 0; i < queryArray.Length; i += 2)
                            {
                                parameters.Add(queryArray[i], queryArray[i + 1].UrlDecode());
                            }
                        }
                    }
                }
            }
            return(parameters);
        }