initial implementation of 'unify request'
[mono.git] / mcs / class / System.Web / System.Web / HttpServerUtility.cs
index 2973ee5d7c202799880467034f98fa6eab4f7663..819c47dba6ffe83fa63ae2ec127eb6b3bb300238 100644 (file)
@@ -86,13 +86,20 @@ 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 preserveQuery)
-               {
+               void Execute (string path, TextWriter writer, bool preserveForm)
+               {                       
                        if (path == null)
                                throw new ArgumentNullException ("path");
 
@@ -105,13 +112,13 @@ namespace System.Web {
                        if (qmark != -1) {
                                request.QueryStringRaw = path.Substring (qmark + 1);
                                path = path.Substring (0, qmark);
-                       } else if (!preserveQuery) {
+                       } else if (!preserveForm) {
                                request.QueryStringRaw = "";
                        }
 
                        HttpResponse response = context.Response;
                        WebROCollection oldForm = null;
-                       if (!preserveQuery) {
+                       if (!preserveForm) {
                                oldForm = request.Form as WebROCollection;
                                request.SetForm (new WebROCollection ());
                        }
@@ -123,12 +130,22 @@ namespace System.Web {
                        string oldFilePath = request.FilePath;
                        request.SetCurrentExePath (UrlUtils.Combine (request.BaseVirtualDir, path));
                        IHttpHandler handler = context.ApplicationInstance.GetHandler (context);
+                       request.SetCurrentExePath (oldFilePath);
+                       
+#if NET_2_0
+                       // If the target handler is not Page, the transfer must not occur.
+                       // InTransit == true means we're being called from Transfer
+                       if (context.InTransit && !(handler is Page))
+                               throw new HttpException ("Transfer is possible only to .aspx files");
+#endif
+                       
                        TextWriter previous = null;
                        try {
 #if NET_2_0
                                context.PushHandler (handler);
 #endif
                                previous = response.SetTextWriter (output);
+                               
                                if (!(handler is IHttpAsyncHandler)) {
                                        handler.ProcessRequest (context);
                                } else {
@@ -138,14 +155,14 @@ namespace System.Web {
                                        asyncHandler.EndProcessRequest (ar);
                                }
                        } finally {
-                               request.SetCurrentExePath (oldFilePath);
                                if (oldQuery != null && oldQuery != "" && oldQuery != request.QueryStringRaw) {
                                        oldQuery = oldQuery.Substring (1); // Ignore initial '?'
                                        request.QueryStringRaw = oldQuery; // which is added here.
                                }
                                response.SetTextWriter (previous);
-                               if (!preserveQuery)
+                               if (!preserveForm)
                                        request.SetForm (oldForm);
+                               context.InTransit = false;
 #if NET_2_0
                                context.PopHandler ();
 #endif
@@ -193,20 +210,88 @@ namespace System.Web {
                                Page page = (Page) context.Handler;
                                preserveForm = !page.IsPostBack;
                        }
+#if NET_2_0
+                       else
+                               throw new HttpException ("Transfer may only be called from within a Page instance");
+#endif
 
                        Transfer (path, preserveForm);
                }
 
                public void Transfer (string path, bool preserveForm)
                {
+#if NET_2_0
+                       if (!(context.Handler is Page))
+                               throw new HttpException ("Transfer may only be called from within a Page instance");
+#endif
+
+                       context.InTransit = true;
                        Execute (path, null, preserveForm);
                        context.Response.End ();
                }
 #if NET_2_0
-               [MonoTODO ("Not implemented")]
                public void Transfer (IHttpHandler handler, bool preserveForm)
                {
-                       throw new NotImplementedException ();
+                       if (handler == null)
+                               throw new ArgumentNullException ("handler");
+                       if (!(handler is Page))
+                               throw new HttpException ("Transfer may only be called from within a Page instance");
+                       
+                       // TODO: see the MS doc and search for "enableViewStateMac": this method is not
+                       // allowed for pages when preserveForm is true and the page IsCallback property
+                       // is true.
+
+                       Execute (handler, null, preserveForm);
+                       context.Response.End ();
+               }
+
+               public void Execute (IHttpHandler handler, TextWriter writer, bool preserveForm)
+               {
+                       if (handler == null)
+                               throw new ArgumentNullException ("handler");
+
+                       // If the target handler is not Page, the transfer must not occur.
+                       // InTransit == true means we're being called from Transfer
+                       if (context.InTransit && !(handler is Page))
+                               throw new HttpException ("Transfer is possible only to .aspx files");
+                       
+                       HttpRequest request = context.Request;
+                       string oldQuery = request.QueryStringRaw;
+                       if (!preserveForm) {
+                               request.QueryStringRaw = "";
+                       }
+
+                       HttpResponse response = context.Response;
+                       WebROCollection oldForm = null;
+                       if (!preserveForm) {
+                               oldForm = request.Form as WebROCollection;
+                               request.SetForm (new WebROCollection ());
+                       }
+
+                       TextWriter output = writer;
+                       if (output == null)
+                               output = response.Output;
+
+                       TextWriter previous = null;
+                       try {
+                               previous = response.SetTextWriter (output);
+                               if (!(handler is IHttpAsyncHandler)) {
+                                       handler.ProcessRequest (context);
+                               } else {
+                                       IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
+                                       IAsyncResult ar = asyncHandler.BeginProcessRequest (context, null, null);
+                                       ar.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.
+                               }
+                               response.SetTextWriter (previous);
+                               if (!preserveForm)
+                                       request.SetForm (oldForm);
+                       }
                }
 
                public static byte[] UrlTokenDecode (string input)