WebServiceHandlerFactory.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // System.Web.Services.Protocols.WebServiceHandlerFactory.cs
  3. //
  4. // Authors:
  5. // Tim Coleman ([email protected])
  6. // Dave Bettin ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. // Lluis Sanchez Gual ([email protected])
  9. //
  10. // Copyright (C) Tim Coleman, 2002
  11. // Copyright (c) 2003 Ximian, Inc. (http://www.ximian.com)
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.IO;
  34. using System.Web.Services;
  35. using System.Web.Services.Configuration;
  36. using System.Web.SessionState;
  37. using System.Web.UI;
  38. using System.Collections.Specialized;
  39. namespace System.Web.Services.Protocols
  40. {
  41. class DummyHttpHandler : IHttpHandler
  42. {
  43. bool IHttpHandler.IsReusable {
  44. get { return false; }
  45. }
  46. void IHttpHandler.ProcessRequest (HttpContext context)
  47. {
  48. // Do nothing
  49. }
  50. }
  51. class SessionWrapperHandler : IHttpHandler, IRequiresSessionState
  52. {
  53. IHttpHandler handler;
  54. public SessionWrapperHandler (IHttpHandler handler)
  55. {
  56. this.handler = handler;
  57. }
  58. public bool IsReusable {
  59. get { return handler.IsReusable; }
  60. }
  61. public void ProcessRequest (HttpContext context)
  62. {
  63. handler.ProcessRequest (context);
  64. }
  65. }
  66. class ReadOnlySessionWrapperHandler : IHttpHandler, IRequiresSessionState, IReadOnlySessionState
  67. {
  68. IHttpHandler handler;
  69. public ReadOnlySessionWrapperHandler (IHttpHandler handler)
  70. {
  71. this.handler = handler;
  72. }
  73. public bool IsReusable {
  74. get { return handler.IsReusable; }
  75. }
  76. public void ProcessRequest (HttpContext context)
  77. {
  78. handler.ProcessRequest (context);
  79. }
  80. }
  81. public class WebServiceHandlerFactory : IHttpHandlerFactory
  82. {
  83. #region Constructors
  84. public WebServiceHandlerFactory ()
  85. {
  86. }
  87. #endregion // Constructors
  88. #region Methods
  89. public IHttpHandler GetHandler (HttpContext context, string verb, string url, string filePath)
  90. {
  91. Type type = WebServiceParser.GetCompiledType (filePath, context);
  92. WSProtocol protocol = GuessProtocol (context, verb);
  93. IHttpHandler handler = null;
  94. if (!WSConfig.IsSupported (protocol))
  95. return new DummyHttpHandler ();
  96. switch (protocol) {
  97. case WSProtocol.HttpSoap:
  98. handler = GetTypeHandler (context, new HttpSoapWebServiceHandler (type));
  99. break;
  100. case WSProtocol.HttpPost:
  101. case WSProtocol.HttpGet:
  102. handler = GetTypeHandler (context, new HttpSimpleWebServiceHandler (type, protocol.ToString ()));
  103. break;
  104. case WSProtocol.Documentation:
  105. SoapDocumentationHandler soapHandler;
  106. soapHandler = new SoapDocumentationHandler (type, context);
  107. if (soapHandler.PageHandler is IRequiresSessionState) {
  108. if (soapHandler.PageHandler is IReadOnlySessionState)
  109. handler = new ReadOnlySessionWrapperHandler (soapHandler);
  110. else
  111. handler = new SessionWrapperHandler (soapHandler);
  112. } else {
  113. handler = soapHandler;
  114. }
  115. break;
  116. }
  117. return handler;
  118. }
  119. IHttpHandler GetTypeHandler (HttpContext context, WebServiceHandler handler)
  120. {
  121. MethodStubInfo method = handler.GetRequestMethod (context);
  122. if (method == null) return null;
  123. if (method.MethodInfo.EnableSession)
  124. return new SessionWrapperHandler (handler);
  125. else
  126. return handler;
  127. }
  128. static WSProtocol GuessProtocol (HttpContext context, string verb)
  129. {
  130. if (context.Request.PathInfo == null || context.Request.PathInfo == "")
  131. {
  132. if (context.Request.RequestType == "GET")
  133. return WSProtocol.Documentation;
  134. else
  135. return WSProtocol.HttpSoap;
  136. }
  137. else
  138. {
  139. if (context.Request.RequestType == "GET")
  140. return WSProtocol.HttpGet;
  141. else
  142. return WSProtocol.HttpPost;
  143. }
  144. }
  145. public void ReleaseHandler (IHttpHandler handler)
  146. {
  147. }
  148. #endregion // Methods
  149. }
  150. }