[asp.net] Ignore JavaScript blocks enclosed in HTML comments
[mono.git] / mcs / class / System.Web / System.Web.Compilation / AssemblyBuilder.cs
index a8d610633a6bf4efdf55cecf0d5b8fbc4a8d4031..99f704a34d703d8fce10a08dc4d9ee9e8e5f0eb6 100644 (file)
@@ -30,7 +30,7 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
+
 
 using System;
 using System.CodeDom;
@@ -39,14 +39,16 @@ using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.IO;
+using System.Security.Cryptography;
 using System.Reflection;
 using System.Text;
 using System.Web.Configuration;
 using System.Web.Util;
 using System.Web.Hosting;
 
-namespace System.Web.Compilation {
-       internal class CompileUnitPartialType
+namespace System.Web.Compilation
+{
+       class CompileUnitPartialType
        {
                public readonly CodeCompileUnit Unit;
                public readonly CodeNamespace ParentNamespace;
@@ -79,7 +81,128 @@ namespace System.Web.Compilation {
                }
        }
        
-       public class AssemblyBuilder {
+       public class AssemblyBuilder
+       {
+               struct CodeUnit
+               {
+                       public readonly BuildProvider BuildProvider;
+                       public readonly CodeCompileUnit Unit;
+
+                       public CodeUnit (BuildProvider bp, CodeCompileUnit unit)
+                       {
+                               this.BuildProvider = bp;
+                               this.Unit = unit;
+                       }
+               }
+
+               interface ICodePragmaGenerator
+               {
+                       int ReserveSpace (string filename);
+                       void DecorateFile (string path, string filename, MD5 checksum, Encoding enc);
+               }
+
+               class CSharpCodePragmaGenerator : ICodePragmaGenerator
+               {
+                       // Copied from CSharpCodeGenerator.cs
+                       string QuoteSnippetString (string value)
+                       {
+                               // FIXME: this is weird, but works.
+                               string output = value.Replace ("\\", "\\\\");
+                               output = output.Replace ("\"", "\\\"");
+                               output = output.Replace ("\t", "\\t");
+                               output = output.Replace ("\r", "\\r");
+                               output = output.Replace ("\n", "\\n");
+                               
+                               return "\"" + output + "\"";
+                       }
+
+                       string ChecksumToHex (MD5 checksum)
+                       {
+                               var ret = new StringBuilder ();
+                               foreach (byte b in checksum.Hash)
+                                       ret.Append (b.ToString ("X2"));
+
+                               return ret.ToString ();
+                       }
+
+                       const int pragmaChecksumStaticCount = 23;
+                       const int pragmaLineStaticCount = 8;
+                       const int md5ChecksumCount = 32;
+                       
+                       public int ReserveSpace (string filename) 
+                       {
+                               return pragmaChecksumStaticCount +
+                                       pragmaLineStaticCount +
+                                       md5ChecksumCount +
+                                       (QuoteSnippetString (filename).Length * 2) +
+                                       (Environment.NewLine.Length * 3) +
+                                       BaseCompiler.HashMD5.ToString ("B").Length;
+                       }
+                       
+                       public void DecorateFile (string path, string filename, MD5 checksum, Encoding enc)
+                       {
+                               string newline = Environment.NewLine;
+                               var sb = new StringBuilder ();
+                               
+                               sb.AppendFormat ("#pragma checksum {0} \"{1}\" \"{2}\"{3}{3}",
+                                                QuoteSnippetString (filename),
+                                                BaseCompiler.HashMD5.ToString ("B"),
+                                                ChecksumToHex (checksum),
+                                                newline);
+                               sb.AppendFormat ("#line 1 {0}{1}", QuoteSnippetString (filename), newline);
+
+                               byte[] bytes = enc.GetBytes (sb.ToString ());
+                               using (FileStream fs = new FileStream (path, FileMode.Open, FileAccess.Write)) {
+                                       fs.Seek (enc.GetPreamble ().Length, SeekOrigin.Begin);
+                                       fs.Write (bytes, 0, bytes.Length);
+                                       bytes = null;
+                               
+                                       sb.Length = 0;
+                                       sb.AppendFormat ("{0}#line default{0}#line hidden{0}", newline);
+                                       bytes = Encoding.UTF8.GetBytes (sb.ToString ());
+                               
+                                       fs.Seek (0, SeekOrigin.End);
+                                       fs.Write (bytes, 0, bytes.Length);
+                               }
+                               
+                               sb = null;
+                               bytes = null;
+                       }
+               }
+
+               class VBCodePragmaGenerator : ICodePragmaGenerator
+               {
+                       const int pragmaExternalSourceCount = 21;
+                       public int ReserveSpace (string filename)
+                       {
+                               return pragmaExternalSourceCount +
+                                       filename.Length +
+                                       (Environment.NewLine.Length);
+                       }
+                       
+                       public void DecorateFile (string path, string filename, MD5 checksum, Encoding enc)
+                       {
+                               string newline = Environment.NewLine;
+                               var sb = new StringBuilder ();
+
+                               sb.AppendFormat ("#ExternalSource(\"{0}\",1){1}", filename, newline);
+                               byte[] bytes = enc.GetBytes (sb.ToString ());
+                               using (FileStream fs = new FileStream (path, FileMode.Open, FileAccess.Write)) {
+                                       fs.Seek (enc.GetPreamble ().Length, SeekOrigin.Begin);
+                                       fs.Write (bytes, 0, bytes.Length);
+                                       bytes = null;
+
+                                       sb.Length = 0;
+                                       sb.AppendFormat ("{0}#End ExternalSource{0}", newline);
+                                       bytes = enc.GetBytes (sb.ToString ());
+                                       fs.Seek (0, SeekOrigin.End);
+                                       fs.Write (bytes, 0, bytes.Length);
+                               }
+                               sb = null;
+                               bytes = null;
+                       }
+               }
+               
                const string DEFAULT_ASSEMBLY_BASE_NAME = "App_Web_";
                const int COPY_BUFFER_SIZE = 8192;
                
@@ -90,7 +213,8 @@ namespace System.Web.Compilation {
 
                Dictionary <string, bool> code_files;
                Dictionary <string, List <CompileUnitPartialType>> partial_types;
-               List <CodeCompileUnit> units;
+               Dictionary <string, BuildProvider> path_to_buildprovider;
+               List <CodeUnit> units;
                List <string> source_files;
                List <Assembly> referenced_assemblies;
                Dictionary <string, string> resource_files;
@@ -116,13 +240,10 @@ namespace System.Web.Compilation {
                        this.provider = provider;
                        this.outputFilesPrefix = assemblyBaseName ?? DEFAULT_ASSEMBLY_BASE_NAME;
                        
-                       units = new List <CodeCompileUnit> ();
+                       units = new List <CodeUnit> ();
 
                        CompilationSection section;
-                       if (virtualPath != null)
-                               section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation", virtualPath.Absolute);
-                       else
-                               section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
+                       section = (CompilationSection) WebConfigurationManager.GetWebApplicationSection ("system.web/compilation");
                        string tempdir = section.TempDirectory;
                        if (String.IsNullOrEmpty (tempdir))
                                tempdir = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
@@ -183,20 +304,12 @@ namespace System.Web.Compilation {
                        set { parameters = value; }
                }
                
-               internal CodeCompileUnit [] GetUnitsAsArray ()
+               CodeUnit[] GetUnitsAsArray ()
                {
-                       CodeCompileUnit [] result = new CodeCompileUnit [units.Count];
+                       CodeUnit[] result = new CodeUnit [units.Count];
                        units.CopyTo (result, 0);
                        return result;
                }
-
-               internal List <CodeCompileUnit> Units {
-                       get {
-                               if (units == null)
-                                       units = new List <CodeCompileUnit> ();
-                               return units;
-                       }
-               }
                
                internal Dictionary <string, List <CompileUnitPartialType>> PartialTypes {
                        get {
@@ -230,6 +343,18 @@ namespace System.Web.Compilation {
                        }
                }
 
+               internal BuildProvider GetBuildProviderForPhysicalFilePath (string path)
+               {
+                       if (String.IsNullOrEmpty (path) || path_to_buildprovider == null || path_to_buildprovider.Count == 0)
+                               return null;
+
+                       BuildProvider ret;
+                       if (path_to_buildprovider.TryGetValue (path, out ret))
+                               return ret;
+
+                       return null;
+               }
+               
                public void AddAssemblyReference (Assembly a)
                {
                        if (a == null)
@@ -288,9 +413,9 @@ namespace System.Web.Compilation {
                {
                        if (compileUnit == null)
                                throw new ArgumentNullException ("compileUnit");
-                       units.Add (CheckForPartialTypes (compileUnit));
+                       units.Add (CheckForPartialTypes (new CodeUnit (null, compileUnit)));
                }
-               
+                               
                public void AddCodeCompileUnit (BuildProvider buildProvider, CodeCompileUnit compileUnit)
                {
                        if (buildProvider == null)
@@ -299,9 +424,20 @@ namespace System.Web.Compilation {
                        if (compileUnit == null)
                                throw new ArgumentNullException ("compileUnit");
 
-                       units.Add (CheckForPartialTypes (compileUnit));
+                       units.Add (CheckForPartialTypes (new CodeUnit (buildProvider, compileUnit)));
                }
 
+               void AddPathToBuilderMap (string path, BuildProvider bp)
+               {
+                       if (path_to_buildprovider == null)
+                               path_to_buildprovider = new Dictionary <string, BuildProvider> ();
+
+                       if (path_to_buildprovider.ContainsKey (path))
+                               return;
+
+                       path_to_buildprovider.Add (path, bp);
+               }
+               
                public TextWriter CreateCodeFile (BuildProvider buildProvider)
                {
                        if (buildProvider == null)
@@ -310,6 +446,7 @@ namespace System.Web.Compilation {
                        // Generate a file name with the correct source language extension
                        string filename = GetTempFilePhysicalPath (provider.FileExtension);
                        SourceFiles.Add (filename);
+                       AddPathToBuilderMap (filename, buildProvider);
                        return new StreamWriter (File.OpenWrite (filename));
                }
 
@@ -322,7 +459,11 @@ namespace System.Web.Compilation {
                {
                        AddCodeFile (path, bp, false);
                }
-               
+
+               // The kludge of using ICodePragmaGenerator for C# and VB code files is bad, but
+               // it's better than allowing for potential DoS while reading a file with arbitrary
+               // size in memory for use with the CodeSnippetCompileUnit class.
+               // Files with extensions other than .cs and .vb use CodeSnippetCompileUnit.
                internal void AddCodeFile (string path, BuildProvider bp, bool isVirtual)
                {
                        if (String.IsNullOrEmpty (path))
@@ -339,31 +480,104 @@ namespace System.Web.Compilation {
                                return; // maybe better to throw an exception here?
                        extension = extension.Substring (1);
                        string filename = GetTempFilePhysicalPath (extension);
-
+                       ICodePragmaGenerator pragmaGenerator;
+                       
+                       switch (extension.ToLowerInvariant ()) {
+                               case "cs":
+                                       pragmaGenerator = new CSharpCodePragmaGenerator ();
+                                       break;
+
+                               case "vb":
+                                       pragmaGenerator = new VBCodePragmaGenerator ();
+                                       break;
+
+                               default:
+                                       pragmaGenerator = null;
+                                       break;
+                       }
+                       
                        if (isVirtual) {
                                VirtualFile vf = HostingEnvironment.VirtualPathProvider.GetFile (path);
                                if (vf == null)
                                        throw new HttpException (404, "Virtual file '" + path + "' does not exist.");
 
-                               CopyFile (vf.Open (), filename);
+                               if (vf is DefaultVirtualFile)
+                                       path = HostingEnvironment.MapPath (path);
+                               CopyFileWithChecksum (vf.Open (), filename, path, pragmaGenerator);
                        } else
-                               CopyFile (path, filename);
+                               CopyFileWithChecksum (path, filename, path, pragmaGenerator);
+
+                       if (pragmaGenerator != null) {
+                               if (bp != null)
+                                       AddPathToBuilderMap (filename, bp);
                        
-                       SourceFiles.Add (filename);
+                               SourceFiles.Add (filename);
+                       }
                }
 
-               void CopyFile (string input, string filename)
+               void CopyFileWithChecksum (string input, string to, string from, ICodePragmaGenerator pragmaGenerator)
                {
-                       CopyFile (new FileStream (input, FileMode.Open, FileAccess.Read), filename);
+                       CopyFileWithChecksum (new FileStream (input, FileMode.Open, FileAccess.Read), to, from, pragmaGenerator);
                }
                
-               void CopyFile (Stream input, string filename)
+               void CopyFileWithChecksum (Stream input, string to, string from, ICodePragmaGenerator pragmaGenerator)
                {
-                       using (StreamWriter sw = new StreamWriter (new FileStream (filename, FileMode.Create, FileAccess.Write), Encoding.UTF8)) {
+                       if (pragmaGenerator == null) {
+                               // This is BAD, BAD, BAD! CodeDOM API is really no good in this
+                               // instance.
+                               string filedata;
                                using (StreamReader sr = new StreamReader (input, WebEncoding.FileEncoding)) {
-                                       sw.Write (sr.ReadToEnd ());
+                                       filedata = sr.ReadToEnd ();
+                               }
+
+                               var snippet = new CodeSnippetCompileUnit (filedata);
+                               snippet.LinePragma = new CodeLinePragma (from, 1);
+                               filedata = null;
+                               AddCodeCompileUnit (snippet);
+                               snippet = null;
+                               
+                               return;
+                       }
+                       
+                       MD5 checksum = MD5.Create ();
+                       using (FileStream fs = new FileStream (to, FileMode.Create, FileAccess.Write)) {
+                               using (StreamWriter sw = new StreamWriter (fs, Encoding.UTF8)) {
+                                       using (StreamReader sr = new StreamReader (input, WebEncoding.FileEncoding)) {
+                                               int count = pragmaGenerator.ReserveSpace (from);
+                                               char[] src;
+                                               
+                                               if (count > COPY_BUFFER_SIZE)
+                                                       src = new char [count];
+                                               else
+                                                       src = new char [COPY_BUFFER_SIZE];
+
+                                               sw.Write (src, 0, count);
+                                               do {
+                                                       count = sr.Read (src, 0, COPY_BUFFER_SIZE);
+                                                       if (count == 0) {
+                                                               UpdateChecksum (src, 0, checksum, true);
+                                                               break;
+                                                       }
+                                               
+                                                       sw.Write (src, 0, count);
+                                                       UpdateChecksum (src, count, checksum, false);
+                                               } while (true);
+                                               src = null;
+                                       }
                                }
                        }
+                       pragmaGenerator.DecorateFile (to, from, checksum, Encoding.UTF8);
+               }
+
+               void UpdateChecksum (char[] buf, int count, MD5 checksum, bool final)
+               {
+                       byte[] input = Encoding.UTF8.GetBytes (buf, 0, count);
+
+                       if (final)
+                               checksum.TransformFinalBlock (input, 0, input.Length);
+                       else
+                               checksum.TransformBlock (input, 0, input.Length, input, 0);
+                       input = null;
                }
                
                public Stream CreateEmbeddedResource (BuildProvider buildProvider, string name)
@@ -410,18 +624,15 @@ namespace System.Web.Compilation {
                        }
                }
                
-               CodeCompileUnit CheckForPartialTypes (CodeCompileUnit compileUnit)
+               CodeUnit CheckForPartialTypes (CodeUnit codeUnit)
                {
-                       if (compileUnit == null)
-                               return null;
-
                        CodeTypeDeclarationCollection types;
                        CompileUnitPartialType partialType;
                        string partialTypeName;
                        List <CompileUnitPartialType> tmp;
                        Dictionary <string, List <CompileUnitPartialType>> partialTypes = PartialTypes;
                        
-                       foreach (CodeNamespace ns in compileUnit.Namespaces) {
+                       foreach (CodeNamespace ns in codeUnit.Unit.Namespaces) {
                                if (ns == null)
                                        continue;
                                types = ns.Types;
@@ -433,7 +644,7 @@ namespace System.Web.Compilation {
                                                continue;
 
                                        if (type.IsPartial) {
-                                               partialType = new CompileUnitPartialType (compileUnit, ns, type);
+                                               partialType = new CompileUnitPartialType (codeUnit.Unit, ns, type);
                                                partialTypeName = partialType.TypeName;
                                                
                                                if (!partialTypes.TryGetValue (partialTypeName, out tmp)) {
@@ -445,7 +656,7 @@ namespace System.Web.Compilation {
                                }
                        }
                                                
-                       return compileUnit;
+                       return codeUnit;
                }
                
                void ProcessPartialTypes ()
@@ -489,21 +700,6 @@ namespace System.Web.Compilation {
 
                        foreach (CodeTypeMember member in membersToRemove)
                                targetMembers.Remove (member);
-               }               
-
-               bool TypeHasMember (CodeTypeDeclaration type, CodeMemberMethod member)
-               {
-                       if (type == null || member == null)
-                               return false;
-
-                       CodeMemberMethod method = FindMemberByName (type, member.Name) as CodeMemberMethod;
-                       if (method == null)
-                               return false;
-
-                       if (method.Parameters.Count != member.Parameters.Count)
-                               return false;
-                       
-                       return true;
                }
 
                bool TypeHasMember (CodeTypeDeclaration type, CodeTypeMember member)
@@ -544,7 +740,6 @@ namespace System.Web.Compilation {
                {
                        if (options == null)
                                throw new ArgumentNullException ("options");
-
                        options.TempFiles = temp_files;
                        if (options.OutputAssembly == null)
                                options.OutputAssembly = OutputAssemblyName;
@@ -552,7 +747,7 @@ namespace System.Web.Compilation {
                        ProcessPartialTypes ();
                        
                        CompilerResults results;
-                       CodeCompileUnit [] units = GetUnitsAsArray ();
+                       CodeUnit [] units = GetUnitsAsArray ();
 
                        // Since we may have some source files and some code
                        // units, we generate code from all of them and then
@@ -564,15 +759,31 @@ namespace System.Web.Compilation {
 
                        if (units.Length == 0 && files.Count == 0 && resources.Count == 0 && options.EmbeddedResources.Count == 0)
                                return null;
+
+                       string compilerOptions = options.CompilerOptions;
+                       if (options.IncludeDebugInformation) {
+                               if (String.IsNullOrEmpty (compilerOptions))
+                                       compilerOptions = "/d:DEBUG";
+                               else if (compilerOptions.IndexOf ("d:DEBUG", StringComparison.OrdinalIgnoreCase) == -1)
+                                       compilerOptions += " /d:DEBUG";
+                               
+                               options.CompilerOptions = compilerOptions;
+                       }
+
+                       if (String.IsNullOrEmpty (compilerOptions))
+                               compilerOptions = "/noconfig";
+                       else if (compilerOptions.IndexOf ("noconfig", StringComparison.OrdinalIgnoreCase) == -1)
+                               compilerOptions += " /noconfig";
+                       options.CompilerOptions = compilerOptions;
                        
                        string filename;
                        StreamWriter sw = null;
                        
-                       foreach (CodeCompileUnit unit in units) {
+                       foreach (CodeUnit unit in units) {
                                filename = GetTempFilePhysicalPath (provider.FileExtension);
                                try {
                                        sw = new StreamWriter (File.OpenWrite (filename), Encoding.UTF8);
-                                       provider.GenerateCodeFromCompileUnit (unit, sw, null);
+                                       provider.GenerateCodeFromCompileUnit (unit.Unit, sw, null);
                                        files.Add (filename);
                                } catch {
                                        throw;
@@ -582,30 +793,61 @@ namespace System.Web.Compilation {
                                                sw.Close ();
                                        }
                                }
+
+                               if (unit.BuildProvider != null)
+                                       AddPathToBuilderMap (filename, unit.BuildProvider);
                        }
 
                        foreach (KeyValuePair <string, string> de in resources)
                                options.EmbeddedResources.Add (de.Value);
+
                        AddAssemblyReference (BuildManager.GetReferencedAssemblies ());
-                       foreach (Assembly refasm in ReferencedAssemblies)
-                               options.ReferencedAssemblies.Add (refasm.Location);
+                       List <Assembly> referencedAssemblies = ReferencedAssemblies;
+                       StringCollection optRefAsm = options.ReferencedAssemblies;
+                       Type appType = HttpApplicationFactory.AppType;
+                       if (appType != null && !referencedAssemblies.Contains (appType.Assembly))
+                               referencedAssemblies.Add (appType.Assembly);
+
+                       foreach (Assembly refasm in ReferencedAssemblies) {
+                               string path = new Uri (refasm.CodeBase).LocalPath;
+                               string originalPath = refasm.Location;
+                               if (!optRefAsm.Contains (path) && !optRefAsm.Contains (originalPath))
+                                       optRefAsm.Add (path);
+                       }
+
+                       
                        
                        results = provider.CompileAssemblyFromFile (options, files.ToArray ());
 
                        if (results.NativeCompilerReturnValue != 0) {
                                string fileText = null;
+                               CompilerErrorCollection errors = results.Errors;
                                try {
-                                       using (StreamReader sr = File.OpenText (results.Errors [0].FileName)) {
-                                               fileText = sr.ReadToEnd ();
+                                       if (errors != null && errors.Count > 0) {
+                                               using (StreamReader sr = File.OpenText (results.Errors [0].FileName))
+                                                       fileText = sr.ReadToEnd ();
                                        }
                                } catch (Exception) {}
                                
 #if DEBUG
-                               Console.WriteLine ("Compilation failed. Errors:");
+                               Console.WriteLine ("********************************************************************");
+                               Console.WriteLine ("Compilation failed.");
+                               Console.WriteLine ("Output:");
+                               foreach (string s in results.Output)
+                                       Console.WriteLine ("  " + s);
+                               Console.WriteLine ("\nErrors:");
                                foreach (CompilerError err in results.Errors)
                                        Console.WriteLine (err);
+                               if (errors != null && errors.Count > 0)
+                                       Console.WriteLine ("File name: {0}", results.Errors [0].FileName);
+                               else
+                                       Console.WriteLine ("File name not available");
+                               if (!String.IsNullOrEmpty (fileText))
+                                       Console.WriteLine ("File text:\n{0}\n", fileText);
+                               else
+                                       Console.WriteLine ("No file text available");
+                               Console.WriteLine ("********************************************************************");
 #endif
-                               
                                throw new CompilationException (virtualPath != null ? virtualPath.Original : String.Empty, results, fileText);
                        }
                        
@@ -631,5 +873,5 @@ namespace System.Web.Compilation {
                }
        }
 }
-#endif
+