Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[mono.git] / mcs / class / System.Web.Extensions / System.Web.Handlers / ScriptModule.cs
index 76bb7d55863f091fa22a3a5620c61edc1d696d05..bf4bad4bfce183594ff96e3cfc37abf1a5a50c2a 100644 (file)
@@ -31,6 +31,7 @@ using System;
 using System.Collections.Generic;\r
 using System.Text;\r
 using System.Web.UI;\r
+using System.Web.Script.Services;\r
 \r
 namespace System.Web.Handlers\r
 {\r
@@ -43,18 +44,71 @@ namespace System.Web.Handlers
                }\r
 \r
                void AuthenticateRequest (object sender, EventArgs e) {\r
+                       // The AuthenticateRequest event is raised after the identity of the current user has been \r
+                       // established. The handler for this event sets the SkipAuthorization property of the HttpContext \r
+                       // for the current request. This property is checked in the authorization module to see \r
+                       // if it has to omit authorization checking for the requested url. Usually an HttpModule \r
+                       // use this property to allow anonymous access to some resources (for example, \r
+                       // the Login Page if we’re using forms authentication). In our scenario, \r
+                       // the ScriptModule sets the SkipAuthorization to true if the requested url is \r
+                       // scriptresource.axd or if the authorization module is enabled and the request is a rest \r
+                       // request to the authorization web service.\r
                }\r
 \r
                void PostAcquireRequestState (object sender, EventArgs e) {\r
+                       // The PostAcquireRequestState event is raised after the session data has been obtained. \r
+                       // If the request is for a class that implements System.Web.UI.Page and it is a rest \r
+                       // method call, the WebServiceData class (that was explained in a previous post) is used \r
+                       // to call the requested method from the Page. After the method has been called, \r
+                       // the CompleteRequest method is called, bypassing all pipeline events and executing \r
+                       // the EndRequest method. This allows MS AJAX to be able to call a method on a page \r
+                       // instead of having to create a web service to call a method.\r
+                       HttpApplication app = (HttpApplication) sender;\r
+                       HttpContext context = app.Context;\r
+                       HttpRequest request = context.Request;\r
+                       string contentType = request.ContentType;\r
+                       Type pageType = context.CurrentHandler.GetType ();\r
+#if TARGET_J2EE\r
+                       if (!(context.CurrentHandler is Page) && context.CurrentHandler is IServiceProvider) {\r
+                               pageType = (Type) ((IServiceProvider) context.CurrentHandler).GetService (typeof (Type));\r
+                               if (pageType == null)\r
+                                       return;\r
+                       }\r
+#endif\r
+                       if (typeof (Page).IsAssignableFrom (pageType) && !String.IsNullOrEmpty (contentType) && contentType.StartsWith ("application/json", StringComparison.OrdinalIgnoreCase)) {\r
+                               IHttpHandler h = RestHandler.GetHandler (context, pageType, request.FilePath);\r
+                               h.ProcessRequest (context);\r
+                               app.CompleteRequest ();\r
+                       }\r
                }\r
 \r
                void PreSendRequestHeaders (object sender, EventArgs e) {\r
                        HttpApplication app = (HttpApplication) sender;\r
                        HttpContext context = app.Context;\r
-                       if (context.Request.Headers ["X-MicrosoftAjax"] == "Delta=true" && context.Response.StatusCode == 302) {\r
-                               context.Response.StatusCode = 200;\r
-                               context.Response.ClearContent ();\r
-                               ScriptManager.WriteCallbackRedirect (context.Response.Output, context.Response.RedirectLocation);\r
+                       if (context.Request.Headers ["X-MicrosoftAjax"] == "Delta=true") {\r
+                               Page p = context.CurrentHandler as Page;\r
+#if TARGET_J2EE\r
+                               if (p == null && context.CurrentHandler is IServiceProvider)\r
+                                       p = (Page) ((IServiceProvider) context.CurrentHandler).GetService (typeof (Page));\r
+#endif\r
+                               if (p == null)\r
+                                       return;\r
+                               ScriptManager sm = ScriptManager.GetCurrent (p);\r
+                               if (sm == null)\r
+                                       return;\r
+                               if (context.Response.StatusCode == 302) {\r
+                                       context.Response.StatusCode = 200;\r
+                                       context.Response.ClearContent ();\r
+                                       if (context.Error == null || sm.AllowCustomErrorsRedirect)\r
+                                               ScriptManager.WriteCallbackRedirect (context.Response.Output, context.Response.RedirectLocation);\r
+                                       else\r
+                                               sm.WriteCallbackException (context.Response.Output, context.Error, false);\r
+                               }\r
+                               else if (context.Error != null) {\r
+                                       context.Response.StatusCode = 200;\r
+                                       context.Response.ClearContent ();\r
+                                       sm.WriteCallbackException (context.Response.Output, context.Error, true);\r
+                               }\r
                        }\r
                }\r
 \r