示例#1
0
        ///// <summary>
        ///// 初始化内容帧
        ///// </summary>
        ///// <param name="buffer"></param>
        //public WebSocketDataFrame(byte[] buffer)
        //{
        //    //帧头
        //    _header = new WebSocketDataFrameHeader(buffer);

        //    //扩展长度
        //    if (_header.Length == 126)
        //    {
        //        _extend = new byte[2];
        //        Buffer.BlockCopy(buffer, 2, _extend, 0, 2);
        //    }
        //    else if (_header.Length == 127)
        //    {
        //        _extend = new byte[8];
        //        Buffer.BlockCopy(buffer, 2, _extend, 0, 8);
        //    }

        //    //是否有掩码
        //    if (_header.HasMask)
        //    {
        //        _mask = new byte[4];
        //        Buffer.BlockCopy(buffer, _extend.Length + 2, _mask, 0, 4);
        //    }

        //    //消息体
        //    if (_extend.Length == 0)
        //    {
        //        _content = new byte[_header.Length];
        //        Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
        //    }
        //    else if (_extend.Length == 2)
        //    {
        //        int contentLength = (int)_extend[0] * 256 + (int)_extend[1];
        //        _content = new byte[contentLength];
        //        Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, contentLength > 1024 * 100 ? 1024 * 100 : contentLength);
        //    }
        //    else
        //    {
        //        long len = 0;
        //        int n = 1;
        //        for (int i = 7; i >= 0; i--)
        //        {
        //            len += (int)_extend[i] * n;
        //            n *= 256;
        //        }
        //        _content = new byte[len];
        //        Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
        //    }

        //    if (_header.HasMask) _content = Mask(_content, _mask);

        //}

        /// <summary>
        /// 初始化一个Socket数据帧
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="headerBuffer"></param>
        public WebSocketDataFrame(Socket socket, byte[] headerBuffer)
        {
            //var header = new WebSocketDataFrameHeader(this.headerBuffer);
            //byte[] extend = new byte[0];

            ////扩展长度
            //if (header.Length == 126)
            //{
            //    extend = SocketHelper.Receive(this.Socket, 2);
            //}
            //else if (header.Length == 127)
            //{
            //    extend = SocketHelper.Receive(this.Socket, 8);
            //}

            ////是否有掩码
            //byte[] _mask = null;
            //if (header.HasMask)
            //{
            //    _mask = SocketHelper.Receive(this.Socket, 4);
            //}

            ////content length
            //var content_length = 0L;
            //if (extend.Length == 0)
            //    content_length = header.Length;
            //else if (extend.Length == 2)
            //    content_length = (int)extend[0] * 256 + (int)extend[1];
            //else
            //{
            //    int n = 1;
            //    for (int i = 7; i >= 0; i--)
            //    {
            //        content_length += (int)extend[i] * n;
            //        n *= 256;
            //    }

            //    if (content_length > int.MaxValue)
            //        throw new WebException("Data length transfinite");
            //}

            //var contentBuffer = SocketHelper.Receive(this.Socket, (int)content_length);
            //if (header.HasMask)
            //{
            //    WebSocketDataFrame.Mask(contentBuffer, _mask);
            //}

            _header = new WebSocketDataFrameHeader(headerBuffer);

            //扩展长度
            if (_header.Length == 126)
            {
                _extend = SocketHelper.Receive(socket, 2);
            }
            else if (_header.Length == 127)
            {
                _extend = SocketHelper.Receive(socket, 8);
            }

            //是否有掩码
            if (_header.HasMask)
            {
                _mask = SocketHelper.Receive(socket, 4);
            }

            //content length
            var content_length = 0L;

            if (_extend.Length == 0)
            {
                content_length = _header.Length;
            }
            else if (_extend.Length == 2)
            {
                content_length = (int)_extend[0] * 256 + (int)_extend[1];
            }
            else
            {
                int n = 1;
                for (int i = 7; i >= 0; i--)
                {
                    content_length += (int)_extend[i] * n;
                    n *= 256;
                }

                if (content_length > int.MaxValue)
                {
                    throw new WebException("Data length transfinite");
                }
            }

            this.Content = SocketHelper.Receive(socket, (int)content_length);
            if (_header.HasMask)
            {
                Mask(this.Content, _mask);
            }
        }
示例#2
0
        /// <summary>
        /// 解析POST数扰
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="context"></param>
        /// <returns>是否成功</returns>
        protected virtual bool ParsePostData(Socket socket, HttpServerContext context)
        {
            var success       = false;
            var contentLength = 0;

            int.TryParse(context.RequestHeader["Content-Length"], out contentLength);
            if (contentLength > context.MaxRequestContentLength)
            {
                //请求主体超过限制
                if (string.IsNullOrEmpty(context.Content) && context.ContentBuffer == null)
                {
                    context.Content = "Request Entity Too Large";
                }
                context.Response(HttpStatusCode.RequestEntityTooLarge);
            }
            else if (contentLength > 0)
            {
                string contentType = context.RequestHeader["Content-Type"];
                if (contentType == null)
                {
                    context.PostData = SocketHelper.Receive(socket, contentLength);
                    success          = true;
                }
                else if ("application/x-www-form-urlencoded".Equals(contentType, StringComparison.OrdinalIgnoreCase))
                {
                    var data     = SocketHelper.Receive(socket, contentLength);
                    var postdata = this.encoding.GetString(data);
                    UriHelper.ParseQueryString(postdata, this.encoding, context.Form);
                    success = true;
                }
                else if (contentType.IndexOf("multipart/form-data", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    //"multipart/form-data; boundary=---------------------boundary123data"
                    var boundary = contentType.Substring(contentType.IndexOf("boundary=") + 9);
                    var receiver = new HttpServerMultipartReceiver(socket, boundary, context, contentLength);
                    try
                    {
                        return(receiver.Receive());
                    }
                    catch (Exception exception)
                    {
                        //请求主体超过限制
                        if (string.IsNullOrEmpty(context.Content) && context.ContentBuffer == null)
                        {
                            context.Content = "Paser Post Data Failure " + exception.Message;
                        }
                        context.Response(HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    context.PostData = SocketHelper.Receive(socket, contentLength);
                    success          = true;
                }
            }
            else
            {
                //要求必要的 Content-Length
                if (string.IsNullOrEmpty(context.Content) && context.ContentBuffer == null)
                {
                    context.Content = "Require Content-Length";
                }
                context.Response(HttpStatusCode.LengthRequired);
            }

            return(success);
        }