ScriptModule.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. HttpRequest request = context.Request;
  65. string contentType = request.ContentType;
  66. if (context.CurrentHandler is Page && !String.IsNullOrEmpty (contentType) && contentType.StartsWith ("application/json", StringComparison.OrdinalIgnoreCase)) {
  67. RestHandler h = new RestHandler (((Page) context.CurrentHandler).GetType (), request.FilePath);
  68. h.ProcessRequest (context);
  69. app.CompleteRequest ();
  70. }
  71. }
  72. void PreSendRequestHeaders (object sender, EventArgs e) {
  73. HttpApplication app = (HttpApplication) sender;
  74. HttpContext context = app.Context;
  75. if (context.Request.Headers ["X-MicrosoftAjax"] == "Delta=true") {
  76. if (context.Error != null) {
  77. context.Response.StatusCode = 200;
  78. context.Response.ClearContent ();
  79. ScriptManager.WriteCallbackException (context.Response.Output, context.Error);
  80. }
  81. else if (context.Response.StatusCode == 302) {
  82. context.Response.StatusCode = 200;
  83. context.Response.ClearContent ();
  84. ScriptManager.WriteCallbackRedirect (context.Response.Output, context.Response.RedirectLocation);
  85. }
  86. }
  87. }
  88. protected virtual void Dispose () {
  89. }
  90. #region IHttpModule Members
  91. void IHttpModule.Dispose () {
  92. Dispose ();
  93. }
  94. void IHttpModule.Init (HttpApplication context) {
  95. Init (context);
  96. }
  97. #endregion
  98. }
  99. }