Beispiel #1
0
        private void ParseFile(string physicalPath, VirtualPath virtualPath)
        {
            string o = (physicalPath != null) ? physicalPath : virtualPath.VirtualPathString;

            if (this._circularReferenceChecker.Contains(o))
            {
                throw new HttpException(System.Web.SR.GetString("Circular_include"));
            }
            this._circularReferenceChecker.Add(o);
            try
            {
                TextReader reader;
                if (physicalPath != null)
                {
                    using (reader = Util.ReaderFromFile(physicalPath, virtualPath))
                    {
                        this.ParseReader(reader);
                        return;
                    }
                }
                using (Stream stream = virtualPath.OpenFile())
                {
                    reader = Util.ReaderFromStream(stream, virtualPath);
                    this.ParseReader(reader);
                }
            }
            finally
            {
                this._circularReferenceChecker.Remove(o);
            }
        }
Beispiel #2
0
 internal static string StringFromVirtualPath(VirtualPath virtualPath)
 {
     using (Stream stream = virtualPath.OpenFile())
     {
         return(ReaderFromStream(stream, virtualPath).ReadToEnd());
     }
 }
Beispiel #3
0
        private void ParseFile(string physicalPath, VirtualPath virtualPath)
        {
            // Determine the file used for the circular references checker.  Normally,
            // we use the virtualPath, but we use the physical path if it specified,
            // as is the case for <!-- #include file="foo.inc" -->
            string fileToReferenceCheck = physicalPath != null ? physicalPath : virtualPath.VirtualPathString;

            // Check for circular references of include files
            if (_circularReferenceChecker.Contains(fileToReferenceCheck))
            {
                throw new HttpException(
                          SR.GetString(SR.Circular_include));
            }

            // Add the current file to the circular references checker.
            _circularReferenceChecker.Add(fileToReferenceCheck);

            try {
                // Open a TextReader either from the physical or virtual path
                TextReader reader;
                if (physicalPath != null)
                {
                    using (reader = Util.ReaderFromFile(physicalPath, virtualPath)) {
                        ParseReader(reader);
                    }
                }
                else
                {
                    using (Stream stream = virtualPath.OpenFile()) {
                        reader = Util.ReaderFromStream(stream, virtualPath);
                        ParseReader(reader);
                    }
                }
            }
            finally {
                // Remove the current file from the circular references checker
                _circularReferenceChecker.Remove(fileToReferenceCheck);
            }
        }
        ///
        /// Open a stream from either a virtual or physical path, and if possible get a CacheDependency
        /// for the resulting Stream.
        ///
        internal Stream OpenFileAndGetDependency(VirtualPath virtualPath, string physicalPath, out CacheDependency dependency) {

            // Only one of the paths should be non-null
            Debug.Assert((virtualPath == null) != (physicalPath == null));

            // If we got a virtual path, and we're using the default VPP, call MapPath
            if (physicalPath == null && HostingEnvironment.UsingMapPathBasedVirtualPathProvider) {
                physicalPath = virtualPath.MapPathInternal(TemplateControlVirtualDirectory,
                    true /*allowCrossAppMapping*/);
            }

            Stream stream;
            if (physicalPath != null) {
                // Security check
                HttpRuntime.CheckFilePermission(physicalPath);

                // Work directly with the physical file, bypassing the VPP
                stream = new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                dependency = new CacheDependency(0, physicalPath);
            }
            else {
                // It's non file system based, so go though the VirtualPathProvider
                stream = virtualPath.OpenFile();
                dependency = VirtualPathProvider.GetCacheDependency(virtualPath);
            }

            return stream;
        }
Beispiel #5
0
    internal void ParseFile(string physicalPath, VirtualPath virtualPath) {

        // Determine the file used for the circular references checker.  Normally,
        // we use the virtualPath, but we use the physical path if it specified,
        // as is the case for <!-- #include file="foo.inc" -->
        string fileToReferenceCheck = physicalPath != null ? physicalPath : virtualPath.VirtualPathString;

        // Check for circular references of include files
        if (_circularReferenceChecker.Contains(fileToReferenceCheck)) {
            ProcessError(SR.GetString(SR.Circular_include));

            return;
        }

        // Add the current file to the circular references checker.
        _circularReferenceChecker.Add(fileToReferenceCheck);

        try {
            // Open a TextReader either from the physical or virtual path
            StreamReader reader;
            if (physicalPath != null) {
                using (reader = Util.ReaderFromFile(physicalPath, CurrentVirtualPath)) {
                    ParseReader(reader, virtualPath);
                }
            }
            else {
                // Open a TextReader for the virtualPath we're parsing
                using (Stream stream = virtualPath.OpenFile()) {
                    reader = Util.ReaderFromStream(stream, CurrentVirtualPath);
                    ParseReader(reader, virtualPath);
                }
            }
        }
        finally {
            // Remove the current file from the circular references checker
            _circularReferenceChecker.Remove(fileToReferenceCheck);
        }
    }
    /*
     * Return a String which holds the contents of a file
     */
    internal /*public*/ static String StringFromVirtualPath(VirtualPath virtualPath) {

        using (Stream stream = virtualPath.OpenFile()) {
            // Create a reader on the file, and read the whole thing
            TextReader reader = Util.ReaderFromStream(stream, virtualPath);
            return reader.ReadToEnd();
        }
    }
        internal CodeCompileUnit GenerateCodeCompileUnit(
            VirtualPath virtualPath, string virtualFileString, out Type codeDomProviderType,
            out CompilerParameters compilerParameters, out IDictionary linePragmasTable)
        {
            // Add a pending call to make sure our thread doesn't get killed
            AddPendingCall();

            try {
                BuildManager.SkipTopLevelCompilationExceptions = true;
                _buildManager.EnsureTopLevelFilesCompiled();

                // Get the virtual file content so that we can use the correct hash code.
                if (virtualFileString == null)
                {
                    using (Stream stream = virtualPath.OpenFile()) {
                        TextReader reader = Util.ReaderFromStream(stream, virtualPath);
                        virtualFileString = reader.ReadToEnd();
                    }
                }

                _virtualPathProvider.RegisterVirtualFile(virtualPath, virtualFileString);

                string cacheKey = BuildManager.GetCacheKeyFromVirtualPath(virtualPath) + "_CBMResult";
                BuildResultCodeCompileUnit result = (BuildResultCodeCompileUnit)BuildManager.GetBuildResultFromCache(cacheKey, virtualPath);

                if (result == null)
                {
                    lock (_lock) {
                        // Don't need to check the result again since it's very unlikely in CBM scenarios.
                        DateTime utcStart = DateTime.UtcNow;

                        BuildProvider internalBuildProvider = GetCompilerParamsAndBuildProvider(
                            virtualPath, out codeDomProviderType, out compilerParameters);

                        // This is the no-compile case
                        if (internalBuildProvider == null)
                        {
                            linePragmasTable = null;
                            return(null);
                        }

                        CodeCompileUnit ccu = internalBuildProvider.GetCodeCompileUnit(out linePragmasTable);

                        result             = new BuildResultCodeCompileUnit(codeDomProviderType, ccu, compilerParameters, linePragmasTable);
                        result.VirtualPath = virtualPath;
                        result.SetCacheKey(cacheKey);

                        FixupReferencedAssemblies(virtualPath, compilerParameters);

                        // CodeCompileUnit could be null, do not try to fix referenced assemblies.
                        // This happens for example when an .asmx file does not contain any code.
                        if (ccu != null)
                        {
                            // VSWhidbey 501260 Add all the referenced assemblies to the CodeCompileUnit
                            // in case the CodeDom provider needs them for code generation
                            foreach (String assemblyString in compilerParameters.ReferencedAssemblies)
                            {
                                ccu.ReferencedAssemblies.Add(assemblyString);
                            }
                        }

                        // Add all the dependencies, so that the ccu gets cached correctly (VSWhidbey 275091)
                        ICollection dependencies = internalBuildProvider.VirtualPathDependencies;
                        if (dependencies != null)
                        {
                            result.AddVirtualPathDependencies(dependencies);
                        }

                        BuildManager.CacheBuildResult(cacheKey, result, utcStart);

                        return(ccu);
                    }
                }

                codeDomProviderType = result.CodeDomProviderType;
                compilerParameters  = result.CompilerParameters;
                linePragmasTable    = result.LinePragmasTable;

                FixupReferencedAssemblies(virtualPath, compilerParameters);

                return(result.CodeCompileUnit);
            }
            finally {
                if (virtualFileString != null)
                {
                    _virtualPathProvider.RevertVirtualFile(virtualPath);
                }
                BuildManager.SkipTopLevelCompilationExceptions = false;

                RemovePendingCall();
            }
        }
 internal /*protected*/ Stream OpenStream(VirtualPath virtualPath)
 {
     return(virtualPath.OpenFile());
 }
 internal /*protected*/ Stream OpenStream(VirtualPath virtualPath) {
     return virtualPath.OpenFile();
 }
        internal CodeCompileUnit GenerateCodeCompileUnit(VirtualPath virtualPath, string virtualFileString, out Type codeDomProviderType, out CompilerParameters compilerParameters, out IDictionary linePragmasTable)
        {
            CodeCompileUnit codeCompileUnit;

            this.AddPendingCall();
            try
            {
                BuildManager.SkipTopLevelCompilationExceptions = true;
                this._buildManager.EnsureTopLevelFilesCompiled();
                if (virtualFileString == null)
                {
                    using (Stream stream = virtualPath.OpenFile())
                    {
                        virtualFileString = Util.ReaderFromStream(stream, virtualPath).ReadToEnd();
                    }
                }
                this._virtualPathProvider.RegisterVirtualFile(virtualPath, virtualFileString);
                string cacheKey = BuildManager.GetCacheKeyFromVirtualPath(virtualPath) + "_CBMResult";
                BuildResultCodeCompileUnit buildResultFromCache = (BuildResultCodeCompileUnit)BuildManager.GetBuildResultFromCache(cacheKey, virtualPath);
                if (buildResultFromCache == null)
                {
                    lock (this._lock)
                    {
                        DateTime utcNow = DateTime.UtcNow;
                        System.Web.Compilation.BuildProvider provider = this.GetCompilerParamsAndBuildProvider(virtualPath, out codeDomProviderType, out compilerParameters);
                        if (provider == null)
                        {
                            linePragmasTable = null;
                            return(null);
                        }
                        CodeCompileUnit unit2 = provider.GetCodeCompileUnit(out linePragmasTable);
                        buildResultFromCache = new BuildResultCodeCompileUnit(codeDomProviderType, unit2, compilerParameters, linePragmasTable)
                        {
                            VirtualPath = virtualPath
                        };
                        buildResultFromCache.SetCacheKey(cacheKey);
                        this.FixupReferencedAssemblies(virtualPath, compilerParameters);
                        if (unit2 != null)
                        {
                            foreach (string str2 in compilerParameters.ReferencedAssemblies)
                            {
                                unit2.ReferencedAssemblies.Add(str2);
                            }
                        }
                        ICollection virtualPathDependencies = provider.VirtualPathDependencies;
                        if (virtualPathDependencies != null)
                        {
                            buildResultFromCache.AddVirtualPathDependencies(virtualPathDependencies);
                        }
                        BuildManager.CacheBuildResult(cacheKey, buildResultFromCache, utcNow);
                        return(unit2);
                    }
                }
                codeDomProviderType = buildResultFromCache.CodeDomProviderType;
                compilerParameters  = buildResultFromCache.CompilerParameters;
                linePragmasTable    = buildResultFromCache.LinePragmasTable;
                this.FixupReferencedAssemblies(virtualPath, compilerParameters);
                codeCompileUnit = buildResultFromCache.CodeCompileUnit;
            }
            finally
            {
                if (virtualFileString != null)
                {
                    this._virtualPathProvider.RevertVirtualFile(virtualPath);
                }
                BuildManager.SkipTopLevelCompilationExceptions = false;
                this.RemovePendingCall();
            }
            return(codeCompileUnit);
        }