示例#1
0
        public OSHTTPURI(string uri, bool withDNSResolve = false)
        {
            Flags = OSHTTPURIFlags.None;
            Port  = -1;
            IP    = null;
            Host  = string.Empty;
            URI   = string.Empty;
            URL   = string.Empty;
            Path  = string.Empty;

            if (string.IsNullOrEmpty(uri))
            {
                return;
            }

            try
            {
                Uri m_checkuri = new Uri(uri);

                if (m_checkuri.Scheme != Uri.UriSchemeHttp && m_checkuri.Scheme != Uri.UriSchemeHttps)
                {
                    return;
                }

                Flags = OSHTTPURIFlags.ValidHost;
                Host  = m_checkuri.DnsSafeHost.ToLowerInvariant();

                Port = m_checkuri.Port;
                Path = m_checkuri.AbsolutePath;
                if (Path[Path.Length - 1] == '/')
                {
                    Path = Path.Substring(0, Path.Length - 1);
                }

                URL = m_checkuri.Scheme + "://" + Host + ":" + Port;
                URI = URL + Path;

                if (withDNSResolve)
                {
                    IPAddress ip = Util.GetHostFromDNS(Host);
                    if (ip != null)
                    {
                        IP    = ip;
                        Flags = OSHTTPURIFlags.ValidResolved;
                    }
                }
            }
            catch
            {
                Flags = OSHTTPURIFlags.None;
                IP    = null;
                URI   = string.Empty;
            }
        }
示例#2
0
        public bool ResolveDNS()
        {
            IPAddress ip = Util.GetHostFromDNS(Host);

            if (ip == null)
            {
                Flags &= ~OSHTTPURIFlags.Resolved;
                return(false);
            }
            Flags |= OSHTTPURIFlags.Resolved;
            return(true);
        }
示例#3
0
        public OSHHTPHost(string url, bool withDNSResolve = false)
        {
            Flags = OSHTTPURIFlags.Empty;
            Port  = 80;
            IP    = null;
            Host  = string.Empty;
            URI   = string.Empty;

            bool secureHTTP = false;

            if (string.IsNullOrEmpty(url))
            {
                return;
            }

            Flags = OSHTTPURIFlags.None;

            url = url.ToLowerInvariant();
            try
            {
                int urllen = url.Length;
                if (url[urllen - 1] == '/')
                {
                    --urllen;
                }
                int start;
                if (url.StartsWith("http"))
                {
                    if (url[4] == 's')
                    {
                        start      = 8;
                        secureHTTP = true;
                    }
                    else
                    {
                        start = 7;
                    }
                }
                else
                {
                    start = 0;
                }

                string          host;
                UriHostNameType type;
                int             indx = url.IndexOf(':', start, urllen - start);
                if (indx > 0)
                {
                    host = url.Substring(start, indx - start);
                    type = Uri.CheckHostName(host);
                    if (type == UriHostNameType.Unknown || type == UriHostNameType.Basic)
                    {
                        return;
                    }
                    ++indx;
                    string sport = url.Substring(indx, urllen - indx);
                    int    tmp;
                    if (!int.TryParse(sport, out tmp) || tmp < 0 || tmp > 65535)
                    {
                        return;
                    }

                    Flags = OSHTTPURIFlags.ValidHost;
                    Host  = host;
                    Port  = tmp;
                    URI   = (secureHTTP ? "https://" : "http://") + Host + ":" + Port.ToString();
                }
                else
                {
                    host = url.Substring(start, urllen - start);
                    type = Uri.CheckHostName(host);
                    if (type == UriHostNameType.Unknown || type == UriHostNameType.Basic)
                    {
                        return;
                    }

                    Flags = OSHTTPURIFlags.ValidHost;
                    Host  = host;
                    if (secureHTTP)
                    {
                        Port = 443;
                        URI  = "https://" + Host + ":443";
                    }
                    else
                    {
                        Port = 80;
                        URI  = "http://" + Host + ":80";
                    }
                }

                if (withDNSResolve)
                {
                    IPAddress ip = Util.GetHostFromDNS(host);
                    if (ip != null)
                    {
                        Flags = OSHTTPURIFlags.ValidResolved;
                        IP    = ip;
                    }
                }
            }
            catch
            {
                Flags = OSHTTPURIFlags.None;
                IP    = null;
                URI   = string.Empty;
            }
        }