WebServiceHandlerFactory.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.Net;
  35. using System.Web.Services;
  36. using System.Web.Services.Configuration;
  37. using System.Web.SessionState;
  38. using System.Web.UI;
  39. using System.Collections.Specialized;
  40. #if NET_2_0
  41. using WSConfig = System.Web.Services.Configuration.WebServicesSection;
  42. using WSProtocol = System.Web.Services.Configuration.WebServiceProtocols;
  43. #endif
  44. namespace System.Web.Services.Protocols
  45. {
  46. class SessionWrapperHandler : IHttpHandler, IRequiresSessionState
  47. {
  48. IHttpHandler handler;
  49. public SessionWrapperHandler (IHttpHandler handler)
  50. {
  51. this.handler = handler;
  52. }
  53. public bool IsReusable {
  54. get { return handler.IsReusable; }
  55. }
  56. public void ProcessRequest (HttpContext context)
  57. {
  58. handler.ProcessRequest (context);
  59. }
  60. }
  61. class ReadOnlySessionWrapperHandler : IHttpHandler, IRequiresSessionState, IReadOnlySessionState
  62. {
  63. IHttpHandler handler;
  64. public ReadOnlySessionWrapperHandler (IHttpHandler handler)
  65. {
  66. this.handler = handler;
  67. }
  68. public bool IsReusable {
  69. get { return handler.IsReusable; }
  70. }
  71. public void ProcessRequest (HttpContext context)
  72. {
  73. handler.ProcessRequest (context);
  74. }
  75. }
  76. public class WebServiceHandlerFactory : IHttpHandlerFactory
  77. {
  78. #region Constructors
  79. public WebServiceHandlerFactory ()
  80. {
  81. }
  82. #endregion // Constructors
  83. #region Methods
  84. public IHttpHandler GetHandler (HttpContext context, string verb, string url, string filePath)
  85. {
  86. Type type = WebServiceParser.GetCompiledType (filePath, context);
  87. WSProtocol protocol = GuessProtocol (context, verb);
  88. #if NET_2_0
  89. context.Items ["WebServiceSoapVersion"] =
  90. protocol == WSProtocol.HttpSoap12 ?
  91. SoapProtocolVersion.Soap12 :
  92. SoapProtocolVersion.Default;
  93. #endif
  94. bool supported = false;
  95. IHttpHandler handler = null;
  96. supported = WSConfig.IsSupported (protocol);
  97. if (!supported) {
  98. switch (protocol) {
  99. #if NET_2_0
  100. default:
  101. if (((protocol & WSProtocol.AnyHttpSoap) != WSProtocol.Unknown) &&
  102. (WSConfig.Current.EnabledProtocols & WSProtocol.AnyHttpSoap) != WSProtocol.Unknown)
  103. throw new InvalidOperationException ("Possible SOAP version mismatch.");
  104. break;
  105. #endif
  106. case WSProtocol.HttpPost:
  107. if (WSConfig.IsSupported (WSProtocol.HttpPostLocalhost)) {
  108. string localAddr = context.Request.ServerVariables ["LOCAL_ADDR"];
  109. supported = localAddr != null &&
  110. (localAddr == context.Request.ServerVariables ["REMOTE_ADDR"] ||
  111. IPAddress.IsLoopback (IPAddress.Parse (localAddr)));
  112. }
  113. break;
  114. }
  115. }
  116. if (!supported)
  117. throw new InvalidOperationException ("Unsupported request format.");
  118. switch (protocol) {
  119. case WSProtocol.HttpSoap12:
  120. case WSProtocol.HttpSoap:
  121. handler = GetTypeHandler (context, new HttpSoapWebServiceHandler (type));
  122. break;
  123. case WSProtocol.HttpPost:
  124. case WSProtocol.HttpGet:
  125. handler = GetTypeHandler (context, new HttpSimpleWebServiceHandler (type, protocol.ToString ()));
  126. break;
  127. case WSProtocol.Documentation:
  128. SoapDocumentationHandler soapHandler;
  129. soapHandler = new SoapDocumentationHandler (type, context);
  130. if (soapHandler.PageHandler is IRequiresSessionState) {
  131. if (soapHandler.PageHandler is IReadOnlySessionState)
  132. handler = new ReadOnlySessionWrapperHandler (soapHandler);
  133. else
  134. handler = new SessionWrapperHandler (soapHandler);
  135. } else {
  136. handler = soapHandler;
  137. }
  138. break;
  139. }
  140. return handler;
  141. }
  142. IHttpHandler GetTypeHandler (HttpContext context, WebServiceHandler handler)
  143. {
  144. MethodStubInfo method = handler.GetRequestMethod (context);
  145. if (method == null) return null;
  146. if (method.MethodInfo.EnableSession)
  147. return new SessionWrapperHandler (handler);
  148. else
  149. return handler;
  150. }
  151. static WSProtocol GuessProtocol (HttpContext context, string verb)
  152. {
  153. if (context.Request.PathInfo == null || context.Request.PathInfo == "")
  154. {
  155. if (context.Request.RequestType == "GET")
  156. return WSProtocol.Documentation;
  157. else
  158. #if NET_2_0
  159. return context.Request.Headers ["SOAPAction"] != null ?
  160. WSProtocol.HttpSoap : WSProtocol.HttpSoap12;
  161. #else
  162. return WSProtocol.HttpSoap;
  163. #endif
  164. }
  165. else
  166. {
  167. if (context.Request.RequestType == "GET")
  168. return WSProtocol.HttpGet;
  169. else
  170. return WSProtocol.HttpPost;
  171. }
  172. }
  173. public void ReleaseHandler (IHttpHandler handler)
  174. {
  175. }
  176. #endregion // Methods
  177. }
  178. }