correctly remove the ApplicationPath from url
[mono.git] / mcs / class / System.Web / System.Web.J2EE / PageMapper.cs
index 5a2d2ee5cc2739962a350b885e114439f60d7d9c..2e7a8a824b8e90d2a403e14abe5058c53012bafc 100644 (file)
@@ -40,9 +40,30 @@ namespace System.Web.J2EE
        /// </summary>
        public class PageMapper
        {
-               private static readonly string _fileListName = "/filelist.xml";
+               //private static readonly string _fileListName = "/filelist.xml";
                private static readonly object LOCK_GETASSEMBLIESCACHEDDOCUMENT = new object();
-               private static readonly object LOCK_GETFROMMAPPATHCACHE = new object();
+               //private static readonly object LOCK_GETFROMMAPPATHCACHE = new object();
+
+
+               static Assembly CurrentDomain_AssemblyResolve (object sender, ResolveEventArgs args)
+               {
+                       Assembly resolvedAssembly = null;
+                       try
+                       {
+                               resolvedAssembly = GetCachedAssembly (HttpContext.Current, args.Name);
+                       }
+                       catch (Exception ex)
+                       {
+#if DEBUG
+                               Console.WriteLine (ex.ToString ());
+#endif
+                               resolvedAssembly = null;
+                       }
+
+                       return resolvedAssembly;
+               }
+
+#if UNUSED
 
                public static string GetFromMapPathCache(string key)
                {
@@ -62,13 +83,11 @@ namespace System.Web.J2EE
                                                answer[currentFile]= IAppDomainConfig.WAR_ROOT_SYMBOL + currentFile;
                                        }
                                        AppDomain.CurrentDomain.SetData(J2EEConsts.MAP_PATH_CACHE,answer);
-
                                }
                        }
                        return (string)answer[key];
                }
 
-#if UNUSED
                // UNUSED METHOD
                //The method was used by runtime to force file names casesensitivity
                // problem. The filelist.xml file should contain correct file names,
@@ -113,20 +132,34 @@ namespace System.Web.J2EE
 
                }
 #endif
-               private static ICachedXmlDoc GetAssembliesCachedDocument()
-               {
-                       lock(LOCK_GETASSEMBLIESCACHEDDOCUMENT)
-                       {
-                               ICachedXmlDoc doc = (ICachedXmlDoc) AppDomain.CurrentDomain.GetData(J2EEConsts.ASSEMBLIES_FILE);
-                               if (doc == null)
-                               {
-                                       doc = CreateDocument();
-                                       if (doc != null)
-                                               AppDomain.CurrentDomain.SetData(J2EEConsts.ASSEMBLIES_FILE, doc);
+               private static ICachedXmlDoc GetAssembliesCachedDocument(HttpContext context)
+               {
+                       ICachedXmlDoc doc = (ICachedXmlDoc) AppDomain.CurrentDomain.GetData (J2EEConsts.ASSEMBLIES_FILE);
+
+                       if (doc == null) {
+                               lock (LOCK_GETASSEMBLIESCACHEDDOCUMENT) {
+                                       doc = (ICachedXmlDoc) AppDomain.CurrentDomain.GetData (J2EEConsts.ASSEMBLIES_FILE);
+                                       if (doc == null) {
+                                               doc = CreateDocument ();
+                                               if (doc != null) {
+                                                       AppDomain.CurrentDomain.SetData (J2EEConsts.ASSEMBLIES_FILE, doc);
+
+                                                       AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler (CurrentDomain_AssemblyResolve);
+                                                       try {
+                                                               //Try to load the  global resources
+                                                               HttpContext.AppGlobalResourcesAssembly = GetCachedAssembly (context,  context.Request.ApplicationPath + "/app_globalresources");
+                                                       }
+                                                       catch (Exception ex) {
+#if DEBUG
+                                                               Console.WriteLine (ex.ToString ());
+#endif
+                                                       }
+                                               }
+                                       }
                                }
-
-                               return doc;
                        }
+
+                       return doc;
                }
 
                private static String NormalizeName(string url)
@@ -143,44 +176,40 @@ namespace System.Web.J2EE
                        return new CachedDocumentTypeStorage();
                }
 
-               public static Type GetObjectType(string url)
+               public static Type GetObjectType (HttpContext context, string url)
                {
-                       return GetCachedType(NormalizeName(url), true);
+                       return GetCachedType(context, NormalizeName(url), true);
                }
 
-               public static Type GetObjectType (string url, bool throwException) {
-                       return GetCachedType (NormalizeName (url), throwException);
+               public static Type GetObjectType (HttpContext context, string url, bool throwException) {
+                       return GetCachedType (context, NormalizeName (url), throwException);
                }
 
-               public static Assembly GetObjectAssembly(string url)
+               public static Assembly GetObjectAssembly (HttpContext context, string url)
                {
-                       return GetCachedAssembly(NormalizeName(url));
+                       return GetCachedAssembly (context, NormalizeName (url));
                }
-               public static string GetAssemblyResource(string url)
+               public static string GetAssemblyResource (HttpContext context, string url)
                {
-                       return GetCachedResource(NormalizeName(url));
+                       return GetCachedResource (context, NormalizeName (url));
                }
-               private static string GetCachedResource(string url)
+               private static string GetCachedResource (HttpContext context, string url)
                {
-                       ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument();
-                       return doc.GetAssemblyResourceName(url);
+                       ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument(context);
+                       return doc.GetAssemblyResourceName (context, url);
                }
-               private static Assembly GetCachedAssembly(string url)
+               private static Assembly GetCachedAssembly (HttpContext context, string url)
                {
-                       ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument();
-                       Assembly t = doc.GetAssembly(url);
-                       if (t == null)
-                               throw new HttpException(404, "The requested resource (" + url + ") is not available.");
-
-                       return t;
+                       ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument(context);
+                       return doc.GetAssembly (context, url);
                }
-               private static Type GetCachedType (string url) {
-                       return GetCachedType (url, true);
+               private static Type GetCachedType (HttpContext context, string url) {
+                       return GetCachedType (context, url, true);
                }
-               private static Type GetCachedType (string url, bool throwException)
+               private static Type GetCachedType (HttpContext context, string url, bool throwException)
                {
-                       ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument();                                           
-                       Type t = doc.GetType(url);
+                       ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument(context);                                            
+                       Type t = doc.GetType(context, url);
                        if (t == null && throwException)
                                throw new HttpException(404,"The requested resource (" + url + ") is not available.");
 
@@ -190,9 +219,9 @@ namespace System.Web.J2EE
                #region ICachedXmlDoc interface
                interface ICachedXmlDoc
                {
-                       Type GetType(string key);
-                       Assembly GetAssembly(string key);
-                       string GetAssemblyResourceName(string key);
+                       Type GetType (HttpContext context, string key);
+                       Assembly GetAssembly (HttpContext context, string key);
+                       string GetAssemblyResourceName (HttpContext context, string key);
                }
                #endregion
 
@@ -216,17 +245,17 @@ namespace System.Web.J2EE
                                this(DEFAULT_PAGES_NUMBER)
                        {}
 
-                       string ICachedXmlDoc.GetAssemblyResourceName(string o)
+                       string ICachedXmlDoc.GetAssemblyResourceName (HttpContext context, string o)
                        {
-                               return GetMetaByURL(o).Resource;
+                               return GetMetaByURL(context, o).Resource;
                        }
-                       Type ICachedXmlDoc.GetType(string o)
+                       Type ICachedXmlDoc.GetType (HttpContext context, string o)
                        {
-                               return GetMetaByURL(o).Type;
+                               return GetMetaByURL(context, o).Type;
                        }
-                       Assembly ICachedXmlDoc.GetAssembly(string o)
+                       Assembly ICachedXmlDoc.GetAssembly (HttpContext context, string o)
                        {
-                               return GetMetaByURL(o).Assembly;
+                               return GetMetaByURL(context, o).Assembly;
                        }
 
                        internal IDictionaryEnumerator GetEnumerator()
@@ -239,16 +268,17 @@ namespace System.Web.J2EE
                        //but only will became important in production mode when dynamyc compilation will be enabled
                        //Anyway, locking whole table and compiling under lock looks odd
                        //spivak.December 07 2006
-                       public MetaProvider GetMetaByURL(string url)
+                       public MetaProvider GetMetaByURL(HttpContext context, string url)
                        {
+
 #if !NO_GLOBAL_LOCK_ON_COMPILE
-                               string lwUrl = url.ToLower();
+                               string lwUrl = url.ToLowerInvariant();
                                lock (_table)
                                {
                                        object retVal = _table[lwUrl];
                                        if (retVal == null)
                                        {
-                                               retVal = PageCompiler.GetCompiler(url);
+                                               retVal = PageCompiler.GetCompiler(context, url);
                                                _table[lwUrl] = retVal;
                                        }
                                
@@ -304,7 +334,6 @@ namespace System.Web.J2EE
                private static readonly string ASSEM_ATTRIB_NAME = "assem";
                private static readonly string TYPE_ATTRIB_NAME = "type";
                private static string _parser = null;
-               private static PageCompiler _errorProvider = new PageCompiler();
 
                private Type _type = null;
                private string _typeName = null;
@@ -313,34 +342,20 @@ namespace System.Web.J2EE
                private string _xmlDescriptor = null;
                private string _url = null;
                private string _session = null;
+               readonly private HttpContext _context;
 
-               PageCompiler(string url)
+               PageCompiler(HttpContext context, string url)
                {
                        _url = url;
+                       _context = context;
                        _xmlDescriptor = GetDescFromUrl();
                        _session = DateTime.Now.Ticks.ToString();
                        LoadTypeAndAssem();
                }
-               PageCompiler()
-               {
-               }
 
-               public static PageCompiler Error
-               {
-                       get 
-                       {
-                               return _errorProvider; 
-                       }
-               }
-               public static PageCompiler GetCompiler(string url)
+               public static PageCompiler GetCompiler(HttpContext context, string url)
                {
-                       try{
-                               return new PageCompiler(url);
-                       }
-                       catch(Exception e)
-                       {
-                               return Error;
-                       }
+                       return new PageCompiler(context, url);
                }
 
                Type MetaProvider.Type
@@ -376,11 +391,11 @@ namespace System.Web.J2EE
                                }
                        }
                }
-               private void InternalCompile()
+               private bool InternalCompile()
                {
-                       string fileName = Path.GetFileName(_url);
+                       string fileName = VirtualPathUtility.GetFileName (_url);
 
-                       string fullFileName = (fileName.ToLower() == "global.asax") ? _url : HttpContext.Current.Request.MapPath(_url);
+                       string fullFileName = (fileName.ToLower () == "global.asax") ? _url : _context.Request.MapPath (_url);
 #if DEBUG
                        Console.WriteLine("fullFileName=" + fullFileName);
 #endif
@@ -390,11 +405,14 @@ namespace System.Web.J2EE
                                string[] command = GetParserCmd(fileName.ToLower() == "global.asax");
                                if (J2EEUtils.RunProc(command) != 0)
                                        throw GetCompilerError();
+
+                               return true;
                        }
                        else
                        {
-                               string message = "The requested resource (" + _url + ") is not available.";
-                               throw new HttpException(404, message);
+                               return false;
+                               //string message = "The requested resource (" + _url + ") is not available.";
+                               //throw new HttpException(404, message);
                        }
                }
                private string GetDescriptorPath()
@@ -405,7 +423,7 @@ namespace System.Web.J2EE
                {
                        try
                        {
-                               using (StreamReader sr = new StreamReader(HttpContext.Current.Request.MapPath("/" + GetDescriptorPath())))
+                               using (StreamReader sr = new StreamReader(_context.Request.MapPath("~/" + GetDescriptorPath())))
                                {
                                        return GetTypeFromDescStream(sr.BaseStream);
                                }
@@ -450,8 +468,8 @@ namespace System.Web.J2EE
                        if (typeName == null)
                        {
                                //spawn dynamic compilation and lookup typename from created folder
-                               InternalCompile();
-                               typeName = GetTypeNameFromAppFolder();
+                               if (InternalCompile())
+                                       typeName = GetTypeNameFromAppFolder();
                        }
                        return typeName;
                }
@@ -513,7 +531,7 @@ namespace System.Web.J2EE
                        if (_parser == null)
                        {
                                StreamReader sr =
-                                       File.OpenText(HttpContext.Current.Request.MapPath("/AspxParser.params"));
+                                       File.OpenText (_context.Request.MapPath ("~/AspxParser.params"));
                                _parser = sr.ReadLine();
                                sr.Close();
                        }
@@ -523,7 +541,7 @@ namespace System.Web.J2EE
 
                private string GetDescFromUrl()
                {
-                       string fileName = Path.GetFileName(_url);
+                       string fileName = VirtualPathUtility.GetFileName (_url);
                        
                        if (fileName.ToLower() == "global.asax")
                                return "global.asax.xml";
@@ -535,11 +553,11 @@ namespace System.Web.J2EE
 
                private string GetIdFromUrl(string path)
                {
-                       path = path.Trim('/');
-                       string fileName = Path.GetFileName(path);
+                       string fileName = VirtualPathUtility.GetFileName(path);
                        string id = string.Empty;
 
-                       path = path.Substring (path.IndexOf ("/") + 1);
+                       if (VirtualPathUtility.IsAbsolute (path))
+                               path = path.Substring (_context.Request.ApplicationPath.Length + 1);
 
                        if (path.Length > fileName.Length)
                                id = "." + path.Substring(0,path.Length - fileName.Length).Replace('/','_');
@@ -548,7 +566,7 @@ namespace System.Web.J2EE
 
                private Exception GetCompilerError()
                {
-                       string _errFile = HttpContext.Current.Request.MapPath("/" + _session + ".vmwerr");
+                       string _errFile = _context.Request.MapPath ("~/" + _session + ".vmwerr");
                        
                        if (!File.Exists(_errFile))
                                throw new FileNotFoundException("Internal Error",_errFile);