2010-06-30 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpServerUtility.cs
index c0505fcbc84ba559b1a6062770cd6ef746cd5e98..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,9 +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
        //
@@ -86,24 +89,18 @@ 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);
                }
 
-               void Execute (string path, TextWriter writer, bool preserveForm, bool isTransfer) {
+               void Execute (string path, TextWriter writer, bool preserveForm, bool isTransfer)
+               {
                        if (path == null)
                                throw new ArgumentNullException ("path");
 
@@ -118,16 +115,25 @@ namespace System.Web {
                        }
 
                        string exePath = UrlUtils.Combine (context.Request.BaseVirtualDir, path);
+                       bool cookieless = false;
+                       SessionStateSection config = WebConfigurationManager.GetWebApplicationSection ("system.web/sessionState") as SessionStateSection;
+                       cookieless = SessionStateModule.IsCookieLess (context, config);
+                       
+                       if (cookieless)
+                               exePath = UrlUtils.RemoveSessionId (VirtualPathUtility.GetDirectory (exePath), exePath);
+                       
                        IHttpHandler handler = context.ApplicationInstance.GetHandler (context, exePath, true);
                        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;
@@ -152,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;
                                
@@ -163,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;
                        }
@@ -214,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);
@@ -223,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)
@@ -323,28 +352,35 @@ namespace System.Web {
                        }
                        return new string (chars);
                }
-#endif
 
                public string UrlDecode (string s)
                {
-                       return HttpUtility.UrlDecode (s);
+                       HttpRequest request = context.Request;
+                       if(request != null)
+                               return HttpUtility.UrlDecode (s, request.ContentEncoding);
+                       else
+                               return HttpUtility.UrlDecode (s);
                }
 
                public void UrlDecode (string s, TextWriter output)
                {
                        if (s != null)
-                               output.Write (HttpUtility.UrlDecode (s));
+                               output.Write (UrlDecode (s));
                }
 
                public string UrlEncode (string s)
                {
-                       return HttpUtility.UrlEncode (s);
+                       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)
                {
                        if (s != null)
-                               output.Write (HttpUtility.UrlEncode (s));
+                               output.Write (UrlEncode (s));
                }
 
                public string UrlPathEncode (string s)
@@ -352,10 +388,10 @@ 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-1);
+                               s2 = s.Substring (0, idx);
                                s2 = HttpUtility.UrlEncode (s2) + s.Substring (idx);
                        } else {
                                s2 = HttpUtility.UrlEncode (s);