WebServiceHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // System.Web.Services.Protocols.WebServiceHelper.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) Ximian, Inc. 2003
  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.IO;
  31. using System.Net;
  32. using System.Text;
  33. using System.Xml;
  34. using System.Xml.Schema;
  35. using System.Xml.Serialization;
  36. using System.Web.Services.Description;
  37. namespace System.Web.Services.Protocols
  38. {
  39. internal class WebServiceHelper
  40. {
  41. public const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
  42. static readonly char [] trimChars = { '"', '\'' };
  43. static readonly bool prettyXml;
  44. static WebServiceHelper ()
  45. {
  46. string pxml = Environment.GetEnvironmentVariable ("MONO_WEBSERVICES_PRETTYXML");
  47. prettyXml = (pxml != null && pxml != "no");
  48. }
  49. public static XmlTextWriter CreateXmlWriter (Stream s)
  50. {
  51. // What a waste of UTF8encoders, but it has to be thread safe.
  52. XmlTextWriter xtw = new XmlTextWriter (s, new UTF8Encoding (false));
  53. if (prettyXml)
  54. xtw.Formatting = Formatting.Indented;
  55. return xtw;
  56. }
  57. public static Encoding GetContentEncoding (string cts, out string content_type)
  58. {
  59. string encoding;
  60. if (cts == null) cts = "";
  61. encoding = "utf-8";
  62. int start = 0;
  63. int idx = cts.IndexOf (';');
  64. if (idx == -1)
  65. content_type = cts;
  66. else
  67. content_type = cts.Substring (0, idx);
  68. content_type = content_type.Trim ();
  69. for (start = idx + 1; idx != -1;)
  70. {
  71. idx = cts.IndexOf (";", start);
  72. string body;
  73. if (idx == -1)
  74. body = cts.Substring (start);
  75. else
  76. {
  77. body = cts.Substring (start, idx - start);
  78. start = idx + 1;
  79. }
  80. body = body.Trim ();
  81. if (body.StartsWith ("charset="))
  82. {
  83. encoding = body.Substring (8);
  84. encoding = encoding.TrimStart (trimChars).TrimEnd (trimChars);
  85. }
  86. }
  87. return Encoding.GetEncoding (encoding);
  88. }
  89. public static void WriteSoapMessage (XmlTextWriter xtw, SoapTypeStubInfo info, SoapBindingUse methodUse, XmlSerializer bodySerializer, object bodyContent, SoapHeaderCollection headers)
  90. {
  91. xtw.WriteStartDocument ();
  92. xtw.WriteStartElement ("soap", "Envelope", WebServiceHelper.SoapEnvelopeNamespace);
  93. xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
  94. xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
  95. // Serialize headers
  96. if (headers != null)
  97. {
  98. foreach (SoapHeader header in headers)
  99. {
  100. XmlSerializer ser = info.GetHeaderSerializer (header.GetType(), methodUse);
  101. xtw.WriteStartElement ("soap", "Header", WebServiceHelper.SoapEnvelopeNamespace);
  102. ser.Serialize (xtw, header);
  103. xtw.WriteEndElement ();
  104. }
  105. }
  106. // Serialize body
  107. xtw.WriteStartElement ("soap", "Body", WebServiceHelper.SoapEnvelopeNamespace);
  108. if (methodUse == SoapBindingUse.Encoded)
  109. xtw.WriteAttributeString ("encodingStyle", WebServiceHelper.SoapEnvelopeNamespace, "http://schemas.xmlsoap.org/soap/encoding/");
  110. bodySerializer.Serialize (xtw, bodyContent);
  111. xtw.WriteEndElement ();
  112. xtw.WriteEndElement ();
  113. xtw.Flush ();
  114. }
  115. public static void ReadSoapMessage (XmlTextReader xmlReader, SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlSerializer bodySerializer, out object body, out SoapHeaderCollection headers)
  116. {
  117. xmlReader.MoveToContent ();
  118. xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);
  119. headers = ReadHeaders (typeStubInfo, methodUse, xmlReader);
  120. xmlReader.MoveToContent ();
  121. xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);
  122. xmlReader.MoveToContent ();
  123. if (xmlReader.LocalName == "Fault" && xmlReader.NamespaceURI == SoapEnvelopeNamespace)
  124. bodySerializer = Fault.Serializer;
  125. body = bodySerializer.Deserialize (xmlReader);
  126. }
  127. static SoapHeaderCollection ReadHeaders (SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlTextReader xmlReader)
  128. {
  129. SoapHeaderCollection headers = new SoapHeaderCollection ();
  130. while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
  131. {
  132. if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Header"
  133. && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace && !xmlReader.IsEmptyElement)
  134. {
  135. xmlReader.ReadStartElement ();
  136. xmlReader.MoveToContent ();
  137. while (xmlReader.NodeType != XmlNodeType.Element && xmlReader.NodeType != XmlNodeType.EndElement)
  138. xmlReader.Skip ();
  139. xmlReader.MoveToContent ();
  140. if (xmlReader.NodeType == XmlNodeType.Element) {
  141. XmlQualifiedName qname = new XmlQualifiedName (xmlReader.LocalName, xmlReader.NamespaceURI);
  142. XmlSerializer headerSerializer = typeStubInfo.GetHeaderSerializer (qname, methodUse);
  143. if (headerSerializer != null)
  144. {
  145. SoapHeader header = (SoapHeader) headerSerializer.Deserialize (xmlReader);
  146. headers.Add (header);
  147. }
  148. else
  149. {
  150. XmlDocument doc = new XmlDocument ();
  151. XmlElement elem = (XmlElement) doc.ReadNode (xmlReader);
  152. headers.Add (new SoapUnknownHeader (elem));
  153. }
  154. }
  155. while (xmlReader.NodeType != XmlNodeType.EndElement)
  156. xmlReader.Skip ();
  157. xmlReader.ReadEndElement ();
  158. }
  159. else
  160. xmlReader.Skip ();
  161. }
  162. return headers;
  163. }
  164. public static void InvalidOperation (string message, WebResponse response, Encoding enc)
  165. {
  166. if (response == null)
  167. throw new InvalidOperationException (message);
  168. if (enc == null)
  169. enc = Encoding.UTF8;
  170. StringBuilder sb = new StringBuilder ();
  171. sb.Append (message);
  172. sb.Append ("\r\nResponse error message:\r\n--\r\n");
  173. try {
  174. StreamReader resp = new StreamReader (response.GetResponseStream (), enc);
  175. sb.Append (resp.ReadToEnd ());
  176. } catch (Exception) {
  177. }
  178. throw new InvalidOperationException (sb.ToString ());
  179. }
  180. }
  181. }