Implemented EnablePageMethods feature
[mono.git] / mcs / class / System.Web.Extensions / System.Web.Handlers / ScriptModule.cs
1 //\r
2 // ScriptModule.cs\r
3 //\r
4 // Author:\r
5 //   Igor Zelmanovich <igorz@mainsoft.com>\r
6 //\r
7 // (C) 2007 Mainsoft, Inc.  http://www.mainsoft.com\r
8 //\r
9 //\r
10 // Permission is hereby granted, free of charge, to any person obtaining\r
11 // a copy of this software and associated documentation files (the\r
12 // "Software"), to deal in the Software without restriction, including\r
13 // without limitation the rights to use, copy, modify, merge, publish,\r
14 // distribute, sublicense, and/or sell copies of the Software, and to\r
15 // permit persons to whom the Software is furnished to do so, subject to\r
16 // the following conditions:\r
17 // \r
18 // The above copyright notice and this permission notice shall be\r
19 // included in all copies or substantial portions of the Software.\r
20 // \r
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
28 //\r
29 \r
30 using System;\r
31 using System.Collections.Generic;\r
32 using System.Text;\r
33 using System.Web.UI;\r
34 using System.Web.Script.Services;\r
35 \r
36 namespace System.Web.Handlers\r
37 {\r
38         public class ScriptModule : IHttpModule\r
39         {\r
40                 protected virtual void Init (HttpApplication context) {\r
41                         context.PreSendRequestHeaders += new EventHandler (PreSendRequestHeaders);\r
42                         context.PostAcquireRequestState += new EventHandler (PostAcquireRequestState);\r
43                         context.AuthenticateRequest += new EventHandler (AuthenticateRequest);\r
44                 }\r
45 \r
46                 void AuthenticateRequest (object sender, EventArgs e) {\r
47                         // The AuthenticateRequest event is raised after the identity of the current user has been \r
48                         // established. The handler for this event sets the SkipAuthorization property of the HttpContext \r
49                         // for the current request. This property is checked in the authorization module to see \r
50                         // if it has to omit authorization checking for the requested url. Usually an HttpModule \r
51                         // use this property to allow anonymous access to some resources (for example, \r
52                         // the Login Page if we’re using forms authentication). In our scenario, \r
53                         // the ScriptModule sets the SkipAuthorization to true if the requested url is \r
54                         // scriptresource.axd or if the authorization module is enabled and the request is a rest \r
55                         // request to the authorization web service.\r
56                 }\r
57 \r
58                 void PostAcquireRequestState (object sender, EventArgs e) {\r
59                         // The PostAcquireRequestState event is raised after the session data has been obtained. \r
60                         // If the request is for a class that implements System.Web.UI.Page and it is a rest \r
61                         // method call, the WebServiceData class (that was explained in a previous post) is used \r
62                         // to call the requested method from the Page. After the method has been called, \r
63                         // the CompleteRequest method is called, bypassing all pipeline events and executing \r
64                         // the EndRequest method. This allows MS AJAX to be able to call a method on a page \r
65                         // instead of having to create a web service to call a method.\r
66                         HttpApplication app = (HttpApplication) sender;\r
67                         HttpContext context = app.Context;\r
68                         HttpRequest request = context.Request;\r
69                         string contentType = request.ContentType;\r
70                         if (context.CurrentHandler is Page && !String.IsNullOrEmpty (contentType) && contentType.StartsWith ("application/json", StringComparison.OrdinalIgnoreCase)) {\r
71                                 RestHandler h = new RestHandler (((Page) context.CurrentHandler).GetType (), request.FilePath);\r
72                                 h.ProcessRequest (context);\r
73                                 app.CompleteRequest ();\r
74                         }\r
75                 }\r
76 \r
77                 void PreSendRequestHeaders (object sender, EventArgs e) {\r
78                         HttpApplication app = (HttpApplication) sender;\r
79                         HttpContext context = app.Context;\r
80                         if (context.Request.Headers ["X-MicrosoftAjax"] == "Delta=true") {\r
81                                 if (context.Error != null) {\r
82                                         context.Response.StatusCode = 200;\r
83                                         context.Response.ClearContent ();\r
84                                         ScriptManager.WriteCallbackException (context.Response.Output, context.Error);\r
85                                 }\r
86                                 else if (context.Response.StatusCode == 302) {\r
87                                         context.Response.StatusCode = 200;\r
88                                         context.Response.ClearContent ();\r
89                                         ScriptManager.WriteCallbackRedirect (context.Response.Output, context.Response.RedirectLocation);\r
90                                 }\r
91                         }\r
92                 }\r
93 \r
94                 protected virtual void Dispose () {\r
95                 }\r
96 \r
97                 #region IHttpModule Members\r
98 \r
99                 void IHttpModule.Dispose () {\r
100                         Dispose ();\r
101                 }\r
102 \r
103                 void IHttpModule.Init (HttpApplication context) {\r
104                         Init (context);\r
105                 }\r
106 \r
107                 #endregion\r
108         }\r
109 }\r
110 \r