Example #1
0
        /// <summary>
        /// 设置握手结果
        /// </summary>
        /// <param name="inputStream">输入流</param>
        /// <returns></returns>
        public bool TrySetResult(IStreamReader inputStream)
        {
            inputStream.Position = 0;
            var index = inputStream.IndexOf(DoubleCrlf);

            if (index < 0)
            {
                return(false);
            }

            var length = index + DoubleCrlf.Length;
            var header = inputStream.ReadString(Encoding.ASCII, length);

            inputStream.Clear(length);

            const string pattern = @"^HTTP/1.1 101 Switching Protocols\r\n((?<field_name>[^:\r\n]+):\s(?<field_value>[^\r\n]*)\r\n)+\r\n";
            var          match   = Regex.Match(header, pattern, RegexOptions.IgnoreCase);

            if (match.Success == true)
            {
                var httpHeader = HttpHeader.Parse(match.Groups["field_name"].Captures, match.Groups["field_value"].Captures);
                var secAccept  = httpHeader["Sec-WebSocket-Accept"];

                const string guid     = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
                var          bytes    = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(this.secKey + guid));
                var          secValue = Convert.ToBase64String(bytes);

                if (secValue == secAccept)
                {
                    return(this.TrySetResult(SocketError.Success));
                }
            }
            return(this.TrySetResult(SocketError.SocketError));
        }
Example #2
0
        /// <summary>
        /// 生成表单和文件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="stream"></param>
        /// <param name="boundary">边界</param>
        private static void GenerateMultipartFormAndFiles(HttpRequest request, IStreamReader stream, string boundary)
        {
            var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary);
            var maxPosition   = stream.Length - Encoding.ASCII.GetBytes("--\r\n").Length;

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

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

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

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

            request.Form  = form;
            request.Files = files.ToArray();
        }
Example #3
0
        /// <summary>
        /// 是否为http协议
        /// </summary>
        /// <param name="stream">收到的数据</param>
        /// <param name="headerLength">头数据长度,包括双换行</param>
        /// <returns></returns>
        private static bool IsHttp(IStreamReader stream, out int headerLength)
        {
            var methodLength = HttpRequestParser.GetMthodLength(stream);
            var methodName   = stream.ReadString(Encoding.ASCII, methodLength);

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

            stream.Position = 0;
            var headerIndex = stream.IndexOf(HttpRequestParser.DoubleCrlf);

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

            headerLength = headerIndex + HttpRequestParser.DoubleCrlf.Length;
            return(true);
        }