Exemple #1
0
        /// <summary>
        /// 解析连接请求信息
        /// </summary>
        /// <param name="context">上下文</param>
        /// <returns></returns>
        public static HttpRequestParseResult Parse(IContext context, IByteStream stream, bool isSSL)
        {
            HttpRequestParseResult result = ParseInternal(context, stream, isSSL);

            stream.SetReaderIndex(0);
            return(result);
        }
Exemple #2
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);
        }
Exemple #3
0
        /// <summary>
        /// 尝试当作http头解析,生成请求对象
        /// 如果不是http头则返回false
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="request">请求对象</param>
        /// <param name="headerLength">请求头长度</param>
        /// <param name="contentLength">请求内容长度</param>
        /// <returns></returns>
        private static bool TryGetRequest(IContext context, IByteStream stream, bool isSSL, out HttpRequest request, out int headerLength, out int contentLength)
        {
            request       = null;
            headerLength  = 0;
            contentLength = 0;
            IByteStream reader  = stream;
            ChannelBase channel = context.Channel;

            if (channel == null)
            {
                return(false);
            }
            // HTTP Method
            reader.SetReaderIndex(0);
            int methodLength = reader.BytesBefore(Space);

            if (methodLength < 0 || methodLength > MedthodMaxLength)
            {
                return(false);
            }
            string methodName = reader.ReadString(Encoding.ASCII, methodLength);

            if (MethodNames.Contains(methodName) == false)
            {
                return(false);
            }
            HttpMethod httpMethod = (HttpMethod)Enum.Parse(typeof(HttpMethod), methodName, true);

            // HTTP Path
            reader.ReadSkip(1);
            int pathLength = reader.BytesBefore(Space);

            if (pathLength < 0)
            {
                return(false);
            }
            string path = reader.ReadString(Encoding.ASCII, pathLength);


            // HTTP Version
            reader.ReadSkip(1);
            if (reader.StartWith(HttpVersion11) == false)
            {
                return(false);
            }
            reader.ReadSkip(HttpVersion11.Length);
            if (reader.StartWith(CRLF) == false)
            {
                return(false);
            }

            // HTTP Second line
            reader.ReadSkip(CRLF.Length);
            int endIndex = reader.BytesBefore(DoubleCRLF);

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

            HttpNameValueCollection httpHeader = new HttpNameValueCollection();

            headerLength = reader.ReaderIndex + endIndex + DoubleCRLF.Length;


            while (reader.ReaderIndex < headerLength - CRLF.Length)
            {
                int keyLength = reader.BytesBefore(KvSpliter);
                if (keyLength <= 0)
                {
                    break;
                }
                string key = reader.ReadString(Encoding.ASCII, keyLength);

                reader.ReadSkip(KvSpliter.Length);
                int valueLength = reader.BytesBefore(CRLF);
                if (valueLength < 0)
                {
                    break;
                }
                string value = reader.ReadString(Encoding.ASCII, valueLength);

                if (reader.StartWith(CRLF) == false)
                {
                    break;
                }
                reader.ReadSkip(CRLF.Length);
                httpHeader.Add(key, value);
            }

            if (httpMethod != HttpMethod.GET)
            {
                contentLength = httpHeader.TryGet <int>("Content-Length");
                if (reader.Length - headerLength < contentLength)
                {
                    return(true);// 数据未完整
                }
            }


            request = new HttpRequest
            {
                LocalEndPoint  = (IPEndPoint)channel.LocalEndPoint,
                RemoteEndPoint = (IPEndPoint)channel.RemoteEndPoint,
                HttpMethod     = httpMethod,
                Headers        = httpHeader
            };

            //获取请求cookie
            string reqcookies = httpHeader.TryGet <string>("Cookie");

            if (reqcookies != null)
            {
                GenerateCookies(request, reqcookies);
            }

            string scheme = isSSL ? "https" : "http";
            string host   = httpHeader["Host"];

            if (string.IsNullOrEmpty(host) == true)
            {
                host = channel.LocalEndPoint.ToString();
            }
            request.Host = host;
            string referer = httpHeader.TryGet <string>("Referer");

            if (!string.IsNullOrEmpty(referer))
            {
                try
                {
                    request.RefererUrl = new Uri(referer);
                }
                catch { }
            }
            request.UserAgent = httpHeader.TryGet <string>("User-Agent", "");
            request.Url       = new Uri(string.Format("{0}://{1}{2}", scheme, host, path));
            request.Path      = request.Url.AbsolutePath;
            request.Query     = HttpNameValueCollection.Parse(request.Url.Query.TrimStart('?'));
            return(true);
        }
Exemple #4
0
 public void SetReaderIndex(int index)
 {
     _stream.SetReaderIndex(index);
 }