HttpSoapWebServiceHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // System.Web.Services.Protocols.HttpSoapWebServiceHandler.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) Ximian, Inc. 2003
  8. //
  9. using System;
  10. using System.Net;
  11. using System.Web;
  12. using System.Xml;
  13. using System.Text;
  14. using System.IO;
  15. using System.Reflection;
  16. using System.Xml.Serialization;
  17. using System.Web.Services.Description;
  18. namespace System.Web.Services.Protocols
  19. {
  20. internal class HttpSoapWebServiceHandler: WebServiceHandler
  21. {
  22. SoapTypeStubInfo _typeStubInfo;
  23. SoapExtension[] _extensionChainHighPrio;
  24. SoapExtension[] _extensionChainMedPrio;
  25. SoapExtension[] _extensionChainLowPrio;
  26. public HttpSoapWebServiceHandler (Type type): base (type)
  27. {
  28. _typeStubInfo = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (ServiceType, "Soap");
  29. }
  30. public override bool IsReusable
  31. {
  32. get { return false; }
  33. }
  34. public override void ProcessRequest (HttpContext context)
  35. {
  36. Context = context;
  37. SoapServerMessage requestMessage = null;
  38. SoapServerMessage responseMessage = null;
  39. try
  40. {
  41. requestMessage = DeserializeRequest (context.Request);
  42. responseMessage = Invoke (requestMessage);
  43. SerializeResponse (context.Response, responseMessage);
  44. }
  45. catch (Exception ex)
  46. {
  47. SerializeFault (context, requestMessage, ex);
  48. return;
  49. }
  50. }
  51. SoapServerMessage DeserializeRequest (HttpRequest request)
  52. {
  53. Stream stream = request.InputStream;
  54. using (stream)
  55. {
  56. string soapAction = null;
  57. SoapMethodStubInfo methodInfo = null;
  58. string ctype;
  59. Encoding encoding = WebServiceHelper.GetContentEncoding (request.ContentType, out ctype);
  60. if (ctype != "text/xml")
  61. throw new WebException ("Content is not XML: " + ctype);
  62. object server = CreateServerInstance ();
  63. SoapServerMessage message = new SoapServerMessage (request, server, stream);
  64. message.SetStage (SoapMessageStage.BeforeDeserialize);
  65. // If the routing style is SoapAction, then we can get the method information now
  66. // and set it to the SoapMessage
  67. if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.SoapAction)
  68. {
  69. soapAction = request.Headers ["SOAPAction"];
  70. if (soapAction == null) throw new SoapException ("Missing SOAPAction header", SoapException.ClientFaultCode);
  71. methodInfo = _typeStubInfo.GetMethodForSoapAction (soapAction);
  72. if (methodInfo == null) throw new SoapException ("Server did not recognize the value of HTTP header SOAPAction: " + soapAction, SoapException.ClientFaultCode);
  73. message.MethodStubInfo = methodInfo;
  74. }
  75. // Execute the high priority global extensions. Do not try to execute the medium and
  76. // low priority extensions because if the routing style is RequestElement we still
  77. // don't have method information
  78. _extensionChainHighPrio = SoapExtension.CreateExtensionChain (_typeStubInfo.SoapExtensions[0]);
  79. stream = SoapExtension.ExecuteChainStream (_extensionChainHighPrio, stream);
  80. SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, false);
  81. // If the routing style is RequestElement, try to get the method name from the
  82. // stream processed by the high priority extensions
  83. if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.RequestElement)
  84. {
  85. MemoryStream mstream;
  86. byte[] buffer = null;
  87. if (stream.CanSeek)
  88. {
  89. buffer = new byte [stream.Length];
  90. for (int n=0; n<stream.Length;)
  91. n += stream.Read (buffer, n, (int)stream.Length-n);
  92. mstream = new MemoryStream (buffer);
  93. }
  94. else
  95. {
  96. buffer = new byte [500];
  97. mstream = new MemoryStream ();
  98. int len;
  99. while ((len = stream.Read (buffer, 0, 500)) > 0)
  100. mstream.Write (buffer, 0, len);
  101. mstream.Position = 0;
  102. buffer = mstream.ToArray ();
  103. }
  104. soapAction = ReadActionFromRequestElement (new MemoryStream (buffer), encoding);
  105. stream = mstream;
  106. methodInfo = (SoapMethodStubInfo) _typeStubInfo.GetMethod (soapAction);
  107. message.MethodStubInfo = methodInfo;
  108. }
  109. // Whatever routing style we used, we should now have the method information.
  110. // We can now notify the remaining extensions
  111. if (methodInfo == null) throw new SoapException ("Method '" + soapAction + "' not defined in the web service '" + _typeStubInfo.LogicalType.WebServiceName + "'", SoapException.ClientFaultCode);
  112. _extensionChainMedPrio = SoapExtension.CreateExtensionChain (methodInfo.SoapExtensions);
  113. _extensionChainLowPrio = SoapExtension.CreateExtensionChain (_typeStubInfo.SoapExtensions[1]);
  114. stream = SoapExtension.ExecuteChainStream (_extensionChainMedPrio, stream);
  115. stream = SoapExtension.ExecuteChainStream (_extensionChainLowPrio, stream);
  116. SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, false);
  117. SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, false);
  118. // Deserialize the request
  119. StreamReader reader = new StreamReader (stream, encoding, false);
  120. XmlTextReader xmlReader = new XmlTextReader (reader);
  121. try
  122. {
  123. object content;
  124. SoapHeaderCollection headers;
  125. WebServiceHelper.ReadSoapMessage (xmlReader, _typeStubInfo, methodInfo.Use, methodInfo.RequestSerializer, out content, out headers);
  126. message.InParameters = (object []) content;
  127. message.SetHeaders (headers);
  128. }
  129. catch (Exception ex)
  130. {
  131. throw new SoapException ("Could not deserialize Soap message", SoapException.ClientFaultCode, ex);
  132. }
  133. // Notify the extensions after deserialization
  134. message.SetStage (SoapMessageStage.AfterDeserialize);
  135. SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, false);
  136. SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, false);
  137. SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, false);
  138. xmlReader.Close ();
  139. return message;
  140. }
  141. }
  142. string ReadActionFromRequestElement (Stream stream, Encoding encoding)
  143. {
  144. try
  145. {
  146. StreamReader reader = new StreamReader (stream, encoding, false);
  147. XmlTextReader xmlReader = new XmlTextReader (reader);
  148. xmlReader.MoveToContent ();
  149. xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);
  150. while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
  151. xmlReader.Skip ();
  152. xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);
  153. xmlReader.MoveToContent ();
  154. return xmlReader.LocalName;
  155. }
  156. catch (Exception ex)
  157. {
  158. string errmsg = "The root element for the request could not be determined. ";
  159. errmsg += "When RoutingStyle is set to RequestElement, SoapExtensions configured ";
  160. errmsg += "via an attribute on the method cannot modify the request stream before it is read. ";
  161. errmsg += "The extension must be configured via the SoapExtensionTypes element in web.config ";
  162. errmsg += "or the request must arrive at the server as clear text.";
  163. throw new SoapException (errmsg, SoapException.ServerFaultCode, ex);
  164. }
  165. }
  166. void SerializeResponse (HttpResponse response, SoapServerMessage message)
  167. {
  168. SoapMethodStubInfo methodInfo = message.MethodStubInfo;
  169. response.ContentType = "text/xml; charset=utf-8";
  170. if (message.Exception != null) response.StatusCode = 500;
  171. long contentLength = 0;
  172. Stream outStream = null;
  173. MemoryStream bufferStream = null;
  174. Stream responseStream = response.OutputStream;
  175. bool bufferResponse = (methodInfo == null || methodInfo.MethodAttribute.BufferResponse);
  176. if (bufferResponse)
  177. {
  178. bufferStream = new MemoryStream ();
  179. outStream = bufferStream;
  180. }
  181. else
  182. outStream = responseStream;
  183. try
  184. {
  185. // While serializing, process extensions in reverse order
  186. if (bufferResponse)
  187. {
  188. outStream = SoapExtension.ExecuteChainStream (_extensionChainHighPrio, outStream);
  189. outStream = SoapExtension.ExecuteChainStream (_extensionChainMedPrio, outStream);
  190. outStream = SoapExtension.ExecuteChainStream (_extensionChainLowPrio, outStream);
  191. message.SetStage (SoapMessageStage.BeforeSerialize);
  192. SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, true);
  193. SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, true);
  194. SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, true);
  195. }
  196. // What a waste of UTF8encoders, but it has to be thread safe.
  197. XmlTextWriter xtw = new XmlTextWriter (outStream, new UTF8Encoding (false));
  198. if (message.Exception == null)
  199. WebServiceHelper.WriteSoapMessage (xtw, _typeStubInfo, methodInfo.Use, methodInfo.ResponseSerializer, message.OutParameters, message.Headers);
  200. else
  201. WebServiceHelper.WriteSoapMessage (xtw, _typeStubInfo, SoapBindingUse.Literal, _typeStubInfo.GetFaultSerializer(), new Fault (message.Exception), null);
  202. if (bufferResponse)
  203. {
  204. message.SetStage (SoapMessageStage.AfterSerialize);
  205. SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, true);
  206. SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, true);
  207. SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, true);
  208. }
  209. if (bufferStream != null) contentLength = bufferStream.Length;
  210. xtw.Close ();
  211. }
  212. catch (Exception ex)
  213. {
  214. // If the response is buffered, we can discard the response and
  215. // serialize a new Fault message as response.
  216. if (bufferResponse) throw ex;
  217. // If it is not buffered, we can't rollback what has been sent,
  218. // so we can only close the connection and return.
  219. responseStream.Close ();
  220. return;
  221. }
  222. try
  223. {
  224. if (bufferResponse)
  225. responseStream.Write (bufferStream.GetBuffer(), 0, (int) contentLength);
  226. }
  227. catch {}
  228. responseStream.Close ();
  229. }
  230. void SerializeFault (HttpContext context, SoapServerMessage requestMessage, Exception ex)
  231. {
  232. SoapException soex = ex as SoapException;
  233. if (soex == null) soex = new SoapException (ex.Message, SoapException.ServerFaultCode, ex);
  234. MethodStubInfo stubInfo;
  235. object server;
  236. Stream stream;
  237. SoapServerMessage faultMessage;
  238. if (requestMessage != null)
  239. faultMessage = new SoapServerMessage (context.Request, soex, requestMessage.MethodStubInfo, requestMessage.Server, requestMessage.Stream);
  240. else
  241. faultMessage = new SoapServerMessage (context.Request, soex, null, null, null);
  242. SerializeResponse (context.Response, faultMessage);
  243. return;
  244. }
  245. private SoapServerMessage Invoke (SoapServerMessage requestMessage)
  246. {
  247. SoapMethodStubInfo methodInfo = requestMessage.MethodStubInfo;
  248. // Assign header values to web service members
  249. requestMessage.UpdateHeaderValues (requestMessage.Server, methodInfo.Headers);
  250. // Fill an array with the input parameters at the right position
  251. object[] parameters = new object[methodInfo.MethodInfo.Parameters.Length];
  252. ParameterInfo[] inParams = methodInfo.MethodInfo.InParameters;
  253. for (int n=0; n<inParams.Length; n++)
  254. parameters [inParams[n].Position] = requestMessage.InParameters [n];
  255. // Invoke the method
  256. try
  257. {
  258. object[] results = methodInfo.MethodInfo.Invoke (requestMessage.Server, parameters);
  259. requestMessage.OutParameters = results;
  260. }
  261. catch (TargetInvocationException ex)
  262. {
  263. throw ex.InnerException;
  264. }
  265. // Check that headers with MustUnderstand flag have been understood
  266. foreach (SoapHeader header in requestMessage.Headers)
  267. {
  268. if (header.MustUnderstand && !header.DidUnderstand)
  269. throw new SoapHeaderException ("Header not understood: " + header.GetType(), SoapException.MustUnderstandFaultCode);
  270. }
  271. // Collect headers that must be sent to the client
  272. requestMessage.CollectHeaders (requestMessage.Server, methodInfo.Headers, SoapHeaderDirection.Out);
  273. return requestMessage;
  274. }
  275. }
  276. }