WebServiceHandlerFactory.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. case WSProtocol.HttpSoap12:
  100. case WSProtocol.HttpSoap:
  101. supported = WSConfig.IsSupported (WSProtocol.HttpSoap12);
  102. break;
  103. case WSProtocol.HttpPost:
  104. if (WSConfig.IsSupported (WSProtocol.HttpPostLocalhost)) {
  105. string localAddr = context.Request.ServerVariables ["LOCAL_ADDR"];
  106. supported = localAddr != null &&
  107. (localAddr == context.Request.ServerVariables ["REMOTE_ADDR"] ||
  108. IPAddress.IsLoopback (IPAddress.Parse (localAddr)));
  109. }
  110. break;
  111. }
  112. }
  113. if (!supported)
  114. throw new InvalidOperationException ("Unsupported request format.");
  115. switch (protocol) {
  116. case WSProtocol.HttpSoap12:
  117. case WSProtocol.HttpSoap:
  118. handler = GetTypeHandler (context, new HttpSoapWebServiceHandler (type));
  119. break;
  120. case WSProtocol.HttpPost:
  121. case WSProtocol.HttpGet:
  122. handler = GetTypeHandler (context, new HttpSimpleWebServiceHandler (type, protocol.ToString ()));
  123. break;
  124. case WSProtocol.Documentation:
  125. SoapDocumentationHandler soapHandler;
  126. soapHandler = new SoapDocumentationHandler (type, context);
  127. if (soapHandler.PageHandler is IRequiresSessionState) {
  128. if (soapHandler.PageHandler is IReadOnlySessionState)
  129. handler = new ReadOnlySessionWrapperHandler (soapHandler);
  130. else
  131. handler = new SessionWrapperHandler (soapHandler);
  132. } else {
  133. handler = soapHandler;
  134. }
  135. break;
  136. }
  137. return handler;
  138. }
  139. IHttpHandler GetTypeHandler (HttpContext context, WebServiceHandler handler)
  140. {
  141. MethodStubInfo method = handler.GetRequestMethod (context);
  142. if (method == null) return null;
  143. if (method.MethodInfo.EnableSession)
  144. return new SessionWrapperHandler (handler);
  145. else
  146. return handler;
  147. }
  148. static WSProtocol GuessProtocol (HttpContext context, string verb)
  149. {
  150. if (context.Request.PathInfo == null || context.Request.PathInfo == "")
  151. {
  152. if (context.Request.RequestType == "GET")
  153. return WSProtocol.Documentation;
  154. else
  155. #if NET_2_0
  156. return context.Request.Headers ["SOAPAction"] != null ?
  157. WSProtocol.HttpSoap : WSProtocol.HttpSoap12;
  158. #else
  159. return WSProtocol.HttpSoap;
  160. #endif
  161. }
  162. else
  163. {
  164. if (context.Request.RequestType == "GET")
  165. return WSProtocol.HttpGet;
  166. else
  167. return WSProtocol.HttpPost;
  168. }
  169. }
  170. public void ReleaseHandler (IHttpHandler handler)
  171. {
  172. }
  173. #endregion // Methods
  174. }
  175. }