ScriptModule.cs 5.5 KB

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