2010-04-21 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpContext.cs
index 3b97dfd64c4c651901458f367d10d491894eb7e6..d183afa0f4d31b816f5d2605417568a5e7fa9617 100644 (file)
@@ -4,10 +4,11 @@
 // Author:
 //     Miguel de Icaza (miguel@novell.com)
 //     Gonzalo Paniagua Javier (gonzalo@novell.com)
+//      Marek Habersack <mhabersack@novell.com>
 //
 
 //
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -41,20 +42,20 @@ using System.Web.Configuration;
 using System.Web.SessionState;
 using System.Web.UI;
 using System.Web.Util;
-#if NET_2_0
+using System.Reflection;
 using System.Collections.Generic;
 using System.IO;
-using System.Reflection;
 using System.Resources;
 using System.Web.Compilation;
 using System.Web.Profile;
 using CustomErrorMode = System.Web.Configuration.CustomErrorsMode;
-#endif
 
-namespace System.Web {
+namespace System.Web
+{
        // CAS - no InheritanceDemand here as the class is sealed
        [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
-       public sealed partial class HttpContext : IServiceProvider {
+       public sealed partial class HttpContext : IServiceProvider
+       {
                internal HttpWorkerRequest WorkerRequest;
                HttpApplication app_instance;
                HttpRequest request;
@@ -74,8 +75,7 @@ namespace System.Web {
                Timer timer;
                Thread thread;
                bool _isProcessingInclude;
-
-#if NET_2_0
+               
                [ThreadStatic]
                static ResourceProviderFactory provider_factory;
 
@@ -104,20 +104,24 @@ namespace System.Web {
                                return default_provider_factory;
                        }
                }
-#endif
                
                public HttpContext (HttpWorkerRequest wr)
                {
                        WorkerRequest = wr;
                        request = new HttpRequest (WorkerRequest, this);
                        response = new HttpResponse (WorkerRequest, this);
+#if NET_4_0
+                       SessionStateBehavior = SessionStateBehavior.Default;
+#endif
                }
 
                public HttpContext (HttpRequest request, HttpResponse response)
                {
                        this.request = request;
                        this.response = response;
-                       
+#if NET_4_0
+                       SessionStateBehavior = SessionStateBehavior.Default;
+#endif
                }
 
                internal bool IsProcessingInclude {
@@ -215,19 +219,7 @@ namespace System.Web {
 
                internal bool IsCustomErrorEnabledUnsafe {
                        get {
-#if NET_2_0
                                CustomErrorsSection cfg = (CustomErrorsSection) WebConfigurationManager.GetSection ("system.web/customErrors");
-#else
-                               CustomErrorsConfig cfg = null;
-                               try {
-                                       cfg = (CustomErrorsConfig) GetConfig ("system.web/customErrors");
-                               } catch {
-                               }
-
-                               if (cfg == null)
-                                       return false;
-#endif
-
                                if (cfg.Mode == CustomErrorMode.On)
                                        return true;
 
@@ -236,7 +228,7 @@ namespace System.Web {
                }
 #if !TARGET_JVM
                public bool IsDebuggingEnabled {
-                       get { return HttpRuntime.IsDebuggingEnabled; }
+                       get { return RuntimeHelpers.DebuggingEnabled; }
                }
 #endif
                public IDictionary Items {
@@ -309,7 +301,6 @@ namespace System.Web {
                        }
                }
 
-#if NET_2_0
                internal bool MapRequestHandlerDone {
                        get;
                        set;
@@ -386,7 +377,6 @@ namespace System.Web {
                                profile = value;
                        }
                }
-#endif
 
                public void AddError (Exception errorInfo)
                {
@@ -424,9 +414,7 @@ namespace System.Web {
                        errors = null;
                }
 
-#if NET_2_0
                [Obsolete ("use WebConfigurationManager.GetWebApplicationSection")]
-#endif
                public static object GetAppConfig (string name)
                {
                        object o = ConfigurationSettings.GetConfig (name);
@@ -434,19 +422,12 @@ namespace System.Web {
                        return o;
                }
 
-#if NET_2_0
                [Obsolete ("see GetSection")]
-#endif
                public object GetConfig (string name)
                {
-#if NET_2_0
                        return GetSection (name);
-#else
-                       return WebConfigurationSettings.GetConfig (name, this);
-#endif
                }
 
-#if NET_2_0
                public static object GetGlobalResourceObject (string classKey, string resourceKey)
                {
                        return GetGlobalResourceObject (classKey, resourceKey, Thread.CurrentThread.CurrentUICulture);
@@ -484,8 +465,8 @@ namespace System.Web {
                        
                        return true;
                }
-
-               internal static IResourceProvider GetResourceProvider (string key, bool isLocal)
+               
+               internal static IResourceProvider GetResourceProvider (string virtualPath, bool isLocal)
                {
                        if (!EnsureProviderFactory ())
                                return null;
@@ -493,23 +474,23 @@ namespace System.Web {
                        // TODO: check if it makes sense to cache the providers and, if yes, maybe
                        // we should expire the entries (or just store them in InternalCache?)
                        IResourceProvider rp = null;
-                       if (!resource_providers.TryGetValue (key, out rp)) {
+                       if (!resource_providers.TryGetValue (virtualPath, out rp)) {
                                if (isLocal)
-                                       rp = provider_factory.CreateLocalResourceProvider (key);
+                                       rp = provider_factory.CreateLocalResourceProvider (virtualPath);
                                else
-                                       rp = provider_factory.CreateGlobalResourceProvider (key);
+                                       rp = provider_factory.CreateGlobalResourceProvider (virtualPath);
                                
                                if (rp == null) {
                                        if (isLocal)
-                                               rp = DefaultProviderFactory.CreateLocalResourceProvider (key);
+                                               rp = DefaultProviderFactory.CreateLocalResourceProvider (virtualPath);
                                        else
-                                               rp = DefaultProviderFactory.CreateGlobalResourceProvider (key);
+                                               rp = DefaultProviderFactory.CreateGlobalResourceProvider (virtualPath);
 
                                        if (rp == null)
                                                return null;
                                }
                                
-                               resource_providers.Add (key, rp);
+                               resource_providers.Add (virtualPath, rp);
                        }
 
                        return rp;
@@ -527,7 +508,7 @@ namespace System.Web {
                
                public static object GetGlobalResourceObject (string classKey, string resourceKey, CultureInfo culture)
                {
-                       return GetGlobalObjectFromFactory (classKey, resourceKey, culture);
+                       return GetGlobalObjectFromFactory ("Resources." + classKey, resourceKey, culture);
                }
 
                public static object GetLocalResourceObject (string virtualPath, string resourceKey)
@@ -556,7 +537,7 @@ namespace System.Web {
                {
                        return WebConfigurationManager.GetSection (name);
                }
-#endif
+
                object IServiceProvider.GetService (Type service)
                {
                        if (service == typeof (HttpWorkerRequest))
@@ -602,22 +583,16 @@ namespace System.Web {
                        return null;
                }
 
-#if NET_2_0
                public void RemapHandler (IHttpHandler handler)
                {
                        if (MapRequestHandlerDone)
                                throw new InvalidOperationException ("The RemapHandler method was called after the MapRequestHandler event occurred.");
                        Handler = handler;
                }
-#endif
                
                public void RewritePath (string path)
                {
-#if NET_2_0
                        RewritePath (path, true);
-#else
-                       RewritePath (path, false);
-#endif
                }
 
                public void RewritePath (string filePath, string pathInfo, string queryString)
@@ -625,26 +600,16 @@ namespace System.Web {
                        RewritePath (filePath, pathInfo, queryString, false);
                }
 
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               void RewritePath (string path, bool rebaseClientPath)
+               public void RewritePath (string path, bool rebaseClientPath)
                {
                        int qmark = path.IndexOf ('?');
                        if (qmark != -1)
-                               RewritePath (path.Substring (0, qmark), "", path.Substring (qmark + 1), rebaseClientPath);
+                               RewritePath (path.Substring (0, qmark), String.Empty, path.Substring (qmark + 1), rebaseClientPath);
                        else
                                RewritePath (path, null, null, rebaseClientPath);
                }
 
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               void RewritePath (string filePath, string pathInfo, string queryString, bool setClientFilePath)
+               public void RewritePath (string filePath, string pathInfo, string queryString, bool setClientFilePath)
                {
                        if (filePath == null)
                                throw new ArgumentNullException ("filePath");
@@ -653,38 +618,41 @@ namespace System.Web {
 
                        bool pathRelative = VirtualPathUtility.IsAppRelative (filePath);
                        bool pathAbsolute = pathRelative ? false : VirtualPathUtility.IsAbsolute (filePath);
+                       HttpRequest req = Request;
+                       if (req == null)
+                               return;
+                       
                        if (pathRelative || pathAbsolute) {
-                               bool needSubstring = false;
-
-                               if (pathRelative && filePath.Length > 1)
-                                       needSubstring = true;
-
-                               string bvd = Request.BaseVirtualDir;
-                               if (bvd.Length > 1)
-                                       bvd += "/";
-
-                               string canonizedFilePath = VirtualPathUtility.Canonize (filePath);
-                               filePath = VirtualPathUtility.Combine (bvd, needSubstring ? canonizedFilePath.Substring (2) : canonizedFilePath);
-                       } else 
-                               filePath = VirtualPathUtility.Combine (VirtualPathUtility.GetDirectory (Request.FilePath), filePath);
+                               if (pathRelative)
+                                       filePath = VirtualPathUtility.ToAbsolute (filePath);
+                       } else
+                               filePath = VirtualPathUtility.AppendTrailingSlash (req.BaseVirtualDir) + filePath;
                        
                        if (!StrUtils.StartsWith (filePath, HttpRuntime.AppDomainAppVirtualPath))
                                throw new HttpException (404, "The virtual path '" + HttpUtility.HtmlEncode (filePath) + "' maps to another application.", filePath);
-                       
-                       Request.SetCurrentExePath (filePath);
+
+                       req.SetCurrentExePath (filePath);
+                       req.SetFilePath (filePath);
+
                        if (setClientFilePath)
-                               Request.SetFilePath (filePath);
+                               req.ClientFilePath = filePath;
                        
                        // A null pathInfo or queryString is ignored and previous values remain untouched
                        if (pathInfo != null)
-                               Request.SetPathInfo (pathInfo);
+                               req.SetPathInfo (pathInfo);
 
                        if (queryString != null)
-                               Request.QueryStringRaw = queryString;
+                               req.QueryStringRaw = queryString;
                }
 
-#region internals
+#if NET_4_0
+               public void SetSessionStateBehavior (SessionStateBehavior sessionStateBehavior)
+               {
+                       SessionStateBehavior = sessionStateBehavior;
+               }
+#endif
                
+#region internals
                internal void SetSession (HttpSessionState state)
                {
                        session_state = state;
@@ -704,14 +672,8 @@ namespace System.Web {
                internal TimeSpan ConfigTimeout {
                        get {
                                if (config_timeout == null) {
-#if NET_2_0
                                        HttpRuntimeSection section = (HttpRuntimeSection)WebConfigurationManager.GetSection ("system.web/httpRuntime");
                                        config_timeout = section.ExecutionTimeout;
-#else
-                                       HttpRuntimeConfig config = (HttpRuntimeConfig)
-                                                               GetConfig ("system.web/httpRuntime");
-                                       config_timeout = new TimeSpan (0, 0, config.ExecutionTimeout);
-#endif
                                }
 
                                return (TimeSpan) config_timeout;
@@ -734,6 +696,13 @@ namespace System.Web {
                        }
                }
 
+#if NET_4_0
+               internal SessionStateBehavior SessionStateBehavior {
+                       get;
+                       private set;
+               }
+#endif
+               
 #if !TARGET_J2EE
                void TimeoutReached(object state) {
                        HttpRuntime.QueuePendingRequest (false);