2010-06-30 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpServerUtility.cs
index 94c888c148ba431106aae49269a6e5555ac4621d..58b5caeee32c92461ed37e99be82e73eef09a71a 100644 (file)
@@ -8,7 +8,7 @@
 //
 
 //
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005-2010 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
@@ -36,11 +36,12 @@ using System.Web.Util;
 using System.Collections.Specialized;
 using System.Security.Permissions;
 using System.Text;
+using System.Threading;
 using System.Web.Configuration;
 using System.Web.SessionState;
 
-namespace System.Web {
-
+namespace System.Web
+{
        //
        // Methods exposed through HttpContext.Server property
        //
@@ -88,19 +89,12 @@ namespace System.Web {
                        Execute (path, writer, true);
                }
 
-#if NET_2_0
                public void Execute (string path, bool preserveForm)
                {
                        Execute (path, null, preserveForm);
                }
-#endif
-               
-#if NET_2_0
-               public
-#else
-               internal
-#endif
-               void Execute (string path, TextWriter writer, bool preserveForm)
+
+               public void Execute (string path, TextWriter writer, bool preserveForm)
                {                       
                        Execute (path, writer, preserveForm, false);
                }
@@ -122,14 +116,9 @@ namespace System.Web {
 
                        string exePath = UrlUtils.Combine (context.Request.BaseVirtualDir, path);
                        bool cookieless = false;
-                       
-#if NET_2_0
-                       SessionStateSection config = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
+                       SessionStateSection config = WebConfigurationManager.GetWebApplicationSection ("system.web/sessionState") as SessionStateSection;
                        cookieless = SessionStateModule.IsCookieLess (context, config);
-#else
-                       SessionConfig config = HttpContext.GetAppConfig ("system.web/sessionState") as SessionConfig;
-                       cookieless = config.CookieLess;
-#endif
+                       
                        if (cookieless)
                                exePath = UrlUtils.RemoveSessionId (VirtualPathUtility.GetDirectory (exePath), exePath);
                        
@@ -137,12 +126,14 @@ namespace System.Web {
                        Execute (handler, writer, preserveForm, exePath, queryString, isTransfer, true);
                }
 
-               internal void Execute (IHttpHandler handler, TextWriter writer, bool preserveForm, string exePath, string queryString, bool isTransfer, bool isInclude) {
-#if NET_2_0 && !TARGET_J2EE
+               internal void Execute (IHttpHandler handler, TextWriter writer, bool preserveForm, string exePath, string queryString, bool isTransfer, bool isInclude)
+               {
+#if !TARGET_J2EE
                        // If the target handler is not Page, the transfer must not occur.
                        // InTransit == true means we're being called from Transfer
-                       if (isTransfer && !(handler is Page))
-                               throw new HttpException ("Transfer is possible only to .aspx files");
+                       bool is_static = (handler is StaticFileHandler);
+                       if (isTransfer && !(handler is Page) && !is_static)
+                               throw new HttpException ("Transfer is only allowed to .aspx and static files");
 #endif
 
                        HttpRequest request = context.Request;
@@ -167,9 +158,10 @@ namespace System.Web {
                        string oldExePath = request.CurrentExecutionFilePath;
                        bool oldIsInclude = context.IsProcessingInclude;
                        try {
-#if NET_2_0
                                context.PushHandler (handler);
-#endif
+                               if (is_static) // Not sure if this should apply to Page too
+                                       request.SetFilePath (exePath);
+
                                request.SetCurrentExePath (exePath);
                                context.IsProcessingInclude = isInclude;
                                
@@ -178,20 +170,25 @@ namespace System.Web {
                                } else {
                                        IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
                                        IAsyncResult ar = asyncHandler.BeginProcessRequest (context, null, null);
-                                       ar.AsyncWaitHandle.WaitOne ();
+                                       WaitHandle asyncWaitHandle = ar != null ? ar.AsyncWaitHandle : null;
+                                       if (asyncWaitHandle != null)
+                                               asyncWaitHandle.WaitOne ();
                                        asyncHandler.EndProcessRequest (ar);
                                }
                        } finally {
-                               if (oldQuery != null && oldQuery != "" && oldQuery != request.QueryStringRaw) {
-                                       oldQuery = oldQuery.Substring (1); // Ignore initial '?'
-                                       request.QueryStringRaw = oldQuery; // which is added here.
+                               if (oldQuery != request.QueryStringRaw) {
+                                       if (oldQuery != null && oldQuery.Length > 0) {
+                                               oldQuery = oldQuery.Substring (1); // Ignore initial '?'
+                                               request.QueryStringRaw = oldQuery; // which is added here.
+                                       } else
+                                               request.QueryStringRaw = String.Empty;
                                }
+                               
                                response.SetTextWriter (previous);
                                if (!preserveForm)
                                        request.SetForm (oldForm);
-#if NET_2_0
+
                                context.PopHandler ();
-#endif
                                request.SetCurrentExePath (oldExePath);
                                context.IsProcessingInclude = oldIsInclude;
                        }
@@ -229,6 +226,23 @@ namespace System.Web {
                        return context.Request.MapPath (path);
                }
 
+               
+               public void TransferRequest (string path)
+               {
+                       TransferRequest (path, false, null, null);
+               }
+               
+               public void TransferRequest (string path, bool preserveForm)
+               {
+                       TransferRequest (path, preserveForm, null, null);
+               }
+
+               [MonoTODO ("Always throws PlatformNotSupportedException.")]
+               public void TransferRequest (string path, bool preserveForm, string method, NameValueCollection headers)
+               {
+                       throw new PlatformNotSupportedException ();
+               }
+               
                public void Transfer (string path)
                {
                        Transfer (path, true);
@@ -238,7 +252,7 @@ namespace System.Web {
                        Execute (path, null, preserveForm, true);
                        context.Response.End ();
                }
-#if NET_2_0
+
                public void Transfer (IHttpHandler handler, bool preserveForm)
                {
                        if (handler == null)
@@ -338,12 +352,12 @@ namespace System.Web {
                        }
                        return new string (chars);
                }
-#endif
 
                public string UrlDecode (string s)
                {
-                       if(context.Request != null)
-                               return HttpUtility.UrlDecode (s, context.Request.ContentEncoding);
+                       HttpRequest request = context.Request;
+                       if(request != null)
+                               return HttpUtility.UrlDecode (s, request.ContentEncoding);
                        else
                                return HttpUtility.UrlDecode (s);
                }
@@ -356,7 +370,11 @@ namespace System.Web {
 
                public string UrlEncode (string s)
                {
-                       return HttpUtility.UrlEncode (s, context.Response.ContentEncoding);
+                       HttpResponse response = context.Response;
+                       if (response != null)
+                               return HttpUtility.UrlEncode (s, response.ContentEncoding);
+                       else
+                               return HttpUtility.UrlEncode (s);
                }
 
                public void UrlEncode (string s, TextWriter output)
@@ -370,7 +388,7 @@ namespace System.Web {
                        if (s == null)
                                return null;
 
-                       int idx = s.IndexOf ("?");
+                       int idx = s.IndexOf ('?');
                        string s2 = null;
                        if (idx != -1) {
                                s2 = s.Substring (0, idx);