Exemplo n.º 1
0
        /// <summary>
        /// 生成Post得到的表单和文件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="buffer"></param>
        private static void GeneratePostFormAndFiles(HttpRequest request, IReceiveBuffer buffer)
        {
            var boundary = default(string);

            if (request.IsApplicationFormRequest() == true)
            {
                HttpRequest.GenerateApplicationForm(request);
            }
            else if (request.IsMultipartFormRequest(out boundary) == true)
            {
                if (request.Body.Length >= boundary.Length)
                {
                    HttpRequest.GenerateMultipartFormAndFiles(request, buffer, boundary);
                }
            }


            if (request.Form == null)
            {
                request.Form = new HttpNameValueCollection();
            }

            if (request.Files == null)
            {
                request.Files = new HttpFile[0];
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 获取当前的http方法长度
 /// </summary>
 /// <param name="buffer">收到的数据</param>
 /// <returns></returns>
 private static int GetMthodLength(IReceiveBuffer buffer)
 {
     var maxLength = Math.Min(buffer.Length, Protocol.MedthodMaxLength + 1);
     for (var i = 0; i < maxLength; i++)
     {
         if (buffer[i] == Protocol.Space)
         {
             return i - 1;
         }
     }
     return maxLength;
 }
Exemplo n.º 3
0
        /// <summary>
        /// 获取当前的http方法长度
        /// </summary>
        /// <param name="buffer">收到的数据</param>
        /// <returns></returns>
        private static int GetMthodLength(IReceiveBuffer buffer)
        {
            var maxLength = Math.Min(buffer.Length, Protocol.MedthodMaxLength + 1);

            for (var i = 0; i < maxLength; i++)
            {
                if (buffer[i] == Protocol.Space)
                {
                    return(i - 1);
                }
            }
            return(maxLength);
        }
Exemplo n.º 4
0
 /// <summary>
 /// 当接收到远程端的数据时,将触发此方法
 /// </summary>
 /// <param name="buffer">接收到的历史数据</param>
 protected sealed override void OnReceive(IReceiveBuffer buffer)
 {
     while (true)
     {
         var packet = default(FastPacket);
         if (FastPacket.Parse(buffer, out packet) == false)
         {
             buffer.Clear();
         }
         if (packet == null)
         {
             break;
         }
         // 新线程处理业务内容
         Task.Factory.StartNew(() => this.OnReceivePacket(packet));
     }
 }
Exemplo n.º 5
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 doubleCrlf    = Encoding.ASCII.GetBytes("\r\n\r\n");
            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(doubleCrlf) + doubleCrlf.Length;
                if (headLength < 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();
        }
Exemplo n.º 6
0
        /// <summary>
        /// 是否为http协议
        /// </summary>
        /// <param name="buffer">收到的数据</param>
        /// <param name="headerLength">头数据长度,包括双换行</param>
        /// <returns></returns>
        public static bool IsHttp(IReceiveBuffer buffer, out int headerLength)
        {
            var methodLength = Protocol.GetMthodLength(buffer);
            var methodName = buffer.ReadString(methodLength, Encoding.ASCII);

            if (Protocol.MethodNames.Any(m => m.StartsWith(methodName, StringComparison.OrdinalIgnoreCase)) == false)
            {
                headerLength = 0;
                return false;
            }

            buffer.Position = 0;
            var headerIndex = buffer.IndexOf(Protocol.DoubleCrlf);
            if (headerIndex < 0)
            {
                headerLength = 0;
                return true;
            }

            headerLength = headerIndex + Protocol.DoubleCrlf.Length;
            return true;
        }
Exemplo n.º 7
0
        /// <summary>
        /// 是否为http协议
        /// </summary>
        /// <param name="buffer">收到的数据</param>
        /// <param name="headerLength">头数据长度,包括双换行</param>
        /// <returns></returns>
        public static bool IsHttp(IReceiveBuffer buffer, out int headerLength)
        {
            var methodLength = Protocol.GetMthodLength(buffer);
            var methodName   = buffer.ReadString(methodLength, Encoding.ASCII);

            if (Protocol.MethodNames.Any(m => m.StartsWith(methodName, StringComparison.OrdinalIgnoreCase)) == false)
            {
                headerLength = 0;
                return(false);
            }

            buffer.Position = 0;
            var headerIndex = buffer.IndexOf(Protocol.DoubleCrlf);

            if (headerIndex < 0)
            {
                headerLength = 0;
                return(true);
            }

            headerLength = headerIndex + Protocol.DoubleCrlf.Length;
            return(true);
        }
Exemplo n.º 8
0
 /// <summary>
 /// 当接收到远程端的数据时,将触发此方法
 /// </summary>
 /// <param name="buffer">接收到的历史数据</param>
 /// <returns></returns>
 protected abstract void OnReceive(IReceiveBuffer buffer);
Exemplo n.º 9
0
        /// <summary>
        /// 解析请求的数据
        /// 返回请求数据包
        /// </summary>
        /// <param name="buffer">所有收到的数据</param>
        /// <returns></returns>
        public unsafe static FrameRequest Parse(IReceiveBuffer buffer)
        {
            if (buffer.Length < 2)
            {
                return(null);
            }

            ByteBits byte0     = buffer[0];
            var      fin       = byte0[0];
            var      frameCode = (FrameCodes)(byte)byte0.Take(4, 4);

            if (fin == false || frameCode == FrameCodes.Continuation)
            {
                return(null);
            }

            var      rsv   = byte0.Take(1, 3);
            ByteBits byte1 = buffer[1];
            var      mask  = byte1[0];

            if (mask == false || Enum.IsDefined(typeof(FrameCodes), frameCode) == false || rsv != 0)
            {
                return(null);
            }

            var contentLength = (int)byte1.Take(1, 7);

            buffer.Position = 2;

            if (contentLength == 127)
            {
                contentLength = (int)buffer.ReadUInt64();
            }
            else if (contentLength == 126)
            {
                contentLength = (int)buffer.ReadUInt16();
            }

            var packetLength = 6 + contentLength;

            if (buffer.Length < packetLength)
            {
                return(null);
            }

            var maskingKey = buffer.ReadArray(4);
            var content    = buffer.ReadArray(contentLength);

            buffer.Clear(packetLength);

            if (contentLength > 0)
            {
                fixed(byte *pcontent = &content[0], pmask = &maskingKey[0])
                {
                    for (var i = 0; i < contentLength; i++)
                    {
                        *(pcontent + i) = (byte)(*(pcontent + i) ^ *(pmask + i % 4));
                    }
                }
            }

            return(new FrameRequest
            {
                Fin = fin,
                Rsv = rsv,
                Mask = mask,
                Frame = frameCode,
                ContentLength = contentLength,
                MaskingKey = maskingKey,
                Content = content
            });
        }
Exemplo n.º 10
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();
        }
Exemplo n.º 11
0
        /// <summary>
        /// 生成Post得到的表单和文件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="buffer"></param>      
        private static void GeneratePostFormAndFiles(HttpRequest request, IReceiveBuffer buffer)
        {
            var boundary = default(string);
            if (request.IsApplicationFormRequest() == true)
            {
                HttpRequest.GenerateApplicationForm(request);
            }
            else if (request.IsMultipartFormRequest(out boundary) == true)
            {
                if (request.Body.Length >= boundary.Length)
                {
                    HttpRequest.GenerateMultipartFormAndFiles(request, buffer, boundary);
                }
            }


            if (request.Form == null)
            {
                request.Form = new HttpNameValueCollection();
            }

            if (request.Files == null)
            {
                request.Files = new HttpFile[0];
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// 解析请求的数据
        /// 返回请求数据包
        /// </summary>
        /// <param name="buffer">所有收到的数据</param>  
        /// <returns></returns>
        public unsafe static FrameRequest Parse(IReceiveBuffer buffer)
        {
            if (buffer.Length < 2)
            {
                return null;
            }

            ByteBits byte0 = buffer[0];
            var fin = byte0[0];
            var frameCode = (FrameCodes)(byte)byte0.Take(4, 4);

            if (fin == false || frameCode == FrameCodes.Continuation)
            {
                return null;
            }

            var rsv = byte0.Take(1, 3);
            ByteBits byte1 = buffer[1];
            var mask = byte1[0];

            if (mask == false || Enum.IsDefined(typeof(FrameCodes), frameCode) == false || rsv != 0)
            {
                return null;
            }

            var contentLength = (int)byte1.Take(1, 7);
            buffer.Position = 2;

            if (contentLength == 127)
            {
                contentLength = (int)buffer.ReadUInt64();
            }
            else if (contentLength == 126)
            {
                contentLength = (int)buffer.ReadUInt16();
            }

            var packetLength = 6 + contentLength;
            if (buffer.Length < packetLength)
            {
                return null;
            }

            var maskingKey = buffer.ReadArray(4);
            var content = buffer.ReadArray(contentLength);
            buffer.Clear(packetLength);

            if (contentLength > 0)
            {
                fixed (byte* pcontent = &content[0], pmask = &maskingKey[0])
                {
                    for (var i = 0; i < contentLength; i++)
                    {
                        *(pcontent + i) = (byte)(*(pcontent + i) ^ *(pmask + i % 4));
                    }
                }
            }

            return new FrameRequest
            {
                Fin = fin,
                Rsv = rsv,
                Mask = mask,
                Frame = frameCode,
                ContentLength = contentLength,
                MaskingKey = maskingKey,
                Content = content
            };
        }
Exemplo n.º 13
0
        /// <summary>
        /// 解析一个数据包       
        /// 不足一个封包时返回null
        /// </summary>
        /// <param name="buffer">接收到的历史数据</param>
        /// <param name="packet">数据包</param>
        /// <returns></returns>
        public static bool Parse(IReceiveBuffer buffer, out FastPacket packet)
        {
            if (buffer.Length < 1 || buffer[0] != FastPacket.Mark)
            {
                packet = null;
                return false;
            }

            if (buffer.Length < 5)
            {
                packet = null;
                return true;
            }

            buffer.Position = 1;
            const int packetMinSize = 16;
            var totalBytes = buffer.ReadInt32();

            if (totalBytes < packetMinSize)
            {
                packet = null;
                return false;
            }

            // 数据包未接收完整
            if (buffer.Length < totalBytes)
            {
                packet = null;
                return true;
            }

            // api名称数据长度
            var apiNameLength = buffer.ReadByte();
            if (totalBytes < apiNameLength + packetMinSize)
            {
                packet = null;
                return false;
            }

            // api名称数据
            var apiNameBytes = buffer.ReadArray(apiNameLength);
            // 标识符
            var id = buffer.ReadInt64();
            // 是否为客户端封包
            var isFromClient = buffer.ReadBoolean();
            // 是否异常
            var isException = buffer.ReadBoolean();
            // 实体数据
            var body = buffer.ReadArray(totalBytes - buffer.Position);

            // 清空本条数据
            buffer.Clear(totalBytes);

            var apiName = Encoding.UTF8.GetString(apiNameBytes);
            packet = new FastPacket(apiName, id, isFromClient)
            {
                TotalBytes = totalBytes,
                ApiNameLength = apiNameLength,
                IsException = isException,
                Body = body
            };
            return true;
        }
Exemplo n.º 14
0
        /// <summary>
        /// 解析一个数据包
        /// 不足一个封包时返回null
        /// </summary>
        /// <param name="buffer">接收到的历史数据</param>
        /// <param name="packet">数据包</param>
        /// <returns></returns>
        public static bool Parse(IReceiveBuffer buffer, out FastPacket packet)
        {
            if (buffer.Length < 1 || buffer[0] != FastPacket.Mark)
            {
                packet = null;
                return(false);
            }

            if (buffer.Length < 5)
            {
                packet = null;
                return(true);
            }

            buffer.Position = 1;
            const int packetMinSize = 16;
            var       totalBytes    = buffer.ReadInt32();

            if (totalBytes < packetMinSize)
            {
                packet = null;
                return(false);
            }

            // 数据包未接收完整
            if (buffer.Length < totalBytes)
            {
                packet = null;
                return(true);
            }

            // api名称数据长度
            var apiNameLength = buffer.ReadByte();

            if (totalBytes < apiNameLength + packetMinSize)
            {
                packet = null;
                return(false);
            }

            // api名称数据
            var apiNameBytes = buffer.ReadArray(apiNameLength);
            // 标识符
            var id = buffer.ReadInt64();
            // 是否为客户端封包
            var isFromClient = buffer.ReadBoolean();
            // 是否异常
            var isException = buffer.ReadBoolean();
            // 实体数据
            var body = buffer.ReadArray(totalBytes - buffer.Position);

            // 清空本条数据
            buffer.Clear(totalBytes);

            var apiName = Encoding.UTF8.GetString(apiNameBytes);

            packet = new FastPacket(apiName, id, isFromClient)
            {
                TotalBytes    = totalBytes,
                ApiNameLength = apiNameLength,
                IsException   = isException,
                Body          = body
            };
            return(true);
        }