Exemple #1
0
        /// <summary>
        /// 分析request 头部
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public bool Analysis(byte[] buffer)
        {
            //获取headerStr
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                ms.Position = 0;

                using (SAEA.Common.StreamReader streamReader = new SAEA.Common.StreamReader(ms))
                {
                    while (true)
                    {
                        var str = streamReader.ReadLine();
                        if (str == string.Empty)
                        {
                            this.HeaderStr = _stringBuilder.ToString();
                            _stringBuilder.Clear();
                            break;
                        }
                        else if (str == null && string.IsNullOrEmpty(this.HeaderStr))
                        {
                            return(false);
                        }
                        else
                        {
                            _stringBuilder.AppendLine(str);
                        }
                    }
                }
            }

            //分析requestHeader

            var rows = Regex.Split(this.HeaderStr, ENTER);

            var arr = Regex.Split(rows[0], @"(\s+)").Where(e => e.Trim() != string.Empty).ToArray();

            this.Method = arr[0];

            this.RelativeUrl = arr[1];

            if (this.RelativeUrl.Contains("?"))
            {
                var qarr = this.RelativeUrl.Split("?");
                this.Url   = qarr[0];
                this.Query = GetRequestQuerys(qarr[1]);
            }
            else
            {
                this.Url = this.RelativeUrl;
            }

            var uarr = this.Url.Split("/");

            if (long.TryParse(uarr[uarr.Length - 1], out long id))
            {
                this.Url = this.Url.Substring(0, this.Url.LastIndexOf("/"));
                if (this.Query == null)
                {
                    this.Query = new Dictionary <string, string>();
                }
                this.Query.Add(ConstHelper.ID, id.ToString());
            }

            this.Protocal = arr[2];

            this.Headers = GetRequestHeaders(rows);

            if (this.Headers != null)
            {
                //cookies
                var cookiesStr = string.Empty;
                if (this.Headers.TryGetValue(RequestHeaderType.Cookie.GetDescription(), out cookiesStr))
                {
                    this.Cookies = GetCookies(cookiesStr);
                }

                //post数据分析
                if (this.Method == ConstString.POSTStr)
                {
                    using (MemoryStream ms = new MemoryStream(buffer))
                    {
                        var poistion = ms.Position = 0;
                        while (true)
                        {
                            if (ms.ReadByte() == 13 && ms.ReadByte() == 10 && ms.ReadByte() == 13 && ms.ReadByte() == 10)
                            {
                                poistion = ms.Position;
                                break;
                            }
                        }
                        this.Position = (int)poistion;
                    }

                    string contentTypeStr = string.Empty;
                    if (this.Headers.TryGetValue(RequestHeaderType.ContentType.GetDescription(), out contentTypeStr))
                    {
                        //form-data
                        if (contentTypeStr.IndexOf(ConstString.FORMENCTYPE2) > -1)
                        {
                            this.IsFormData = true;

                            this.Boundary = "--" + Regex.Split(contentTypeStr, ";")[1].Replace(ConstHelper.BOUNDARY, "");
                        }
                    }
                    string contentLengthStr = string.Empty;
                    if (this.Headers.TryGetValue(RequestHeaderType.ContentLength.GetDescription(), out contentLengthStr))
                    {
                        int cl = 0;

                        if (int.TryParse(contentLengthStr, out cl))
                        {
                            this.ContentLength = cl;
                        }
                    }
                }
            }
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// 分析request body
        /// </summary>
        /// <param name="requestData"></param>
        /// <param name="httpMessage"></param>
        public static void AnalysisBody(byte[] requestData, HttpMessage httpMessage)
        {
            var contentLen = httpMessage.ContentLength;

            if (contentLen > 0)
            {
                var positon = httpMessage.Position;

                var totlalLen = contentLen + positon;

                httpMessage.Body = requestData.AsSpan().Slice(positon, contentLen).ToArray();

                switch (httpMessage.ContentType)
                {
                case ConstHelper.FORMENCTYPE1:
                    httpMessage.Forms = GetRequestForms(Encoding.UTF8.GetString(httpMessage.Body));
                    break;

                case ConstHelper.FORMENCTYPE2:
                    using (MemoryStream ms = new MemoryStream(httpMessage.Body))
                    {
                        ms.Position = 0;
                        using (var sr = new SAEA.Common.StreamReader(ms))
                        {
                            StringBuilder sb  = new StringBuilder();
                            var           str = string.Empty;
                            do
                            {
                                str = sr.ReadLine();
                                if (str == null)
                                {
                                    break;
                                }
                                else
                                {
                                    sb.AppendLine(str);
                                    if (str.Contains(ConstHelper.CT))
                                    {
                                        var filePart = GetRequestFormsWithMultiPart(sb.ToString(), httpMessage);

                                        if (filePart != null)
                                        {
                                            sr.ReadLine();

                                            filePart.Data = sr.ReadData(sr.Position, httpMessage.Boundary);
                                            if (filePart.Data != null)
                                            {
                                                filePart.Data = filePart.Data.Take(filePart.Data.Length - 2).ToArray();
                                            }
                                            httpMessage.PostFiles.Add(filePart);
                                        }
                                        sb.Clear();
                                        sr.ReadLine();
                                    }
                                }
                            }while (true);
                        }
                    }
                    break;

                case ConstHelper.FORMENCTYPE3:
                default:
                    httpMessage.Json = Encoding.UTF8.GetString(httpMessage.Body);
                    break;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 分析request body
        /// </summary>
        /// <param name="requestData"></param>
        public void AnalysisBody(byte[] requestData)
        {
            var contentLen = this.ContentLength;

            if (contentLen > 0)
            {
                var positon   = this.Position;
                var totlalLen = contentLen + positon;
                this.Body = new byte[contentLen];
                Buffer.BlockCopy(requestData, positon, this.Body, 0, this.Body.Length);

                switch (this.ContentType)
                {
                case ConstString.FORMENCTYPE2:
                    using (MemoryStream ms = new MemoryStream(this.Body))
                    {
                        ms.Position = 0;
                        using (var sr = new SAEA.Common.StreamReader(ms))
                        {
                            StringBuilder sb  = new StringBuilder();
                            var           str = string.Empty;
                            do
                            {
                                str = sr.ReadLine();
                                if (str == null)
                                {
                                    break;
                                }
                                else
                                {
                                    sb.AppendLine(str);
                                    if (str.IndexOf(ConstHelper.CT) > -1)
                                    {
                                        var filePart = GetRequestFormsWithMultiPart(sb.ToString());

                                        if (filePart != null)
                                        {
                                            sr.ReadLine();

                                            filePart.Data = sr.ReadData(sr.Position, this.Boundary);
                                            if (filePart.Data != null)
                                            {
                                                filePart.Data = filePart.Data.Take(filePart.Data.Length - 2).ToArray();
                                            }
                                            if (this.PostFiles == null)
                                            {
                                                this.PostFiles = new List <FilePart>();
                                            }
                                            this.PostFiles.Add(filePart);
                                        }
                                        sb.Clear();
                                        sr.ReadLine();
                                    }
                                }
                            }while (true);
                        }
                    }
                    break;

                case ConstString.FORMENCTYPE3:
                    this.Json = Encoding.UTF8.GetString(this.Body);
                    break;

                default:
                    this.Forms = GetRequestForms(Encoding.UTF8.GetString(this.Body));
                    break;
                }
            }
        }