Exemplo n.º 1
0
        public void ReadFromStream(NetworkStream stream)
        {
            //wait for data avalability
            while (!stream.DataAvailable)
            {
                ;
            }

            var sb = new StringBuilder();

            while (stream.DataAvailable)
            {
                var c = (char)(byte)stream.ReadByte();
                if (c == '\n')
                {
                    var x = sb.ToString();
                    if (string.IsNullOrEmpty(x.Trim()))
                    {
                        break;
                    }

                    var header = HttpHeader.Parse(x);
                    if (header != null)
                    {
                        Headers.Add(header);
                        if (header is CookieHeader ch)
                        {
                            foreach (var cookie in ch.Cookies)
                            {
                                Cookies.Add(cookie.Key, cookie.Value);
                            }
                        }

                        if (header is ContentLengthHeader cl)
                        {
                            BodySize = cl.Length;
                        }

                        if (header is ForwardedForHeader ff)
                        {
                            ForwardIp = ff.Ip;
                        }
                    }

                    sb.Clear();
                }
                else
                {
                    sb.Append(c);
                }
            }

            try
            {
                //@Hack this feels wrong so probably a @CleanUp to lol
                if (Headers.Count != 0)
                {
                    var meth = Headers.First(x => x is RequestMethodHeader) as RequestMethodHeader;

                    Url = meth?.Url;

                    Path = Url?.Split('?')[0];

                    if (Url != null && Url.Contains("?"))
                    {
                        var qry = Url?.Split('?')[1];
                        if (!string.IsNullOrEmpty(qry))
                        {
                            foreach (var s in qry.Split('&'))
                            {
                                var segs = s.Split('=');
                                Query.Add(segs[0], HttpUtility.UrlDecode(segs[1]));
                            }
                        }
                    }

                    Method = meth.Method;
                    //Logger.Debug(Method.ToString());
                }
            }
            catch (Exception e)
            {
                Logger.Debug(e.ToString());
            }

            if (Method == RequestMethod.Post)
            {
                Body = stream;
            }
        }
Exemplo n.º 2
0
 public virtual bool TryParse(string line, out HttpHeader header)
 {
     header = null;
     return(false);
 }