Merge pull request #4542 from lateralusX/jlorenss/win-fix-unwind-tramp-reg-aot
[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 app) {\r
41                         app.PreSendRequestHeaders += new EventHandler (PreSendRequestHeaders);\r
42                         app.PostAcquireRequestState += new EventHandler (PostAcquireRequestState);\r
43                         app.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                         if (context == null)\r
69                                 return;\r
70                         \r
71                         HttpRequest request = context.Request;\r
72                         string contentType = request.ContentType;\r
73                         IHttpHandler currentHandler = context.CurrentHandler;\r
74                         if (currentHandler == null)\r
75                                 return;\r
76                         Type pageType = currentHandler.GetType ();\r
77                         if (typeof (Page).IsAssignableFrom (pageType) && !String.IsNullOrEmpty (contentType) && contentType.StartsWith ("application/json", StringComparison.OrdinalIgnoreCase)) {\r
78                                 IHttpHandler h = RestHandler.GetHandler (context, pageType, request.FilePath);\r
79                                 h.ProcessRequest (context);\r
80                                 app.CompleteRequest ();\r
81                         }\r
82                 }\r
83 \r
84                 void PreSendRequestHeaders (object sender, EventArgs e)\r
85                 {\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                                 ScriptManager sm = ScriptManager.GetCurrentInternal (p);\r
91                                 if (context.Response.StatusCode == 302) {\r
92                                         context.Response.StatusCode = 200;\r
93                                         context.Response.ClearContent ();\r
94                                         if (context.Error == null || (sm != null && sm.AllowCustomErrorsRedirect))\r
95                                                 ScriptManager.WriteCallbackRedirect (context.Response.Output, context.Response.RedirectLocation);\r
96                                         else\r
97                                                 ScriptManager.WriteCallbackException (sm, context.Response.Output, context.Error, false);\r
98                                 } else if (context.Error != null) {\r
99                                         context.Response.StatusCode = 200;\r
100                                         context.Response.ClearContent ();\r
101                                         ScriptManager.WriteCallbackException (sm, context.Response.Output, context.Error, true);\r
102                                 }\r
103                         }\r
104                 }\r
105 \r
106                 protected virtual void Dispose () {\r
107                 }\r
108 \r
109                 #region IHttpModule Members\r
110 \r
111                 void IHttpModule.Dispose () {\r
112                         Dispose ();\r
113                 }\r
114 \r
115                 void IHttpModule.Init (HttpApplication context) {\r
116                         Init (context);\r
117                 }\r
118 \r
119                 #endregion\r
120         }\r
121 }\r
122 \r