コード例 #1
0
        internal static object GetSection(string sectionName, string path, HttpContext context)
        {
            if (String.IsNullOrEmpty(sectionName))
            {
                return(null);
            }

            _Configuration c            = OpenWebConfiguration(path, null, null, null, null, null, false);
            string         configPath   = c.ConfigPath;
            int            baseCacheKey = 0;
            int            cacheKey;
            bool           pathPresent  = !String.IsNullOrEmpty(path);
            string         locationPath = null;
            bool           locked       = false;

            if (pathPresent)
            {
                locationPath = "location_" + path;
            }

            baseCacheKey = sectionName.GetHashCode();
            if (configPath != null)
            {
                baseCacheKey ^= configPath.GetHashCode();
            }

            try {
#if SYSTEMCORE_DEP
                sectionCacheLock.EnterReadLock();
#endif
                locked = true;

                object o;
                if (pathPresent)
                {
                    cacheKey = baseCacheKey ^ locationPath.GetHashCode();
                    if (sectionCache.TryGetValue(cacheKey, out o))
                    {
                        return(o);
                    }

                    cacheKey = baseCacheKey ^ path.GetHashCode();
                    if (sectionCache.TryGetValue(cacheKey, out o))
                    {
                        return(o);
                    }
                }

                if (sectionCache.TryGetValue(baseCacheKey, out o))
                {
                    return(o);
                }
            } finally {
#if SYSTEMCORE_DEP
                if (locked)
                {
                    sectionCacheLock.ExitReadLock();
                }
#endif
            }

            string cachePath = null;
            if (pathPresent)
            {
                string relPath;

                if (VirtualPathUtility.IsRooted(path))
                {
                    if (path [0] == '~')
                    {
                        relPath = path.Substring(2);
                    }
                    else if (path [0] == '/')
                    {
                        relPath = path.Substring(1);
                    }
                    else
                    {
                        relPath = path;
                    }
                }
                else
                {
                    relPath = path;
                }

                _Configuration cnew;

                HttpRequest req = context != null ? context.Request : null;
                if (req != null)
                {
                    string vdir = VirtualPathUtility.GetDirectory(req.Path);
                    if (vdir != null)
                    {
                        vdir = vdir.TrimEnd(pathTrimChars);
                        if (String.Compare(c.ConfigPath, vdir, StringComparison.Ordinal) != 0 && LookUpLocation(vdir.Trim(pathTrimChars), ref c))
                        {
                            cachePath = path;
                        }
                    }
                }

                if (LookUpLocation(relPath, ref c))
                {
                    cachePath = locationPath;
                }
                else
                {
                    cachePath = path;
                }
            }

            ConfigurationSection section = c.GetSection(sectionName);
            if (section == null)
            {
                return(null);
            }

#if TARGET_J2EE
            object value = get_runtime_object.Invoke(section, new object [0]);
            if (String.CompareOrdinal("appSettings", sectionName) == 0)
            {
                NameValueCollection collection;
                collection = new KeyValueMergedCollection(HttpContext.Current, (NameValueCollection)value);
                value      = collection;
            }
#else
#if MONOWEB_DEP
            object value = SettingsMappingManager.MapSection(get_runtime_object.Invoke(section, new object [0]));
#else
            object value = null;
#endif
#endif
            if (cachePath != null)
            {
                cacheKey = baseCacheKey ^ cachePath.GetHashCode();
            }
            else
            {
                cacheKey = baseCacheKey;
            }

            AddSectionToCache(cacheKey, value);
            return(value);
        }
コード例 #2
0
        internal static object GetSection(string sectionName, string path, HttpContext context)
        {
            // FindWebConfig must not be used here with its result being passed to
            // OpenWebConfiguration below. The reason is that if we have a request for
            // ~/somepath/, but FindWebConfig returns ~/ and the ~/web.config contains
            // <location path="somepath"> then OpenWebConfiguration will NOT return the
            // contents of <location>, thus leading to bugs (ignored authorization
            // section for instance)
            string config_vdir = FindWebConfig(path);

            if (String.IsNullOrEmpty(config_vdir))
            {
                config_vdir = "/";
            }

            int    sectionCacheKey = GetSectionCacheKey(sectionName, config_vdir);
            object cachedSection;
            bool   locked = false;

            try {
#if SYSTEMCORE_DEP
                sectionCacheLock.EnterReadLock();
#endif
                locked = true;

                if (sectionCache.TryGetValue(sectionCacheKey, out cachedSection) && cachedSection != null)
                {
                    return(cachedSection);
                }
            } finally {
#if SYSTEMCORE_DEP
                if (locked)
                {
                    sectionCacheLock.ExitReadLock();
                }
#endif
            }

            HttpRequest    req = context != null ? context.Request : null;
            _Configuration c   = OpenWebConfiguration(path,                                                           /* path */
                                                      null,                                                           /* site */
                                                      req != null ? VirtualPathUtility.GetDirectory(req.Path) : null, /* locationSubPath */
                                                      null,                                                           /* server */
                                                      null,                                                           /* userName */
                                                      null,                                                           /* password */
                                                      false /* path from FindWebConfig */);
            ConfigurationSection section = c.GetSection(sectionName);
            if (section == null)
            {
                return(null);
            }

#if TARGET_J2EE
            object value = get_runtime_object.Invoke(section, new object [0]);
            if (String.CompareOrdinal("appSettings", sectionName) == 0)
            {
                NameValueCollection collection;
                collection = new KeyValueMergedCollection(HttpContext.Current, (NameValueCollection)value);
                value      = collection;
            }

            AddSectionToCache(sectionCacheKey, value);
            return(value);
#else
#if MONOWEB_DEP
            object value = SettingsMappingManager.MapSection(get_runtime_object.Invoke(section, new object [0]));
#else
            object value = null;
#endif
            AddSectionToCache(sectionCacheKey, value);
            return(value);
#endif
        }