Esempio n. 1
0
        public static string MakeFullLink(string link, string CodeBase, string Application)
        {
            link = CanonizeURL(link, URL_CANONFLAGS.URL_WININET_COMPATIBILITY);
            link = link.Replace('\\', '/');
            int j1 = link.IndexOf(':');

            if (j1 == -1 ||
                !(j1 + 3 < link.Length &&
                  link[j1 + 1] == '/' &&
                  link[j1 + 2] == '/')) //no protocol, so it is relative.
            //yes, UNC and DOS path are treated as relative
            {
                string AppBase = CodeBase;
                if (CodeBase != null)
                {
                    if (Application != null)
                    {
                        if (CodeBase[CodeBase.Length - 1] == '/')
                        {
                            AppBase += Application;
                        }
                        else
                        {
                            AppBase = AppBase + "/" + Application;
                        }
                    }
                }
                else
                {
                    AppBase = Application;
                }

                if (link[0] != '/')  //just relative path
                {
                    if (AppBase != null && AppBase[0] != 0)
                    {
                        if (AppBase[AppBase.Length - 1] == '/')
                        {
                            link = AppBase + link;
                        }
                        else
                        {
                            link = AppBase + "/" + link;
                        }
                        Log(true, "Link is relative: replaced with " + link, "");
                    }
                }
                else     // path is relative to root
                {
                    if (AppBase != null && AppBase[0] != 0)
                    {
                        string sRoot = null;
                        int    j     = AppBase.IndexOf(':');
                        if (j != -1 &&
                            (j + 3 < AppBase.Length &&
                             AppBase[j + 1] == '/' &&
                             AppBase[j + 2] == '/')) //protocol present

                        {
                            int i = AppBase.IndexOf('/', j + 3);
                            if (i == -1)
                            {
                                sRoot = AppBase;
                            }
                            else
                            {
                                sRoot = AppBase.Substring(0, i);
                            }
                        }
                        else
                        {
                            //no protocol
                            // Log(this,true,"Code base doesnot have a protocol - "+AppBase,"");
                            // no need to throw: the path is relative anyway, so there's no hazard

                            j = AppBase.IndexOf('/');
                            if (j != -1)
                            {
                                sRoot = AppBase.Substring(0, j); //take root
                            }
                        }
                        if (sRoot != null)
                        {
                            link = sRoot + link;
                            Log(true, "Link is relative to the root: replaced with " + link, "");
                        }
                    }
                }
            }
            else
            {
                // full path to cfg
                // must be on the same site
                if (!AreOnTheSameSite(link, CodeBase))
                {
                    Log(true, "Security block: Config file is on another site " + CodeBase + ": " + link, "");
                    throw new ArgumentException("Config file is on another site " + CodeBase + ": " + link);
                }
            }
            return(CanonizeURL(link, URL_CANONFLAGS.URL_WININET_COMPATIBILITY));
        }