Esempio n. 1
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);
        }
Esempio n. 2
0
 public bool StartWith(byte[] bs)
 {
     return(_stream.StartWith(bs));
 }