示例#1
0
        /// <summary>
        /// ExtractSegmentedReversedUrl
        /// </summary>
        /// <param name="key"></param>
        /// <param name="reversed"></param>
        /// <param name="srUrl"></param>
        /// <returns></returns>
        public SegmentedReversedUrl ExtractSegmentedReversedUrl(String reversedKey, bool reversed)
        {
            SegmentedReversedUrl srUrl = null;

            if (!reversed)
            {
                reversedKey = UrlReverser.Reverse(reversedKey);
            }

            if (String.IsNullOrEmpty(reversedKey))
            {
                return srUrl;
            }

            if (reversedKey.Length < ProtocolPrefixLength)
            {
                return srUrl;
            }

            String port = null;

            if (reversedKey[0] == SchemeHttpEncodingSymbol)
            {
            }
            else if (reversedKey[0] == schemeHttpsEncodingSymbol)
            {
            }
            else
            {
                return srUrl;
            }

            Int32 portEndIndex = reversedKey.IndexOf(PortSpacer);

            if (portEndIndex == -1 || portEndIndex > (ProtocolPrefixLength) - 1)
            {
                portEndIndex = 1 + MaxPortLength;
            }

            if (portEndIndex > 1)
            {
                port = reversedKey.Substring(1, portEndIndex - 1);
            }

            // Start to parse host here

            Int32 hostStartIndex = ProtocolPrefixLength;

            if (hostStartIndex >= reversedKey.Length)
            {
                return srUrl;
            }

            Int32 hostEndIndex = reversedKey.IndexOf(PathDelimeter, hostStartIndex);
            if (hostEndIndex == -1)
            {
                hostEndIndex = reversedKey.Length;
            }

            if (hostEndIndex == hostStartIndex)
            {
                return srUrl;
            }

            String host = reversedKey.Substring(hostStartIndex, hostEndIndex - hostStartIndex);
            String[] byDot = host.Split(new char[]{HostDelimeter}, StringSplitOptions.RemoveEmptyEntries);
            Boolean isIP = byDot.Length == 4 && IsIPHost(byDot[0], byDot[1], byDot[2], byDot[3]);

            int domainEndIndex = hostStartIndex;

            if (isIP)
            {
                domainEndIndex = hostEndIndex;
            }
            else if (byDot.Length <= 2)
            {
                domainEndIndex = hostEndIndex;
            }
            else
            {
                // Get domain
                bool tldfound = false;

                for (; domainEndIndex < hostEndIndex; ++domainEndIndex)
                {
                    if (reversedKey[domainEndIndex] != HostDelimeter)
                    {
                        continue;
                    }
                    else
                    {

                        string suspiciousTld = reversedKey.Substring(hostStartIndex, domainEndIndex - hostStartIndex + 1);

                        if (tlds.ContainsKey(suspiciousTld))
                        {
                            tldfound = true;
                        }
                        else
                        {
                            if (tldfound)
                            {
                                // The sub string up to the last HostDelimeter is the tld
                                // so that the sub string up to here is the domain

                                ++domainEndIndex;
                                break;
                            }
                        }

                    }
                }

                // tld prefix not found, so just treat the first two segments as the domain
                if (!tldfound)
                {
                    domainEndIndex = hostStartIndex;
                    domainEndIndex += byDot[0].Length + 1 + byDot[1].Length + 1;
                }
            }

            // Find L1Path
            int L1EndIndex = -1;
            int L1StartIndex = -1;
            int UrlEndIndex = reversedKey.Length;

            if (hostEndIndex == reversedKey.Length)
            {
                L1EndIndex = 0;
                L1StartIndex = 0;
                UrlEndIndex = 0;
            }
            else
            {
                L1StartIndex = hostEndIndex;
                L1EndIndex = reversedKey.IndexOfAny(L1PathDelimeters, L1StartIndex + 1);

                if (L1EndIndex == -1)
                {
                    L1EndIndex = L1StartIndex;
                }

                ++L1EndIndex;
            }

            String domain = reversedKey.Substring(0, domainEndIndex );
            String hostSuffix = reversedKey.Substring(domainEndIndex, hostEndIndex - domainEndIndex);
            String l1PathSuffix = reversedKey.Substring(L1StartIndex, L1EndIndex - L1StartIndex);
            String urlSuffix = reversedKey.Substring(L1EndIndex, UrlEndIndex - L1EndIndex);

            // Sanity check for segments
            // will potentially hurt the performance, so don't use it in production
            /*
            if (l1PathSuffix.Length > 0)
            {
                Common.Assert(l1PathSuffix[0] == '/',
                    "Assertion failed: L1PathSuffix doesn't start with /, L1PathSuffix = {0}, Key = {1}",
                    l1PathSuffix,
                    reversedKey);

                Common.Assert(l1PathSuffix[l1PathSuffix.Length - 1] == L1PathDelimeters[0] ||
                              l1PathSuffix[l1PathSuffix.Length - 1] == L1PathDelimeters[1],
                    "Assertion failed: L1PathSuffix doesn't end with /, L1PathSuffix = {0}, Key = {1}",
                    l1PathSuffix,
                    reversedKey);
            }
            */

            srUrl = new SegmentedReversedUrl(domain,
                                          hostSuffix,
                                          l1PathSuffix,
                                          urlSuffix);

            return srUrl;
        }
示例#2
0
        public string MapSegmentedUrlToSite( SegmentedReversedUrl segUrl)
        {
            String domain, host, l1Path;
            domain =  segUrl.ReversedDomain;
            host = segUrl.ReversedHost;
            l1Path = segUrl.ReversedL1Path ;
            string site = null;

            if( !isDefined )
            {
               throw new Exception("SitePrefixTable is not defined" );
            }

            try
            {

               // same filtering of host/l1path  in construction of SitePrefixTable
               // normalization of  prefixe dupes
               {
                   if( !String.IsNullOrEmpty(l1Path ))
                   {
                       l1Path = l1Path.Trim().Replace( " ", "%20" );
                       if( host == l1Path || l1Path == (host + "/") || l1Path == (host + "//"))
                       {
                           l1Path = "";
                       }
                   }

                   if( !String.IsNullOrEmpty(host))
                   {

                       if( host == domain || host == (domain + "/") || host == (domain + "//"))
                       {
                           host = "";
                       }
                   }
               }

               if(!String.IsNullOrEmpty(l1Path) && table.ContainsKey( l1Path ))
               {
                   site =  l1Path;
               }
               else if( !String.IsNullOrEmpty(host) && table.ContainsKey( host ))
               {
                   site =  host;
               }
               else
               {
                   site = domain;
               }

               if( !getReversedSite)
               {
                   site = UrlReverser.ReverseBack( site );
               }
            }
            catch(Exception)
            {
               return null;
            }
            return site;
        }