cc2b0c97982846a4402fb764b9c829bcbd2fd678
[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                         Type pageType = context.CurrentHandler.GetType ();\r
71 #if TARGET_J2EE\r
72                         if (!(context.CurrentHandler is Page) && context.CurrentHandler is IServiceProvider) {\r
73                                 pageType = (Type) ((IServiceProvider) context.CurrentHandler).GetService (typeof (Type));\r
74                                 if (pageType == null)\r
75                                         return;\r
76                         }\r
77 #endif\r
78                         if (typeof (Page).IsAssignableFrom (pageType) && !String.IsNullOrEmpty (contentType) && contentType.StartsWith ("application/json", StringComparison.OrdinalIgnoreCase)) {\r
79                                 IHttpHandler h = RestHandler.GetHandler (context, pageType, request.FilePath);\r
80                                 h.ProcessRequest (context);\r
81                                 app.CompleteRequest ();\r
82                         }\r
83                 }\r
84 \r
85                 void PreSendRequestHeaders (object sender, EventArgs e) {\r
86                         HttpApplication app = (HttpApplication) sender;\r
87                         HttpContext context = app.Context;\r
88                         if (context.Request.Headers ["X-MicrosoftAjax"] == "Delta=true") {\r
89                                 Page p = context.CurrentHandler as Page;\r
90                                 if (p == null)\r
91                                         return;\r
92                                 ScriptManager sm = ScriptManager.GetCurrent (p);\r
93                                 if (sm == null)\r
94                                         return;\r
95                                 if (context.Response.StatusCode == 302) {\r
96                                         context.Response.StatusCode = 200;\r
97                                         context.Response.ClearContent ();\r
98                                         if (context.Error == null || sm.AllowCustomErrorsRedirect)\r
99                                                 ScriptManager.WriteCallbackRedirect (context.Response.Output, context.Response.RedirectLocation);\r
100                                         else\r
101                                                 sm.WriteCallbackException (context.Response.Output, context.Error, false);\r
102                                 }\r
103                                 else if (context.Error != null) {\r
104                                         context.Response.StatusCode = 200;\r
105                                         context.Response.ClearContent ();\r
106                                         sm.WriteCallbackException (context.Response.Output, context.Error, true);\r
107                                 }\r
108                         }\r
109                 }\r
110 \r
111                 protected virtual void Dispose () {\r
112                 }\r
113 \r
114                 #region IHttpModule Members\r
115 \r
116                 void IHttpModule.Dispose () {\r
117                         Dispose ();\r
118                 }\r
119 \r
120                 void IHttpModule.Init (HttpApplication context) {\r
121                         Init (context);\r
122                 }\r
123 \r
124                 #endregion\r
125         }\r
126 }\r
127 \r