Exemplo n.º 1
0
        /// <summary>
        /// 解析连接请求信息
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        private static HttpRequestParseResult ParseInternal(IContext context, IByteStream stream, bool isSSL)
        {
            HttpRequest            request;
            int                    headerLength;
            int                    contentLength;
            HttpRequestParseResult result = new HttpRequestParseResult
            {
                IsHttp = HttpRequestParser.TryGetRequest(context, stream, isSSL, out request, out headerLength, out contentLength)
            };

            result.ContentLength = contentLength;
            result.HeaderLength  = headerLength;

            if (result.IsHttp == false)
            {
                return(result);
            }

            if (request == null) // 数据未完整
            {
                return(result);
            }


            switch (request.HttpMethod)
            {
            case HttpMethod.GET:
                request.Body        = new byte[0];
                request.InputStream = new MemoryStream(request.Body);
                request.Form        = new HttpNameValueCollection();
                request.Files       = new HttpFile[0];
                break;

            default:
                stream.SetReaderIndex(headerLength);
                request.Body        = stream.ReadBytes(contentLength);
                request.InputStream = new MemoryStream(request.Body);
                stream.SetReaderIndex(headerLength);
                HttpRequestParser.GeneratePostFormAndFiles(request, context, stream);
                break;
            }

            result.Request       = request;
            result.PackageLength = headerLength + contentLength;
            return(result);
        }