Ejemplo n.º 1
0
        bool parseRequest()
        {
            string request;
            if (!streamReadLine(stream, out request)) return false;

            string host = "";
            string line;
            bool issocket = false;

            ESO so = ESO.Unknown;
            EBrowser br = EBrowser.Unknown;
            HttpAuth aut = null;
            Dictionary<string, string> headers = new Dictionary<string, string>();
            while (streamReadLine(stream, out line))
            {
                if (string.IsNullOrEmpty(line)) break;

                string name, val;
                SeparaEnDos(line, ':', out name, out val);
                string lname = name.ToLower();
                val = val.TrimStart(' ');

                switch (lname)
                {
                    case "connection": { if (_Server != null && _Server.AllowKeepAlive) _keep_alive = val.ToLower().Contains("keep-alive"); break; }
                    case "accept-encoding": { _gzip = val.ToLower().Contains("gzip"); break; }
                    case "host": { host = val; break; }
                    case "upgrade": { issocket = val == "websocket"; break; }
                    case "cookie":
                        {
                            string nc, vc;
                            foreach (string s in val.Split(';'))
                            {
                                SeparaEnDos(s, '=', out nc, out vc);
                                nc = HttpUtility.UrlDecode(nc.TrimStart(' '));
                                vc = HttpUtility.UrlDecode(vc);
                                try { httpCookie.Add(nc, new HttpCookie(nc, vc)); }
                                catch { }
                            }
                            break;
                        }
                    case "authorization":
                        {
                            string tp, vl;
                            SeparaEnDos(val, ' ', out tp, out vl);
                            if (tp.ToLower() == "basic")
                            {
                                byte[] dv = Convert.FromBase64String(vl);
                                vl = codec_iso.GetString(dv);
                                SeparaEnDos(vl, ':', out tp, out vl);
                                aut = new HttpAuth(tp, vl);
                            }
                            break;
                        }
                    case "user-agent":
                        {
                            string tp, vl;
                            SeparaEnDos(val, ' ', out tp, out vl);
                            //SO
                            vl = vl.ToLower();
                            if (vl.Contains("windows")) so = ESO.Windows;
                            else
                            {
                                if (vl.Contains("android")) so = ESO.Android;
                                else
                                {
                                    if (vl.Contains("iphone")) so = ESO.Iphone;
                                    else
                                    {
                                        if (vl.Contains("ipad")) so = ESO.Ipad;
                                        else
                                        {
                                            if (vl.Contains("ipod")) so = ESO.Ipod;
                                            else
                                            {
                                                if (vl.Contains("mac")) so = ESO.Mac;
                                                else
                                                {
                                                    if (vl.Contains("linux")) so = ESO.Linux;
                                                    else
                                                    {
                                                        if (vl.Contains("x11")) so = ESO.Unix;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            //Browser
                            if (vl.Contains("chrome/")) br = EBrowser.Chrome;
                            else
                            {
                                if (vl.Contains("firefox")) br = EBrowser.FireFox;
                                else
                                {
                                    if (vl.Contains("msie")) br = EBrowser.IExplorer;
                                    else
                                    {
                                        if (vl.Contains("konqueror")) br = EBrowser.Konqueror;
                                        else
                                        {
                                            if (vl.Contains("opera/")) br = EBrowser.Opera;
                                            else
                                            {
                                                if (vl.Contains("safari/")) br = EBrowser.Safari;
                                                else
                                                {
                                                    tp = tp.ToLower();
                                                    if (tp.Contains("dalvik/")) br = EBrowser.Dalvik;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        }
                }

                headers.Add(name, val);
            }

            _req = new HttpRequest(request, host, _IP, headers, aut, so, br, issocket, _Server.GetAndPostNamesToLowerCase);
            return true;
        }
Ejemplo n.º 2
0
        public HttpRequest(string request, string host, string ip, Dictionary <string, string> header, HttpAuth auth, ESO so, EBrowser br, bool is_socket, bool varsToLowerCase)
        {
            if (header == null)
            {
                http_method = EHttpMethod.Unknown;
                return;
            }

            int ixa = request.IndexOf(' ');

            if (ixa <= -1)
            {
                http_method = EHttpMethod.Unknown;
                return;
            }
            int ixb = request.LastIndexOf(' ');

            if (ixb <= -1 || ixa == ixb)
            {
                http_method = EHttpMethod.Unknown;
                return;
            }

            _VarsToLowerCase = varsToLowerCase;
            _IP     = ip;
            _so     = so;
            _br     = br;
            _header = header;
            _auth   = auth;
            _host   = new HttpHost(host);

            switch (request.Substring(0, ixa).ToUpper())
            {
            case "HEAD": http_method = EHttpMethod.HEAD; break;

            case "GET": http_method = EHttpMethod.GET; break;

            case "POST": http_method = EHttpMethod.POST; break;

            default: http_method = EHttpMethod.Unknown; break;
            }

            if (is_socket)
            {
                http_method = EHttpMethod.SOCKET;
            }

            http_url = request.Substring(ixa + 1, ixb - ixa - 1).Trim('/');
            http_protocol_version = request.Substring(ixb + 1);

            int ix_h = http_url.IndexOf('?');

            if (ix_h != -1)
            {
                foreach (string key in http_url.Remove(0, ix_h + 1).Split('&'))
                {
                    try
                    {
                        string iz, dr;
                        HttpProcessor.SeparaEnDos(key, '=', out iz, out dr);
                        if (varsToLowerCase)
                        {
                            iz = iz.ToLowerInvariant();
                        }

                        _get.Add(HttpServer.UrlDecode(iz), HttpServer.UrlDecode(dr));
                    }
                    catch { }
                }
                http_url = http_url.Substring(0, ix_h);
            }
        }