Esempio n. 1
0
        /// <summary>
        /// 生成表单和文件
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="streamReader">数据读取器</param>
        /// <param name="boundary">边界</param>
        private static void GenerateMultipartFormAndFiles(HttpRequest request, ISessionStreamReader streamReader, string boundary)
        {
            var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary);
            var maxPosition   = streamReader.Length - Encoding.ASCII.GetBytes("--\r\n").Length;

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

            streamReader.Position = streamReader.Position + boundaryBytes.Length;
            while (streamReader.Position < maxPosition)
            {
                var headLength = streamReader.IndexOf(DoubleCRLF) + DoubleCRLF.Length;
                if (headLength < DoubleCRLF.Length)
                {
                    break;
                }

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

                var mHead = new MultipartHead(head);
                if (mHead.TryGetFileName(out string fileName) == true)
                {
                    var bytes = streamReader.ReadArray(bodyLength);
                    var file  = new HttpFile(mHead.Name, fileName, bytes);
                    files.Add(file);
                }
Esempio n. 2
0
        /// <summary>
        /// 设置握手结果
        /// </summary>
        /// <param name="streamReader">数据读取器</param>
        /// <returns></returns>
        public bool TrySetResult(ISessionStreamReader streamReader)
        {
            streamReader.Position = 0;
            var index = streamReader.IndexOf(DoubleCrlf);

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

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

            streamReader.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));
        }
Esempio n. 3
0
        /// <summary>
        /// 生成表单和文件
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="streamReader">数据读取器</param>
        /// <param name="boundary">边界</param>
        private static void GenerateMultipartFormAndFiles(HttpRequest request, ISessionStreamReader streamReader, string boundary)
        {
            var boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary);
            var maxPosition   = streamReader.Length - Encoding.ASCII.GetBytes("--\r\n").Length;

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

            streamReader.Position = streamReader.Position + boundaryBytes.Length;
            while (streamReader.Position < maxPosition)
            {
                var headLength = streamReader.IndexOf(DoubleCRLF) + DoubleCRLF.Length;
                if (headLength < DoubleCRLF.Length)
                {
                    break;
                }

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

                string fileName = null;
                var    partItem = new MultipartItem(head);

                if (partItem.TryGetFileName(out fileName) == true)
                {
                    var bytes = streamReader.ReadArray(bodyLength);
                    var file  = new HttpFile(partItem.Name, fileName, bytes);
                    files.Add(file);
                }
                else
                {
                    var byes  = streamReader.ReadArray(bodyLength);
                    var value = HttpUtility.UrlDecode(byes, Encoding.UTF8);
                    form.Add(partItem.Name, value);
                }
                streamReader.Position = streamReader.Position + boundaryBytes.Length;
            }

            request.Form  = form;
            request.Files = files.ToArray();
        }
        /// <summary>
        /// 是否为http协议
        /// </summary>
        /// <param name="streamReader">数据读取器</param>
        /// <param name="headerLength">头数据长度,包括双换行</param>
        /// <returns></returns>
        private static bool IsHttp(ISessionStreamReader streamReader, out int headerLength)
        {
            var methodLength = HttpRequestParser.GetMthodLength(streamReader);
            var methodName   = streamReader.ReadString(Encoding.ASCII, methodLength);

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

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

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

            headerLength = headerIndex + HttpRequestParser.DoubleCrlf.Length;
            return(true);
        }
        /// <summary>
        /// 解析回复头信息
        /// </summary>
        /// <param name="streamReader">数据读取器</param>
        /// <returns></returns>
        public static HttpResponseParseResult Parse(ISessionStreamReader streamReader)
        {
            var result = new HttpResponseParseResult();

            streamReader.Position = 0;

            if (streamReader.StartWith(HttpVersion11) == false)
            {
                return(result);
            }

            var endIndex = streamReader.IndexOf(DoubleCRLF);

            if (endIndex < 0)
            {
                return(result);
            }

            streamReader.Position += HttpVersion11.Length + 1;
            var statusLength = streamReader.IndexOf(Space);

            if (statusLength < 0)
            {
                return(result);
            }
            var status = streamReader.ReadString(Encoding.ASCII, statusLength);

            streamReader.Position += 1;
            var descriptionLenth = streamReader.IndexOf(CRLF);

            if (descriptionLenth < 0)
            {
                return(result);
            }
            var description = streamReader.ReadString(Encoding.ASCII, descriptionLenth);

            streamReader.Position += CRLF.Length;
            var httpHeader   = new HttpHeader();
            var headerLength = endIndex + DoubleCRLF.Length;

            while (streamReader.Position < headerLength)
            {
                var keyLength = streamReader.IndexOf(KvSpliter);
                if (keyLength <= 0)
                {
                    break;
                }
                var key = streamReader.ReadString(Encoding.ASCII, keyLength);

                streamReader.Position += KvSpliter.Length;
                var valueLength = streamReader.IndexOf(CRLF);
                if (valueLength < 0)
                {
                    break;
                }
                var value = streamReader.ReadString(Encoding.ASCII, valueLength);

                if (streamReader.StartWith(CRLF) == false)
                {
                    break;
                }
                streamReader.Position += CRLF.Length;
                httpHeader.Add(key, value);
            }

            result.Description = description;
            result.Status      = int.Parse(status);
            result.IsHttp      = true;
            result.Header      = httpHeader;
            return(result);
        }