/// <include file='doc\HttpContext.uex' path='docs/doc[@for="HttpContext.GetAppConfig"]/*' />
        public static Object GetAppConfig(String name)
        {
            HttpContext context = HttpContext.Current;

            HttpContext.ImpersonationSuspendContext ictx = null;

            if (context != null)
            {
                ictx = context.Impersonation.SuspendIfClient();
            }

            try {
                try {
                    HttpConfigurationRecord configRecord = HttpConfigurationSystem.GetCompleteForApp();
                    if (configRecord != null)
                    {
                        return(configRecord[name]);
                    }
                }
                finally {
                    if (ictx != null)
                    {
                        ictx.Resume();
                    }
                }
            }
            catch {
                throw;
            }

            return(null);
        }
        /*
         * Uses the Config system to get the specified configuraiton
         *
         * The following method is the only accessors to the config system,
         * and it wraps config exceptions with an explanatory HTTP
         * exception with an error formatter. It also makes sure the client
         * is not impersonated while accessing the config system.
         */
        private HttpConfigurationRecord GetCompleteConfigRecord(String reqpath, IHttpMapPath configmap)
        {
            HttpConfigurationRecord configrecord = null;

            HttpContext.ImpersonationSuspendContext ictx = Impersonation.SuspendIfClient();

            try {
                try {
                    if (reqpath == null || reqpath.Length == 0 || HttpRuntime.IsPathWithinAppRoot(reqpath))
                    {
                        // lookup by path
                        using (new HttpContextWrapper(this)) {
                            configrecord = HttpConfigurationSystem.GetComplete(reqpath, _wr);
                        }
                    }
                    else
                    {
                        // if the path is outside of the application (and not site or machine)
                        // then use application config
                        configrecord = HttpConfigurationSystem.GetCompleteForApp();
                    }
                }
                finally {
                    ictx.Resume();
                }
            }
            catch { // Protect against exception filters
                throw;
            }

            return(configrecord);
        }
 // Used to implement Clone(), used in batch compilation
 // We only copy the fields necessary to perform batch compilation.
 private HttpContext(HttpContext context)
 {
     //_asyncAppHandler = context._asyncAppHandler;
     _appInstance = context._appInstance;
     //_handler = context._handler;
     _request  = context._request;
     _response = context._response;
     _server   = context._server;
     //_traceContextStack = context._traceContextStack;
     //_topTraceContext = context._topTraceContext;
     //_items = context._items;
     //_errors = context._errors;
     //_tempError = context._tempError;
     //_errorCleared = context._errorCleared;
     _user = context._user;
     //_utcTimestamp = context._utcTimestamp;
     _wr = context._wr;
     //_root = context._root;
     _configPath = context._configPath;
     //_skipAuthorization = context._skipAuthorization;
     //_dynamicCulture = context._dynamicCulture;
     //_dynamicUICulture = context._dynamicUICulture;
     //_impersonation = context._impersonation;
     //_timeoutStartTime = context._timeoutStartTime;
     //_timeoutSet = context._timeoutSet;
     //_timeout = context._timeout;
     //_timeoutState = context._timeoutState;
     //_timeoutLink = context._timeoutLink;
     _configrecord = context._configrecord;
 }
 internal HttpConfigurationRecord GetCompleteConfig()
 {
     if (_configrecord == null)
     {
         _configrecord = GetCompleteConfigRecord(ConfigPath, _wr);
     }
     return(_configrecord);
 }
Beispiel #5
0
        object IConfigurationSystem.GetConfig(string configKey)
        {
            HttpContext context = HttpContext.Current;

            if (context != null)
            {
                return(context.GetConfig(configKey));
            }

            // if no context is available we assume application-level configuration
            HttpConfigurationRecord applicationLevelConfigRecord = HttpConfigurationSystem.GetComplete();

            return(applicationLevelConfigRecord.GetConfig(configKey));
        }
        // look up item in cache (case-insensitive)
        internal static HttpConfigurationRecord CacheLookup(string vpath)
        {
            string cachekey = CacheKey(vpath);

            HttpConfigurationRecord record = (HttpConfigurationRecord)HttpRuntime.CacheInternal.Get(cachekey);

            Debug.Trace("config_verbose", "Cache " +
                        ((record == null) ? "miss" : "hit") + " on \"" + cachekey + "\"");

            if (record != null)
            {
                record.CheckCachedException();
            }

            return(record);
        }
        // This function is named after a comment in the original code that sums up what it does nicely.
        //  // now we zip up the path, composing config
        HttpConfigurationRecord ComposeConfig(String reqPath, IHttpMapPath configmap)
        {
            // remove ending "/"
            if (reqPath != null && reqPath.Length > 0 && reqPath[reqPath.Length - 1] == '/')
            {
                reqPath = reqPath.Substring(0, reqPath.Length - 1);
            }

            if (configmap == null && reqPath != null)
            {
                //
                // s_contextlessMapPath only works for paths from machine-level to application-level
                // This is only exposed to internal APIs, so this is just to make sure we don't break ourselves.
                //
                Debug.Assert(reqPath != null && (reqPath.Length > s_contextlessMapPath.ApplicationPath.Length ||
                                                 s_contextlessMapPath.ApplicationPath.Substring(0, reqPath.Length) == reqPath));

                configmap = s_contextlessMapPath;
            }
            // QUICK: look up record in cache (case-insensitive)

            //
            // This is the common case because most pages are requested more than once every five minutes
            // Note: will be null on first invocation
            //
            HttpConfigurationRecord configRecord = CacheLookup(reqPath);

            if (configRecord != null)
            {
                return(configRecord);
            }

            DateTime utcStart = DateTime.UtcNow;

            // SLOW: hunt around

            //
            // Next, we start from the end of the request path and work our way toward the machine-level config
            // record.  The first record we find is the one we use.  Later we'll walk back down the path stack
            // building all the records that weren't found in the cache.
            //
            // Note: On first invocation for this appdomain we won't find anything in the cache, so we'll compose
            //      all config records from machine-level down.
            //
            ArrayList pathStack = new ArrayList();
            String    path      = reqPath;
            HttpConfigurationRecord parentRecord = null;

            for (;;)
            {
                // stack child
                pathStack.Add(path);

                // escape from root
                if (path == null)
                {
                    break;
                }

                // go to parent
                path = ParentPath(path);

                // look it up
                parentRecord = CacheLookup(path);

                if (parentRecord == null)
                {
                    configRecord = null;
                }
                else
                {
                    if (parentRecord.IsInheritable)   // only inherit from directory ConfigRecords, else keep looking
                    {
                        Debug.Trace("config", "Config located in cache " + QuoteString(path));
                        break;
                    }
                    configRecord = parentRecord;
                }
            }

            // now we zip down the path, composing config

            //
            // We walk down the path ...
            // For each directory, we build a config record and add a dependency on the config file
            // The first path that doesn't map to a directory we assume
            //   is a file (leaf in the config system and not inheritable)
            // Anything left in the path stack after the file* is assumed to
            //      be pathinfo** and is ignored.
            //
            // Notes:
            // * - we never verify the physical file, it's assumed if the mapped path
            //      is not a directory (is this correct behavior?)
            // ** - pathinfo is the possible junk on the path after the file path:
            //      example: http://localhost/myapp/foo.aspx/bar
            //          /myapp/foo.aspx - file path (see Request.FilePath)
            //          /bar            - pathinfo
            //
            bool          isDir         = true;
            CacheInternal cacheInternal = HttpRuntime.CacheInternal;

            for (int i = pathStack.Count - 1; i >= 0 && isDir == true; i--)
            {
                String    configFile = null;
                String    mappedPath = null;
                Hashtable cachedeps  = null;

                path = (String)pathStack[i];


                if (path == null)
                {
                    mappedPath = "";
                    if (configmap == null)
                    {
                        configFile = HttpConfigurationSystemBase.MachineConfigurationFilePath;
                    }
                    else
                    {
                        configFile = configmap.MachineConfigPath;
                    }

                    // null MachineConfigPath -> never cache config - always return an empty record.
                    if (configFile == null)
                    {
                        return(HttpConfigurationRecord.Empty);
                    }

                    AddFileDependency(configFile);
                }
                else
                {
                    if (IsPath(path))
                    {
                        mappedPath = configmap.MapPath(path);

                        if (IsDirectory(mappedPath))
                        {
                            // for the directory case, grab the config file and a dependency on it
                            configFile = Path.Combine(mappedPath, WebConfigFileName);
                            AddFileDependency(configFile);
                        }
                        else
                        {
                            // otherwise, we're a file and we go ahead and compute as a file
                            isDir = false; // break loop (after record is built)

                            // we need make the cache item dependent on the mapped file location
                            // in case it becomes a directory.
                            if (mappedPath != null)
                            {
                                Debug.Assert(cachedeps == null, "ctracy investigation - cachedeps == null");
                                cachedeps             = new Hashtable(SymbolHashCodeProvider.Default, SymbolEqualComparer.Default);
                                cachedeps[mappedPath] = mappedPath;
                            }
                        }
                    }
                }

                configRecord = new HttpConfigurationRecord(configFile, parentRecord, /*mappedPath,*/ isDir, path);
                string          key        = CacheKey(path);
                CacheDependency dependency = GetCacheDependencies(cachedeps, utcStart);

                Debug.Trace("config_verbose", "Inserting :" + path);

                if (IsBreakOnUnrecognizedElement)
                {
                    // If the registry key is set to debug the rare 'Unrecognized Element'
                    // stress error, lets try to reproduce the error by having an absolute
                    // expiry of 5 minutes (this will cause us to re-read config much more
                    // often.  Before it took memory pressure.
                    DateTime absouteExpiry = DateTime.UtcNow + new TimeSpan(0, 5, 0);
                    cacheInternal.UtcInsert(key, configRecord, dependency, absouteExpiry, Cache.NoSlidingExpiration);
                }
                else
                {
                    if (configRecord.HasError)
                    {
                        cacheInternal.UtcInsert(key, configRecord, dependency,
                                                DateTime.UtcNow.AddSeconds(5), Cache.NoSlidingExpiration);
                    }
                    else
                    {
                        // default: default cache priority, sliding expiration
                        // this will make us rarely expire, config is expensive
                        cacheInternal.UtcInsert(key, configRecord, dependency);
                    }
                }

                // This is to wire-up notification of directories who exist (without a config file) and
                // are then deleted.  In this case we don't want to restart the appdomain because no config information has
                // changed.  We do want to remove the cache record and recreate it on the next request.  (Something in
                // the cache record might be assuming the directory still exists.)
                if (isDir && !FileUtil.FileExists(configFile) && HandlerBase.IsPathAtAppLevel(path) == PathLevel.BelowApp)
                {
                    //AddDirectoryDependency(mappedPath, path, configRecord);
                }

                configRecord.CheckCachedException();
                parentRecord = configRecord;
            }

            return(configRecord);
        }